You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求将适配其他LLM的SQL生成Prompt模板转为Llama3格式

适配Llama3的SQL查询生成模板修改方案

原有适配其他LLM的模板

# Prompt
template = """Based on the table schema below, write a SQLite query that would answer the user's question:
{schema}

Question: {question}
SQL Query:"""  # noqa: E501

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "Given an input question, convert it to a SQL query. No pre-amble."),
        MessagesPlaceholder(variable_name="history"),
        ("human", template),
    ]
)

Llama3要求的专属格式规范

Llama3需要使用特定分隔token区分角色与对话边界,官方规定格式如下:

template = """
        <|begin_of_text|>
        <|start_header_id|>system<|end_header_id|>
        {system_prompt}
        <|eot_id|>
        <|start_header_id|>user<|end_header_id|>
        {user_prompt}
        <|eot_id|>
        <|start_header_id|>assistant<|end_header_id|>
        """

修改后的Llama3兼容模板代码

结合原有业务逻辑(SQL生成、多轮对话支持),调整后的可用代码如下:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

# 构建符合Llama3格式的完整模板字符串
llama3_template = """
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
Given an input question, convert it to a SQL query. No pre-amble.
<|eot_id|>
{history_messages}
<|start_header_id|>user<|end_header_id|>
Based on the table schema below, write a SQLite query that would answer the user's question:
{schema}

Question: {question}
SQL Query:
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""

# 格式化历史对话,使其符合Llama3的角色分隔规则
def format_history(history):
    formatted_msgs = []
    for msg in history:
        role = "user" if msg.type == "human" else "assistant"
        formatted_msgs.extend([
            f"<|start_header_id|>{role}<|end_header_id|>",
            msg.content,
            "<|eot_id|>"
        ])
    return "\n".join(formatted_msgs)

# 创建Prompt模板,整合历史对话的动态处理逻辑
prompt = ChatPromptTemplate.from_template(
    llama3_template,
    partial_variables={"history_messages": lambda vars: format_history(vars["history"])},
)

关键修改说明

  • 严格遵循Llama3的begin_of_text、角色header token、eot_id等特殊分隔符要求
  • 完整保留原有系统提示与SQL生成的业务逻辑
  • 新增历史对话格式化函数,确保多轮对话场景也符合Llama3格式规范
  • 使用from_template结合partial变量实现动态历史内容的注入

内容的提问来源于stack exchange,提问作者Dayo Salam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 11:05:03