我之前使用 SQLDatabaseChain 将 LLM(语言模型)与我的数据库连接,并且它在 GPT-3.5 下运行正常。然而,当尝试使用 GPT-4 进行相同的过程时,我遇到了一个错误,指出“'s 附近的语法不正确”
为了解决这个问题,我选择使用
SQLDatabaseToolkit
和 create_sql_agent
函数。但是,我遇到了这种方法的问题,因为我无法通过提示。当尝试在 PromptTemplate
参数中包含 create_sql_agent
时,会导致错误。
ValueError: Prompt missing required variables: {'tool_names', 'agent_scratchpad', 'tools'}
下面是我的代码:
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
agent_executor = create_sql_agent(
llm=llm,
toolkit=toolkit,
verbose=True,
prompt=MSSQL_PROMPT,
)
我找到了解决方案。
agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=False)
将为我工作。它也适用于基于提示的方法。所以如果你想在其中添加提示那么它应该像
agent_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=False, prompt=MSSQL_PROMPT)
哪里
MSSQL_PROMPT = """You are an MS SQL expert. Given an input question, first create a syntactically correct MS SQL query to run, then look at the results of the query and return the answer to the input question.
Use the following Domain Knowledge about Database: One Order can have multiple shipments & shipment containers.
Use the following format:
Question: Question here
SQLQuery: SQL Query to run
SQLResult: Result of the SQLQuery
Answer: Final answer here
Only use the following tables:
{table_info}
Thought: I should look at the tables in the database to see what I can query. Then I should query the schema of the most relevant tables.
{agent_scratchpad}
Question: {input}"""
您可以根据您的用例在提示中添加更多数据。