From c4599444eed2b99c26a3b58e6953870b705adcf6 Mon Sep 17 00:00:00 2001 From: ccurme Date: Fri, 22 Mar 2024 16:03:48 -0400 Subject: [PATCH] mistralai: update tool calling (#19451) ```python from langchain.agents import tool from langchain_mistralai import ChatMistralAI llm = ChatMistralAI(model="mistral-large-latest", temperature=0) @tool def get_word_length(word: str) -> int: """Returns the length of a word.""" return len(word) tools = [get_word_length] llm_with_tools = llm.bind_tools(tools) llm_with_tools.invoke("how long is the word chrysanthemum") ``` currently raises ``` AttributeError: 'dict' object has no attribute 'model_dump' ``` Same with `.with_structured_output` ```python from langchain_mistralai import ChatMistralAI from langchain_core.pydantic_v1 import BaseModel class AnswerWithJustification(BaseModel): """An answer to the user question along with justification for the answer.""" answer: str justification: str llm = ChatMistralAI(model="mistral-large-latest", temperature=0) structured_llm = llm.with_structured_output(AnswerWithJustification) structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers") ``` This appears to fix. --- .../langchain_mistralai/chat_models.py | 2 +- .../integration_tests/test_chat_models.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libs/partners/mistralai/langchain_mistralai/chat_models.py b/libs/partners/mistralai/langchain_mistralai/chat_models.py index eb562b7d9db..2566f999cd3 100644 --- a/libs/partners/mistralai/langchain_mistralai/chat_models.py +++ b/libs/partners/mistralai/langchain_mistralai/chat_models.py @@ -83,7 +83,7 @@ def _convert_mistral_chat_message_to_message( additional_kwargs: Dict = {} if tool_calls := _message.get("tool_calls"): - additional_kwargs["tool_calls"] = [tc.model_dump() for tc in tool_calls] + additional_kwargs["tool_calls"] = tool_calls return AIMessage(content=content, additional_kwargs=additional_kwargs) diff --git a/libs/partners/mistralai/tests/integration_tests/test_chat_models.py b/libs/partners/mistralai/tests/integration_tests/test_chat_models.py index 1bdd99305ba..8fdcde33a79 100644 --- a/libs/partners/mistralai/tests/integration_tests/test_chat_models.py +++ b/libs/partners/mistralai/tests/integration_tests/test_chat_models.py @@ -61,3 +61,24 @@ def test_invoke() -> None: result = llm.invoke("I'm Pickle Rick", config=dict(tags=["foo"])) assert isinstance(result.content, str) + + +def test_structred_output() -> None: + llm = ChatMistralAI(model="mistral-large-latest", temperature=0) + schema = { + "title": "AnswerWithJustification", + "description": ( + "An answer to the user question along with justification for the answer." + ), + "type": "object", + "properties": { + "answer": {"title": "Answer", "type": "string"}, + "justification": {"title": "Justification", "type": "string"}, + }, + "required": ["answer", "justification"], + } + structured_llm = llm.with_structured_output(schema) + result = structured_llm.invoke( + "What weighs more a pound of bricks or a pound of feathers" + ) + assert isinstance(result, dict)