langroid 0.33.4__py3-none-any.whl → 0.33.7__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 (129) hide show
  1. langroid/__init__.py +106 -0
  2. langroid/agent/__init__.py +41 -0
  3. langroid/agent/base.py +1983 -0
  4. langroid/agent/batch.py +398 -0
  5. langroid/agent/callbacks/__init__.py +0 -0
  6. langroid/agent/callbacks/chainlit.py +598 -0
  7. langroid/agent/chat_agent.py +1899 -0
  8. langroid/agent/chat_document.py +454 -0
  9. langroid/agent/openai_assistant.py +882 -0
  10. langroid/agent/special/__init__.py +59 -0
  11. langroid/agent/special/arangodb/__init__.py +0 -0
  12. langroid/agent/special/arangodb/arangodb_agent.py +656 -0
  13. langroid/agent/special/arangodb/system_messages.py +186 -0
  14. langroid/agent/special/arangodb/tools.py +107 -0
  15. langroid/agent/special/arangodb/utils.py +36 -0
  16. langroid/agent/special/doc_chat_agent.py +1466 -0
  17. langroid/agent/special/lance_doc_chat_agent.py +262 -0
  18. langroid/agent/special/lance_rag/__init__.py +9 -0
  19. langroid/agent/special/lance_rag/critic_agent.py +198 -0
  20. langroid/agent/special/lance_rag/lance_rag_task.py +82 -0
  21. langroid/agent/special/lance_rag/query_planner_agent.py +260 -0
  22. langroid/agent/special/lance_tools.py +61 -0
  23. langroid/agent/special/neo4j/__init__.py +0 -0
  24. langroid/agent/special/neo4j/csv_kg_chat.py +174 -0
  25. langroid/agent/special/neo4j/neo4j_chat_agent.py +433 -0
  26. langroid/agent/special/neo4j/system_messages.py +120 -0
  27. langroid/agent/special/neo4j/tools.py +32 -0
  28. langroid/agent/special/relevance_extractor_agent.py +127 -0
  29. langroid/agent/special/retriever_agent.py +56 -0
  30. langroid/agent/special/sql/__init__.py +17 -0
  31. langroid/agent/special/sql/sql_chat_agent.py +654 -0
  32. langroid/agent/special/sql/utils/__init__.py +21 -0
  33. langroid/agent/special/sql/utils/description_extractors.py +190 -0
  34. langroid/agent/special/sql/utils/populate_metadata.py +85 -0
  35. langroid/agent/special/sql/utils/system_message.py +35 -0
  36. langroid/agent/special/sql/utils/tools.py +64 -0
  37. langroid/agent/special/table_chat_agent.py +263 -0
  38. langroid/agent/task.py +2095 -0
  39. langroid/agent/tool_message.py +393 -0
  40. langroid/agent/tools/__init__.py +38 -0
  41. langroid/agent/tools/duckduckgo_search_tool.py +50 -0
  42. langroid/agent/tools/file_tools.py +234 -0
  43. langroid/agent/tools/google_search_tool.py +39 -0
  44. langroid/agent/tools/metaphor_search_tool.py +68 -0
  45. langroid/agent/tools/orchestration.py +303 -0
  46. langroid/agent/tools/recipient_tool.py +235 -0
  47. langroid/agent/tools/retrieval_tool.py +32 -0
  48. langroid/agent/tools/rewind_tool.py +137 -0
  49. langroid/agent/tools/segment_extract_tool.py +41 -0
  50. langroid/agent/xml_tool_message.py +382 -0
  51. langroid/cachedb/__init__.py +17 -0
  52. langroid/cachedb/base.py +58 -0
  53. langroid/cachedb/momento_cachedb.py +108 -0
  54. langroid/cachedb/redis_cachedb.py +153 -0
  55. langroid/embedding_models/__init__.py +39 -0
  56. langroid/embedding_models/base.py +74 -0
  57. langroid/embedding_models/models.py +461 -0
  58. langroid/embedding_models/protoc/__init__.py +0 -0
  59. langroid/embedding_models/protoc/embeddings.proto +19 -0
  60. langroid/embedding_models/protoc/embeddings_pb2.py +33 -0
  61. langroid/embedding_models/protoc/embeddings_pb2.pyi +50 -0
  62. langroid/embedding_models/protoc/embeddings_pb2_grpc.py +79 -0
  63. langroid/embedding_models/remote_embeds.py +153 -0
  64. langroid/exceptions.py +71 -0
  65. langroid/language_models/__init__.py +53 -0
  66. langroid/language_models/azure_openai.py +153 -0
  67. langroid/language_models/base.py +678 -0
  68. langroid/language_models/config.py +18 -0
  69. langroid/language_models/mock_lm.py +124 -0
  70. langroid/language_models/openai_gpt.py +1964 -0
  71. langroid/language_models/prompt_formatter/__init__.py +16 -0
  72. langroid/language_models/prompt_formatter/base.py +40 -0
  73. langroid/language_models/prompt_formatter/hf_formatter.py +132 -0
  74. langroid/language_models/prompt_formatter/llama2_formatter.py +75 -0
  75. langroid/language_models/utils.py +151 -0
  76. langroid/mytypes.py +84 -0
  77. langroid/parsing/__init__.py +52 -0
  78. langroid/parsing/agent_chats.py +38 -0
  79. langroid/parsing/code_parser.py +121 -0
  80. langroid/parsing/document_parser.py +718 -0
  81. langroid/parsing/para_sentence_split.py +62 -0
  82. langroid/parsing/parse_json.py +155 -0
  83. langroid/parsing/parser.py +313 -0
  84. langroid/parsing/repo_loader.py +790 -0
  85. langroid/parsing/routing.py +36 -0
  86. langroid/parsing/search.py +275 -0
  87. langroid/parsing/spider.py +102 -0
  88. langroid/parsing/table_loader.py +94 -0
  89. langroid/parsing/url_loader.py +111 -0
  90. langroid/parsing/urls.py +273 -0
  91. langroid/parsing/utils.py +373 -0
  92. langroid/parsing/web_search.py +156 -0
  93. langroid/prompts/__init__.py +9 -0
  94. langroid/prompts/dialog.py +17 -0
  95. langroid/prompts/prompts_config.py +5 -0
  96. langroid/prompts/templates.py +141 -0
  97. langroid/pydantic_v1/__init__.py +10 -0
  98. langroid/pydantic_v1/main.py +4 -0
  99. langroid/utils/__init__.py +19 -0
  100. langroid/utils/algorithms/__init__.py +3 -0
  101. langroid/utils/algorithms/graph.py +103 -0
  102. langroid/utils/configuration.py +98 -0
  103. langroid/utils/constants.py +30 -0
  104. langroid/utils/git_utils.py +252 -0
  105. langroid/utils/globals.py +49 -0
  106. langroid/utils/logging.py +135 -0
  107. langroid/utils/object_registry.py +66 -0
  108. langroid/utils/output/__init__.py +20 -0
  109. langroid/utils/output/citations.py +41 -0
  110. langroid/utils/output/printing.py +99 -0
  111. langroid/utils/output/status.py +40 -0
  112. langroid/utils/pandas_utils.py +30 -0
  113. langroid/utils/pydantic_utils.py +602 -0
  114. langroid/utils/system.py +286 -0
  115. langroid/utils/types.py +93 -0
  116. langroid/vector_store/__init__.py +50 -0
  117. langroid/vector_store/base.py +359 -0
  118. langroid/vector_store/chromadb.py +214 -0
  119. langroid/vector_store/lancedb.py +406 -0
  120. langroid/vector_store/meilisearch.py +299 -0
  121. langroid/vector_store/momento.py +278 -0
  122. langroid/vector_store/qdrantdb.py +468 -0
  123. {langroid-0.33.4.dist-info → langroid-0.33.7.dist-info}/METADATA +95 -94
  124. langroid-0.33.7.dist-info/RECORD +127 -0
  125. {langroid-0.33.4.dist-info → langroid-0.33.7.dist-info}/WHEEL +1 -1
  126. langroid-0.33.4.dist-info/RECORD +0 -7
  127. langroid-0.33.4.dist-info/entry_points.txt +0 -4
  128. pyproject.toml +0 -356
  129. {langroid-0.33.4.dist-info → langroid-0.33.7.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,186 @@
1
+ from langroid.agent.special.arangodb.tools import (
2
+ aql_creation_tool_name,
3
+ aql_retrieval_tool_name,
4
+ arango_schema_tool_name,
5
+ )
6
+ from langroid.agent.tools.orchestration import DoneTool
7
+
8
+ done_tool_name = DoneTool.default_value("request")
9
+
10
+ arango_schema_tool_description = f"""
11
+ `{arango_schema_tool_name}` tool/function-call to find the schema
12
+ of the graph database, or for some SPECIFIC collections, i.e. get information on
13
+ (document and edge), their attributes, and graph definitions available in your
14
+ ArangoDB database. You MUST use this tool BEFORE attempting to use the
15
+ `{aql_retrieval_tool_name}` tool/function-call, to ensure that you are using the
16
+ correct collection names and attributes in your `{aql_retrieval_tool_name}` tool.
17
+ """
18
+
19
+ aql_retrieval_tool_description = f"""
20
+ `{aql_retrieval_tool_name}` tool/function-call to retrieve information from
21
+ the database using AQL (ArangoDB Query Language) queries, to answer
22
+ the user's questions, OR for you to learn more about the SCHEMA of the database.
23
+ """
24
+
25
+ aql_creation_tool_description = f"""
26
+ `{aql_creation_tool_name}` tool/function-call to execute AQL query that creates
27
+ documents/edges in the database.
28
+ """
29
+
30
+ aql_retrieval_query_example = """
31
+ EXAMPLE:
32
+ Suppose you are asked this question "Does Bob have a father?".
33
+ Then you will go through the following steps, where YOU indicates
34
+ the message YOU will be sending, and RESULTS indicates the RESULTS
35
+ you will receive from the helper executing the query:
36
+
37
+ 1. YOU:
38
+ {{ "request": "aql_retrieval_tool",
39
+ "aql_query": "FOR v, e, p in ... [query truncated for brevity]..."}}
40
+
41
+ 2. RESULTS:
42
+ [.. results from the query...]
43
+ 3. YOU: [ since results were not satisfactory, you try ANOTHER query]
44
+ {{ "request": "aql_retrieval_tool",
45
+ "aql_query": "blah blah ... [query truncated for brevity]..."}}
46
+ }}
47
+ 4. RESULTS:
48
+ [.. results from the query...]
49
+ 5. YOU: [ now you have the answer, you can generate your response ]
50
+ The answer is YES, Bob has a father, and his name is John.
51
+ """
52
+
53
+ aql_query_instructions = """
54
+ When writing AQL queries:
55
+ 1. Use the exact property names shown in the schema
56
+ 2. Pay attention to the 'type' field of each node
57
+ 3. Note that all names are case-sensitive:
58
+ - collection names
59
+ - property names
60
+ - node type values
61
+ - relationship type values
62
+ 4. Always include type filters in your queries, e.g.:
63
+ FILTER doc.type == '<type-from-schema>'
64
+
65
+ The schema shows:
66
+ - Collections (usually 'nodes' and 'edges')
67
+ - Node types in each collection
68
+ - Available properties for each node type
69
+ - Relationship types and their properties
70
+
71
+ Examine the schema carefully before writing queries to ensure:
72
+ - Correct property names
73
+ - Correct node types
74
+ - Correct relationship types
75
+
76
+ You must be smart about using the right collection names and attributes
77
+ based on the English description. If you are thinking of using a collection
78
+ or attribute that does not exist, you are probably on the wrong track,
79
+ so you should try your best to answer based on existing collections and attributes.
80
+ DO NOT assume any collections or graphs other than those above.
81
+ """
82
+
83
+ tool_result_instruction = """
84
+ REMEMBER:
85
+ [1] DO NOT FORGET TO USE ONE OF THE AVAILABLE TOOLS TO ANSWER THE USER'S QUERY!!
86
+ [2] When using a TOOL/FUNCTION, you MUST WAIT for the tool result before continuing
87
+ with your response. DO NOT MAKE UP RESULTS FROM A TOOL!
88
+ [3] YOU MUST NOT ANSWER queries from your OWN KNOWLEDGE; ALWAYS RELY ON
89
+ the result of a TOOL/FUNCTION to compose your response.
90
+ [4] Use ONLY ONE TOOL/FUNCTION at a TIME!
91
+ """
92
+ # sys msg to use when schema already provided initially,
93
+ # so agent should not use schema tool
94
+ SCHEMA_PROVIDED_SYS_MSG = f"""You are a data scientist and expert in Graph Databases,
95
+ with expertise in answering questions by interacting with an ArangoDB database.
96
+
97
+ The schema below describes the ArangoDB database structure,
98
+ collections (document and edge),
99
+ and their attribute keys available in your ArangoDB database.
100
+
101
+ === SCHEMA ===
102
+ {{schema}}
103
+ === END SCHEMA ===
104
+
105
+
106
+ To help with the user's question or database update/creation request,
107
+ you have access to these tools:
108
+
109
+ - {aql_retrieval_tool_description}
110
+
111
+ - {aql_creation_tool_description}
112
+
113
+
114
+ {tool_result_instruction}
115
+ """
116
+
117
+ # sys msg to use when schema is not initially provided,
118
+ # and we want agent to use schema tool to get schema
119
+ SCHEMA_TOOLS_SYS_MSG = f"""You are a data scientist and expert in
120
+ Arango Graph Databases,
121
+ with expertise in answering questions by querying ArangoDB database
122
+ using the Arango Query Language (AQL).
123
+ You have access to the following tools:
124
+
125
+ - {arango_schema_tool_description}
126
+
127
+ - {aql_retrieval_tool_description}
128
+
129
+ - {aql_creation_tool_description}
130
+
131
+ {tool_result_instruction}
132
+ """
133
+
134
+ DEFAULT_ARANGO_CHAT_SYSTEM_MESSAGE = f"""
135
+ {{mode}}
136
+
137
+ You do not need to be able to answer a question with just one query.
138
+ You can make a query, WAIT for the result,
139
+ THEN make ANOTHER query, WAIT for result,
140
+ THEN make ANOTHER query, and so on, until you have the answer.
141
+
142
+ {aql_query_instructions}
143
+
144
+ RETRY-SUGGESTIONS:
145
+ If you receive a null or other unexpected result,
146
+ (a) make sure you use the available TOOLs correctly,
147
+ (b) learn more about the schema using EITHER:
148
+ - `{arango_schema_tool_name}` tool/function-call to find properties of specific
149
+ collections or other parts of the schema, OR
150
+ - `{aql_retrieval_tool_name}` tool/function-call to use AQL queries to
151
+ find specific parts of the schema.
152
+ (c) Collection names are CASE-SENSITIVE -- make sure you adhere to the exact
153
+ collection name you found in the schema.
154
+ (d) see if you have made an assumption in your AQL query, and try another way,
155
+ or use `{aql_retrieval_tool_name}` to explore the database contents before
156
+ submitting your final query.
157
+ (e) Try APPROXIMATE or PARTIAL MATCHES to strings in the user's query,
158
+ e.g. user may ask about "Godfather" instead of "The Godfather",
159
+ or try using CASE-INSENSITIVE MATCHES.
160
+
161
+ Start by asking what the user needs help with.
162
+
163
+ {tool_result_instruction}
164
+
165
+ {aql_retrieval_query_example}
166
+ """
167
+
168
+ ADDRESSING_INSTRUCTION = """
169
+ IMPORTANT - Whenever you are NOT writing an AQL query, make sure you address the
170
+ user using {prefix}User. You MUST use the EXACT syntax {prefix} !!!
171
+
172
+ In other words, you ALWAYS EITHER:
173
+ - write an AQL query using one of the tools,
174
+ - OR address the user using {prefix}User.
175
+
176
+ YOU CANNOT ADDRESS THE USER WHEN USING A TOOL!!
177
+ """
178
+
179
+ DONE_INSTRUCTION = f"""
180
+ When you are SURE you have the CORRECT answer to a user's query or request,
181
+ use the `{done_tool_name}` with `content` set to the answer or result.
182
+ If you DO NOT think you have the answer to the user's query or request,
183
+ you SHOULD NOT use the `{done_tool_name}` tool.
184
+ Instead, you must CONTINUE to improve your queries (tools) to get the correct answer,
185
+ and finally use the `{done_tool_name}` tool to send the correct answer to the user.
186
+ """
@@ -0,0 +1,107 @@
1
+ from typing import List, Tuple
2
+
3
+ from langroid.agent.tool_message import ToolMessage
4
+
5
+
6
+ class AQLRetrievalTool(ToolMessage):
7
+ request: str = "aql_retrieval_tool"
8
+ purpose: str = """
9
+ To send an <aql_query> in response to a user's request/question,
10
+ OR to find SCHEMA information,
11
+ and WAIT for results of the <aql_query> BEFORE continuing with response.
12
+ You will receive RESULTS from this tool, and ONLY THEN you can continue.
13
+ """
14
+ aql_query: str
15
+
16
+ _max_result_tokens = 500
17
+ _max_retained_tokens = 200
18
+
19
+ @classmethod
20
+ def examples(cls) -> List[ToolMessage | Tuple[str, ToolMessage]]:
21
+ """Few-shot examples to include in tool instructions."""
22
+ return [
23
+ (
24
+ "I want to see who Bob's Father is",
25
+ cls(
26
+ aql_query="""
27
+ FOR v, e, p IN 1..1 OUTBOUND 'users/Bob' GRAPH 'family_tree'
28
+ FILTER p.edges[0].type == 'father'
29
+ RETURN v
30
+ """
31
+ ),
32
+ ),
33
+ (
34
+ "I want to know the properties of the Actor node",
35
+ cls(
36
+ aql_query="""
37
+ FOR doc IN Actor
38
+ LIMIT 1
39
+ RETURN ATTRIBUTES(doc)
40
+ """
41
+ ),
42
+ ),
43
+ ]
44
+
45
+ @classmethod
46
+ def instructions(cls) -> str:
47
+ return """
48
+ When using this TOOL/Function-call, you must WAIT to receive the RESULTS
49
+ of the AQL query, before continuing your response!
50
+ DO NOT ASSUME YOU KNOW THE RESULTs BEFORE RECEIVING THEM.
51
+ """
52
+
53
+
54
+ aql_retrieval_tool_name = AQLRetrievalTool.default_value("request")
55
+
56
+
57
+ class AQLCreationTool(ToolMessage):
58
+ request: str = "aql_creation_tool"
59
+ purpose: str = """
60
+ To send the <aql_query> to create documents/edges in the graph database.
61
+ IMPORTANT: YOU MUST WAIT FOR THE RESULT OF THE TOOL BEFORE CONTINUING.
62
+ You will receive RESULTS from this tool, and ONLY THEN you can continue.
63
+ """
64
+ aql_query: str
65
+
66
+ @classmethod
67
+ def examples(cls) -> List[ToolMessage | Tuple[str, ToolMessage]]:
68
+ """Few-shot examples to include in tool instructions."""
69
+ return [
70
+ (
71
+ "Create a new document in the collection 'users'",
72
+ cls(
73
+ aql_query="""
74
+ INSERT {
75
+ "name": "Alice",
76
+ "age": 30
77
+ } INTO users
78
+ """
79
+ ),
80
+ ),
81
+ ]
82
+
83
+
84
+ aql_creation_tool_name = AQLCreationTool.default_value("request")
85
+
86
+
87
+ class ArangoSchemaTool(ToolMessage):
88
+ request: str = "arango_schema_tool"
89
+ purpose: str = """
90
+ To get the schema of the Arango graph database,
91
+ or some part of it. Follow these instructions:
92
+ 1. Set <properties> to True to get the properties of the collections,
93
+ and False if you only want to see the graph structure and get only the
94
+ from/to relations of the edges.
95
+ 2. Set <collections> to a list of collection names if you want to see,
96
+ or leave it as None to see all ALL collections.
97
+ IMPORTANT: YOU MUST WAIT FOR THE RESULT OF THE TOOL BEFORE CONTINUING.
98
+ You will receive RESULTS from this tool, and ONLY THEN you can continue.
99
+ """
100
+
101
+ properties: bool = True
102
+ collections: List[str] | None = None
103
+
104
+ _max_result_tokens = 500
105
+
106
+
107
+ arango_schema_tool_name = ArangoSchemaTool.default_value("request")
@@ -0,0 +1,36 @@
1
+ from typing import Any, Dict, List
2
+
3
+
4
+ def count_fields(schema: Dict[str, List[Dict[str, Any]]]) -> int:
5
+ total = 0
6
+ for coll in schema["Collection Schema"]:
7
+ # Count all keys in each collection's dict
8
+ total += len(coll)
9
+ # Also count properties if they exist
10
+ props = coll.get(f"{coll['collection_type']}_properties", [])
11
+ total += len(props)
12
+ return total
13
+
14
+
15
+ def trim_schema(
16
+ schema: Dict[str, List[Dict[str, Any]]]
17
+ ) -> Dict[str, List[Dict[str, Any]]]:
18
+ """Keep only edge connection info, remove properties and examples"""
19
+ trimmed: Dict[str, List[Dict[str, Any]]] = {
20
+ "Graph Schema": schema["Graph Schema"],
21
+ "Collection Schema": [],
22
+ }
23
+ for coll in schema["Collection Schema"]:
24
+ col_info: Dict[str, Any] = {
25
+ "collection_name": coll["collection_name"],
26
+ "collection_type": coll["collection_type"],
27
+ }
28
+ if coll["collection_type"] == "edge":
29
+ # preserve from/to info if present
30
+ if f"example_{coll['collection_type']}" in coll:
31
+ example = coll[f"example_{coll['collection_type']}"]
32
+ if example and "_from" in example:
33
+ col_info["from_collection"] = example["_from"].split("/")[0]
34
+ col_info["to_collection"] = example["_to"].split("/")[0]
35
+ trimmed["Collection Schema"].append(col_info)
36
+ return trimmed