调过那么多api,就属腾讯混元模型最难调用,官方文档支支吾吾,一定引流到他的测试平台去调试。浪费了一个小时以后,最终还是用官方提供的SDK才完成开发。
先pip安装SDK,整个互联网就找不到除官方SDK以外其他访问的方法了
pip install qcloud-python-sdk
官方SDK:tencentcloud-sdk-python: Tencent Cloud API 3.0 SDK for Python - Gitee.com
import tencentcloud.common.exception.tencent_cloud_sdk_exception as exce
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.credential import Credential
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models
# 腾讯云API密钥
SECRET_ID = '你的id'
SECRET_KEY = '你的key'
# 初始化凭据
credential = Credential(SECRET_ID, SECRET_KEY)
# 创建HTTP配置
http_profile = HttpProfile()
http_profile.req_method = "POST"
http_profile.req_timeout = 30
http_profile.endpoint = "hunyuan.tencentcloudapi.com"
# 创建客户端配置
client_profile = ClientProfile()
client_profile.http_profile = http_profile
# 创建客户端
client = hunyuan_client.HunyuanClient(credential, "", client_profile)
# 创建请求对象
req = models.ChatCompletionsRequest()
# 设置请求参数
params = {
"TopP": 1,
"Temperature": 0.7,
"Model": "hunyuan-lite", # 指定使用hunyuan-lite模型
"Stream": False,
"Messages": [
{
"Role": "system",
"Content": "将用户的输入转换成你的需求"
},
{
"Role": "user",
"Content": "你好"
}
]
}
# 将参数编码为JSON字符串
req.from_json_string(json.dumps(params))
# 发送请求
try:
resp = client.ChatCompletions(req)
# 解析响应
response_data = json.loads(resp.to_json_string())
print(response_data)
except exce.TencentCloudSDKException as err:
print(err)