检查其他资源
- 为这个问题添加了一个非常描述性的标题。
- 使用集成搜索在LangChain文档中进行了搜索。
- 使用GitHub搜索查找类似的问题,但没有找到。
- 我确信这是LangChain中的一个错误,而不是我的代码。
- 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此错误。
示例代码
from datetime import date
import requests
from langchain_community.utilities import SerpAPIWrapper
from langchain_core.output_parsers import StrOutputParser
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import create_openai_functions_agent
from langchain.agents import AgentExecutor
serpapi_api_key = "xxxxxxxxxx"
api_key = "sk-xxxxxxxxx"
api_url = "https://ai-yyds.com/v1"
llm = ChatOpenAI(base_url=api_url, api_key=api_key, model_name="gpt-4")
prompt = hub.pull("hwchase17/openai-functions-agent")
print(prompt.messages)
@tool
def search(text: str):
"""This tool is only used when real-time information needs to be searched. The search returns only the first 3 items"""
serp = SerpAPIWrapper(serpapi_api_key=serpapi_api_key)
response = serp.run(text)
print(type(response))
content = ""
if type(response) is list:
for item in response:
content += str(item["title"]) + "\n"
else:
content = response
return content
@tool
def time() -> str:
"""Return today's date and use it for any questions related to today's date.
The input should always be an empty string, and this function will always return today's date. Any mathematical operation on a date should occur outside of this function"""
return str(date.today())
@tool
def weather(city: str):
"""When you need to check the weather, you can use this tool, which returns the weather conditions for the day, tomorrow, and the day after tomorrow"""
url = "https://api.seniverse.com/v3/weather/daily.json?key=SrlXSW6OX9PssfOJ1&location=beijing&language=zh-Hans&unit=c&start=0"
response = requests.get(url)
data = response.json()
if not data or len(data['results']) == 0:
return None
daily = data['results'][0]["daily"]
content = ""
res = []
for day in daily:
info = {"city": city, "date": day["date"], "info": day["text_day"], "temperature_high": day["high"],
"temperature_low": day["low"]}
content += f"{city} date:{day['date']} info:{day['text_day']} maximum temperature:{day['high']} minimum temperature:{day['low']}\n"
res.append(info)
return content
tools = [time, weather, search]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
response1 = agent_executor.invoke({"input": "What's the weather like in Shanghai tomorrow"})
print(response1)
错误信息和堆栈跟踪(如果适用)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/chains/base.py", line 156, in invoke
self._call(inputs, run_manager=run_manager)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1636, in _call
next_step_output = self._take_next_step(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1342, in _take_next_step
[
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1342, in <listcomp>
[
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 1370, in _iter_next_step
output = self.agent.plan(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 463, in plan
for chunk in self.runnable.stream(inputs, config={"callbacks": callbacks}):
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 3251, in stream
yield from self.transform(iter([input]), config, **kwargs)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 3238, in transform
yield from self._transform_stream_with_config(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 2052, in _transform_stream_with_config
chunk: Output = context.run(next, iterator) # type: ignore
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 3200, in _transform
for output in final_pipeline:
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 1270, in transform
for ichunk in input:
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 5262, in transform
yield from self.bound.transform(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/runnables/base.py", line 1288, in transform
yield from self.stream(final, config, **kwargs)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/language_models/chat_models.py", line 360, in stream
raise e
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_core/language_models/chat_models.py", line 340, in stream
for chunk in self._stream(messages, stop=stop, **kwargs):
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/langchain_openai/chat_models/base.py", line 520, in _stream
response = self.client.create(**payload)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_utils/_utils.py", line 277, in wrapper
return func(*args, **kwargs)
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/resources/chat/completions.py", line 643, in create
return self._post(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_base_client.py", line 1266, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_base_client.py", line 942, in request
return self._request(
File "/home/gujiachun/PycharmProjects/rainbow-robot/.venv/lib/python3.8/site-packages/openai/_base_client.py", line 1046, in _request
raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid value for 'content': expected a string, got null. (request id: 20240723144941715017377sn10oSMg) (request id: 2024072306494157956522013257597)", 'type': 'invalid_request_error', 'param': 'messages.[2].content', 'code': None}}
描述
执行上述代码,有时它会正常返回,有时会报告错误
openai.BadRequestError:错误代码:400 - {'error': {'message': "Invalid value for 'content': expected a string, got null. (request id: 20240723111146966761056DQSQiv7T) (request id: 2024072303114683478387128512399)", 'type': 'invalid_request_error', 'param': 'messages.[2].content', 'code': None}}
系统信息
平台:Mac
python: 3.8
langchain_core: 0.2.22
langchain: 0.2.9
langchain_community: 0.2.9
langsmith: 0.1.90
langchain_openai: 0.1.17
langchain_text_splitters: 0.2.2
langchainhub: 0.1.20
openai 1.35.13
3条答案
按热度按时间zvokhttg1#
@gujiachun 危险,暴露你的API密钥。
6uxekuva2#
收到
t98cgbkg3#
change api_url to api_url = " https://api.openai.com/v1 "
working fine