微信扫码
添加专属顾问
我要投稿
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-12-10
最新力作:一招提升RAG检索精度20%
2025-12-10
Apple 入局 RAG:深度解析 CLaRa 框架,如何实现 128x 文档语义压缩?
2025-12-09
客服、代码、法律场景适配:Milvus Ngram Index如何百倍优化LIKE查询| Milvus Week
2025-12-09
一键把碎片变成有料笔记:NoteGen,一款跨平台的 Markdown 笔记应用
2025-12-07
Embedding模型选型思路:相似度高不再代表检索准确(文末附实战指南)
2025-12-06
Palantir Ontology 助力AIP Agent落地工具介绍:Object Query
2025-12-05
把AI记忆做好,是一个价值6千亿美元的市场
2025-12-05
我错了,RAG还没完!AI记忆的结合会成为下一个技术风口
2025-09-15
2025-10-04
2025-10-11
2025-09-30
2025-10-12
2025-11-04
2025-12-04
2025-09-12
2025-11-13
2025-10-31
2025-12-10
2025-11-23
2025-11-20
2025-11-19
2025-11-04
2025-10-04
2025-09-30
2025-09-10