Llama-2-70b-chat
POST
https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/llama_2_70bLlama-2-70b-chat由Meta AI研发并开源,在编码、推理及知识应用等场景表现优秀,Llama-2-70b-chat是高精度效果的原生开源版本。本文介绍了相关API。
创建chat
调用本接口,发起一次对话请求。
返回说明
注意 :同步模式和流式模式,响应参数返回不同,详细内容参考示例描述。
- 同步模式下,响应参数为以上字段的完整json包。
- 流式模式下,各字段的响应参数为 data: {响应参数}。
请求示例(多轮)
# 步骤一,获取access_token,替换下列示例中的应用API Key与应用Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]'
# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的“调用接口获取的access_token”
curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/llama_2_70b?access_token=[步骤一调用接口获取的access_token]' -d '{
"messages": [
{"role":"user","content":"Please introduce yourself"},
{"role":"assistant","content":" Hello! My name is Assistant, and I\\’m here to help you with any questions or concerns you may have. I strive to provide respectful, honest, and helpful responses that are free from harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. I\\’m here to assist you in a socially unbiased and positive manner, and I will do my best to provide accurate and reliable information. If a question doesn\\’t make sense or is not factually coherent, I will explain why instead of providing an incorrect answer. If I don\\’t know the answer to a question, I will let you know and do my best to find a reliable source of information for you. Please feel free to ask me anything, and I\\’ll do my best to assist you."},
{"role":"user","content": "How about the weather in Shanghai"}
]
}' | iconv -f utf-8 -t utf-8
响应示例(多轮)
{
"id": "as-i4g16spxm8",
"object": "chat.completion",
"created": 1693291334,
"result": " Sure, here's the current weather conditions and forecast for Shanghai, China:\n\nCurrent Weather:\n\n* Temperature: 22°C (72°F)\n* Humidity: 62%\n* Wind Speed: 20 km/h (12 mph)\n* Conditions: Partly Cloudy\n\nForecast:\n\n* Today: Partly cloudy skies with a high of 25°C (77°F) and a low of 18°C (64°F).\n* Tomorrow: Intervals of clouds and sunshine with a high of 26°C (79°F) and a low of 19°C (66°F).\n* Weekend: Mostly sunny with a high of 28°C (82°F) on Saturday and 27°C (81°F) on Sunday.\n\nNote: These weather conditions are subject to change and are based on the current data available.\n\nI hope this helps! Let me know if you have any other questions.",
"is_truncated": false,
"need_clear_history": false,
"usage": {
"prompt_tokens": 182,
"completion_tokens": 141,
"total_tokens": 323
}
}
请求示例(流式)
# 步骤一,获取access_token,替换下列示例中的应用API Key与应用Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]'
# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的“调用接口获取的access_token”
curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/llama_2_70b?access_token=[步骤一调用接口获取的access_token]' -d '{
"messages": [
{"role":"user", "content": "推荐几条中国自驾游路线"}
],
"stream": true
}'
响应示例(流式)
data: {"id":"as-b2wn45xp3d","object":"chat.completion","created":1693291403,"sentence_id":0,"is_end":false,"is_truncated":false,"result":" Certainly! Here are a few popular self-driving routes in China that you may find helpful:\\n\\n1. The Silk Road: This route runs from Xi\'an to Kashgar, passing through the ancient cities of the Silk Road. It\'s a long route, but it offers stunning scenery and a glimpse into China\'s rich ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":70,"total_tokens":81}}
data: {"id":"as-b2wn45xp3d","object":"chat.completion","created":1693291408,"sentence_id":1,"is_end":false,"is_truncated":false,"result":"history.\\n2. The Yellow Mountains: Located in eastern China, the Yellow Mountains (Huangshan) offer breathtaking views of granite peaks, pine forests, and scenic valleys. The area is also home to many ancient villages and cultural sites.\\n3. The Li River: This route runs from Guilin to Yangshuo, passing through some of China\'s ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":68,"total_tokens":149}}
data: {"id":"as-b2wn45xp3d","object":"chat.completion","created":1693291413,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"most beautiful karst landscape. The scenery is stunning, with towering limestone cliffs, picturesque villages, and scenic river views.\\n4. The Tibetan Plateau: If you\'re looking for a more adventurous route, consider driving across the Tibetan Plateau. The route takes you through high-altitude mountains, vast grasslands, and picturesque villages. However, keep in ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":68,"total_tokens":217}}
data: {"id":"as-b2wn45xp3d","object":"chat.completion","created":1693291417,"sentence_id":3,"is_end":false,"is_truncated":false,"result":"mind that the roads can be rough, and the high altitude may pose some health risks.\\n5. The Coastal Route: This route runs along China\'s eastern coast, passing through major cities like Shanghai, Hangzhou, and Beijing. It offers a mix of urban and rural scenery, as well as many historical and ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":67,"total_tokens":284}}
data: {"id":"as-b2wn45xp3d","object":"chat.completion","created":1693291421,"sentence_id":4,"is_end":false,"is_truncated":false,"result":"cultural sites.\\n\\nThese are just a few examples of popular self-driving routes in China. Keep in mind that driving in China can be challenging, especially for foreigners who may not be familiar with local roads and driving customs. Be sure to research the route thoroughly, obtain the necessary permits and insurance, ","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":67,"total_tokens":351}}
data: {"id":"as-b2wn45xp3d","object":"chat.completion","created":1693291422,"sentence_id":5,"is_end":true,"is_truncated":false,"result":"and drive defensively.","need_clear_history":false,"usage":{"prompt_tokens":11,"completion_tokens":3,"total_tokens":354}}
错误码
如果请求错误,服务器返回的JSON文本包含以下参数。
名称 | 描述 |
---|---|
error_code | 错误码 |
error_msg | 错误描述信息,帮助理解和解决发生的错误 |
例如Access Token失效返回以下内容,需要重新获取新的Access Token再次请求。
{
"error_code": 110,
"error_msg": "Access token invalid or no longer valid"
}
千帆大模型平台相关错误码,请查看错误码说明。
请求参数
聊天上下文信息。说明: (1)messages成员不能为空,1个成员表示单轮对话,多个成员表示多轮对话 (2)最后一个message为当前请求的信息,前面的message为历史对话信息 (3)必须为奇数个成员,成员中message的role必须依次为user、assistant (4)最后一个message的content长度(即此轮对话的问题)不能超过3000 token;如果messages中content总长度大于3000 token,系统会依次遗忘最早的历史会话,直到content的总长度不超过3000 token
当前支持以下: user: 表示用户 assistant: 表示对话助
是否以流式接口的形式返回数据,默认false
说明: (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定 (2)范围 (0, 1.0],不能为0 (3)建议该参数和top_p只设置1个
Top-K 采样参数,在每轮token生成时,保留k个概率最高的token作为候选。说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围:正整数
说明: (1)影响输出文本的多样性,取值越大,生成文本的多样性越强 (2)取值范围 [0, 1.0] (3)建议该参数和temperature只设置1个
通过对已生成的token增加惩罚,减少重复生成的现象。说明: (1)值越大表示惩罚越大 (2)取值范围:[1.0, 2.0]
生成停止标识。当模型生成结果以stop中某个元素结尾时,停止文本生成。说明: (1)每个元素长度不超过20字符。 (2)最多4个元素
表示最终用户的唯一标识符,可以监视和检测滥用行为,防止接口恶意调用
{
"messages\t": [
{
"role\t": "string",
"content": "string"
}
],
"stream": true,
"temperature\t": 0,
"top_k\t": 0,
"top_p\t": 0,
"penalty_score": 0,
"stop\t": [
"string"
],
"user_id": "string"
}
示例代码
返回响应
回包类型。 chat.completion:多轮对话返回
当前生成的结果是否被截断
表示用户输入是否存在安全,是否关闭当前会话,清理历史会话信息 true:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息 false:否,表示用户输入无安全风险
token统计信息,token数 = 汉字数+单词数*1.3 (仅为估算逻辑)
{
"id": "as-85n3nbnbv5",
"object": "chat.completion",
"created": 1693291160,
"result": " Hello! My name is Assistant, and I'm here to help you with any questions or concerns you may have. I strive to provide respectful, honest, and helpful responses that are free from harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. I'm here to assist you in a socially unbiased and positive manner, and I will do my best to provide accurate and reliable information. If a question doesn't make sense or is not factually coherent, I will explain why instead of providing an incorrect answer. If I don't know the answer to a question, I will let you know and do my best to find a reliable source of information for you. Please feel free to ask me anything, and I'll do my best to assist you.",
"is_truncated": false,
"need_clear_history": false,
"usage": {
"prompt_tokens": 3,
"completion_tokens": 172,
"total_tokens": 175
}
}