langroid 0.1.196__tar.gz → 0.1.197__tar.gz

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 (121) hide show
  1. {langroid-0.1.196 → langroid-0.1.197}/PKG-INFO +1 -1
  2. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/chat_agent.py +10 -2
  3. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/chat_document.py +1 -1
  4. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/task.py +7 -3
  5. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tool_message.py +28 -6
  6. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/json.py +38 -2
  7. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/pydantic_utils.py +47 -0
  8. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/system.py +35 -0
  9. {langroid-0.1.196 → langroid-0.1.197}/pyproject.toml +1 -1
  10. {langroid-0.1.196 → langroid-0.1.197}/LICENSE +0 -0
  11. {langroid-0.1.196 → langroid-0.1.197}/README.md +0 -0
  12. {langroid-0.1.196 → langroid-0.1.197}/langroid/__init__.py +0 -0
  13. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/__init__.py +0 -0
  14. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/base.py +0 -0
  15. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/batch.py +0 -0
  16. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/callbacks/__init__.py +0 -0
  17. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/callbacks/chainlit.py +0 -0
  18. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/helpers.py +0 -0
  19. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/junk +0 -0
  20. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/openai_assistant.py +0 -0
  21. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/__init__.py +0 -0
  22. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/doc_chat_agent.py +0 -0
  23. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/lance_doc_chat_agent.py +0 -0
  24. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/lance_rag/__init__.py +0 -0
  25. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/lance_rag/critic_agent.py +0 -0
  26. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/lance_rag/lance_rag_task.py +0 -0
  27. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/lance_rag/lance_tools.py +0 -0
  28. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/lance_rag/query_planner_agent.py +0 -0
  29. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/neo4j/__init__.py +0 -0
  30. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/neo4j/csv_kg_chat.py +0 -0
  31. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/neo4j/neo4j_chat_agent.py +0 -0
  32. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/neo4j/utils/__init__.py +0 -0
  33. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/neo4j/utils/system_message.py +0 -0
  34. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/relevance_extractor_agent.py +0 -0
  35. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/retriever_agent.py +0 -0
  36. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/__init__.py +0 -0
  37. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/sql_chat_agent.py +0 -0
  38. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/utils/__init__.py +0 -0
  39. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/utils/description_extractors.py +0 -0
  40. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/utils/populate_metadata.py +0 -0
  41. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/utils/system_message.py +0 -0
  42. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/sql/utils/tools.py +0 -0
  43. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/special/table_chat_agent.py +0 -0
  44. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/__init__.py +0 -0
  45. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/duckduckgo_search_tool.py +0 -0
  46. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/extract_tool.py +0 -0
  47. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/generator_tool.py +0 -0
  48. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/google_search_tool.py +0 -0
  49. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/metaphor_search_tool.py +0 -0
  50. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/recipient_tool.py +0 -0
  51. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/run_python_code.py +0 -0
  52. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/sciphi_search_rag_tool.py +0 -0
  53. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent/tools/segment_extract_tool.py +0 -0
  54. {langroid-0.1.196 → langroid-0.1.197}/langroid/agent_config.py +0 -0
  55. {langroid-0.1.196 → langroid-0.1.197}/langroid/cachedb/__init__.py +0 -0
  56. {langroid-0.1.196 → langroid-0.1.197}/langroid/cachedb/base.py +0 -0
  57. {langroid-0.1.196 → langroid-0.1.197}/langroid/cachedb/momento_cachedb.py +0 -0
  58. {langroid-0.1.196 → langroid-0.1.197}/langroid/cachedb/redis_cachedb.py +0 -0
  59. {langroid-0.1.196 → langroid-0.1.197}/langroid/embedding_models/__init__.py +0 -0
  60. {langroid-0.1.196 → langroid-0.1.197}/langroid/embedding_models/base.py +0 -0
  61. {langroid-0.1.196 → langroid-0.1.197}/langroid/embedding_models/clustering.py +0 -0
  62. {langroid-0.1.196 → langroid-0.1.197}/langroid/embedding_models/models.py +0 -0
  63. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/__init__.py +0 -0
  64. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/azure_openai.py +0 -0
  65. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/base.py +0 -0
  66. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/config.py +0 -0
  67. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/openai_assistants.py +0 -0
  68. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/openai_gpt.py +0 -0
  69. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/prompt_formatter/__init__.py +0 -0
  70. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/prompt_formatter/base.py +0 -0
  71. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/prompt_formatter/hf_formatter.py +0 -0
  72. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/prompt_formatter/llama2_formatter.py +0 -0
  73. {langroid-0.1.196 → langroid-0.1.197}/langroid/language_models/utils.py +0 -0
  74. {langroid-0.1.196 → langroid-0.1.197}/langroid/mytypes.py +0 -0
  75. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/__init__.py +0 -0
  76. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/agent_chats.py +0 -0
  77. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/code-parsing.md +0 -0
  78. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/code_parser.py +0 -0
  79. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/config.py +0 -0
  80. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/document_parser.py +0 -0
  81. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/para_sentence_split.py +0 -0
  82. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/parser.py +0 -0
  83. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/repo_loader.py +0 -0
  84. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/search.py +0 -0
  85. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/spider.py +0 -0
  86. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/table_loader.py +0 -0
  87. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/url_loader.py +0 -0
  88. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/url_loader_cookies.py +0 -0
  89. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/urls.py +0 -0
  90. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/utils.py +0 -0
  91. {langroid-0.1.196 → langroid-0.1.197}/langroid/parsing/web_search.py +0 -0
  92. {langroid-0.1.196 → langroid-0.1.197}/langroid/prompts/__init__.py +0 -0
  93. {langroid-0.1.196 → langroid-0.1.197}/langroid/prompts/chat-gpt4-system-prompt.md +0 -0
  94. {langroid-0.1.196 → langroid-0.1.197}/langroid/prompts/dialog.py +0 -0
  95. {langroid-0.1.196 → langroid-0.1.197}/langroid/prompts/prompts_config.py +0 -0
  96. {langroid-0.1.196 → langroid-0.1.197}/langroid/prompts/templates.py +0 -0
  97. {langroid-0.1.196 → langroid-0.1.197}/langroid/prompts/transforms.py +0 -0
  98. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/__init__.py +0 -0
  99. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/algorithms/__init__.py +0 -0
  100. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/algorithms/graph.py +0 -0
  101. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/configuration.py +0 -0
  102. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/constants.py +0 -0
  103. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/docker.py +0 -0
  104. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/globals.py +0 -0
  105. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/llms/__init__.py +0 -0
  106. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/llms/strings.py +0 -0
  107. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/logging.py +0 -0
  108. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/output/__init__.py +0 -0
  109. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/output/printing.py +0 -0
  110. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/pandas_utils.py +0 -0
  111. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/web/__init__.py +0 -0
  112. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/web/login.py +0 -0
  113. {langroid-0.1.196 → langroid-0.1.197}/langroid/utils/web/selenium_login.py +0 -0
  114. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/__init__.py +0 -0
  115. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/base.py +0 -0
  116. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/chromadb.py +0 -0
  117. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/lancedb.py +0 -0
  118. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/meilisearch.py +0 -0
  119. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/momento.py +0 -0
  120. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/qdrant_cloud.py +0 -0
  121. {langroid-0.1.196 → langroid-0.1.197}/langroid/vector_store/qdrantdb.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langroid
