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
相关推荐
goehou9 小时前
MCP 查数实战:让 LLM 安全地查数据库(只读账号、SQL 校验、参数漂移)
数据库·ai·llm·教程·mcp·function calling
VIP_CQCRE11 小时前
给团队搭一个 AI 工作台:Open WebUI 接入 Ace Data Cloud 实战
docker·ai·rag·open webui·ace data cloud
Smoothcloud润云11 小时前
PyTorch推理服务动态批处理实战:兼顾吞吐与延迟
人工智能·pytorch·python·深度学习·ai·gpu算力
量子-Alex14 小时前
【大模型后训练】Does Fine-Tuning LLMs on New Knowledge Encourage Hallucinations
人工智能·语言模型·自然语言处理
空 白II14 小时前
10.11 大语言模型研究简报:OpenAI 公布 AI 数学研究新进展
人工智能·语言模型·openai
木土雨成小小测试员14 小时前
WorkBuddy的安装使用教程详解
ai
Dawson Zhu15 小时前
《Agentic Design Patterns》第 13 章导读:人机协同(Human-in-the-Loop)
人工智能·语言模型·架构·aigc·agi
Martina_032117 小时前
AI生成的海岸城市一移动镜头,水面反射就跳?用6步统一反射探针、SSR与平面反射
图像处理·人工智能·平面·3d·ai·自然语言处理·游戏策划
tianyatest17 小时前
给 AI Agent 写了个 Skill:扫描版电子书自动解压合并 + 加书签(附 PDG 格式逆向)
ai·pdf·电子书
云和数据.ChenGuang17 小时前
Codex 下载与本地部署实战:从零搭建你的 AI 编程助手
人工智能·深度学习·机器学习·语言模型·fastapi