python快速接入阿里云百炼大模型

1.注册阿里云账号

访问阿里云官网,完成账号注册流程,并开通百炼服务,网址:https://bailian.console.aliyun.com

2.获取 API Key

登录阿里云百炼平台,在个人中心或相关设置页面找到并生成 API Key,妥善保管此密钥,后续调用模型时需使用

3.编写python代码

1.下载dashscope库并引入

复制代码
DashScope SDK是阿里云提供的用于调用百炼大模型的 Python 库
复制代码
pip install dashscope

import dashscope

2.配置API Key

复制代码
dashscope.api_key = '你的api-key'

3.构建请求参数

复制代码
根据具体的任务需求,构建请求参数,如模型名称、输入文本、生成参数等
复制代码
messages = [
    {'role': 'system', 'content': 'You are a helpful assistant.'},
    {'role': 'user', 'content': '请生成一篇关于人工智能的文章'}
]
parameters = {
    "model": "qwen-max",
    "result_format": "message"
}

4.发起请求并获取结果

复制代码
使用 Generation.call() 方法发起请求,将构建好的请求参数传入,等待模型生成结果
复制代码
response = dashscope.Generation.call(
    model=parameters["model"],
    messages=messages,
    result_format=parameters["result_format"]
)
if response.status_code == 200:
    print(response.output.choices[0].message.content)
else:
    print('请求失败,错误信息:', response)

5.测试

相关推荐
烦德恒21 小时前
本地五恒系统五恒品牌哪家好
阿里云
Geek-Chow1 天前
Assuming an AWS IAM Role via Federation
云计算·aws
全云在线allcloudonline2 天前
上海阿里云代理商怎么选?跨区域交付能力核验清单
阿里云·云计算·企业上云
阿斯科阀门2 天前
惠州自动化阀门厂家
阿里云
Qianyunzhiyun2 天前
AWS EC2实例启动失败?常见报错和解决办法
云计算·aws
Database_Cool_2 天前
记忆张量MemOS + 阿里云PolarDB一站式记忆管理方案发布:给AI装上不断片的记忆
数据库·人工智能·阿里云
Qianyunzhiyun2 天前
AWS EBS快照怎么创建?服务器备份详细教程
服务器·云计算·aws
Database_Cool_2 天前
阿里云 Lindorm vs Elasticsearch 检索存储一体对比:多模数据库一站式方案首选
数据库·阿里云