From 4b6b0a87b6e4f0a7d3655d63908b946c5eed3f60 Mon Sep 17 00:00:00 2001 From: ccurme Date: Sat, 13 Apr 2024 18:40:55 -0400 Subject: [PATCH] groq[patch]: Make stream robust to ToolMessage (#20417) ```python from langchain.agents import AgentExecutor, create_tool_calling_agent, tool from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_groq import ChatGroq prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant"), ("human", "{input}"), MessagesPlaceholder("agent_scratchpad"), ] ) model = ChatGroq(model_name="mixtral-8x7b-32768", temperature=0) @tool def magic_function(input: int) -> int: """Applies a magic function to an input.""" return input + 2 tools = [magic_function] agent = create_tool_calling_agent(model, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.invoke({"input": "what is the value of magic_function(3)?"}) ``` ``` > Entering new AgentExecutor chain... Invoking: `magic_function` with `{'input': 3}` 5The value of magic\_function(3) is 5. > Finished chain. {'input': 'what is the value of magic_function(3)?', 'output': 'The value of magic\\_function(3) is 5.'} ``` --- libs/partners/groq/langchain_groq/chat_models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 5b58b36e6e..61fecd7b46 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -287,7 +287,7 @@ class ChatGroq(BaseChatModel): "id": rtc.get("id"), "index": rtc.get("index"), } - for rtc in message.additional_kwargs["tool_calls"] + for rtc in message.additional_kwargs.get("tool_calls", []) ] chunk_ = ChatGenerationChunk( message=AIMessageChunk( @@ -358,7 +358,7 @@ class ChatGroq(BaseChatModel): "id": rtc.get("id"), "index": rtc.get("index"), } - for rtc in message.additional_kwargs["tool_calls"] + for rtc in message.additional_kwargs.get("tool_calls", []) ] chunk_ = ChatGenerationChunk( message=AIMessageChunk(