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

如何在Vertex AI的LLaMA 2-7B聊天模型中加入系统提示词与指令

解决Vertex AI上LLaMA 2-7B聊天模型的提示格式适配问题

直接结论:你需要按照本地使用的LLaMA 2原生提示格式,先把system_prompt和user_message拼接成完整的prompt字符串,再放到请求的instances的prompt字段中——Vertex AI部署的LLaMA 2模型遵循原生的提示模板要求,不需要额外的Vertex特定结构化格式。

具体修改步骤

  1. 保留你本地的prompt格式化逻辑,生成符合LLaMA 2要求的完整prompt
  2. 将生成的formatted_prompt直接赋值给instances里的prompt字段

修改后的完整代码示例

from google.cloud import aiplatform

PROJECT_ID = "--"
ENDPOINT_ID = "--"
LOCATION = "us-central1"
SERVICE_ACCOUNT_FILE = "service_account.json"

def endpoint_predict_sample(project: str, location: str, instances: list, endpoint: str):
    aiplatform.init(project=project, location=location)
    endpoint = aiplatform.Endpoint(endpoint)
    prediction = endpoint.predict(instances=instances)
    print(prediction)
    return prediction

# 定义系统提示和用户消息
system_prompt = "Your system instruction here"
user_message = "Your user input here"

# 按照LLaMA 2原生格式拼接完整prompt
formatted_prompt = f"""
<s>[INST] <<SYS>>
{ system_prompt }
<</SYS>>

{ user_message } [/INST]
"""

# 构造请求实例
instances = [
    {
        "prompt": formatted_prompt.strip(),  # 去掉首尾空白避免多余换行干扰
        "max_tokens": 2000
    }
]

response = endpoint_predict_sample(
    project=PROJECT_ID,
    location=LOCATION,
    instances=instances,
    endpoint=ENDPOINT_ID
)

关键注意事项

  • 必须严格保留LLaMA 2的特殊标记:<s>、[INST]、<<SYS>>、<</SYS>>、[/INST]是模型区分系统指令、用户提问的核心标识,不能省略或修改格式
  • 保持标记间的换行和空格与原生格式一致,避免模型解析错误
  • 如果是多轮对话,也要遵循LLaMA 2的多轮模板,比如:
    <s>[INST] <<SYS>>
    系统提示内容
    <</SYS>>
    
    第一轮用户消息 [/INST] 助手回复内容 </s><s>[INST] 第二轮用户消息 [/INST]
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 14:33:28