@@ -96,6 +96,7 @@ def _default_runnable_builder(
96
96
prompt : Optional ["RunnableSerializable" ] = None ,
97
97
output_parser : Optional ["RunnableSerializable" ] = None ,
98
98
chat_history : Optional ["GetSessionHistoryCallable" ] = None ,
99
+ model_tool_kwargs : Optional [Mapping [str , Any ]] = None ,
99
100
agent_executor_kwargs : Optional [Mapping [str , Any ]] = None ,
100
101
runnable_kwargs : Optional [Mapping [str , Any ]] = None ,
101
102
) -> "RunnableSerializable" :
@@ -109,10 +110,11 @@ def _default_runnable_builder(
109
110
has_history : bool = chat_history is not None
110
111
prompt = prompt or _default_prompt (has_history )
111
112
output_parser = output_parser or _default_output_parser ()
113
+ model_tool_kwargs = model_tool_kwargs or {}
112
114
agent_executor_kwargs = agent_executor_kwargs or {}
113
115
runnable_kwargs = runnable_kwargs or _default_runnable_kwargs (has_history )
114
116
if tools :
115
- model = model .bind_tools (tools = tools )
117
+ model = model .bind_tools (tools = tools , ** model_tool_kwargs )
116
118
else :
117
119
tools = []
118
120
agent_executor = AgentExecutor (
@@ -202,6 +204,7 @@ def __init__(
202
204
output_parser : Optional ["RunnableSerializable" ] = None ,
203
205
chat_history : Optional ["GetSessionHistoryCallable" ] = None ,
204
206
model_kwargs : Optional [Mapping [str , Any ]] = None ,
207
+ model_tool_kwargs : Optional [Mapping [str , Any ]] = None ,
205
208
agent_executor_kwargs : Optional [Mapping [str , Any ]] = None ,
206
209
runnable_kwargs : Optional [Mapping [str , Any ]] = None ,
207
210
model_builder : Optional [Callable ] = None ,
@@ -233,8 +236,9 @@ def __init__(
233
236
# runnable_builder
234
237
from langchain import agents
235
238
from langchain_core.runnables.history import RunnableWithMessageHistory
239
+ llm_with_tools = llm.bind_tools(tools=tools, **model_tool_kwargs)
236
240
agent_executor = agents.AgentExecutor(
237
- agent=prompt | llm.bind_tools(tools=tools) | output_parser,
241
+ agent=prompt | llm_with_tools | output_parser,
238
242
tools=tools,
239
243
**agent_executor_kwargs,
240
244
)
@@ -282,6 +286,9 @@ def __init__(
282
286
"top_k": 40,
283
287
}
284
288
```
289
+ model_tool_kwargs (Mapping[str, Any]):
290
+ Optional. Additional keyword arguments when binding tools to the
291
+ model using `model.bind_tools()`.
285
292
agent_executor_kwargs (Mapping[str, Any]):
286
293
Optional. Additional keyword arguments for the constructor of
287
294
langchain.agents.AgentExecutor. An example would be
@@ -334,6 +341,7 @@ def __init__(
334
341
self ._output_parser = output_parser
335
342
self ._chat_history = chat_history
336
343
self ._model_kwargs = model_kwargs
344
+ self ._model_tool_kwargs = model_tool_kwargs
337
345
self ._agent_executor_kwargs = agent_executor_kwargs
338
346
self ._runnable_kwargs = runnable_kwargs
339
347
self ._model = None
@@ -365,6 +373,7 @@ def set_up(self):
365
373
tools = self ._tools ,
366
374
output_parser = self ._output_parser ,
367
375
chat_history = self ._chat_history ,
376
+ model_tool_kwargs = self ._model_tool_kwargs ,
368
377
agent_executor_kwargs = self ._agent_executor_kwargs ,
369
378
runnable_kwargs = self ._runnable_kwargs ,
370
379
)
0 commit comments