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
相关推荐
Dawson Zhu7 小时前
基于Palantir Foundry构建半导体制造AI友好型数据中台:从良率分析场景谈起
人工智能·语言模型·架构·制造·agi
fthux8 小时前
装闭 RenoPit 源码解析(04):装修图纸和合同文件上传处理流程
人工智能·ai·开源·github·open source·renopit
运维开发王义杰11 小时前
DefectDojo 里的 curl 高危漏洞修不修?剖析 C/C++ 依赖陷阱与 Web3 真实攻防
ai
(╹◡╹)12 小时前
05.RK3588部署Qwen3-VL - 模型量化
ai
VIP_CQCRE14 小时前
用 Ace Data Cloud 快速接入 OpenAI Chat Completions API:兼容官方格式,更适合开发者落地
ai·大模型·openai·api·ace data cloud
JaydenAI14 小时前
[基于OpenEvals的自动化评估-07]评估Agent输出文本的质量[下篇]
ai·langchain·agent·evaluation·openevals
VIP_CQCRE15 小时前
用 Ace Data Cloud 自动化运营 CSDN:AI 写作、配图、发布与数据复盘
ai·自动化·api·csdn·acedatacloud
俊哥V15 小时前
每日 AI 研究简报 · 2026-08-03
人工智能·ai
JaydenAI16 小时前
[基于OpenEvals的自动化评估-08]对Agent的输出和输入进行安全性评估
ai·langchain·agent·evaluation·openevals
佛祖让我来巡山16 小时前
你以为AI在画画?醒醒,它只是在玩"马赛克消消乐"
ai·ai绘画·ai画图