微信扫码
添加专属顾问
我要投稿
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-05-11
到底是谁会相信RAG已死啊?
2026-05-11
RAG又进化了!微软整了个企业级AgenticRAG
2026-05-11
AI Agent 如何重构 App 稳定性治理流程
2026-05-09
阿里云知识存储 skill?能接入openclaw/Hermes/codex吗
2026-05-07
阿里云知识存储 Skill 上架阿里云官网首批 Agent Skill:让智能体拥有企业级知识库
2026-05-07
1G内存检索2500万向量,Milvus中如何用FLAT在强标量过滤场景搞定毫秒响应?
2026-05-06
多Agent场景,子agent 之间数据读写不同步,如何解决?
2026-05-06
看 AgentRun 如何玩转记忆存储,最佳实践来了!
2026-02-13
2026-03-23
2026-04-06
2026-02-22
2026-03-18
2026-03-20
2026-02-15
2026-02-27
2026-02-21
2026-03-21
2026-05-11
2026-05-07
2026-05-06
2026-04-27
2026-04-21
2026-03-17
2026-03-11
2026-02-22