微信扫码
添加专属顾问
我要投稿
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+中大型企业
2025-05-16
解锁AI时代的核心通关密码——为什么你必须搞懂RAG、Agent、MCP?
2025-05-16
RAG-MCP:突破大模型工具调用瓶颈,告别Prompt膨胀
2025-05-16
图检索增强生成(GraphRAG):让AI真正理解复杂知识
2025-05-16
RAG往后发展的三点感触及语言文化分析的两个工作
2025-05-16
基于LangChain实现RAG的技术原理
2025-05-15
所有AI工具共享记忆!MCP协议让知识库进化成‘活’的智能中枢
2025-05-15
快速上车!RAGflow 保姆级安装指南!小白也能轻松搞定!
2025-05-15
如何设计 AI 与人的交互?以及为什么真正的创新必然是集中式的?
2024-10-27
2024-09-04
2024-05-05
2024-07-18
2024-06-20
2024-06-13
2024-07-09
2024-07-09
2024-05-19
2024-07-07
2025-05-16
2025-05-15
2025-05-14
2025-05-14
2025-05-13
2025-05-11
2025-05-08
2025-05-05