You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LangChain与LLaMA2操作SQLite数据库QA:无法获取ID及输出表格结果求助

解决SQLDatabaseChain查询特定字段失败及表格输出问题

一、修复VAERS_ID查询失败问题

  1. 排查生成的SQL语句
    将sql_chain的verbose参数设为True,运行测试查询3,查看模型生成的SQL是否正确:
sql_chain = SQLDatabaseChain(llm=local_llm, database=db, prompt=SQLITE_PROMPT, return_direct=False, return_intermediate_steps=False, verbose=True)
res=sql_chain("What is the VAERS_ID with 'Abdominal pain', VAX_TYPE='COVID19', SEX= 'F' and HOSPITAL= 'Y' in this db. ")

重点检查SQL是否包含"VAERS_ID"列、WHERE条件是否匹配需求,以及是否存在列名拼写错误或未用双引号包裹的情况。

  1. 强化提示词引导
    修改SQLITE_PROMPT_TEXT,明确要求模型在查询特定标识字段时必须包含对应列:
SQLITE_PROMPT_TEXT = '''You are a SQLite expert. Given an input question, first create a 
syntactically correct SQLite query to run, then look at the results of the query and return 
the answer to the input question.
Unless the user specifies in the question a specific number of examples to obtain, query for 
at most {top_k} results using the LIMIT clause as per SQLite. You can order the results to 
 return the most informative data in the database.
 Never query for all columns from a table. You must query only the columns that are needed to 
 answer the question. Wrap each column name in double quotes (") to denote them as delimited 
 identifiers.
**If the user asks for specific record identifiers like VAERS_ID, ensure the SQL query includes the corresponding column name wrapped in double quotes.**
 Pay attention to use only the column names you can see in the tables below. Be careful to not 
 query for columns that do not exist. Also, pay attention to which column is in which table.

 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}

 Question: {input}'''
  1. 验证数据库列名
    确认input_table中确实存在VAERS_ID列,且列名大小写与提示词中的一致(SQLite使用双引号时区分大小写,需确保完全匹配)。

二、实现表格形式输出

  1. 修改提示词的输出格式要求
    更新提示词中的格式说明,明确要求返回Markdown表格:
SQLITE_PROMPT_TEXT = '''You are a SQLite expert. Given an input question, first create a 
syntactically correct SQLite query to run, then look at the results of the query and return 
the answer to the input question.
Unless the user specifies in the question a specific number of examples to obtain, query for 
at most {top_k} results using the LIMIT clause as per SQLite. You can order the results to 
 return the most informative data in the database.
 Never query for all columns from a table. You must query only the columns that are needed to 
 answer the question. Wrap each column name in double quotes (") to denote them as delimited 
 identifiers.
**If the user asks for specific record identifiers like VAERS_ID, ensure the SQL query includes the corresponding column name wrapped in double quotes.**
**When returning multiple records, present the answer as a Markdown table. For example, if returning VAERS_IDs, use this format:**
| VAERS_ID |
|----------|
| 123456   |
| 789012   |
 Pay attention to use only the column names you can see in the tables below. Be careful to not 
 query for columns that do not exist. Also, pay attention to which column is in which table.

 Use the following format:

  Question: Question here
  SQLQuery: SQL Query to run
  SQLResult: Result of the SQLQuery
  Answer: Final answer here, using Markdown table for multiple records

 Only use the following tables:
 {table_info}

 Question: {input}'''
  1. 调整top_k参数
    如果需要返回更多结果,在创建SQLDatabaseChain时指定合适的top_k值(比如100):
sql_chain = SQLDatabaseChain(llm=local_llm, database=db, prompt=SQLITE_PROMPT, return_direct=False, return_intermediate_steps=False, verbose=True, top_k=100)
  1. 测试表格输出
    重新运行测试查询3,模型应返回包含VAERS_ID的Markdown表格。

内容的提问来源于stack exchange,提问作者Rapa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 02:42:51