微信扫码
添加专属顾问
我要投稿
01。
概述
02。
核心技术
02。
传统RAG实现
03。
新方案
from langchain import OpenAI, LLMChain
from langchain.prompts import PromptTemplate
from langchain.utilities import SQLDatabase
from sqlalchemy import create_engine, MetaData, Table, Column, inspect
from langchain_experimental.sql import SQLDatabaseChain
# we kept the temp=0 as we dont want LLM to use creativity and randomness
llm = OpenAI(temperature=0, openai_api_key="your_openai_api_key")
def extract_schema(db_url):
engine = create_engine(db_url)
inspector = inspect(engine)
schema_info = []
for table_name in inspector.get_table_names():
columns = inspector.get_columns(table_name)
schema_info.append(f"Table: {table_name}")
for column in columns:
schema_info.append(f" - {column['name']} ({column['type']})")
return "\n".join(schema_info)
prompt_template = """
You are an AI assistant that generates SQL queries based on user requests.
You have access to the following database schema:
{schema}
Based on this schema, generate a SQL query to answer the following question:
{question}
SQL Query:
"""
prompt = PromptTemplate(
input_variables=["schema", "question"],
template=prompt_template,
)
chain = LLMChain(llm=llm, prompt=prompt)
def generate_sql_query(question):
return chain.run(schema=schema, question=question)
user_question = "Find me the registration id of the hackathon"
sql_query = generate_sql_query(user_question)
print(f"Generated SQL Query: {sql_query}")
04。
结论
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-06-25
5.2k星星爆火开源!你的知识库迎来了史诗级更新,「像素级原生搜索」来了
2026-06-25
1.5K Star!网页提取神器 webclaw:让 AI 精准抓取网页核心内容!
2026-06-25
聊一聊检索即推理:基于LLM-Wiki的自演化智能体原生检索
2026-06-24
企业级 Agent 最缺的不是聪明,是"不敢编"——企查查智能体数据平台的三层反幻觉工程
2026-06-24
别再怪向量检索不行!90% RAG 检索拉胯,都是关键词提取在拖后腿
2026-06-24
别再把 RAG 当搜索框了:Bayer 这套 Agentic RAG,把上下文、反思、恢复和评测全焊进生产系统
2026-06-24
上生产GraphRAG的重活,SAG请外援解决了
2026-06-23
RAG之后,知识库开始自己长大
2026-04-06
2026-04-27
2026-04-02
2026-04-23
2026-03-31
2026-04-20
2026-04-09
2026-04-12
2026-04-22
2026-04-10
2026-06-23
2026-06-23
2026-06-15
2026-06-10
2026-06-10
2026-05-20
2026-05-18
2026-05-11