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.测试

相关推荐
大今野16 小时前
专转本计算机·云计算核心考点精讲(概念·关键技术·服务模式)
大数据·云计算·big data·专转本
Johny_Zhao3 天前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
66IT6613 天前
Veeam ONE v13.1 发布:全新 HTML5 Web 客户端与 PostgreSQL 报告库
云计算·虚拟化·监控·数据保护·veeam
2601_9679801114 天前
Python 与云计算:AWS、GCP、Azure 实践
python·云计算·azure·aws·gcp
cc57250265314 天前
云计算技术应用专业大学期间考证怎么安排?
云计算