python成功调用 腾讯混元ai模型

调过那么多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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值