3
- Version: 0.1.196
3
+ Version: 0.1.197
4
4
  Summary: Harness LLMs with Multi-Agent Programming
5
5
  License: MIT
6
6
  Author: Prasad Chalasani
@@ -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
  )
@@ -84,7 +84,7 @@ class ChatDocument(Document):
84
84
  json_data = json.loads(j)
85
85
  tool = json_data.get("request")
86
86
  if tool is not None:
87
- tools.append(tool)
87
+ tools.append(str(tool))
88
88
  return tools
89
89
 
90
90
  def log_fields(self) -> ChatDocLoggerFields:
@@ -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 content.split(":")[1] != "":
789
- recipient = content.split(":")[1]
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}:{recipient}", "").strip()[1:]
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
@@ -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 _recursive_purge_dict_key
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
@@ -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 using regular expressions
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 normalized_candidates if is_valid_json(candidate)
100
+ candidate for candidate in candidates if is_valid_json(candidate)
65
101
  ]
66
102
 
67
103
  return top_level_jsons
@@ -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 = "",
@@ -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.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "langroid"
3
- version = "0.1.196"
3
+ version = "0.1.197"
4
4
  description = "Harness LLMs with Multi-Agent Programming"
5
5
  authors = ["Prasad Chalasani <pchalasani@gmail.com>"]
6
6
  readme = "README.md"
File without changes
File without changes