将GPT 4或GPT 3.5与SQL数据库代理一起使用时会引发OutputParserException:无法分析LLM输出:

qni6mghb  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(785)

我正在使用SQL Database Agent查询postgres数据库。我想在传递给代理的OpenAI llm中使用gpt 4或gpt 3.5模型,但它说我必须使用ChatOpenAI。使用ChatOpenAI会抛出解析错误。
想要切换模型的原因是降低成本,更好的性能,最重要的是-令牌限制。text-davinci-003的最大令牌大小是4k,我需要至少两倍。
这是我的代码

from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.agents import create_sql_agent
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
import os

os.environ["OPENAI_API_KEY"] = ""
db = SQLDatabase.from_uri(
    "postgresql://<my-db-uri>",
    engine_args={
        "connect_args": {"sslmode": "require"},
    },
)

llm = ChatOpenAI(model_name="gpt-3.5-turbo")
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent(
    llm=llm,
    toolkit=toolkit,
    verbose=True,
)

agent_executor.run("list the tables in the db. Give the answer in a table json format.")

当我这样做时,它在链的中途抛出一个错误

> Entering new AgentExecutor chain...
Traceback (most recent call last):
  File "/home/ramlah/Documents/projects/langchain-test/sql.py", line 96, in <module>
    agent_executor.run("list the tables in the db. Give the answer in a table json format.")
  File "/home/ramlah/Documents/projects/langchain/langchain/chains/base.py", line 236, in run
    return self(args[0], callbacks=callbacks)[self.output_keys[0]]
  File "/home/ramlah/Documents/projects/langchain/langchain/chains/base.py", line 140, in __call__
    raise e
  File "/home/ramlah/Documents/projects/langchain/langchain/chains/base.py", line 134, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 953, in _call
    next_step_output = self._take_next_step(
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 773, in _take_next_step
    raise e
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 762, in _take_next_step
    output = self.agent.plan(
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/agent.py", line 444, in plan
    return self.output_parser.parse(full_output)
  File "/home/ramlah/Documents/projects/langchain/langchain/agents/mrkl/output_parser.py", line 51, in parse
    raise OutputParserException(
langchain.schema.OutputParserException: Could not parse LLM output: `Action: list_tables_sql_db, ''`

请帮帮我谢谢!

cgh8pdjw

cgh8pdjw1#

在create_sql_agent函数中传递handle_parsing_errors=True,大多数时候它可以帮助代码克服解析错误

相关问题