langroid 0.1.85__py3-none-any.whl → 0.1.219__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.
Files changed (107) hide show
  1. langroid/__init__.py +95 -0
  2. langroid/agent/__init__.py +40 -0
  3. langroid/agent/base.py +222 -91
  4. langroid/agent/batch.py +264 -0
  5. langroid/agent/callbacks/chainlit.py +608 -0
  6. langroid/agent/chat_agent.py +247 -101
  7. langroid/agent/chat_document.py +41 -4
  8. langroid/agent/openai_assistant.py +842 -0
  9. langroid/agent/special/__init__.py +50 -0
  10. langroid/agent/special/doc_chat_agent.py +837 -141
  11. langroid/agent/special/lance_doc_chat_agent.py +258 -0
  12. langroid/agent/special/lance_rag/__init__.py +9 -0
  13. langroid/agent/special/lance_rag/critic_agent.py +136 -0
  14. langroid/agent/special/lance_rag/lance_rag_task.py +80 -0
  15. langroid/agent/special/lance_rag/query_planner_agent.py +180 -0
  16. langroid/agent/special/lance_tools.py +44 -0
  17. langroid/agent/special/neo4j/__init__.py +0 -0
  18. langroid/agent/special/neo4j/csv_kg_chat.py +174 -0
  19. langroid/agent/special/neo4j/neo4j_chat_agent.py +370 -0
  20. langroid/agent/special/neo4j/utils/__init__.py +0 -0
  21. langroid/agent/special/neo4j/utils/system_message.py +46 -0
  22. langroid/agent/special/relevance_extractor_agent.py +127 -0
  23. langroid/agent/special/retriever_agent.py +32 -198
  24. langroid/agent/special/sql/__init__.py +11 -0
  25. langroid/agent/special/sql/sql_chat_agent.py +47 -23
  26. langroid/agent/special/sql/utils/__init__.py +22 -0
  27. langroid/agent/special/sql/utils/description_extractors.py +95 -46
  28. langroid/agent/special/sql/utils/populate_metadata.py +28 -21
  29. langroid/agent/special/table_chat_agent.py +43 -9
  30. langroid/agent/task.py +475 -122
  31. langroid/agent/tool_message.py +75 -13
  32. langroid/agent/tools/__init__.py +13 -0
  33. langroid/agent/tools/duckduckgo_search_tool.py +66 -0
  34. langroid/agent/tools/google_search_tool.py +11 -0
  35. langroid/agent/tools/metaphor_search_tool.py +67 -0
  36. langroid/agent/tools/recipient_tool.py +16 -29
  37. langroid/agent/tools/run_python_code.py +60 -0
  38. langroid/agent/tools/sciphi_search_rag_tool.py +79 -0
  39. langroid/agent/tools/segment_extract_tool.py +36 -0
  40. langroid/cachedb/__init__.py +9 -0
  41. langroid/cachedb/base.py +22 -2
  42. langroid/cachedb/momento_cachedb.py +26 -2
  43. langroid/cachedb/redis_cachedb.py +78 -11
  44. langroid/embedding_models/__init__.py +34 -0
  45. langroid/embedding_models/base.py +21 -2
  46. langroid/embedding_models/models.py +120 -18
  47. langroid/embedding_models/protoc/embeddings.proto +19 -0
  48. langroid/embedding_models/protoc/embeddings_pb2.py +33 -0
  49. langroid/embedding_models/protoc/embeddings_pb2.pyi +50 -0
  50. langroid/embedding_models/protoc/embeddings_pb2_grpc.py +79 -0
  51. langroid/embedding_models/remote_embeds.py +153 -0
  52. langroid/language_models/__init__.py +45 -0
  53. langroid/language_models/azure_openai.py +80 -27
  54. langroid/language_models/base.py +117 -12
  55. langroid/language_models/config.py +5 -0
  56. langroid/language_models/openai_assistants.py +3 -0
  57. langroid/language_models/openai_gpt.py +558 -174
  58. langroid/language_models/prompt_formatter/__init__.py +15 -0
  59. langroid/language_models/prompt_formatter/base.py +4 -6
  60. langroid/language_models/prompt_formatter/hf_formatter.py +135 -0
  61. langroid/language_models/utils.py +18 -21
  62. langroid/mytypes.py +25 -8
  63. langroid/parsing/__init__.py +46 -0
  64. langroid/parsing/document_parser.py +260 -63
  65. langroid/parsing/image_text.py +32 -0
  66. langroid/parsing/parse_json.py +143 -0
  67. langroid/parsing/parser.py +122 -59
  68. langroid/parsing/repo_loader.py +114 -52
  69. langroid/parsing/search.py +68 -63
  70. langroid/parsing/spider.py +3 -2
  71. langroid/parsing/table_loader.py +44 -0
  72. langroid/parsing/url_loader.py +59 -11
  73. langroid/parsing/urls.py +85 -37
  74. langroid/parsing/utils.py +298 -4
  75. langroid/parsing/web_search.py +73 -0
  76. langroid/prompts/__init__.py +11 -0
  77. langroid/prompts/chat-gpt4-system-prompt.md +68 -0
  78. langroid/prompts/prompts_config.py +1 -1
  79. langroid/utils/__init__.py +17 -0
  80. langroid/utils/algorithms/__init__.py +3 -0
  81. langroid/utils/algorithms/graph.py +103 -0
  82. langroid/utils/configuration.py +36 -5
  83. langroid/utils/constants.py +4 -0
  84. langroid/utils/globals.py +2 -2
  85. langroid/utils/logging.py +2 -5
  86. langroid/utils/output/__init__.py +21 -0
  87. langroid/utils/output/printing.py +47 -1
  88. langroid/utils/output/status.py +33 -0
  89. langroid/utils/pandas_utils.py +30 -0
  90. langroid/utils/pydantic_utils.py +616 -2
  91. langroid/utils/system.py +98 -0
  92. langroid/vector_store/__init__.py +40 -0
  93. langroid/vector_store/base.py +203 -6
  94. langroid/vector_store/chromadb.py +59 -32
  95. langroid/vector_store/lancedb.py +463 -0
  96. langroid/vector_store/meilisearch.py +10 -7
  97. langroid/vector_store/momento.py +262 -0
  98. langroid/vector_store/qdrantdb.py +104 -22
  99. {langroid-0.1.85.dist-info → langroid-0.1.219.dist-info}/METADATA +329 -149
  100. langroid-0.1.219.dist-info/RECORD +127 -0
  101. {langroid-0.1.85.dist-info → langroid-0.1.219.dist-info}/WHEEL +1 -1
  102. langroid/agent/special/recipient_validator_agent.py +0 -157
  103. langroid/parsing/json.py +0 -64
  104. langroid/utils/web/selenium_login.py +0 -36
  105. langroid-0.1.85.dist-info/RECORD +0 -94
  106. /langroid/{scripts → agent/callbacks}/__init__.py +0 -0
  107. {langroid-0.1.85.dist-info → langroid-0.1.219.dist-info}/LICENSE +0 -0
