dashscope 介绍及使用(调用阿里云 AI 大模型的核心工具)

dashscope 就是阿里云百炼大模型的 Python 工具包
让你的 Python 代码能直 接调用 通义千问、DeepSeek 等 AI 大模型

pip install dashscope -i https://pypi.tuna.tsinghua.edu.cn/simple

基本设置

python 复制代码
import dashscope
from dashscope.api_entities.dashscope_response import Role

设置 API Key

python 复制代码
dashscope.api_key="your-api-key"  #去阿里百炼平台申请apikey

模型调用

基本调用格式

python 复制代码
response=dashscope.Generation.call(
    model="模型名称", #'qwen-turbo' 'deepseek-r1'等
    messages = messages ,#消息列表
    result format='message' #格式输出
)

message格式

python 复制代码
messages=[
     {"role":"system","content":"系统提示信息"},
     {"role":"user","content":"用户输入"},
     #如果有历史对话
     {"role":"assistant","content":"助手回复"},
     {"role":"user","content":"用户新的输入"},
]

例子

python 复制代码
import json		 #处理数据格式
import os        #读取系统环境
import dashscope #调用阿里云 AI 大模型的核心工具
from dashscope.api_entities.dashscope_response import Role #定义对话角色(system/user) 
api_key = 'api-key' # 从环境变量中,获取 DASHSCOPE_API_KEY
dashscope.api_key = api_key

# 封装模型响应函数
def get_response(messages):
    response = dashscope.Generation.call(
        model='deepseek-v3',
        messages=messages,
        result_format='message'  # 将输出设置为message形式
    )
    return response 
    
review = '这款音效特别不好 给你意想不到的音质。'
messages=[
    {"role": "system", "content": "你是一名舆情分析师,帮我判断产品口碑的正负向,回复请用一个词语:正向 或者 负向"},
    {"role": "user", "content": review}
  ]

response = get_response(messages)
response.output.choices[0].message.content
相关推荐
omenkk714 小时前
上下文工程:优化系统提示词,利用 KV Cache 降低 Token 成本
ai
代码方舟14 小时前
零信任架构实战:基于天远手机在网状态V即时版构建自动化通信分发网关
人工智能·ai·工具分享
MicrosoftReactor15 小时前
技术速递|GitHub Copilot App 入门指南:使用 Diff、终端和浏览器
ai·copilot·agent
TechEdu20260615 小时前
[人工智能]人工智能时代的零日漏洞与零日攻击
人工智能·网络安全·ai·信息安全
程序员无隅15 小时前
Pi Agent Loop 完整流程源码解析:从 Agent.prompt() 到 agent_end
ai
运维开发那些事15 小时前
volcano千卡集群训推最佳实践
ai·gpu算力
霸道流氓气质16 小时前
Prompt注入攻击与防御体系
ai
新中地GIS开发老师16 小时前
2026年GIS行业正在发生什么?
ai·gis开发·低空经济
啦啦啦!16 小时前
RAG初阶学习
学习·ai·知识库·rag