申请腾讯混元的API Key并且使用LobeChat调用混元AI

申请腾讯混元的API Key并且使用LobeChat调用混元AI

之前星哥写了一篇文章《手把手教拥有你自己的大模型ChatGPT和Gemini等应用-开源lobe-chat》搭建的开源项目,今天这篇文章教大家如何添加腾讯云的混元模型,并且使用LobeChat调用腾讯混元AI。

image-20241224205905694

申请腾讯混元的API Key

步骤1:登录腾讯云

注册并通过个人实名认证或企业认证后,登录 腾讯云。如果没有账号,需要注册腾讯云,并且实名。

步骤2:开通服务

腾讯混元管理地址:https://console.cloud.tencent.com/hunyuan

腾讯混元大模型 API 已对外开放,可前往控制台 立即开通服务

image-20241224105308589

### 使用 Python 调用腾讯混元 AI API 的方法 为了成功调用腾讯混元AI API,在构建请求参数时需要注意Python字典默认无序的特点。因此,建议先利用`sorted()`函数对字典按键值进行升序排列[^1]。之后通过循环遍历已排序字典中的value,并采用`urllib.parse.quote()`来执行URL编码操作,从而形成形如“key1=value1&key2=value2”的查询字符串。 考虑到官方文档可能存在表述不清的情况以及直接使用HTTP请求方式可能遇到困难,推荐优先考虑使用官方提供的SDK来进行开发工作,这可以有效减少不必要的麻烦并提高效率[^2]。 下面给出一段基于上述原则构造的简单示例代码用于调用腾讯混元API: ```python import urllib.parse from tencentcloud.common import credential from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.hunyuan.v20230704 import hunyuan_client, models def create_query_string(params_dict): sorted_params = dict(sorted(params_dict.items())) encoded_pairs = [] for key, value in sorted_params.items(): encoded_key = urllib.parse.quote(str(key)) encoded_value = urllib.parse.quote(str(value)) encoded_pairs.append(f"{encoded_key}={encoded_value}") return "&".join(encoded_pairs) try: cred = credential.Credential("SecretId", "SecretKey") http_profile = HttpProfile() http_profile.reqMethod = "POST" http_profile.reqTimeout = 30 client_profile = ClientProfile() client_profile.httpProfile = http_profile client = hunyuan_client.HunYuanClient(cred, "", client_profile) request = models.YourRequestType() # 替换成实际使用的请求类名 response = client.YourRequestMethod(request) # 同样替换成对应的方法名称 except Exception as e: print(e) ``` 请注意替换其中的密钥信息(`SecretId`, `SecretKey`)、具体的请求类型(`YourRequestType`)和请求方法(`YourRequestMethod`)为真实情况下的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星哥玩云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值