@@ -1,8 +1,9 @@
1
1
  import json
2
- from typing import List, Optional, Type
2
+ from typing import List, Optional, Union
3
3
 
4
4
  from pydantic import BaseModel, Extra
5
5
 
6
+ from langroid.agent.tool_message import ToolMessage
6
7
  from langroid.language_models.base import (
7
8
  LLMFunctionCall,
8
9
  LLMMessage,
@@ -12,7 +13,7 @@ from langroid.language_models.base import (
12
13
  )
13
14
  from langroid.mytypes import DocMetaData, Document, Entity
14
15
  from langroid.parsing.agent_chats import parse_message
15
- from langroid.parsing.json import extract_top_level_json, top_level_json_field
16
+ from langroid.parsing.parse_json import extract_top_level_json, top_level_json_field
16
17
  from langroid.utils.output.printing import shorten_text
17
18
 
18
19
 
@@ -25,6 +26,7 @@ class ChatDocAttachment(BaseModel):
25
26
  class ChatDocMetaData(DocMetaData):
26
27
  parent: Optional["ChatDocument"] = None
27
28
  sender: Entity
29
+ tool_ids: List[str] = [] # stack of tool_ids; used by OpenAIAssistant
28
30
  # when result returns to parent, pretend message is from this entity
29
31
  parent_responder: None | Entity = None
30
32
  block: None | Entity = None
@@ -52,6 +54,7 @@ class ChatDocLoggerFields(BaseModel):
52
54
 
53
55
  class ChatDocument(Document):
54
56
  function_call: Optional[LLMFunctionCall] = None
57
+ tool_messages: List[ToolMessage] = []
55
58
  metadata: ChatDocMetaData
56
59
  attachment: None | ChatDocAttachment = None
57
60
 
@@ -81,7 +84,7 @@ class ChatDocument(Document):
81
84
  json_data = json.loads(j)
82
85
  tool = json_data.get("request")
83
86
  if tool is not None:
84
- tools.append(tool)
87
+ tools.append(str(tool))
85
88
  return tools
86
89
 
87
90
  def log_fields(self) -> ChatDocLoggerFields:
@@ -102,6 +105,8 @@ class ChatDocument(Document):
102
105
  content = self.content
103
106
  sender_entity = self.metadata.sender
104
107
  sender_name = self.metadata.sender_name
108
+ if tool_type == "FUNC":
109
+ content += str(self.function_call)
105
110
  return ChatDocLoggerFields(
106
111
  sender_entity=sender_entity,
107
112
  sender_name=sender_name,
@@ -118,12 +123,30 @@ class ChatDocument(Document):
118
123
  field_values = fields.dict().values()
119
124
  return "\t".join(str(v) for v in field_values)
120
125
 
126
+ def pop_tool_ids(self) -> None:
127
+ """
128
+ Pop the last tool_id from the stack of tool_ids.
129
+ """
130
+ if len(self.metadata.tool_ids) > 0:
131
+ self.metadata.tool_ids.pop()
132
+
121
133
  @staticmethod
122
134
  def from_LLMResponse(
123
135
  response: LLMResponse,
124
136
  displayed: bool = False,
125
137
  ) -> "ChatDocument":
138
+ """
139
+ Convert LLMResponse to ChatDocument.
140
+ Args:
141
+ response (LLMResponse): LLMResponse to convert.
142
+ displayed (bool): Whether this response was displayed to the user.
143
+ Returns:
144
+ ChatDocument: ChatDocument representation of this LLMResponse.
145
+ """
126
146
  recipient, message = response.get_recipient_and_message()
147
+ message = message.strip()
148
+ if message in ["''", '""']:
149
+ message = ""
127
150
  return ChatDocument(
128
151
  content=message,
129
152
  function_call=response.function_call,
@@ -155,7 +178,7 @@ class ChatDocument(Document):
155
178
  )
156
179
 
157
180
  @staticmethod
158
- def to_LLMMessage(message: str | Type["ChatDocument"]) -> LLMMessage:
181
+ def to_LLMMessage(message: Union[str, "ChatDocument"]) -> LLMMessage:
159
182
  """
160
183
  Convert to LLMMessage for use with LLM.
161
184
 
@@ -168,10 +191,23 @@ class ChatDocument(Document):
168
191
  sender_name = None
169
192
  sender_role = Role.USER
170
193
  fun_call = None
194
+ tool_id = ""
171
195
  if isinstance(message, ChatDocument):
172
196
  content = message.content
173
197
  fun_call = message.function_call
198
+ if message.metadata.sender == Entity.USER and fun_call is not None:
199
+ # This may happen when a (parent agent's) LLM generates a
200
+ # a Function-call, and it ends up being sent to the current task's
201
+ # LLM (possibly because the function-call is mis-named or has other
202
+ # issues and couldn't be handled by handler methods).
203
+ # But a function-call can only be generated by an entity with
204
+ # Role.ASSISTANT, so we instead put the content of the function-call
205
+ # in the content of the message.
206
+ content += " " + str(fun_call)
207
+ fun_call = None
174
208
  sender_name = message.metadata.sender_name
209
+ tool_ids = message.metadata.tool_ids
210
+ tool_id = tool_ids[-1] if len(tool_ids) > 0 else ""
175
211
  if message.metadata.sender == Entity.SYSTEM:
176
212
  sender_role = Role.SYSTEM
177
213
  if (
@@ -188,6 +224,7 @@ class ChatDocument(Document):
188
224
 
189
225
  return LLMMessage(
190
226
  role=sender_role,
227
+ tool_id=tool_id,
191
228
  content=content,
192
229
  function_call=fun_call,
193
230
  name=sender_name,