langroid 0.1.196__py3-none-any.whl → 0.1.197__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.
- langroid/agent/chat_agent.py +10 -2
- langroid/agent/chat_document.py +1 -1
- langroid/agent/task.py +7 -3
- langroid/agent/tool_message.py +28 -6
- langroid/parsing/json.py +38 -2
- langroid/utils/pydantic_utils.py +47 -0
- langroid/utils/system.py +35 -0
- {langroid-0.1.196.dist-info → langroid-0.1.197.dist-info}/METADATA +1 -1
- {langroid-0.1.196.dist-info → langroid-0.1.197.dist-info}/RECORD +11 -11
- {langroid-0.1.196.dist-info → langroid-0.1.197.dist-info}/LICENSE +0 -0
- {langroid-0.1.196.dist-info → langroid-0.1.197.dist-info}/WHEEL +0 -0
langroid/agent/chat_agent.py
CHANGED
@@ -225,14 +225,22 @@ class ChatAgent(Agent):
|
|
225
225
|
enabled_classes: List[Type[ToolMessage]] = list(self.llm_tools_map.values())
|
226
226
|
if len(enabled_classes) == 0:
|
227
227
|
return "You can ask questions in natural language."
|
228
|
-
|
229
228
|
json_instructions = "\n\n".join(
|
230
229
|
[
|
231
|
-
msg_cls.json_instructions()
|
230
|
+
msg_cls.json_instructions(tool=self.config.use_tools)
|
232
231
|
for _, msg_cls in enumerate(enabled_classes)
|
233
232
|
if msg_cls.default_value("request") in self.llm_tools_usable
|
234
233
|
]
|
235
234
|
)
|
235
|
+
# if any of the enabled classes has json_group_instructions, then use that,
|
236
|
+
# else fall back to ToolMessage.json_group_instructions
|
237
|
+
for msg_cls in enabled_classes:
|
238
|
+
if hasattr(msg_cls, "json_group_instructions") and callable(
|
239
|
+
getattr(msg_cls, "json_group_instructions")
|
240
|
+
):
|
241
|
+
return msg_cls.json_group_instructions().format(
|
242
|
+
json_instructions=json_instructions
|
243
|
+
)
|
236
244
|
return ToolMessage.json_group_instructions().format(
|
237
245
|
json_instructions=json_instructions
|
238
246
|
)
|
langroid/agent/chat_document.py
CHANGED
langroid/agent/task.py
CHANGED
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|
2
2
|
|
3
3
|
import copy
|
4
4
|
import logging
|
5
|
+
import re
|
5
6
|
from collections import Counter
|
6
7
|
from types import SimpleNamespace
|
7
8
|
from typing import (
|
@@ -781,17 +782,20 @@ class Task:
|
|
781
782
|
# handle routing instruction in result if any,
|
782
783
|
# of the form PASS=<recipient>
|
783
784
|
content = msg.content if isinstance(msg, ChatDocument) else msg
|
785
|
+
content = content.strip()
|
784
786
|
if PASS in content and PASS_TO not in content:
|
785
787
|
return True, None
|
786
788
|
if PASS_TO in content and content.split(":")[1] != "":
|
787
789
|
return True, content.split(":")[1]
|
788
|
-
if SEND_TO in content and
|
789
|
-
recipient
|
790
|
+
if SEND_TO in content and (send_parts := re.split(r"[,: ]", content))[1] != "":
|
791
|
+
# assume syntax is SEND_TO:<recipient> <content>
|
792
|
+
# or SEND_TO:<recipient>,<content> or SEND_TO:<recipient>:<content>
|
793
|
+
recipient = send_parts[1].strip()
|
790
794
|
# get content to send, clean out routing instruction, and
|
791
795
|
# start from 1 char after SEND_TO:<recipient>,
|
792
796
|
# because we expect there is either a blank or some other separator
|
793
797
|
# after the recipient
|
794
|
-
content_to_send = content.replace(f"{SEND_TO}
|
798
|
+
content_to_send = content.replace(f"{SEND_TO}{recipient}", "").strip()[1:]
|
795
799
|
# if no content then treat same as PASS_TO
|
796
800
|
if content_to_send == "":
|
797
801
|
return True, recipient
|
langroid/agent/tool_message.py
CHANGED
@@ -16,7 +16,10 @@ from docstring_parser import parse
|
|
16
16
|
from pydantic import BaseModel
|
17
17
|
|
18
18
|
from langroid.language_models.base import LLMFunctionSpec
|
19
|
-
from langroid.utils.pydantic_utils import
|
19
|
+
from langroid.utils.pydantic_utils import (
|
20
|
+
_recursive_purge_dict_key,
|
21
|
+
generate_simple_schema,
|
22
|
+
)
|
20
23
|
|
21
24
|
|
22
25
|
class ToolMessage(ABC, BaseModel):
|
@@ -101,22 +104,30 @@ class ToolMessage(ABC, BaseModel):
|
|
101
104
|
return properties.get(f, {}).get("default", None)
|
102
105
|
|
103
106
|
@classmethod
|
104
|
-
def json_instructions(cls) -> str:
|
107
|
+
def json_instructions(cls, tool: bool = False) -> str:
|
105
108
|
"""
|
106
109
|
Default Instructions to the LLM showing how to use the tool/function-call.
|
107
110
|
Works for GPT4 but override this for weaker LLMs if needed.
|
111
|
+
|
112
|
+
Args:
|
113
|
+
tool: instructions for Langroid-native tool use? (e.g. for non-OpenAI LLM)
|
114
|
+
(or else it would be for OpenAI Function calls)
|
108
115
|
Returns:
|
109
116
|
str: instructions on how to use the message
|
110
117
|
"""
|
118
|
+
# TODO: when we attempt to use a "simpler schema"
|
119
|
+
# (i.e. all nested fields explicit without definitions),
|
120
|
+
# we seem to get worse results, so we turn it off for now
|
121
|
+
param_dict = (
|
122
|
+
# cls.simple_schema() if tool else
|
123
|
+
cls.llm_function_schema(request=True).parameters
|
124
|
+
)
|
111
125
|
return textwrap.dedent(
|
112
126
|
f"""
|
113
127
|
TOOL: {cls.default_value("request")}
|
114
128
|
PURPOSE: {cls.default_value("purpose")}
|
115
129
|
JSON FORMAT: {
|
116
|
-
json.dumps(
|
117
|
-
cls.llm_function_schema(request=True).parameters,
|
118
|
-
indent=4,
|
119
|
-
)
|
130
|
+
json.dumps(param_dict, indent=4)
|
120
131
|
}
|
121
132
|
{"EXAMPLE: " + cls.usage_example() if cls.examples() else ""}
|
122
133
|
""".lstrip()
|
@@ -210,3 +221,14 @@ class ToolMessage(ABC, BaseModel):
|
|
210
221
|
description=cls.default_value("purpose"),
|
211
222
|
parameters=parameters,
|
212
223
|
)
|
224
|
+
|
225
|
+
@classmethod
|
226
|
+
def simple_schema(cls) -> Dict[str, Any]:
|
227
|
+
"""
|
228
|
+
Return a simplified schema for the message, with only the request and
|
229
|
+
required fields.
|
230
|
+
Returns:
|
231
|
+
Dict[str, Any]: simplified schema
|
232
|
+
"""
|
233
|
+
schema = generate_simple_schema(cls, exclude=["result", "purpose"])
|
234
|
+
return schema
|
langroid/parsing/json.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import json
|
2
|
+
import re
|
2
3
|
from typing import Any, Iterator, List
|
3
4
|
|
4
5
|
from pyparsing import nestedExpr, originalTextFor
|
@@ -44,6 +45,40 @@ def get_json_candidates(s: str) -> List[str]:
|
|
44
45
|
return []
|
45
46
|
|
46
47
|
|
48
|
+
def replace_undefined(s: str, undefined_placeholder: str = '"<undefined>"') -> str:
|
49
|
+
"""
|
50
|
+
Replace undefined values in a potential json str with a placeholder.
|
51
|
+
|
52
|
+
Args:
|
53
|
+
- s (str): The potential JSON string to parse.
|
54
|
+
- undefined_placeholder (str): The placeholder or error message
|
55
|
+
for undefined values.
|
56
|
+
|
57
|
+
Returns:
|
58
|
+
- str: The (potential) JSON string with undefined values
|
59
|
+
replaced by the placeholder.
|
60
|
+
"""
|
61
|
+
|
62
|
+
# Preprocess the string to replace undefined values with the placeholder
|
63
|
+
# This regex looks for patterns like ": <identifier>" and replaces them
|
64
|
+
# with the placeholder.
|
65
|
+
# It's a simple approach and might need adjustments for complex cases
|
66
|
+
# This is an attempt to handle cases where a weak LLM may produce
|
67
|
+
# a JSON-like string without quotes around some values, e.g.
|
68
|
+
# {"rent": DO-NOT-KNOW }
|
69
|
+
preprocessed_s = re.sub(
|
70
|
+
r":\s*([a-zA-Z_][a-zA-Z_0-9\-]*)", f": {undefined_placeholder}", s
|
71
|
+
)
|
72
|
+
|
73
|
+
# Now, attempt to parse the preprocessed string as JSON
|
74
|
+
try:
|
75
|
+
return preprocessed_s
|
76
|
+
except Exception:
|
77
|
+
# If parsing fails, return an error message instead
|
78
|
+
# (this should be rare after preprocessing)
|
79
|
+
return s
|
80
|
+
|
81
|
+
|
47
82
|
def extract_top_level_json(s: str) -> List[str]:
|
48
83
|
"""Extract all top-level JSON-formatted substrings from a given string.
|
49
84
|
|
@@ -53,15 +88,16 @@ def extract_top_level_json(s: str) -> List[str]:
|
|
53
88
|
Returns:
|
54
89
|
List[str]: A list of top-level JSON-formatted substrings.
|
55
90
|
"""
|
56
|
-
# Find JSON object and array candidates
|
91
|
+
# Find JSON object and array candidates
|
57
92
|
json_candidates = get_json_candidates(s)
|
58
93
|
|
59
94
|
normalized_candidates = [
|
60
95
|
candidate.replace("\\{", "{").replace("\\}", "}").replace("\\_", "_")
|
61
96
|
for candidate in json_candidates
|
62
97
|
]
|
98
|
+
candidates = [replace_undefined(candidate) for candidate in normalized_candidates]
|
63
99
|
top_level_jsons = [
|
64
|
-
candidate for candidate in
|
100
|
+
candidate for candidate in candidates if is_valid_json(candidate)
|
65
101
|
]
|
66
102
|
|
67
103
|
return top_level_jsons
|
langroid/utils/pydantic_utils.py
CHANGED
@@ -135,6 +135,53 @@ def flatten_pydantic_model(
|
|
135
135
|
return create_model("FlatModel", __base__=base_model, **flattened_fields)
|
136
136
|
|
137
137
|
|
138
|
+
def get_field_names(model: Type[BaseModel]) -> List[str]:
|
139
|
+
"""Get all field names from a possibly nested Pydantic model."""
|
140
|
+
mdl = flatten_pydantic_model(model)
|
141
|
+
fields = list(mdl.__fields__.keys())
|
142
|
+
# fields may be like a__b__c , so we only want the last part
|
143
|
+
return [f.split("__")[-1] for f in fields]
|
144
|
+
|
145
|
+
|
146
|
+
def generate_simple_schema(
|
147
|
+
model: Type[BaseModel], exclude: List[str] = []
|
148
|
+
) -> Dict[str, Any]:
|
149
|
+
"""
|
150
|
+
Generates a JSON schema for a Pydantic model,
|
151
|
+
with options to exclude specific fields.
|
152
|
+
|
153
|
+
This function traverses the Pydantic model's fields, including nested models,
|
154
|
+
to generate a dictionary representing the JSON schema. Fields specified in
|
155
|
+
the exclude list will not be included in the generated schema.
|
156
|
+
|
157
|
+
Args:
|
158
|
+
model (Type[BaseModel]): The Pydantic model class to generate the schema for.
|
159
|
+
exclude (List[str]): A list of string field names to be excluded from the
|
160
|
+
generated schema. Defaults to an empty list.
|
161
|
+
|
162
|
+
Returns:
|
163
|
+
Dict[str, Any]: A dictionary representing the JSON schema of the provided model,
|
164
|
+
with specified fields excluded.
|
165
|
+
"""
|
166
|
+
if hasattr(model, "__fields__"):
|
167
|
+
output: Dict[str, Any] = {}
|
168
|
+
for field_name, field in model.__fields__.items():
|
169
|
+
if field_name in exclude:
|
170
|
+
continue # Skip excluded fields
|
171
|
+
|
172
|
+
field_type = field.type_
|
173
|
+
if issubclass(field_type, BaseModel):
|
174
|
+
# Recursively generate schema for nested models
|
175
|
+
output[field_name] = generate_simple_schema(field_type, exclude)
|
176
|
+
else:
|
177
|
+
# Represent the type as a string here
|
178
|
+
output[field_name] = {"type": field_type.__name__}
|
179
|
+
return output
|
180
|
+
else:
|
181
|
+
# Non-model type, return a simplified representation
|
182
|
+
return {"type": model.__name__}
|
183
|
+
|
184
|
+
|
138
185
|
def flatten_pydantic_instance(
|
139
186
|
instance: BaseModel,
|
140
187
|
prefix: str = "",
|
langroid/utils/system.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
import getpass
|
2
2
|
import hashlib
|
3
|
+
import importlib
|
3
4
|
import inspect
|
4
5
|
import logging
|
5
6
|
import shutil
|
6
7
|
import socket
|
7
8
|
import traceback
|
9
|
+
from typing import Any
|
8
10
|
|
9
11
|
logger = logging.getLogger(__name__)
|
10
12
|
|
@@ -15,6 +17,39 @@ DELETION_ALLOWED_PATHS = [
|
|
15
17
|
]
|
16
18
|
|
17
19
|
|
20
|
+
class LazyLoad:
|
21
|
+
"""Lazy loading of modules or classes."""
|
22
|
+
|
23
|
+
def __init__(self, import_path: str) -> None:
|
24
|
+
self.import_path = import_path
|
25
|
+
self._target = None
|
26
|
+
self._is_target_loaded = False
|
27
|
+
|
28
|
+
def _load_target(self) -> None:
|
29
|
+
if not self._is_target_loaded:
|
30
|
+
try:
|
31
|
+
# Attempt to import as a module
|
32
|
+
self._target = importlib.import_module(self.import_path) # type: ignore
|
33
|
+
except ImportError:
|
34
|
+
# If module import fails, attempt to import as a
|
35
|
+
# class or function from a module
|
36
|
+
module_path, attr_name = self.import_path.rsplit(".", 1)
|
37
|
+
module = importlib.import_module(module_path)
|
38
|
+
self._target = getattr(module, attr_name)
|
39
|
+
self._is_target_loaded = True
|
40
|
+
|
41
|
+
def __getattr__(self, name: str) -> Any:
|
42
|
+
self._load_target()
|
43
|
+
return getattr(self._target, name)
|
44
|
+
|
45
|
+
def __call__(self, *args: Any, **kwargs: Any) -> Any:
|
46
|
+
self._load_target()
|
47
|
+
if callable(self._target):
|
48
|
+
return self._target(*args, **kwargs)
|
49
|
+
else:
|
50
|
+
raise TypeError(f"{self.import_path!r} object is not callable")
|
51
|
+
|
52
|
+
|
18
53
|
def rmdir(path: str) -> bool:
|
19
54
|
"""
|
20
55
|
Remove a directory recursively.
|
@@ -4,8 +4,8 @@ langroid/agent/base.py,sha256=82nUFCeQ9M71zCFUdNJHihJVytphRWhy81ZyMTNzrXg,35020
|
|
4
4
|
langroid/agent/batch.py,sha256=8zHdM-863pRD3UoCXUPKEQ4Z4iqjkNVD2xXu1WspBak,6464
|
5
5
|
langroid/agent/callbacks/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
6
|
langroid/agent/callbacks/chainlit.py,sha256=aYuJ8M4VDHr5oymoXL2bpThM7p6P9L45fgJf3MLdkWo,20997
|
7
|
-
langroid/agent/chat_agent.py,sha256=
|
8
|
-
langroid/agent/chat_document.py,sha256=
|
7
|
+
langroid/agent/chat_agent.py,sha256=QCAA56rObVrnKyCdO5pqklUukxkU4aykoUZzZ2tYvU8,38609
|
8
|
+
langroid/agent/chat_document.py,sha256=cxLCFgimnX82sJny3gCDfQGCdRvnerTUfa2zJyd8X28,7963
|
9
9
|
langroid/agent/helpers.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
10
10
|
langroid/agent/junk,sha256=LxfuuW7Cijsg0szAzT81OjWWv1PMNI-6w_-DspVIO2s,339
|
11
11
|
langroid/agent/openai_assistant.py,sha256=yBtxis64XOnxtJzlkwUoTm-wCyvKr4DGo9-laXYMok0,32654
|
@@ -32,8 +32,8 @@ langroid/agent/special/sql/utils/populate_metadata.py,sha256=5-tkmMV50iTT4Af_Lu4
|
|
32
32
|
langroid/agent/special/sql/utils/system_message.py,sha256=qKLHkvQWRQodTtPLPxr1GSLUYUFASZU8x-ybV67cB68,1885
|
33
33
|
langroid/agent/special/sql/utils/tools.py,sha256=6uB2424SLtmapui9ggcEr0ZTiB6_dL1-JRGgN8RK9Js,1332
|
34
34
|
langroid/agent/special/table_chat_agent.py,sha256=GEUTP-VdtMXq4CcPV80gDQrCEn-ZFb9IhuRMtLN5I1o,9030
|
35
|
-
langroid/agent/task.py,sha256=
|
36
|
-
langroid/agent/tool_message.py,sha256=
|
35
|
+
langroid/agent/task.py,sha256=BxMGmwH0ZYbU5lylfQtU9qLMd9D9Qd6qqO1U2V_B0WM,49705
|
36
|
+
langroid/agent/tool_message.py,sha256=HXre9B8kVnwcGTv-czO0y-Z0hMDIuf6TKiS16_6djEQ,8207
|
37
37
|
langroid/agent/tools/__init__.py,sha256=q-maq3k2BXhPAU99G0H6-j_ozoRvx15I1RFpPVicQIU,304
|
38
38
|
langroid/agent/tools/duckduckgo_search_tool.py,sha256=lgBFIPGdEffyxFuP6NUqRVBXyqypqHHSQBf-06xWsZE,2460
|
39
39
|
langroid/agent/tools/extract_tool.py,sha256=u5lL9rKBzaLBOrRyLnTAZ97pQ1uxyLP39XsWMnpaZpw,3789
|
@@ -71,7 +71,7 @@ langroid/parsing/code-parsing.md,sha256=--cyyNiSZSDlIwcjAV4-shKrSiRe2ytF3AdSoS_h
|
|
71
71
|
langroid/parsing/code_parser.py,sha256=BbDAzp35wkYQ9U1dpf1ARL0lVyi0tfqEc6_eox2C090,3727
|
72
72
|
langroid/parsing/config.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
73
73
|
langroid/parsing/document_parser.py,sha256=SEW53fnEsOrsJbVUy9Fq5ygQzF_5UiGB5_Ogkte1u2Y,16697
|
74
|
-
langroid/parsing/json.py,sha256=
|
74
|
+
langroid/parsing/json.py,sha256=2eO-0-VAYyBjeUbeB3FNw-8PKUSmnyFWaRb0EzLxoZk,3859
|
75
75
|
langroid/parsing/para_sentence_split.py,sha256=AJBzZojP3zpB-_IMiiHismhqcvkrVBQ3ZINoQyx_bE4,2000
|
76
76
|
langroid/parsing/parser.py,sha256=727QivWlZNlQiRFgkxTZpPoTMqB2yaltOkAGqLZGI_Q,10513
|
77
77
|
langroid/parsing/repo_loader.py,sha256=52jTajXOkq_66NCRKLMNQoGKMJ59H-m2CZB9arMT7Wo,29346
|
@@ -102,8 +102,8 @@ langroid/utils/logging.py,sha256=R8TN-FqVpwZ4Ajgls9TDMthLvPpQd0QVNXK-PJDj1Z8,391
|
|
102
102
|
langroid/utils/output/__init__.py,sha256=Z58-2ZKnGpGNaKw_nEjHV_CHTzjMz-WRSRQnazTLrWU,289
|
103
103
|
langroid/utils/output/printing.py,sha256=5EsYB1O4qKhocW19aebOUzK82RD9U5nygbY21yo8gfg,2872
|
104
104
|
langroid/utils/pandas_utils.py,sha256=nSA1tIgOUTkRDn-IKq7HP8XGJcL6bA110LcPfRF7h8I,707
|
105
|
-
langroid/utils/pydantic_utils.py,sha256=
|
106
|
-
langroid/utils/system.py,sha256=
|
105
|
+
langroid/utils/pydantic_utils.py,sha256=yb-ghaQYL7EIYeiZ0tailvZvAuJZNF7UBXkd3z35OYc,21728
|
106
|
+
langroid/utils/system.py,sha256=l-kFqIWkSD9YHOTka02dnihdbnR1mWVrnKSGK3LuEjo,4577
|
107
107
|
langroid/utils/web/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
108
108
|
langroid/utils/web/login.py,sha256=1iz9eUAHa87vpKIkzwkmFa00avwFWivDSAr7QUhK7U0,2528
|
109
109
|
langroid/utils/web/selenium_login.py,sha256=mYI6EvVmne34N9RajlsxxRqJQJvV-WG4LGp6sEECHPw,1156
|
@@ -115,7 +115,7 @@ langroid/vector_store/meilisearch.py,sha256=d2huA9P-NoYRuAQ9ZeXJmMKr7ry8u90RUSR2
|
|
115
115
|
langroid/vector_store/momento.py,sha256=j6Eo6oIDN2fe7lsBOlCXJn3uvvERHHTFL5QJfeREeOM,10044
|
116
116
|
langroid/vector_store/qdrant_cloud.py,sha256=3im4Mip0QXLkR6wiqVsjV1QvhSElfxdFSuDKddBDQ-4,188
|
117
117
|
langroid/vector_store/qdrantdb.py,sha256=_egbsP9SWBwmI827EDYSSOqfIQSmwNsmJfFTxrLpWYE,13457
|
118
|
-
langroid-0.1.
|
119
|
-
langroid-0.1.
|
120
|
-
langroid-0.1.
|
121
|
-
langroid-0.1.
|
118
|
+
langroid-0.1.197.dist-info/LICENSE,sha256=EgVbvA6VSYgUlvC3RvPKehSg7MFaxWDsFuzLOsPPfJg,1065
|
119
|
+
langroid-0.1.197.dist-info/METADATA,sha256=hr_8uh8gl747E4gOZE1EfoK5-B1sxPshYkK18ox7CX8,45876
|
120
|
+
langroid-0.1.197.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
121
|
+
langroid-0.1.197.dist-info/RECORD,,
|
File without changes
|
File without changes
|