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
相关推荐
doiito12 小时前
【Agent Harness】Gliding Horse 上下文动态感知与智能压缩:让 Agent 真正“听得进”每一句话
ai·rust·架构设计·系统设计·ai agent
探索云原生1 天前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes
Zy宇1 天前
从养 OpenClaw 到养社区 AI:一套 Multi-Agent 社区的设计思路
人工智能·ai
doiito2 天前
【Agent Harness】Gliding Horse 记忆系统深度剖析:像 CPU 一样思考的 AI 记忆架构
ai·rust·架构设计·系统设计·ai agent
mobility2 天前
免费AI视频生成器:我如何用零成本做出带旁白字幕的多场景AI视频
ai·vibe coding
doiito2 天前
【Agent Harness】Gliding Horse 给 Agent OS 装上双曲空间引擎与默克尔树边云同步
ai·rust·架构设计·系统设计·ai agent
knqiufan2 天前
从 Python 到 TypeScript,用 GLM-5.2 跑通 PowerMem SDK 的长程任务工程
ai·memory·agentic·powermem
小白跃升坊3 天前
Codex 增强部署:基于 Codex++ 接入 DeepSeek
ai·ai编程·codex·deepseek·ai coding·codex++
AlfredZhao3 天前
GPT 省钱,不是别用最新模型,而是别浪费缓存
gpt·ai
doiito3 天前
【Agent Harness】Gliding Horse 本体论系统设计:给 AI Agent 装上“语义大脑”
ai·rust·架构设计·系统设计·ai agent