langroid 0.50.4__py3-none-any.whl → 0.50.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -215,9 +215,17 @@ class ChatDocument(Document):
215
215
  """
216
216
  tool_type = "" # FUNC or TOOL
217
217
  tool = "" # tool name or function name
218
+ oai_tools = (
219
+ []
220
+ if self.oai_tool_calls is None
221
+ else [t for t in self.oai_tool_calls if t.function is not None]
222
+ )
218
223
  if self.function_call is not None:
219
224
  tool_type = "FUNC"
220
225
  tool = self.function_call.name
226
+ elif len(oai_tools) > 0:
227
+ tool_type = "OAI_TOOL"
228
+ tool = ",".join(t.function.name for t in oai_tools) # type: ignore
221
229
  else:
222
230
  try:
223
231
  json_tools = self.get_tool_names()
@@ -354,90 +362,88 @@ class ChatDocument(Document):
354
362
  oai_tool_calls = None
355
363
  tool_id = "" # for OpenAI Assistant
356
364
  chat_document_id: str = ""
357
- if isinstance(message, ChatDocument):
358
- content = message.content or to_string(message.content_any) or ""
359
- fun_call = message.function_call
360
- oai_tool_calls = message.oai_tool_calls
361
- if message.metadata.sender == Entity.USER and fun_call is not None:
362
- # This may happen when a (parent agent's) LLM generates a
363
- # a Function-call, and it ends up being sent to the current task's
364
- # LLM (possibly because the function-call is mis-named or has other
365
- # issues and couldn't be handled by handler methods).
366
- # But a function-call can only be generated by an entity with
367
- # Role.ASSISTANT, so we instead put the content of the function-call
368
- # in the content of the message.
369
- content += " " + str(fun_call)
370
- fun_call = None
371
- if message.metadata.sender == Entity.USER and oai_tool_calls is not None:
372
- # same reasoning as for function-call above
373
- content += " " + "\n\n".join(str(tc) for tc in oai_tool_calls)
374
- oai_tool_calls = None
375
- sender_name = message.metadata.sender_name
376
- tool_ids = message.metadata.tool_ids
377
- tool_id = tool_ids[-1] if len(tool_ids) > 0 else ""
378
- chat_document_id = message.id()
379
- if message.metadata.sender == Entity.SYSTEM:
380
- sender_role = Role.SYSTEM
381
- if (
382
- message.metadata.parent is not None
383
- and message.metadata.parent.function_call is not None
365
+ if isinstance(message, str):
366
+ message = ChatDocument.from_str(message)
367
+ content = message.content or to_string(message.content_any) or ""
368
+ fun_call = message.function_call
369
+ oai_tool_calls = message.oai_tool_calls
370
+ if message.metadata.sender == Entity.USER and fun_call is not None:
371
+ # This may happen when a (parent agent's) LLM generates a
372
+ # a Function-call, and it ends up being sent to the current task's
373
+ # LLM (possibly because the function-call is mis-named or has other
374
+ # issues and couldn't be handled by handler methods).
375
+ # But a function-call can only be generated by an entity with
376
+ # Role.ASSISTANT, so we instead put the content of the function-call
377
+ # in the content of the message.
378
+ content += " " + str(fun_call)
379
+ fun_call = None
380
+ if message.metadata.sender == Entity.USER and oai_tool_calls is not None:
381
+ # same reasoning as for function-call above
382
+ content += " " + "\n\n".join(str(tc) for tc in oai_tool_calls)
383
+ oai_tool_calls = None
384
+ sender_name = message.metadata.sender_name
385
+ tool_ids = message.metadata.tool_ids
386
+ tool_id = tool_ids[-1] if len(tool_ids) > 0 else ""
387
+ chat_document_id = message.id()
388
+ if message.metadata.sender == Entity.SYSTEM:
389
+ sender_role = Role.SYSTEM
390
+ if (
391
+ message.metadata.parent is not None
392
+ and message.metadata.parent.function_call is not None
393
+ ):
394
+ # This is a response to a function call, so set the role to FUNCTION.
395
+ sender_role = Role.FUNCTION
396
+ sender_name = message.metadata.parent.function_call.name
397
+ elif oai_tools is not None and len(oai_tools) > 0:
398
+ pending_tool_ids = [tc.id for tc in oai_tools]
399
+ # The ChatAgent has pending OpenAI tool-call(s),
400
+ # so the current ChatDocument contains
401
+ # results for some/all/none of them.
402
+
403
+ if len(oai_tools) == 1:
404
+ # Case 1:
405
+ # There was exactly 1 pending tool-call, and in this case
406
+ # the result would be a plain string in `content`
407
+ return [
408
+ LLMMessage(
409
+ role=Role.TOOL,
410
+ tool_call_id=oai_tools[0].id,
411
+ content=content,
412
+ chat_document_id=chat_document_id,
413
+ )
414
+ ]
415
+
416
+ elif (
417
+ message.metadata.oai_tool_id is not None
418
+ and message.metadata.oai_tool_id in pending_tool_ids
384
419
  ):
385
- # This is a response to a function call, so set the role to FUNCTION.
386
- sender_role = Role.FUNCTION
387
- sender_name = message.metadata.parent.function_call.name
388
- elif oai_tools is not None and len(oai_tools) > 0:
389
- pending_tool_ids = [tc.id for tc in oai_tools]
390
- # The ChatAgent has pending OpenAI tool-call(s),
391
- # so the current ChatDocument contains
392
- # results for some/all/none of them.
393
-
394
- if len(oai_tools) == 1:
395
- # Case 1:
396
- # There was exactly 1 pending tool-call, and in this case
397
- # the result would be a plain string in `content`
398
- return [
399
- LLMMessage(
400
- role=Role.TOOL,
401
- tool_call_id=oai_tools[0].id,
402
- content=content,
403
- chat_document_id=chat_document_id,
404
- )
405
- ]
406
-
407
- elif (
408
- message.metadata.oai_tool_id is not None
409
- and message.metadata.oai_tool_id in pending_tool_ids
410
- ):
411
- # Case 2:
412
- # ChatDocument.content has result of a single tool-call
413
- return [
414
- LLMMessage(
415
- role=Role.TOOL,
416
- tool_call_id=message.metadata.oai_tool_id,
417
- content=content,
418
- chat_document_id=chat_document_id,
419
- )
420
- ]
421
- elif message.oai_tool_id2result is not None:
422
- # Case 2:
423
- # There were > 1 tool-calls awaiting response,
424
- assert (
425
- len(message.oai_tool_id2result) > 1
426
- ), "oai_tool_id2result must have more than 1 item."
427
- return [
428
- LLMMessage(
429
- role=Role.TOOL,
430
- tool_call_id=tool_id,
431
- content=result,
432
- chat_document_id=chat_document_id,
433
- )
434
- for tool_id, result in message.oai_tool_id2result.items()
435
- ]
436
- elif message.metadata.sender == Entity.LLM:
437
- sender_role = Role.ASSISTANT
438
- else:
439
- # LLM can only respond to text content, so extract it
440
- content = message
420
+ # Case 2:
421
+ # ChatDocument.content has result of a single tool-call
422
+ return [
423
+ LLMMessage(
424
+ role=Role.TOOL,
425
+ tool_call_id=message.metadata.oai_tool_id,
426
+ content=content,
427
+ chat_document_id=chat_document_id,
428
+ )
429
+ ]
430
+ elif message.oai_tool_id2result is not None:
431
+ # Case 2:
432
+ # There were > 1 tool-calls awaiting response,
433
+ assert (
434
+ len(message.oai_tool_id2result) > 1
435
+ ), "oai_tool_id2result must have more than 1 item."
436
+ return [
437
+ LLMMessage(
438
+ role=Role.TOOL,
439
+ tool_call_id=tool_id,
440
+ content=result,
441
+ chat_document_id=chat_document_id,
442
+ )
443
+ for tool_id, result in message.oai_tool_id2result.items()
444
+ ]
445
+ elif message.metadata.sender == Entity.LLM:
446
+ sender_role = Role.ASSISTANT
441
447
 
442
448
  return [
443
449
  LLMMessage(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langroid
3
- Version: 0.50.4
3
+ Version: 0.50.6
4
4
  Summary: Harness LLMs with Multi-Agent Programming
5
5
  Author-email: Prasad Chalasani <pchalasani@gmail.com>
6
6
  License: MIT
@@ -6,7 +6,7 @@ langroid/agent/__init__.py,sha256=ll0Cubd2DZ-fsCMl7e10hf9ZjFGKzphfBco396IKITY,78
6
6
  langroid/agent/base.py,sha256=bs5OLCf534mhsdR7Rgf27GqVNuSV2bOVbD46Y86mGFA,79829
7
7
  langroid/agent/batch.py,sha256=vi1r5i1-vN80WfqHDSwjEym_KfGsqPGUtwktmiK1nuk,20635
8
8
  langroid/agent/chat_agent.py,sha256=m1alf-KNJSU6PeFF6ocwTHSG0cmTE-iy1o7UYAvRGQE,85081
9
- langroid/agent/chat_document.py,sha256=xzMtrPbaW-Y-BnF7kuhr2dorsD-D5rMWzfOqJ8HAoo8,17885
9
+ langroid/agent/chat_document.py,sha256=gWceR8mcggyGbJePJQgVvqzVivYlfPlFp8pUZ7yUZvg,17821
10
10
  langroid/agent/openai_assistant.py,sha256=JkAcs02bIrgPNVvUWVR06VCthc5-ulla2QMBzux_q6o,34340
11
11
  langroid/agent/task.py,sha256=HB6N-Jn80HFqCf0ZYOC1v3Bn3oO7NLjShHQJJFwW0q4,90557
12
12
  langroid/agent/tool_message.py,sha256=BhjP-_TfQ2tgxuY4Yo_JHLOwwt0mJ4BwjPnREvEY4vk,14744
@@ -129,7 +129,7 @@ langroid/vector_store/pineconedb.py,sha256=otxXZNaBKb9f_H75HTaU3lMHiaR2NUp5MqwLZ
129
129
  langroid/vector_store/postgres.py,sha256=wHPtIi2qM4fhO4pMQr95pz1ZCe7dTb2hxl4VYspGZoA,16104
130
130
  langroid/vector_store/qdrantdb.py,sha256=O6dSBoDZ0jzfeVBd7LLvsXu083xs2fxXtPa9gGX3JX4,18443
131
131
  langroid/vector_store/weaviatedb.py,sha256=Yn8pg139gOy3zkaPfoTbMXEEBCiLiYa1MU5d_3UA1K4,11847
132
- langroid-0.50.4.dist-info/METADATA,sha256=3HQH6x3pAFIDSUIt8TLI912CA-8EN_Jl_nKpQ09oli0,63641
133
- langroid-0.50.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
134
- langroid-0.50.4.dist-info/licenses/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
135
- langroid-0.50.4.dist-info/RECORD,,
132
+ langroid-0.50.6.dist-info/METADATA,sha256=JsIZE0WSSPm0jpB-5UX2Bfdvk6-txTtUo4J49ryyQIw,63641
133
+ langroid-0.50.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
134
+ langroid-0.50.6.dist-info/licenses/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
135
+ langroid-0.50.6.dist-info/RECORD,,