1. 接口描述
接口请求域名: hunyuan.tencentcloudapi.com 。
腾讯混元大模型是由腾讯研发的大语言模型,具备强大的中文创作能力,复杂语境下的逻辑推理能力,以及可靠的任务执行能力。本接口支持流式或非流式调用,当使用流式调用时为 SSE 协议。
- 本接口暂不支持返回图片内容。
- 默认每种模型单账号限制并发数为 5 路,如您有提高并发限制的需求请 联系我们 。
- 请使用 SDK 调用本接口,每种开发语言的 SDK Git 仓库 examples/hunyuan/v20230901/ 目录下有提供示例供参考。SDK 链接在文档下方 “开发者资源 - SDK” 部分提供。
- 我们推荐您使用 API Explorer,方便快速地在线调试接口和下载各语言的示例代码,点击打开。
2. 输入参数
以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。
参数名称 | 必选 | 类型 | 描述 |
---|---|---|---|
Action | 是 | String | 公共参数,本接口取值:ChatCompletions。 |
Version | 是 | String | 公共参数,本接口取值:2023-09-01。 |
Region | 否 | String | 公共参数,此参数为可选参数。 |
Model | 是 | String | 模型名称,可选值包括 hunyuan-lite、hunyuan-standard、hunyuan-standard-256K、hunyuan-pro、 hunyuan-code、 hunyuan-role、 hunyuan-functioncall、 hunyuan-vision。 各模型介绍请阅读 产品概述 中的说明。 注意: 不同的模型计费不同,请根据 购买指南 按需调用。 示例值:hunyuan-pro |
Messages.N | 是 | Array of Message | 聊天上下文信息。 说明: 1. 长度最多为 40,按对话时间从旧到新在数组中排列。 2. Message.Role 可选值:system、user、assistant、 tool。 其中,system 角色可选,如存在则必须位于列表的最开始。user(tool) 和 assistant 需交替出现(一问一答),以 user 提问开始,user(tool)提问结束,且 Content 不能为空。Role 的顺序示例:[system(可选) user assistant user assistant user ...]。 3. Messages 中 Content 总长度不能超过模型输入长度上限(可参考 产品概述 文档),超过则会截断最前面的内容,只保留尾部内容。 |
Stream | 否 | Boolean | 流式调用开关。 说明: 1. 未传值时默认为非流式调用(false)。 2. 流式调用时以 SSE 协议增量返回结果(返回值取 Choices[n].Delta 中的值,需要拼接增量数据才能获得完整结果)。 3. 非流式调用时: 调用方式与普通 HTTP 请求无异。 接口响应耗时较长,如需更低时延建议设置为 true。 只返回一次最终结果(返回值取 Choices[n].Message 中的值)。 注意: 通过 SDK 调用时,流式和非流式调用需用不同的方式获取返回值,具体参考 SDK 中的注释或示例(在各语言 SDK 代码仓库的 examples/hunyuan/v20230901/ 目录中)。 示例值:false |
StreamModeration | 否 | Boolean | 流式输出审核开关。 说明: 1. 当使用流式输出(Stream 字段值为 true)时,该字段生效。 2. 输出审核有流式和同步两种模式,流式模式首包响应更快。未传值时默认为流式模式(true)。 3. 如果值为 true,将对输出内容进行分段审核,审核通过的内容流式输出返回。如果出现审核不过,响应中的 FinishReason 值为 sensitive。 4. 如果值为 false,则不使用流式输出审核,需要审核完所有输出内容后再返回结果。 注意: 当选择流式输出审核时,可能会出现部分内容已输出,但中间某一段响应中的 FinishReason 值为 sensitive,此时说明安全审核未通过。如果业务场景有实时文字上屏的需求,需要自行撤回已上屏的内容,并建议自定义替换为一条提示语,如 “这个问题我不方便回答,不如我们换个话题试试”,以保障终端体验。 示例值:false |
TopP | 否 | Float | 说明: 1. 影响输出文本的多样性。模型已有默认参数,不传值时使用各模型推荐值,不推荐用户修改。 2. 取值区间为 [0.0, 1.0]。取值越大,生成文本的多样性越强。 |
Temperature | 否 | Float | 说明: 1. 影响模型输出多样性,模型已有默认参数,不传值时使用各模型推荐值,不推荐用户修改。 2. 取值区间为 [0.0, 2.0]。较高的数值会使输出更加多样化和不可预测,而较低的数值会使其更加集中和确定。 |
EnableEnhancement | 否 | Boolean | 功能增强(如搜索)开关。 说明: 1. hunyuan-lite 无功能增强(如搜索)能力,该参数对 hunyuan-lite 版本不生效。 2. 未传值时默认打开开关。 3. 关闭时将直接由主模型生成回复内容,可以降低响应时延(对于流式输出时的首字时延尤为明显)。但在少数场景里,回复效果可能会下降。 4. 安全审核能力不属于功能增强范围,不受此字段影响。 示例值:true |
Tools.N | 否 | Array of Tool | 可调用的工具列表,仅对 hunyuan-functioncall 模型生效。 |
ToolChoice | 否 | String | 工具使用选项,可选值包括 none、auto、custom。 说明: 1. 仅对 hunyuan-functioncall 模型生效。 2. none:不调用工具;auto:模型自行选择生成回复或调用工具;custom:强制模型调用指定的工具。 3. 未设置时,默认值为auto 示例值:auto |
CustomTool | 否 | Tool | 强制模型调用指定的工具,当参数ToolChoice为custom时,此参数为必填 |
SearchInfo | 否 | Boolean | 默认是false,在值为true且命中搜索时,接口会返回SearchInfo 示例值:false |
Citation | 否 | Boolean | 搜索引文角标开关。 说明: 1. 配合EnableEnhancement和SearchInfo参数使用。打开后,回答中命中搜索的结果会在片段后增加角标标志,对应SearchInfo列表中的链接。 2. false:开关关闭,true:开关打开。 3. 未传值时默认开关关闭(false)。 示例值:false |
EnableSpeedSearch | 否 | Boolean | 是否开启极速版搜索,默认false,不开启;在开启且命中搜索时,会启用极速版搜索,流式输出首字返回更快。 示例值:false |
3. 输出参数
参数名称 | 类型 | 描述 |
---|---|---|
Created | Integer | Unix 时间戳,单位为秒。 示例值:1705634032 |
Usage | Usage | Token 统计信息。 按照总 Token 数量计费。 |
Note | String | 免责声明。 示例值:以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记 |
Id | String | 本次请求的 RequestId。 示例值:9c772634-8824-43e8-bc24-8bc4c19b9151 |
Choices | Array of Choice | 回复内容。 |
ErrorMsg | ErrorMsg | 错误信息。 如果流式返回中服务处理异常,返回该错误信息。 注意:此字段可能返回 null,表示取不到有效值。 |
ModerationLevel | String | 多轮会话风险审核,值为1时,表明存在信息安全风险,建议终止客户多轮会话。 示例值:1 |
SearchInfo | SearchInfo | 搜索结果信息 |
RequestId | String | 唯一请求 ID,由服务端生成,每次请求都会返回(若请求因其他原因未能抵达服务端,则该次请求不会获得 RequestId)。定位问题时需要提供该次请求的 RequestId。本接口为流式响应接口,当请求成功时,RequestId 会被放在 HTTP 响应的 Header "X-TC-RequestId" 中。 |
4. 示例
示例1 system prompt 示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明 system prompt 的使用方法。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"TopP": 1,
"Temperature": 1,
"Model": "hunyuan-pro",
"Stream": true,
"Messages": [
{
"Role": "system",
"Content": "将英文单词转换为包括中文翻译、英文释义和一个例句的完整解释。请检查所有信息是否准确,并在回答时保持简洁,不需要任何其他反馈。"
},
{
"Role": "user",
"Content": "nice"
}
]
}
输出示例
HTTP/1.1 200 OK
Cache-Control: no-cache
Connection: keep-alive
Content-Type: text/event-stream
Date: Tue, 21 Nov 2023 06:56:00 GMT
Transfer-Encoding: chunked
X-TC-RequestId: 61a8459b-27c8-4868-af8f-f374db0245f8
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"很好"}}],"Created":1705634813,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":1,"TotalTokens":37}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":":"}}],"Created":1705634813,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":2,"TotalTokens":38}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"nice"}}],"Created":1705634813,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":3,"TotalTokens":39}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"\n"}}],"Created":1705634813,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":4,"TotalTokens":40}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"英文"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":5,"TotalTokens":41}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"释义"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":6,"TotalTokens":42}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":":"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":7,"TotalTokens":43}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"ple"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":8,"TotalTokens":44}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"asing"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":9,"TotalTokens":45}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":" or"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":10,"TotalTokens":46}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":" acceptable"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":11,"TotalTokens":47}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"\n"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":12,"TotalTokens":48}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"例"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":13,"TotalTokens":49}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"句"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":14,"TotalTokens":50}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":":"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":15,"TotalTokens":51}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"She"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":16,"TotalTokens":52}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":" had"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":17,"TotalTokens":53}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":" a"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":18,"TotalTokens":54}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":" nice"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":19,"TotalTokens":55}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":" smile"}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":20,"TotalTokens":56}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"."}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":21,"TotalTokens":57}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"stop","Delta":{"Role":"assistant","Content":""}}],"Created":1705634814,"Id":"681ef57e-9f1e-4faa-a2d3-07b655a1fa1f","Usage":{"PromptTokens":36,"CompletionTokens":21,"TotalTokens":57}}
示例2 多轮对话示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明如何给模型传入多轮消息。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"TopP": 1,
"Temperature": 1,
"Model": "hunyuan-pro",
"Stream": true,
"Messages": [
{
"Role": "user",
"Content": "请介绍下自己"
},
{
"Role": "assistant",
"Content": "我的名字是腾讯混元大模型,是腾讯开发的一款大型语言模型。我能够理解人类语言,擅长提供问答服务、常识推理、文本创作、数学计算和互动问答等服务。如果您需要帮助或有任何问题,请随时向我提问。"
},
{
"Role": "user",
"Content": "请给我讲个字数小于10个字的搞笑笑话"
},
{
"Role": "assistant",
"Content": "猫头鹰遇见眯眯眼,看得眼睛都眯成线。”"
},
{
"Role": "user",
"Content": "不好笑,换一个吧"
}
]
}
输出示例
HTTP/1.1 200 OK
Cache-Control: no-cache
Connection: keep-alive
Content-Type: text/event-stream
Date: Tue, 21 Nov 2023 06:56:00 GMT
Transfer-Encoding: chunked
X-TC-RequestId: 61a8459b-27c8-4868-af8f-f374db0245f8
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"青蛙"}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":1,"TotalTokens":86}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"跳"}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":2,"TotalTokens":87}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"高"}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":3,"TotalTokens":88}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":","}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":4,"TotalTokens":89}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"比"}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":5,"TotalTokens":90}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"下马"}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":6,"TotalTokens":91}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"。"}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":7,"TotalTokens":92}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"stop","Delta":{"Role":"assistant","Content":""}}],"Created":1705634032,"Id":"9c772634-8824-43e8-bc24-8bc4c19b9151","Usage":{"PromptTokens":85,"CompletionTokens":7,"TotalTokens":92}}
示例3 请求失败示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例给出接口调用失败时的响应例子。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"TopP": 0,
"Temperature": 4.8,
"Model": "hunyuan-pro",
"Messages": [
{
"Role": "user",
"Content": "介绍下自己"
}
]
}
输出示例
{
"Response": {
"RequestId": "188cc996-ab09-49a7-aa9f-1df88f11c6b4",
"Error": {
"Code": "InvalidParameter",
"Message": "Temperature must be 2 or less"
}
}
}
示例4 流式请求成功示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例给出接口正常调用时的响应例子。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"TopP": 1,
"Temperature": 1,
"Model": "hunyuan-pro",
"Stream": true,
"Messages": [
{
"Role": "user",
"Content": "计算1+1"
}
]
}
输出示例
HTTP/1.1 200 OK
Cache-Control: no-cache
Connection: keep-alive
Content-Type: text/event-stream
Date: Tue, 21 Nov 2023 06:56:00 GMT
Transfer-Encoding: chunked
X-TC-RequestId: 61a8459b-27c8-4868-af8f-f374db0245f8
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"1"}}],"Created":1700549760,"Id":"148b89ef-14e1-489f-8e70-b767e5b27d56","Usage":{"PromptTokens":4,"CompletionTokens":2,"TotalTokens":6}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"+"}}],"Created":1700549760,"Id":"148b89ef-14e1-489f-8e70-b767e5b27d56","Usage":{"PromptTokens":4,"CompletionTokens":3,"TotalTokens":7}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"1"}}],"Created":1700549760,"Id":"148b89ef-14e1-489f-8e70-b767e5b27d56","Usage":{"PromptTokens":4,"CompletionTokens":4,"TotalTokens":8}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"="}}],"Created":1700549760,"Id":"148b89ef-14e1-489f-8e70-b767e5b27d56","Usage":{"PromptTokens":4,"CompletionTokens":5,"TotalTokens":9}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"","Delta":{"Role":"assistant","Content":"2"}}],"Created":1700549760,"Id":"148b89ef-14e1-489f-8e70-b767e5b27d56","Usage":{"PromptTokens":4,"CompletionTokens":5,"TotalTokens":9}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"FinishReason":"stop","Delta":{"Role":"assistant","Content":""}}],"Created":1700549760,"Id":"148b89ef-14e1-489f-8e70-b767e5b27d56","Usage":{"PromptTokens":4,"CompletionTokens":5,"TotalTokens":9}}
示例5 非流式请求成功示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明如何用非流式方式调用接口。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"TopP": 0,
"Stream": false,
"Temperature": 0,
"Model": "hunyuan-pro",
"Messages": [
{
"Role": "user",
"Content": "你好呀!"
}
]
}
输出示例
{
"Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记",
"Choices": [
{
"FinishReason": "stop",
"Message": {
"Role": "assistant",
"Content": "你好!很高兴为您提供帮助。请问有什么问题我可以帮助您解决?"
}
}
],
"Created": 1710902312,
"Id": "e4657570-94a5-45f1-896c-a00ac3471d51",
"Usage": {
"PromptTokens": 3,
"CompletionTokens": 14,
"TotalTokens": 17
}
}
示例6 hunyuan-functioncall模型非流式请求成功示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明hunyuan-functioncall模型如何用非流式方式调用接口。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"TopP": 0,
"Stream": false,
"Temperature": 0,
"Model": "hunyuan-functioncall",
"Messages": [
{
"Role": "user",
"Content": "北京和深圳今天天气如何"
}
],
"Tools": [
{
"Type": "function",
"Function": {
"Name": "get_current_weather",
"Description": "获取当前地点的天气",
"Parameters": "{\n \"type\": \"object\",\n \"properties\": {\n \"location\": {\n \"type\": \"string\",\n \"description\": \"城市名称\"\n },\n \"unit\": {\n \"type\": \"string\",\n \"enum\": [\n \"celsius\",\n \"fahrenheit\"\n ]\n }\n },\n \"required\": [\n \"location\"\n ]\n }"
}
}
],
"ToolChoice": "auto"
}
输出示例
{
"Response": {
"RequestId": "e7f5ce41-87fd-4977-803c-54cded687cd9",
"Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记",
"Choices": [
{
"Message": {
"Role": "assistant",
"Content": "使用get_current_weather工具来获取北京和深圳的当前天气情况。\n\t\n\t用户想要知道北京和深圳今天的天气情况。用户的请求是关于天气的查询,需要使用天气查询工具来获取信息。",
"ToolCalls": [
{
"Type": "function",
"Function": {
"Name": "get_current_weather",
"Arguments": "{\"location\":[\"北京\",\"深圳\"],\"unit\":\"celsius\"}"
}
}
]
},
"FinishReason": "tool_calls"
}
],
"Created": 1719638614,
"Usage": {
"PromptTokens": 6,
"CompletionTokens": 46,
"TotalTokens": 52
}
}
}
示例7 hunyuan-functioncall模型流式请求成功示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明hunyuan-functioncall模型如何用流式方式调用接口。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"Model": "hunyuan-functioncall",
"Stream": true,
"Messages": [
{
"Role": "user",
"Content": "北京和深圳今天天气如何"
}
],
"Tools": [
{
"Type": "function",
"Function": {
"Name": "get_current_weather",
"Description": "获取当前地点的天气",
"Parameters": "{\n \"type\": \"object\",\n \"properties\": {\n \"location\": {\n \"type\": \"string\",\n \"description\": \"城市名称\"\n },\n \"unit\": {\n \"type\": \"string\",\n \"enum\": [\n \"celsius\",\n \"fahrenheit\"\n ]\n }\n },\n \"required\": [\n \"location\"\n ]\n }"
}
}
],
"ToolChoice": "auto"
}
输出示例
说明: 对于Delta.ToolCalls每一次的输出值应该以Id为标识对Type、Name、Arguments字段进行合并,本示例中的ToolCalls 最终值为:[{"Id": "call_cq177uk2c3m1v7ep35dg","Type": "function","Function": {"Name": "get_current_weather", "Arguments": "{\"location\":[\"北京\",\"深圳\"],\"unit\":\"celsius\"}"}}]
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"","ToolCalls":[{"Id":"call_cq154vk2c3m1v7ep3530","Type":"function","Function":{"Name":"get_current_weather","Arguments":""}}]},"FinishReason":""}],"Created":1719816830,"Id":"cd37cf66-089f-4ab2-8118-e18baa238462","Usage":{"PromptTokens":6,"CompletionTokens":0,"TotalTokens":6}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"","ToolCalls":[{"Id":"call_cq154vk2c3m1v7ep3530","Type":"function","Function":{"Name":"","Arguments":"{\"location\":\"北京\"}"}}]},"FinishReason":""}],"Created":1719816830,"Id":"cd37cf66-089f-4ab2-8118-e18baa238462","Usage":{"PromptTokens":6,"CompletionTokens":0,"TotalTokens":6}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":"计划使用get_current_weather工具来获取北京和深圳的当前天气。\n\t\n\t用户想要知道北京和深圳今天的天气情况。用户的请求是关于天气的查询,需要使用天气查询工具来获取信息。"},"FinishReason":""}],"Created":1719816830,"Id":"cd37cf66-089f-4ab2-8118-e18baa238462","Usage":{"PromptTokens":6,"CompletionTokens":46,"TotalTokens":52}}
data: {"Note":"以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记","Choices":[{"Delta":{"Role":"assistant","Content":""},"FinishReason":"tool_calls"}],"Created":1719816830,"Id":"cd37cf66-089f-4ab2-8118-e18baa238462","Usage":{"PromptTokens":6,"CompletionTokens":46,"TotalTokens":52}}
示例8 hunyuan-functioncall模型多轮对话示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明hunyuan-functioncall模型多轮对话如何调用接口。
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"Model": "hunyuan-functioncall",
"Messages": [
{
"Role": "user",
"Content": "北京和深圳今天天气如何"
},
{
"Role": "assistant",
"Content": "使用get_current_weather工具来获取北京和深圳的当前天气。\n\t\n\t用户想要知道北京和深圳今天的天气情况。用户提供了两个城市名称,但没有指定温度单位,根据常识,默认用户需要的是摄氏度。",
"ToolCalls": [
{
"Id": "call_cq16e7k2c3m1v7ep35c0",
"Type": "function",
"Function": {
"Name": "get_current_weather",
"Arguments": "{\"location\":\"北京\",\"unit\":\"celsius\"}"
}
}
]
},
{
"Role": "tool",
"ToolCallId": "call_cq16e7k2c3m1v7ep35c0",
"Content": "{\"temperature\": 35, \"wind\": \"南\", \"condition\": \"暴雨\"}"
}
],
"Tools": [
{
"Type": "function",
"Function": {
"Name": "get_current_weather",
"Description": "获取当前地点的天气",
"Parameters": "{\n \"type\": \"object\",\n \"properties\": {\n \"location\": {\n \"type\": \"string\",\n \"description\": \"城市名称\"\n },\n \"unit\": {\n \"type\": \"string\",\n \"enum\": [\n \"celsius\",\n \"fahrenheit\"\n ]\n }\n },\n \"required\": [\n \"location\"\n ]\n }"
}
}
],
"ToolChoice": "auto"
}
输出示例
{
"Response": {
"RequestId": "5a112898-d802-4bca-8ba2-7ce2388b98e8",
"Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记",
"Choices": [
{
"Message": {
"Role": "assistant",
"Content": "北京今天的天气情况是:\n温度:35摄氏度\n风向:南\n天气状况:暴雨\n\n深圳今天的天气情况是:\n温度:35摄氏度\n风向:南\n天气状况:暴雨"
},
"FinishReason": "stop"
}
],
"Created": 1719822322,
"Id": "5a112898-d802-4bca-8ba2-7ce2388b98e8",
"Usage": {
"PromptTokens": 71,
"CompletionTokens": 42,
"TotalTokens": 113
}
}
}
示例9 图片理解示例
推荐使用 API Explorer 调用接口,见本文档顶部说明。该示例说明hunyuan-vision模型如何调用接口。
ImageUrl.Url 支持图片链接以及图片 base64 两种方式
jpeg 图片转 base64 示例 (其他图片格式注意修改为对应的 MIME 类型, 如 image/png, image/webp, image/bmp 等):
import base64
with open("1.jpeg", 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read())
print("data:image/jpeg;base64,"+encoded_image.decode('utf-8'))
输入示例
POST / HTTP/1.1
Host: hunyuan.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: ChatCompletions
<公共请求参数>
{
"Model": "hunyuan-vision",
"Messages": [
{
"Role": "user",
"Contents": [
{
"Type": "text",
"Text": "下面图片中是哪个公司的 Logo?"
},
{
"Type": "image_url",
"ImageUrl": {
"Url": "https://cloudcache.tencent-cloud.com/qcloud/ui/portal-set/build/About/images/bg-product-series_87d.png"
}
}
]
}
],
"Stream": false
}
输出示例
{
"Response": {
"RequestId": "a21f9d7e-c18a-438b-bfb4-7941a2adf8ae",
"Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记",
"Choices": [
{
"Message": {
"Role": "assistant",
"Content": "这张图片中展示的Logo属于腾讯公司。"
},
"FinishReason": "stop"
}
],
"Created": 1714290436,
"Id": "a21f9d7e-c18a-438b-bfb4-7941a2adf8ae",
"Usage": {
"PromptTokens": 7,
"CompletionTokens": 10,
"TotalTokens": 17
}
}
}
5. 开发者资源
腾讯云 API 平台
腾讯云 API 平台 是综合 API 文档、错误码、API Explorer 及 SDK 等资源的统一查询平台,方便您从同一入口查询及使用腾讯云提供的所有 API 服务。
API Inspector
用户可通过 API Inspector 查看控制台每一步操作关联的 API 调用情况,并自动生成各语言版本的 API 代码,也可前往 API Explorer 进行在线调试。
SDK
云 API 3.0 提供了配套的开发工具集(SDK),支持多种编程语言,能更方便的调用 API。
- Tencent Cloud SDK 3.0 for Python: GitHub Gitee
- Tencent Cloud SDK 3.0 for Java: GitHub Gitee
- Tencent Cloud SDK 3.0 for PHP: GitHub Gitee
- Tencent Cloud SDK 3.0 for Go: GitHub Gitee
- Tencent Cloud SDK 3.0 for Node.js: GitHub Gitee
- Tencent Cloud SDK 3.0 for .NET: GitHub Gitee
命令行工具
6. 错误码
以下仅列出了接口业务逻辑相关的错误码,其他错误码详见 公共错误码。
错误码 | 描述 |
---|---|
FailedOperation.EngineRequestTimeout | 引擎层请求超时;请稍后重试。 |
FailedOperation.EngineServerError | 引擎层内部错误;请稍后重试。 |
FailedOperation.EngineServerLimitExceeded | 引擎层请求超过限额;请稍后重试。 |
FailedOperation.FreeResourcePackExhausted | 免费资源包余量已用尽,请购买资源包或开通后付费。 |
FailedOperation.ResourcePackExhausted | 资源包余量已用尽,请购买资源包或开通后付费。 |
FailedOperation.ServiceNotActivated | 服务未开通,请前往控制台申请试用。 |
FailedOperation.ServiceStop | 用户主动停服。 |
FailedOperation.ServiceStopArrears | 欠费停服。 |
InternalError | 内部错误。 |
InvalidParameter | 参数错误。 |
InvalidParameterValue | 参数取值错误。 |
InvalidParameterValue.Model | 模型不存在。 |
LimitExceeded | 超过配额限制。 |