|
3 | 3 | import os |
4 | 4 | from typing import ( |
5 | 5 | Any, |
6 | | - Callable, |
7 | 6 | Dict, |
8 | 7 | List, |
9 | | - Literal, |
10 | 8 | Mapping, |
11 | 9 | Optional, |
12 | 10 | Sequence, |
|
18 | 16 | AsyncCallbackManagerForLLMRun, |
19 | 17 | CallbackManagerForLLMRun, |
20 | 18 | ) |
21 | | -from langchain_core.language_models import LanguageModelInput |
22 | 19 | from langchain_core.language_models.chat_models import LangSmithParams |
23 | | -from langchain_core.messages import AIMessage, BaseMessage, HumanMessage |
| 20 | +from langchain_core.messages import BaseMessage, HumanMessage |
24 | 21 | from langchain_core.messages.utils import convert_to_openai_messages |
25 | 22 | from langchain_core.outputs import ChatResult |
26 | | -from langchain_core.runnables import Runnable |
27 | | -from langchain_core.tools import BaseTool |
28 | 23 | from langchain_core.utils import from_env, secret_from_env |
29 | | -from langchain_core.utils.function_calling import convert_to_openai_tool |
30 | 24 | from langchain_openai.chat_models.base import BaseChatOpenAI |
31 | 25 | from pydantic import Field, SecretStr, model_validator |
32 | 26 | from tokenizers import Tokenizer |
@@ -125,6 +119,10 @@ def _get_ls_params( |
125 | 119 | default_headers: Union[Mapping[str, str], None] = DEFAULT_HEADERS |
126 | 120 | """add trace header.""" |
127 | 121 |
|
| 122 | + disabled_params: dict[str, Any] = Field( |
| 123 | + default_factory=lambda: {"parallel_tool_calls": None} |
| 124 | + ) |
| 125 | + |
128 | 126 | @model_validator(mode="after") |
129 | 127 | def validate_environment(self) -> Self: |
130 | 128 | """Validate that api key and python package exists in environment.""" |
@@ -269,69 +267,3 @@ def _parse_documents(self, file_path: str) -> str: |
269 | 267 | file_title = file_titles[min(i, len(file_titles) - 1)] |
270 | 268 | document_contents += f"{file_title}:\n{doc.page_content}\n\n" |
271 | 269 | return document_contents |
272 | | - |
273 | | - def bind_tools( |
274 | | - self, |
275 | | - tools: Sequence[dict[str, Any] | type | Callable | BaseTool], |
276 | | - *, |
277 | | - tool_choice: Optional[Union[dict, str, Literal["auto"], bool]] = None, |
278 | | - **kwargs: Any, |
279 | | - ) -> Runnable[LanguageModelInput, AIMessage]: |
280 | | - """Bind tool-like objects to this chat model. |
281 | | -
|
282 | | - Assumes model is compatible with Upstage tool-calling API. |
283 | | -
|
284 | | - Args: |
285 | | - tools: A list of tool definitions to bind to this chat model. |
286 | | - Can be a dictionary, pydantic model, callable, or BaseTool. Pydantic |
287 | | - models, callables, and BaseTools will be automatically converted to |
288 | | - their schema dictionary representation. |
289 | | - tool_choice: Which tool to require the model to call. |
290 | | - Options are: |
291 | | - name of the tool (str): calls corresponding tool; |
292 | | - "auto": automatically selects a tool (including no tool); |
293 | | - "none": does not call a tool; |
294 | | - True: forces tool call (requires `tools` be length 1); |
295 | | - False: no effect; |
296 | | - or a dict of the form: |
297 | | - {"type": "function", "function": {"name": <<tool_name>>}}. |
298 | | - **kwargs: Any additional parameters to pass to the |
299 | | - :class:`~langchain.runnable.Runnable` constructor. |
300 | | - """ |
301 | | - |
302 | | - formatted_tools = [convert_to_openai_tool(tool) for tool in tools] |
303 | | - if tool_choice: |
304 | | - if isinstance(tool_choice, str): |
305 | | - # tool_choice is a tool/function name |
306 | | - if tool_choice in ("any", "required", "auto"): |
307 | | - tool_choice = "auto" |
308 | | - elif tool_choice == "none": |
309 | | - tool_choice = "none" |
310 | | - else: |
311 | | - tool_choice = { |
312 | | - "type": "function", |
313 | | - "function": {"name": tool_choice}, |
314 | | - } |
315 | | - |
316 | | - elif isinstance(tool_choice, bool): |
317 | | - tool_choice = "auto" |
318 | | - elif isinstance(tool_choice, dict): |
319 | | - tool_names = [ |
320 | | - formatted_tool["function"]["name"] |
321 | | - for formatted_tool in formatted_tools |
322 | | - ] |
323 | | - if not any( |
324 | | - tool_name == tool_choice["function"]["name"] |
325 | | - for tool_name in tool_names |
326 | | - ): |
327 | | - raise ValueError( |
328 | | - f"Tool choice {tool_choice} was specified, but the only " |
329 | | - f"provided tools were {tool_names}." |
330 | | - ) |
331 | | - else: |
332 | | - raise ValueError( |
333 | | - f"Unrecognized tool_choice type. Expected str, bool or dict. " |
334 | | - f"Received: {tool_choice}" |
335 | | - ) |
336 | | - kwargs["tool_choice"] = tool_choice |
337 | | - return super().bind(tools=formatted_tools, **kwargs) |
0 commit comments