微信扫码
添加专属顾问
我要投稿
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-02-22
不用向量数据库的 RAG,居然跑得更准了?
2026-02-22
AIOps探索:做运维领域的RAG,如何做数据清洗
2026-02-21
Claude Code 每次都要重新探索代码?这个工具直接省下30%成本
2026-02-18
函数计算 AgentRun 重磅上线知识库功能,赋能智能体更“懂”你
2026-02-15
当RAG遇上Agent记忆:为什么相似度检索会"塌方"?
2026-02-15
查个问题还要全图跑一遍?DA-RAG说我只取一瓢
2026-02-14
OpenClaw 终于能"记住"事了!我花了 3 周折腾出的长期记忆系统
2026-02-13
深度解析 PageIndex:无向量 RAG 框架的技术实现与原理剖析
2025-12-04
2026-01-15
2025-12-03
2025-12-02
2026-01-02
2025-12-23
2025-12-07
2026-02-11
2025-12-18
2026-02-03
2026-02-22
2026-02-15
2026-02-04
2026-02-03
2026-01-19
2026-01-12
2026-01-08
2026-01-02