Open boli-design opened 3 months ago
@boli-design you are right, the ChatZhipuAI
as per their official documentation support tools invoking. I am going to add this feature which is right now missing.
In addition, I found another related bug. The code:
import os
import getpass
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
# .环境变量
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "balabala"
os.environ["ZHIPUAI_API_KEY"] = "balabala"
os.environ["TAVILY_API_KEY"]="balabala"
llm_1=ChatZhipuAI(model="glm-4")
llm_2 = ChatOpenAI(
temperature=0.95,
model="glm-4",
openai_api_key="balabala",
openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)#
search = TavilySearchResults(max_results=2)
tools = [search]
agent_1 = create_react_agent(llm_1, tools)
agent_2 = create_react_agent(llm_2, tools)
response = agent_2.invoke(
{"messages": [HumanMessage(content="whats the weather in sf?")]}
)
print("agent_2",response["messages"])
#here error occurs
response = agent_1.invoke(
{"messages": [HumanMessage(content="whats the weather in sf?")]}
)
print("agent_1",response["messages"])
'''
Exception has occurred: TypeError
Got unknown type 'ToolMessage'.
File "E:\GitHub\langchain\1\test.py", line 37, in <module>
response = agent_1.invoke(
^^^^^^^^^^^^^^^
TypeError: Got unknown type 'ToolMessage'.
'''
The error:
Exception has occurred: TypeError
Got unknown type 'ToolMessage'.
File "E:\GitHub\langchain\1\test.py", line 37, in
When using ChatOpenAI, there is no error. I cound not find how to fix it, maybe it is better to use ChatOpenAI to call other models?
Hello, is this resolved? I tried to use bind_tools for ChatZhipuAi as well and got the same error. And updating to the latest version of langchain_community doesn't solve the issue. Thank you.
I sugguest to use ChatOpenAI instead, which could also call the function of zhipuAI llm.
Hello, is this resolved? I tried to use bind_tools for ChatZhipuAi as well and got the same error. And updating to the latest version of langchain_community doesn't solve the issue. Thank you.
Thank you for your suggestion. Your idea is to add the openai_api_base when defining the LLM to call GLM4 with ChatOpenAI. Is that correct?
Thank you for your suggestion. Your idea is to add the openai_api_base when defining the LLM to call GLM4 with ChatOpenAI. Is that correct?
right
I just found the instruction on ZhipuAI's website. Thank you.
Thank you for your suggestion. Your idea is to add the openai_api_base when defining the LLM to call GLM4 with ChatOpenAI. Is that correct?
right
I find that ChatOpenAI can only support GLM4's temperature as low as 0.01. I have no idea why this is the case. Any temperature lower than 0.01 will cause an error.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Exception has occurred: NotImplementedError exception: no description File "E:\GitHub\langchain\1\agent_1.py", line 33, in
llm_with_tools=llm.bind_tools(tools)
^^^^^^^^^^^^^^^^^^^^^ NotImplementedError:
Description
the code in "langchain_core\language_models\chat_models.py.BaseChatModel.bind_tools" is incomplete, as show below"
According to the ChatOpenAI, the code should be:
After replacing this part, the code is running well.
System Info
Python 3.12.3 langchain==0.2.6 langchain-chroma==0.1.2 langchain-community==0.2.6 langchain-core==0.2.10 langchain-huggingface==0.0.3 langchain-openai==0.1.14 langchain-text-splitters==0.2.2 langserve==0.2.2 langsmith==0.1.82