腾讯-混元
请求 URL
https://ai-gateway-intl.eo-edgefunctions7.com
请求方法
POST
请求头
参数名称 | 必选 | 类型 | 描述 |
OE-Key | 是 | string | 网关 API 密钥 |
OE-Gateway-Name | 是 | string | 网关名称 |
OE-AI-Provider | 是 | string | AI 服务提供商,固定值:hunyuan |
OE-Gateway-Version | 是 | string | 网关版本,固定值:2 |
Authorization | 否 | string | LLM 服务商的认证密钥 |
X-TC-Timestamp | 是 | string | 时间戳 |
X-TC-Action | 是 | string | 固定值:ChatCompletions |
X-TC-Version | 否 | string | 固定值:2023-09-01 |
Content-Type | 是 | string | 固定值:application/json |
请求体
参数名称 | 必选 | 类型 | 描述 |
model | 是 | string | 模型名称 |
messages | 是 | array | 消息列表,每个消息包含角色和内容 |
stream | 是 | boolean | 是否启用流式处理 |
请求体示例
{"model": "hunyuan-lite","messages": [{"role": "user","content": "1+1=?"}],"stream": true}
响应示例
响应格式为 JSON,具体内容根据实际 API 返回结果而定。
curl 请求示例
curl --location --request POST 'https://ai-gateway-intl.eo-edgefunctions7.com' \--header 'Authorization: xxxx' \--header 'X-TC-Timestamp: 1732178793' \--header 'X-TC-Action: ChatCompletions' \--header 'X-TC-Version: 2023-09-01' \--header 'X-TC-Language: zh-CN' \--header 'OE-Key: xxxx' \--header 'OE-Gateway-Name: xxxx' \--header 'OE-AI-Provider: hunyuan' \--header 'OE-Gateway-Version: 2' \--header 'Content-Type: application/json' \--data-raw '{"model": "hunyuan-lite","messages": [{"role": "user","content": "1+1=?"}],"stream": true}'
注意事项
注意:
1. 请确保所有必填参数都已正确填写。
2. 根据实际情况调整
Authorization
、X-TC-Timestamp
填写。3. 请求体中的
messages
数组可以包含多个消息对象,具体根据业务需求而定。4. 混元原始接口的请求参数为大驼峰命名规范,为统一请求入参,现约定所有入参统一为小驼峰命名规范,服务内部统一处理。
参考资料-混元大模型 api 签名算法
因腾讯云混元大模型的接口鉴权较其他大模型厂商更为严格,现特为开发者朋友们提供了 node 版本的签名算法。具体如下:
const CryptoJS = require("crypto-js");// SHA256 加密函数function sha256(message, secret = "") {const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secret);return hmac.update(message).finalize();}// 计算消息的 SHA256 哈希值function getHash(message) {const hash = CryptoJS.SHA256(message);return hash.toString(CryptoJS.enc.Hex);}// 根据时间戳获取日期字符串function getDate(timestamp) {const date = new Date(timestamp * 1000);const year = date.getUTCFullYear();const month = ("0" + (date.getUTCMonth() + 1)).slice(-2);const day = ("0" + date.getUTCDate()).slice(-2);return `${year}-${month}-${day}`;}// 签名请求函数function signRequest(secretId,secretKey,endpoint,service,action,timestamp,payload) {const date = getDate(timestamp);const hashedRequestPayload = getHash(JSON.stringify(payload));const httpRequestMethod = "POST";const canonicalUri = "/";const canonicalQueryString = "";const canonicalHeaders = `content-type:application/json\nhost:${endpoint}\nx-tc-action:${action.toLowerCase()}\n`;const signedHeaders = "content-type;host;x-tc-action";const canonicalRequest = `${httpRequestMethod}\n${canonicalUri}\n${canonicalQueryString}\n${canonicalHeaders}\n${signedHeaders}\n${hashedRequestPayload}`;const algorithm = "TC3-HMAC-SHA256";const hashedCanonicalRequest = getHash(canonicalRequest);const credentialScope = `${date}/${service}/tc3_request`;const stringToSign = `${algorithm}\n${timestamp}\n${credentialScope}\n${hashedCanonicalRequest}`;const kDate = sha256(date, `TC3${secretKey}`);const kService = sha256(service, kDate);const kSigning = sha256("tc3_request", kService);const signature = sha256(stringToSign, kSigning, "hex");const authorization = `${algorithm} Credential=${secretId}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;return authorization;}// 将对象的键转换为首字母大写形式function capitalizeKeys(obj) {return Object.keys(obj).reduce((acc, key) => {const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);acc[capitalizedKey] = Array.isArray(obj[key])? obj[key].map(capitalizeKeys): typeof obj[key] === "object" && obj[key] !== null? capitalizeKeys(obj[key]): obj[key];return acc;}, {});}// 生成curl命令function generateCurlCommand(endpoint,authorization,timestamp,action,version,payload) {const curlCommand ="curl -X POST " +"https://" +endpoint +'\n -H "Authorization: ' +authorization +'"' +'\n -H "Content-Type: application/json; charset=utf-8"' +'\n -H "Host: ' +endpoint +'"' +'\n -H "X-TC-Action: ' +action +'"' +'\n -H "X-TC-Timestamp: ' +timestamp.toString() +'"' +'\n -H "X-TC-Version: ' +version +'"' +"\n -d '" +JSON.stringify(payload) +"'";return curlCommand;}// 主函数function main() {try {// 📢 注意:这里的密钥替换为您自己的腾讯云控制台秘钥const SECRET_ID = "SECRET_ID";const SECRET_KEY = "SECRET_KEY";// 定义API的相关参数const endpoint = "hunyuan.tencentcloudapi.com";const service = "hunyuan";const action = "ChatCompletions";const version = "2023-09-01";const timestamp = parseInt(Date.now() / 1000);// 📢 注意:这里的请求参数替换为您实际的请求参数值const reqBody = {model: "hunyuan-lite",messages: [{role: "user",content: "1+1=?",},],};// 获取请求体并处理const body = capitalizeKeys(JSON.parse(pm.request.body.toJSON().raw));console.log("body:", JSON.stringify(body));// 签名请求const authorization = signRequest(SECRET_ID,SECRET_KEY,endpoint,service,action,timestamp,body);// 生成并打印curl命令const curlCommand = generateCurlCommand(endpoint,authorization,timestamp,action,version,body);console.log("curl:", curlCommand);} catch (error) {console.error("Error:", error);}}main();