langchain 0.3.27__py3-none-any.whl → 0.4.0.dev0__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.
- langchain/agents/agent.py +16 -20
- langchain/agents/agent_iterator.py +19 -12
- langchain/agents/agent_toolkits/vectorstore/base.py +2 -0
- langchain/agents/chat/base.py +2 -0
- langchain/agents/conversational/base.py +2 -0
- langchain/agents/conversational_chat/base.py +2 -0
- langchain/agents/initialize.py +1 -1
- langchain/agents/json_chat/base.py +1 -0
- langchain/agents/mrkl/base.py +2 -0
- langchain/agents/openai_assistant/base.py +1 -1
- langchain/agents/openai_functions_agent/agent_token_buffer_memory.py +2 -0
- langchain/agents/openai_functions_agent/base.py +3 -2
- langchain/agents/openai_functions_multi_agent/base.py +1 -1
- langchain/agents/openai_tools/base.py +1 -0
- langchain/agents/output_parsers/json.py +2 -0
- langchain/agents/output_parsers/openai_functions.py +10 -3
- langchain/agents/output_parsers/openai_tools.py +8 -1
- langchain/agents/output_parsers/react_json_single_input.py +3 -0
- langchain/agents/output_parsers/react_single_input.py +3 -0
- langchain/agents/output_parsers/self_ask.py +2 -0
- langchain/agents/output_parsers/tools.py +16 -2
- langchain/agents/output_parsers/xml.py +3 -0
- langchain/agents/react/agent.py +1 -0
- langchain/agents/react/base.py +4 -0
- langchain/agents/react/output_parser.py +2 -0
- langchain/agents/schema.py +2 -0
- langchain/agents/self_ask_with_search/base.py +4 -0
- langchain/agents/structured_chat/base.py +5 -0
- langchain/agents/structured_chat/output_parser.py +13 -0
- langchain/agents/tool_calling_agent/base.py +1 -0
- langchain/agents/tools.py +3 -0
- langchain/agents/xml/base.py +7 -1
- langchain/callbacks/streaming_aiter.py +13 -2
- langchain/callbacks/streaming_aiter_final_only.py +11 -2
- langchain/callbacks/streaming_stdout_final_only.py +5 -0
- langchain/callbacks/tracers/logging.py +11 -0
- langchain/chains/api/base.py +5 -1
- langchain/chains/base.py +8 -2
- langchain/chains/combine_documents/base.py +7 -1
- langchain/chains/combine_documents/map_reduce.py +3 -0
- langchain/chains/combine_documents/map_rerank.py +6 -4
- langchain/chains/combine_documents/reduce.py +1 -0
- langchain/chains/combine_documents/refine.py +1 -0
- langchain/chains/combine_documents/stuff.py +5 -1
- langchain/chains/constitutional_ai/base.py +7 -0
- langchain/chains/conversation/base.py +4 -1
- langchain/chains/conversational_retrieval/base.py +67 -59
- langchain/chains/elasticsearch_database/base.py +2 -1
- langchain/chains/flare/base.py +2 -0
- langchain/chains/flare/prompts.py +2 -0
- langchain/chains/llm.py +7 -2
- langchain/chains/llm_bash/__init__.py +1 -1
- langchain/chains/llm_checker/base.py +12 -1
- langchain/chains/llm_math/base.py +9 -1
- langchain/chains/llm_summarization_checker/base.py +13 -1
- langchain/chains/llm_symbolic_math/__init__.py +1 -1
- langchain/chains/loading.py +4 -2
- langchain/chains/moderation.py +3 -0
- langchain/chains/natbot/base.py +3 -1
- langchain/chains/natbot/crawler.py +29 -0
- langchain/chains/openai_functions/base.py +2 -0
- langchain/chains/openai_functions/citation_fuzzy_match.py +9 -0
- langchain/chains/openai_functions/openapi.py +4 -0
- langchain/chains/openai_functions/qa_with_structure.py +3 -3
- langchain/chains/openai_functions/tagging.py +2 -0
- langchain/chains/qa_generation/base.py +4 -0
- langchain/chains/qa_with_sources/base.py +3 -0
- langchain/chains/qa_with_sources/retrieval.py +1 -1
- langchain/chains/qa_with_sources/vector_db.py +4 -2
- langchain/chains/query_constructor/base.py +4 -2
- langchain/chains/query_constructor/parser.py +64 -2
- langchain/chains/retrieval_qa/base.py +4 -0
- langchain/chains/router/base.py +14 -2
- langchain/chains/router/embedding_router.py +3 -0
- langchain/chains/router/llm_router.py +6 -4
- langchain/chains/router/multi_prompt.py +3 -0
- langchain/chains/router/multi_retrieval_qa.py +18 -0
- langchain/chains/sql_database/query.py +1 -0
- langchain/chains/structured_output/base.py +2 -0
- langchain/chains/transform.py +4 -0
- langchain/chat_models/base.py +55 -18
- langchain/document_loaders/blob_loaders/schema.py +1 -4
- langchain/embeddings/base.py +2 -0
- langchain/embeddings/cache.py +3 -3
- langchain/evaluation/agents/trajectory_eval_chain.py +3 -2
- langchain/evaluation/comparison/eval_chain.py +1 -0
- langchain/evaluation/criteria/eval_chain.py +3 -0
- langchain/evaluation/embedding_distance/base.py +11 -0
- langchain/evaluation/exact_match/base.py +14 -1
- langchain/evaluation/loading.py +1 -0
- langchain/evaluation/parsing/base.py +16 -3
- langchain/evaluation/parsing/json_distance.py +19 -8
- langchain/evaluation/parsing/json_schema.py +1 -4
- langchain/evaluation/qa/eval_chain.py +8 -0
- langchain/evaluation/qa/generate_chain.py +2 -0
- langchain/evaluation/regex_match/base.py +9 -1
- langchain/evaluation/scoring/eval_chain.py +1 -0
- langchain/evaluation/string_distance/base.py +6 -0
- langchain/memory/buffer.py +5 -0
- langchain/memory/buffer_window.py +2 -0
- langchain/memory/combined.py +1 -1
- langchain/memory/entity.py +47 -0
- langchain/memory/simple.py +3 -0
- langchain/memory/summary.py +30 -0
- langchain/memory/summary_buffer.py +3 -0
- langchain/memory/token_buffer.py +2 -0
- langchain/output_parsers/combining.py +4 -2
- langchain/output_parsers/enum.py +5 -1
- langchain/output_parsers/fix.py +8 -1
- langchain/output_parsers/pandas_dataframe.py +16 -1
- langchain/output_parsers/regex.py +2 -0
- langchain/output_parsers/retry.py +21 -1
- langchain/output_parsers/structured.py +10 -0
- langchain/output_parsers/yaml.py +4 -0
- langchain/pydantic_v1/__init__.py +1 -1
- langchain/retrievers/document_compressors/chain_extract.py +4 -2
- langchain/retrievers/document_compressors/cohere_rerank.py +2 -0
- langchain/retrievers/document_compressors/cross_encoder_rerank.py +2 -0
- langchain/retrievers/document_compressors/embeddings_filter.py +3 -0
- langchain/retrievers/document_compressors/listwise_rerank.py +1 -0
- langchain/retrievers/ensemble.py +2 -2
- langchain/retrievers/multi_query.py +3 -1
- langchain/retrievers/multi_vector.py +4 -1
- langchain/retrievers/parent_document_retriever.py +15 -0
- langchain/retrievers/self_query/base.py +19 -0
- langchain/retrievers/time_weighted_retriever.py +3 -0
- langchain/runnables/hub.py +12 -0
- langchain/runnables/openai_functions.py +6 -0
- langchain/smith/__init__.py +1 -0
- langchain/smith/evaluation/config.py +5 -22
- langchain/smith/evaluation/progress.py +12 -3
- langchain/smith/evaluation/runner_utils.py +240 -123
- langchain/smith/evaluation/string_run_evaluator.py +27 -0
- langchain/storage/encoder_backed.py +1 -0
- langchain/tools/python/__init__.py +1 -1
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/METADATA +2 -12
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/RECORD +140 -141
- langchain/smith/evaluation/utils.py +0 -0
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/WHEEL +0 -0
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/entry_points.txt +0 -0
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/licenses/LICENSE +0 -0
langchain/agents/agent.py
CHANGED
|
@@ -64,6 +64,7 @@ class BaseSingleActionAgent(BaseModel):
|
|
|
64
64
|
return ["output"]
|
|
65
65
|
|
|
66
66
|
def get_allowed_tools(self) -> Optional[list[str]]:
|
|
67
|
+
"""Get allowed tools."""
|
|
67
68
|
return None
|
|
68
69
|
|
|
69
70
|
@abstractmethod
|
|
@@ -115,8 +116,8 @@ class BaseSingleActionAgent(BaseModel):
|
|
|
115
116
|
def return_stopped_response(
|
|
116
117
|
self,
|
|
117
118
|
early_stopping_method: str,
|
|
118
|
-
intermediate_steps: list[tuple[AgentAction, str]],
|
|
119
|
-
**
|
|
119
|
+
intermediate_steps: list[tuple[AgentAction, str]], # noqa: ARG002
|
|
120
|
+
**_: Any,
|
|
120
121
|
) -> AgentFinish:
|
|
121
122
|
"""Return response when agent has been stopped due to max iterations.
|
|
122
123
|
|
|
@@ -124,7 +125,6 @@ class BaseSingleActionAgent(BaseModel):
|
|
|
124
125
|
early_stopping_method: Method to use for early stopping.
|
|
125
126
|
intermediate_steps: Steps the LLM has taken to date,
|
|
126
127
|
along with observations.
|
|
127
|
-
**kwargs: User inputs.
|
|
128
128
|
|
|
129
129
|
Returns:
|
|
130
130
|
AgentFinish: Agent finish object.
|
|
@@ -167,6 +167,7 @@ class BaseSingleActionAgent(BaseModel):
|
|
|
167
167
|
"""Return Identifier of an agent type."""
|
|
168
168
|
raise NotImplementedError
|
|
169
169
|
|
|
170
|
+
@override
|
|
170
171
|
def dict(self, **kwargs: Any) -> builtins.dict:
|
|
171
172
|
"""Return dictionary representation of agent.
|
|
172
173
|
|
|
@@ -195,6 +196,7 @@ class BaseSingleActionAgent(BaseModel):
|
|
|
195
196
|
|
|
196
197
|
# If working with agent executor
|
|
197
198
|
agent.agent.save(file_path="path/agent.yaml")
|
|
199
|
+
|
|
198
200
|
"""
|
|
199
201
|
# Convert file to Path object.
|
|
200
202
|
save_path = Path(file_path) if isinstance(file_path, str) else file_path
|
|
@@ -288,8 +290,8 @@ class BaseMultiActionAgent(BaseModel):
|
|
|
288
290
|
def return_stopped_response(
|
|
289
291
|
self,
|
|
290
292
|
early_stopping_method: str,
|
|
291
|
-
intermediate_steps: list[tuple[AgentAction, str]],
|
|
292
|
-
**
|
|
293
|
+
intermediate_steps: list[tuple[AgentAction, str]], # noqa: ARG002
|
|
294
|
+
**_: Any,
|
|
293
295
|
) -> AgentFinish:
|
|
294
296
|
"""Return response when agent has been stopped due to max iterations.
|
|
295
297
|
|
|
@@ -297,7 +299,6 @@ class BaseMultiActionAgent(BaseModel):
|
|
|
297
299
|
early_stopping_method: Method to use for early stopping.
|
|
298
300
|
intermediate_steps: Steps the LLM has taken to date,
|
|
299
301
|
along with observations.
|
|
300
|
-
**kwargs: User inputs.
|
|
301
302
|
|
|
302
303
|
Returns:
|
|
303
304
|
AgentFinish: Agent finish object.
|
|
@@ -316,6 +317,7 @@ class BaseMultiActionAgent(BaseModel):
|
|
|
316
317
|
"""Return Identifier of an agent type."""
|
|
317
318
|
raise NotImplementedError
|
|
318
319
|
|
|
320
|
+
@override
|
|
319
321
|
def dict(self, **kwargs: Any) -> builtins.dict:
|
|
320
322
|
"""Return dictionary representation of agent."""
|
|
321
323
|
_dict = super().model_dump()
|
|
@@ -338,6 +340,7 @@ class BaseMultiActionAgent(BaseModel):
|
|
|
338
340
|
|
|
339
341
|
# If working with agent executor
|
|
340
342
|
agent.agent.save(file_path="path/agent.yaml")
|
|
343
|
+
|
|
341
344
|
"""
|
|
342
345
|
# Convert file to Path object.
|
|
343
346
|
save_path = Path(file_path) if isinstance(file_path, str) else file_path
|
|
@@ -650,6 +653,7 @@ class LLMSingleActionAgent(BaseSingleActionAgent):
|
|
|
650
653
|
"""
|
|
651
654
|
return list(set(self.llm_chain.input_keys) - {"intermediate_steps"})
|
|
652
655
|
|
|
656
|
+
@override
|
|
653
657
|
def dict(self, **kwargs: Any) -> builtins.dict:
|
|
654
658
|
"""Return dictionary representation of agent."""
|
|
655
659
|
_dict = super().dict()
|
|
@@ -734,6 +738,7 @@ class Agent(BaseSingleActionAgent):
|
|
|
734
738
|
allowed_tools: Optional[list[str]] = None
|
|
735
739
|
"""Allowed tools for the agent. If None, all tools are allowed."""
|
|
736
740
|
|
|
741
|
+
@override
|
|
737
742
|
def dict(self, **kwargs: Any) -> builtins.dict:
|
|
738
743
|
"""Return dictionary representation of agent."""
|
|
739
744
|
_dict = super().dict()
|
|
@@ -749,18 +754,6 @@ class Agent(BaseSingleActionAgent):
|
|
|
749
754
|
"""Return values of the agent."""
|
|
750
755
|
return ["output"]
|
|
751
756
|
|
|
752
|
-
def _fix_text(self, text: str) -> str:
|
|
753
|
-
"""Fix the text.
|
|
754
|
-
|
|
755
|
-
Args:
|
|
756
|
-
text: Text to fix.
|
|
757
|
-
|
|
758
|
-
Returns:
|
|
759
|
-
str: Fixed text.
|
|
760
|
-
"""
|
|
761
|
-
msg = "fix_text not implemented for this agent."
|
|
762
|
-
raise ValueError(msg)
|
|
763
|
-
|
|
764
757
|
@property
|
|
765
758
|
def _stop(self) -> list[str]:
|
|
766
759
|
return [
|
|
@@ -1020,6 +1013,7 @@ class ExceptionTool(BaseTool):
|
|
|
1020
1013
|
description: str = "Exception tool"
|
|
1021
1014
|
"""Description of the tool."""
|
|
1022
1015
|
|
|
1016
|
+
@override
|
|
1023
1017
|
def _run(
|
|
1024
1018
|
self,
|
|
1025
1019
|
query: str,
|
|
@@ -1027,6 +1021,7 @@ class ExceptionTool(BaseTool):
|
|
|
1027
1021
|
) -> str:
|
|
1028
1022
|
return query
|
|
1029
1023
|
|
|
1024
|
+
@override
|
|
1030
1025
|
async def _arun(
|
|
1031
1026
|
self,
|
|
1032
1027
|
query: str,
|
|
@@ -1184,9 +1179,10 @@ class AgentExecutor(Chain):
|
|
|
1184
1179
|
to reflect the changes made in the root_validator.
|
|
1185
1180
|
"""
|
|
1186
1181
|
if isinstance(self.agent, Runnable):
|
|
1187
|
-
return cast(RunnableAgentType, self.agent)
|
|
1182
|
+
return cast("RunnableAgentType", self.agent)
|
|
1188
1183
|
return self.agent
|
|
1189
1184
|
|
|
1185
|
+
@override
|
|
1190
1186
|
def save(self, file_path: Union[Path, str]) -> None:
|
|
1191
1187
|
"""Raise error - saving not supported for Agent Executors.
|
|
1192
1188
|
|
|
@@ -1217,7 +1213,7 @@ class AgentExecutor(Chain):
|
|
|
1217
1213
|
callbacks: Callbacks = None,
|
|
1218
1214
|
*,
|
|
1219
1215
|
include_run_info: bool = False,
|
|
1220
|
-
async_: bool = False, # arg kept for backwards compat, but ignored
|
|
1216
|
+
async_: bool = False, # noqa: ARG002 arg kept for backwards compat, but ignored
|
|
1221
1217
|
) -> AgentExecutorIterator:
|
|
1222
1218
|
"""Enables iteration over steps taken to reach final output.
|
|
1223
1219
|
|
|
@@ -157,6 +157,12 @@ class AgentExecutorIterator:
|
|
|
157
157
|
outputs: dict[str, Any],
|
|
158
158
|
run_manager: Union[CallbackManagerForChainRun, AsyncCallbackManagerForChainRun],
|
|
159
159
|
) -> AddableDict:
|
|
160
|
+
"""Make final outputs for the iterator.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
outputs: The outputs from the agent executor.
|
|
164
|
+
run_manager: The run manager to use for callbacks.
|
|
165
|
+
"""
|
|
160
166
|
# have access to intermediate steps by design in iterator,
|
|
161
167
|
# so return only outputs may as well always be true.
|
|
162
168
|
|
|
@@ -172,6 +178,7 @@ class AgentExecutorIterator:
|
|
|
172
178
|
return prepared_outputs
|
|
173
179
|
|
|
174
180
|
def __iter__(self: AgentExecutorIterator) -> Iterator[AddableDict]:
|
|
181
|
+
"""Create an async iterator for the AgentExecutor."""
|
|
175
182
|
logger.debug("Initialising AgentExecutorIterator")
|
|
176
183
|
self.reset()
|
|
177
184
|
callback_manager = CallbackManager.configure(
|
|
@@ -190,14 +197,14 @@ class AgentExecutorIterator:
|
|
|
190
197
|
name=self.run_name,
|
|
191
198
|
)
|
|
192
199
|
try:
|
|
193
|
-
while self.agent_executor._should_continue(
|
|
200
|
+
while self.agent_executor._should_continue( # noqa: SLF001
|
|
194
201
|
self.iterations,
|
|
195
202
|
self.time_elapsed,
|
|
196
203
|
):
|
|
197
204
|
# take the next step: this plans next action, executes it,
|
|
198
205
|
# yielding action and observation as they are generated
|
|
199
206
|
next_step_seq: NextStepOutput = []
|
|
200
|
-
for chunk in self.agent_executor._iter_next_step(
|
|
207
|
+
for chunk in self.agent_executor._iter_next_step( # noqa: SLF001
|
|
201
208
|
self.name_to_tool_map,
|
|
202
209
|
self.color_mapping,
|
|
203
210
|
self.inputs,
|
|
@@ -214,7 +221,7 @@ class AgentExecutorIterator:
|
|
|
214
221
|
yield AddableDict(steps=[chunk], messages=chunk.messages)
|
|
215
222
|
|
|
216
223
|
# convert iterator output to format handled by _process_next_step_output
|
|
217
|
-
next_step = self.agent_executor._consume_next_step(next_step_seq)
|
|
224
|
+
next_step = self.agent_executor._consume_next_step(next_step_seq) # noqa: SLF001
|
|
218
225
|
# update iterations and time elapsed
|
|
219
226
|
self.update_iterations()
|
|
220
227
|
# decide if this is the final output
|
|
@@ -258,14 +265,14 @@ class AgentExecutorIterator:
|
|
|
258
265
|
)
|
|
259
266
|
try:
|
|
260
267
|
async with asyncio_timeout(self.agent_executor.max_execution_time):
|
|
261
|
-
while self.agent_executor._should_continue(
|
|
268
|
+
while self.agent_executor._should_continue( # noqa: SLF001
|
|
262
269
|
self.iterations,
|
|
263
270
|
self.time_elapsed,
|
|
264
271
|
):
|
|
265
272
|
# take the next step: this plans next action, executes it,
|
|
266
273
|
# yielding action and observation as they are generated
|
|
267
274
|
next_step_seq: NextStepOutput = []
|
|
268
|
-
async for chunk in self.agent_executor._aiter_next_step(
|
|
275
|
+
async for chunk in self.agent_executor._aiter_next_step( # noqa: SLF001
|
|
269
276
|
self.name_to_tool_map,
|
|
270
277
|
self.color_mapping,
|
|
271
278
|
self.inputs,
|
|
@@ -288,7 +295,7 @@ class AgentExecutorIterator:
|
|
|
288
295
|
)
|
|
289
296
|
|
|
290
297
|
# convert iterator output to format handled by _process_next_step
|
|
291
|
-
next_step = self.agent_executor._consume_next_step(next_step_seq)
|
|
298
|
+
next_step = self.agent_executor._consume_next_step(next_step_seq) # noqa: SLF001
|
|
292
299
|
# update iterations and time elapsed
|
|
293
300
|
self.update_iterations()
|
|
294
301
|
# decide if this is the final output
|
|
@@ -336,7 +343,7 @@ class AgentExecutorIterator:
|
|
|
336
343
|
# Check for tool return
|
|
337
344
|
if len(next_step_output) == 1:
|
|
338
345
|
next_step_action = next_step_output[0]
|
|
339
|
-
tool_return = self.agent_executor._get_tool_return(next_step_action)
|
|
346
|
+
tool_return = self.agent_executor._get_tool_return(next_step_action) # noqa: SLF001
|
|
340
347
|
if tool_return is not None:
|
|
341
348
|
return self._return(tool_return, run_manager=run_manager)
|
|
342
349
|
|
|
@@ -364,7 +371,7 @@ class AgentExecutorIterator:
|
|
|
364
371
|
# Check for tool return
|
|
365
372
|
if len(next_step_output) == 1:
|
|
366
373
|
next_step_action = next_step_output[0]
|
|
367
|
-
tool_return = self.agent_executor._get_tool_return(next_step_action)
|
|
374
|
+
tool_return = self.agent_executor._get_tool_return(next_step_action) # noqa: SLF001
|
|
368
375
|
if tool_return is not None:
|
|
369
376
|
return await self._areturn(tool_return, run_manager=run_manager)
|
|
370
377
|
|
|
@@ -376,7 +383,7 @@ class AgentExecutorIterator:
|
|
|
376
383
|
"""
|
|
377
384
|
logger.warning("Stopping agent prematurely due to triggering stop condition")
|
|
378
385
|
# this manually constructs agent finish with output key
|
|
379
|
-
output = self.agent_executor._action_agent.return_stopped_response(
|
|
386
|
+
output = self.agent_executor._action_agent.return_stopped_response( # noqa: SLF001
|
|
380
387
|
self.agent_executor.early_stopping_method,
|
|
381
388
|
self.intermediate_steps,
|
|
382
389
|
**self.inputs,
|
|
@@ -389,7 +396,7 @@ class AgentExecutorIterator:
|
|
|
389
396
|
the stopped response.
|
|
390
397
|
"""
|
|
391
398
|
logger.warning("Stopping agent prematurely due to triggering stop condition")
|
|
392
|
-
output = self.agent_executor._action_agent.return_stopped_response(
|
|
399
|
+
output = self.agent_executor._action_agent.return_stopped_response( # noqa: SLF001
|
|
393
400
|
self.agent_executor.early_stopping_method,
|
|
394
401
|
self.intermediate_steps,
|
|
395
402
|
**self.inputs,
|
|
@@ -404,7 +411,7 @@ class AgentExecutorIterator:
|
|
|
404
411
|
"""
|
|
405
412
|
Return the final output of the iterator.
|
|
406
413
|
"""
|
|
407
|
-
returned_output = self.agent_executor._return(
|
|
414
|
+
returned_output = self.agent_executor._return( # noqa: SLF001
|
|
408
415
|
output,
|
|
409
416
|
self.intermediate_steps,
|
|
410
417
|
run_manager=run_manager,
|
|
@@ -421,7 +428,7 @@ class AgentExecutorIterator:
|
|
|
421
428
|
"""
|
|
422
429
|
Return the final output of the async iterator.
|
|
423
430
|
"""
|
|
424
|
-
returned_output = await self.agent_executor._areturn(
|
|
431
|
+
returned_output = await self.agent_executor._areturn( # noqa: SLF001
|
|
425
432
|
output,
|
|
426
433
|
self.intermediate_steps,
|
|
427
434
|
run_manager=run_manager,
|
|
@@ -90,6 +90,7 @@ def create_vectorstore_agent(
|
|
|
90
90
|
|
|
91
91
|
Returns:
|
|
92
92
|
AgentExecutor: Returns a callable AgentExecutor object. Either you can call it or use run method with the query to get the response
|
|
93
|
+
|
|
93
94
|
""" # noqa: E501
|
|
94
95
|
tools = toolkit.get_tools()
|
|
95
96
|
prompt = ZeroShotAgent.create_prompt(tools, prefix=prefix)
|
|
@@ -198,6 +199,7 @@ def create_vectorstore_router_agent(
|
|
|
198
199
|
|
|
199
200
|
Returns:
|
|
200
201
|
AgentExecutor: Returns a callable AgentExecutor object. Either you can call it or use run method with the query to get the response.
|
|
202
|
+
|
|
201
203
|
""" # noqa: E501
|
|
202
204
|
tools = toolkit.get_tools()
|
|
203
205
|
prompt = ZeroShotAgent.create_prompt(tools, prefix=prefix)
|
langchain/agents/chat/base.py
CHANGED
|
@@ -13,6 +13,7 @@ from langchain_core.prompts.chat import (
|
|
|
13
13
|
)
|
|
14
14
|
from langchain_core.tools import BaseTool
|
|
15
15
|
from pydantic import Field
|
|
16
|
+
from typing_extensions import override
|
|
16
17
|
|
|
17
18
|
from langchain._api.deprecation import AGENT_DEPRECATION_WARNING
|
|
18
19
|
from langchain.agents.agent import Agent, AgentOutputParser
|
|
@@ -65,6 +66,7 @@ class ChatAgent(Agent):
|
|
|
65
66
|
return agent_scratchpad
|
|
66
67
|
|
|
67
68
|
@classmethod
|
|
69
|
+
@override
|
|
68
70
|
def _get_default_output_parser(cls, **kwargs: Any) -> AgentOutputParser:
|
|
69
71
|
return ChatOutputParser()
|
|
70
72
|
|
|
@@ -11,6 +11,7 @@ from langchain_core.language_models import BaseLanguageModel
|
|
|
11
11
|
from langchain_core.prompts import PromptTemplate
|
|
12
12
|
from langchain_core.tools import BaseTool
|
|
13
13
|
from pydantic import Field
|
|
14
|
+
from typing_extensions import override
|
|
14
15
|
|
|
15
16
|
from langchain._api.deprecation import AGENT_DEPRECATION_WARNING
|
|
16
17
|
from langchain.agents.agent import Agent, AgentOutputParser
|
|
@@ -35,6 +36,7 @@ class ConversationalAgent(Agent):
|
|
|
35
36
|
"""Output parser for the agent."""
|
|
36
37
|
|
|
37
38
|
@classmethod
|
|
39
|
+
@override
|
|
38
40
|
def _get_default_output_parser(
|
|
39
41
|
cls,
|
|
40
42
|
ai_prefix: str = "AI",
|
|
@@ -20,6 +20,7 @@ from langchain_core.prompts.chat import (
|
|
|
20
20
|
)
|
|
21
21
|
from langchain_core.tools import BaseTool
|
|
22
22
|
from pydantic import Field
|
|
23
|
+
from typing_extensions import override
|
|
23
24
|
|
|
24
25
|
from langchain.agents.agent import Agent, AgentOutputParser
|
|
25
26
|
from langchain.agents.conversational_chat.output_parser import ConvoOutputParser
|
|
@@ -42,6 +43,7 @@ class ConversationalChatAgent(Agent):
|
|
|
42
43
|
"""Template for the tool response."""
|
|
43
44
|
|
|
44
45
|
@classmethod
|
|
46
|
+
@override
|
|
45
47
|
def _get_default_output_parser(cls, **kwargs: Any) -> AgentOutputParser:
|
|
46
48
|
return ConvoOutputParser()
|
|
47
49
|
|
langchain/agents/initialize.py
CHANGED
|
@@ -90,7 +90,7 @@ def initialize_agent(
|
|
|
90
90
|
)
|
|
91
91
|
with contextlib.suppress(NotImplementedError):
|
|
92
92
|
# TODO: Add tags from the serialized object directly.
|
|
93
|
-
tags_.append(agent_obj._agent_type)
|
|
93
|
+
tags_.append(agent_obj._agent_type) # noqa: SLF001
|
|
94
94
|
else:
|
|
95
95
|
msg = (
|
|
96
96
|
"Somehow both `agent` and `agent_path` are None, this should never happen."
|
langchain/agents/mrkl/base.py
CHANGED
|
@@ -12,6 +12,7 @@ from langchain_core.prompts import PromptTemplate
|
|
|
12
12
|
from langchain_core.tools import BaseTool, Tool
|
|
13
13
|
from langchain_core.tools.render import render_text_description
|
|
14
14
|
from pydantic import Field
|
|
15
|
+
from typing_extensions import override
|
|
15
16
|
|
|
16
17
|
from langchain._api.deprecation import AGENT_DEPRECATION_WARNING
|
|
17
18
|
from langchain.agents.agent import Agent, AgentExecutor, AgentOutputParser
|
|
@@ -51,6 +52,7 @@ class ZeroShotAgent(Agent):
|
|
|
51
52
|
output_parser: AgentOutputParser = Field(default_factory=MRKLOutputParser)
|
|
52
53
|
|
|
53
54
|
@classmethod
|
|
55
|
+
@override
|
|
54
56
|
def _get_default_output_parser(cls, **kwargs: Any) -> AgentOutputParser:
|
|
55
57
|
return MRKLOutputParser()
|
|
56
58
|
|
|
@@ -229,7 +229,7 @@ class OpenAIAssistantRunnable(RunnableSerializable[dict, OutputType]):
|
|
|
229
229
|
"""Use as a LangChain agent, compatible with the AgentExecutor."""
|
|
230
230
|
|
|
231
231
|
@model_validator(mode="after")
|
|
232
|
-
def
|
|
232
|
+
def _validate_async_client(self) -> Self:
|
|
233
233
|
if self.async_client is None:
|
|
234
234
|
import openai
|
|
235
235
|
|
|
@@ -4,6 +4,7 @@ from typing import Any
|
|
|
4
4
|
|
|
5
5
|
from langchain_core.language_models import BaseLanguageModel
|
|
6
6
|
from langchain_core.messages import BaseMessage, get_buffer_string
|
|
7
|
+
from typing_extensions import override
|
|
7
8
|
|
|
8
9
|
from langchain.agents.format_scratchpad import (
|
|
9
10
|
format_to_openai_function_messages,
|
|
@@ -55,6 +56,7 @@ class AgentTokenBufferMemory(BaseChatMemory):
|
|
|
55
56
|
"""
|
|
56
57
|
return [self.memory_key]
|
|
57
58
|
|
|
59
|
+
@override
|
|
58
60
|
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
|
|
59
61
|
"""Return history buffer.
|
|
60
62
|
|
|
@@ -134,7 +134,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
|
|
|
134
134
|
messages,
|
|
135
135
|
callbacks=callbacks,
|
|
136
136
|
)
|
|
137
|
-
return self.output_parser.
|
|
137
|
+
return self.output_parser.parse_ai_message(predicted_message)
|
|
138
138
|
|
|
139
139
|
async def aplan(
|
|
140
140
|
self,
|
|
@@ -167,7 +167,7 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent):
|
|
|
167
167
|
functions=self.functions,
|
|
168
168
|
callbacks=callbacks,
|
|
169
169
|
)
|
|
170
|
-
return self.output_parser.
|
|
170
|
+
return self.output_parser.parse_ai_message(predicted_message)
|
|
171
171
|
|
|
172
172
|
def return_stopped_response(
|
|
173
173
|
self,
|
|
@@ -359,6 +359,7 @@ def create_openai_functions_agent(
|
|
|
359
359
|
MessagesPlaceholder("agent_scratchpad"),
|
|
360
360
|
]
|
|
361
361
|
)
|
|
362
|
+
|
|
362
363
|
"""
|
|
363
364
|
if "agent_scratchpad" not in (
|
|
364
365
|
prompt.input_variables + list(prompt.partial_variables)
|
|
@@ -125,7 +125,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
|
|
|
125
125
|
return [t.name for t in self.tools]
|
|
126
126
|
|
|
127
127
|
@model_validator(mode="after")
|
|
128
|
-
def
|
|
128
|
+
def _validate_prompt(self) -> Self:
|
|
129
129
|
prompt: BasePromptTemplate = self.prompt
|
|
130
130
|
if "agent_scratchpad" not in prompt.input_variables:
|
|
131
131
|
msg = (
|
|
@@ -6,6 +6,7 @@ from typing import Union
|
|
|
6
6
|
from langchain_core.agents import AgentAction, AgentFinish
|
|
7
7
|
from langchain_core.exceptions import OutputParserException
|
|
8
8
|
from langchain_core.utils.json import parse_json_markdown
|
|
9
|
+
from typing_extensions import override
|
|
9
10
|
|
|
10
11
|
from langchain.agents.agent import AgentOutputParser
|
|
11
12
|
|
|
@@ -40,6 +41,7 @@ class JSONAgentOutputParser(AgentOutputParser):
|
|
|
40
41
|
```
|
|
41
42
|
"""
|
|
42
43
|
|
|
44
|
+
@override
|
|
43
45
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
44
46
|
try:
|
|
45
47
|
response = parse_json_markdown(text)
|
|
@@ -8,7 +8,10 @@ from langchain_core.messages import (
|
|
|
8
8
|
AIMessage,
|
|
9
9
|
BaseMessage,
|
|
10
10
|
)
|
|
11
|
+
from langchain_core.messages.utils import convert_from_v1_message
|
|
11
12
|
from langchain_core.outputs import ChatGeneration, Generation
|
|
13
|
+
from langchain_core.v1.messages import AIMessage as AIMessageV1
|
|
14
|
+
from typing_extensions import override
|
|
12
15
|
|
|
13
16
|
from langchain.agents.agent import AgentOutputParser
|
|
14
17
|
|
|
@@ -30,7 +33,7 @@ class OpenAIFunctionsAgentOutputParser(AgentOutputParser):
|
|
|
30
33
|
return "openai-functions-agent"
|
|
31
34
|
|
|
32
35
|
@staticmethod
|
|
33
|
-
def
|
|
36
|
+
def parse_ai_message(message: BaseMessage) -> Union[AgentAction, AgentFinish]:
|
|
34
37
|
"""Parse an AI message."""
|
|
35
38
|
if not isinstance(message, AIMessage):
|
|
36
39
|
msg = f"Expected an AI message got {type(message)}"
|
|
@@ -79,18 +82,22 @@ class OpenAIFunctionsAgentOutputParser(AgentOutputParser):
|
|
|
79
82
|
log=str(message.content),
|
|
80
83
|
)
|
|
81
84
|
|
|
85
|
+
@override
|
|
82
86
|
def parse_result(
|
|
83
87
|
self,
|
|
84
|
-
result: list[Generation],
|
|
88
|
+
result: Union[list[Generation], AIMessageV1],
|
|
85
89
|
*,
|
|
86
90
|
partial: bool = False,
|
|
87
91
|
) -> Union[AgentAction, AgentFinish]:
|
|
92
|
+
if isinstance(result, AIMessageV1):
|
|
93
|
+
result = [ChatGeneration(message=convert_from_v1_message(result))]
|
|
88
94
|
if not isinstance(result[0], ChatGeneration):
|
|
89
95
|
msg = "This output parser only works on ChatGeneration output"
|
|
90
96
|
raise ValueError(msg) # noqa: TRY004
|
|
91
97
|
message = result[0].message
|
|
92
|
-
return self.
|
|
98
|
+
return self.parse_ai_message(message)
|
|
93
99
|
|
|
100
|
+
@override
|
|
94
101
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
95
102
|
msg = "Can only parse messages"
|
|
96
103
|
raise ValueError(msg)
|
|
@@ -2,7 +2,10 @@ from typing import Union
|
|
|
2
2
|
|
|
3
3
|
from langchain_core.agents import AgentAction, AgentFinish
|
|
4
4
|
from langchain_core.messages import BaseMessage
|
|
5
|
+
from langchain_core.messages.utils import convert_from_v1_message
|
|
5
6
|
from langchain_core.outputs import ChatGeneration, Generation
|
|
7
|
+
from langchain_core.v1.messages import AIMessage as AIMessageV1
|
|
8
|
+
from typing_extensions import override
|
|
6
9
|
|
|
7
10
|
from langchain.agents.agent import MultiActionAgentOutputParser
|
|
8
11
|
from langchain.agents.output_parsers.tools import (
|
|
@@ -53,18 +56,22 @@ class OpenAIToolsAgentOutputParser(MultiActionAgentOutputParser):
|
|
|
53
56
|
def _type(self) -> str:
|
|
54
57
|
return "openai-tools-agent-output-parser"
|
|
55
58
|
|
|
59
|
+
@override
|
|
56
60
|
def parse_result(
|
|
57
61
|
self,
|
|
58
|
-
result: list[Generation],
|
|
62
|
+
result: Union[list[Generation], AIMessageV1],
|
|
59
63
|
*,
|
|
60
64
|
partial: bool = False,
|
|
61
65
|
) -> Union[list[AgentAction], AgentFinish]:
|
|
66
|
+
if isinstance(result, AIMessageV1):
|
|
67
|
+
result = [ChatGeneration(message=convert_from_v1_message(result))]
|
|
62
68
|
if not isinstance(result[0], ChatGeneration):
|
|
63
69
|
msg = "This output parser only works on ChatGeneration output"
|
|
64
70
|
raise ValueError(msg) # noqa: TRY004
|
|
65
71
|
message = result[0].message
|
|
66
72
|
return parse_ai_message_to_openai_tool_action(message)
|
|
67
73
|
|
|
74
|
+
@override
|
|
68
75
|
def parse(self, text: str) -> Union[list[AgentAction], AgentFinish]:
|
|
69
76
|
msg = "Can only parse messages"
|
|
70
77
|
raise ValueError(msg)
|
|
@@ -5,6 +5,7 @@ from typing import Union
|
|
|
5
5
|
|
|
6
6
|
from langchain_core.agents import AgentAction, AgentFinish
|
|
7
7
|
from langchain_core.exceptions import OutputParserException
|
|
8
|
+
from typing_extensions import override
|
|
8
9
|
|
|
9
10
|
from langchain.agents.agent import AgentOutputParser
|
|
10
11
|
from langchain.agents.chat.prompt import FORMAT_INSTRUCTIONS
|
|
@@ -46,9 +47,11 @@ class ReActJsonSingleInputOutputParser(AgentOutputParser):
|
|
|
46
47
|
pattern: Pattern = re.compile(r"^.*?`{3}(?:json)?\n?(.*?)`{3}.*?$", re.DOTALL)
|
|
47
48
|
"""Regex pattern to parse the output."""
|
|
48
49
|
|
|
50
|
+
@override
|
|
49
51
|
def get_format_instructions(self) -> str:
|
|
50
52
|
return FORMAT_INSTRUCTIONS
|
|
51
53
|
|
|
54
|
+
@override
|
|
52
55
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
53
56
|
includes_answer = FINAL_ANSWER_ACTION in text
|
|
54
57
|
try:
|
|
@@ -3,6 +3,7 @@ from typing import Union
|
|
|
3
3
|
|
|
4
4
|
from langchain_core.agents import AgentAction, AgentFinish
|
|
5
5
|
from langchain_core.exceptions import OutputParserException
|
|
6
|
+
from typing_extensions import override
|
|
6
7
|
|
|
7
8
|
from langchain.agents.agent import AgentOutputParser
|
|
8
9
|
from langchain.agents.mrkl.prompt import FORMAT_INSTRUCTIONS
|
|
@@ -45,9 +46,11 @@ class ReActSingleInputOutputParser(AgentOutputParser):
|
|
|
45
46
|
|
|
46
47
|
"""
|
|
47
48
|
|
|
49
|
+
@override
|
|
48
50
|
def get_format_instructions(self) -> str:
|
|
49
51
|
return FORMAT_INSTRUCTIONS
|
|
50
52
|
|
|
53
|
+
@override
|
|
51
54
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
52
55
|
includes_answer = FINAL_ANSWER_ACTION in text
|
|
53
56
|
regex = (
|
|
@@ -3,6 +3,7 @@ from typing import Union
|
|
|
3
3
|
|
|
4
4
|
from langchain_core.agents import AgentAction, AgentFinish
|
|
5
5
|
from langchain_core.exceptions import OutputParserException
|
|
6
|
+
from typing_extensions import override
|
|
6
7
|
|
|
7
8
|
from langchain.agents.agent import AgentOutputParser
|
|
8
9
|
|
|
@@ -35,6 +36,7 @@ class SelfAskOutputParser(AgentOutputParser):
|
|
|
35
36
|
followups: Sequence[str] = ("Follow up:", "Followup:")
|
|
36
37
|
finish_string: str = "So the final answer is: "
|
|
37
38
|
|
|
39
|
+
@override
|
|
38
40
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
39
41
|
last_line = text.split("\n")[-1]
|
|
40
42
|
if not any(follow in last_line for follow in self.followups):
|
|
@@ -9,12 +9,17 @@ from langchain_core.messages import (
|
|
|
9
9
|
BaseMessage,
|
|
10
10
|
ToolCall,
|
|
11
11
|
)
|
|
12
|
+
from langchain_core.messages.utils import convert_from_v1_message
|
|
12
13
|
from langchain_core.outputs import ChatGeneration, Generation
|
|
14
|
+
from langchain_core.v1.messages import AIMessage as AIMessageV1
|
|
15
|
+
from typing_extensions import override
|
|
13
16
|
|
|
14
17
|
from langchain.agents.agent import MultiActionAgentOutputParser
|
|
15
18
|
|
|
16
19
|
|
|
17
20
|
class ToolAgentAction(AgentActionMessageLog):
|
|
21
|
+
""" "Tool agent action."""
|
|
22
|
+
|
|
18
23
|
tool_call_id: str
|
|
19
24
|
"""Tool call that this message is responding to."""
|
|
20
25
|
|
|
@@ -44,7 +49,12 @@ def parse_ai_message_to_tool_action(
|
|
|
44
49
|
try:
|
|
45
50
|
args = json.loads(function["arguments"] or "{}")
|
|
46
51
|
tool_calls.append(
|
|
47
|
-
ToolCall(
|
|
52
|
+
ToolCall(
|
|
53
|
+
name=function_name,
|
|
54
|
+
args=args,
|
|
55
|
+
id=tool_call["id"],
|
|
56
|
+
type="tool_call",
|
|
57
|
+
)
|
|
48
58
|
)
|
|
49
59
|
except JSONDecodeError as e:
|
|
50
60
|
msg = (
|
|
@@ -90,18 +100,22 @@ class ToolsAgentOutputParser(MultiActionAgentOutputParser):
|
|
|
90
100
|
def _type(self) -> str:
|
|
91
101
|
return "tools-agent-output-parser"
|
|
92
102
|
|
|
103
|
+
@override
|
|
93
104
|
def parse_result(
|
|
94
105
|
self,
|
|
95
|
-
result: list[Generation],
|
|
106
|
+
result: Union[list[Generation], AIMessageV1],
|
|
96
107
|
*,
|
|
97
108
|
partial: bool = False,
|
|
98
109
|
) -> Union[list[AgentAction], AgentFinish]:
|
|
110
|
+
if isinstance(result, AIMessageV1):
|
|
111
|
+
result = [ChatGeneration(message=convert_from_v1_message(result))]
|
|
99
112
|
if not isinstance(result[0], ChatGeneration):
|
|
100
113
|
msg = "This output parser only works on ChatGeneration output"
|
|
101
114
|
raise ValueError(msg) # noqa: TRY004
|
|
102
115
|
message = result[0].message
|
|
103
116
|
return parse_ai_message_to_tool_action(message)
|
|
104
117
|
|
|
118
|
+
@override
|
|
105
119
|
def parse(self, text: str) -> Union[list[AgentAction], AgentFinish]:
|
|
106
120
|
msg = "Can only parse messages"
|
|
107
121
|
raise ValueError(msg)
|