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
相关推荐
程序员无隅1 天前
面向Skills学习:用领域知识工程驱动 Code Agent 学习
ai
_oP_i1 天前
ai制作系统操作手册
ai
Mark White1 天前
具身智能论文伴读-第一期
人工智能·深度学习·语言模型
geinvse_seg1 天前
飞算JavaAI:把企业采购协同需求拆成一套可运行的慧采平台
java·ai·飞算
不爱记笔记1 天前
音视频内容如何纳入Obsidian知识库?分享一套完整的输入层解决方案
人工智能·ai·chatgpt·音视频·知识库·知识管理·obsidian
笨蛋©1 天前
2026制造业质量管理实战:从工程图纸识别到检验计划自动化的进阶指南
ai·数字化·质量管理·制造业·图纸识别
zhangfeng11331 天前
llamafactory 大语言模型llm 微调,关键参数深度讲解(法律大模型微调专属版)
人工智能·语言模型·自然语言处理
俊哥V1 天前
每日 AI 研究简报 · 2026-07-16
人工智能·ai
曦尧1 天前
OpenMAIC:清华团队用多智能体重新发明「网课」,从 MOOC 到 MAIC 的范式跃迁
ai·自动化
程序猿编码1 天前
没有AI框架,没有GPU,一个C语言文件跑通大模型
c语言·开发语言·人工智能·深度学习·ai·大模型