配置阿里云千问大模型--环境变量dashscope

1 开通百炼

首先要进入到阿里云平台,然后进入百炼平台。

2 获取API-KEY

进入之后再右上角可以查看到自己的API-KEY,这个东西就是需要配置在环境变量里的。

点击查看就可以获取

3 配置DASHSCOPE环境变量

如果使用dashscope来进行千问大模型的API对接,则需要配置环境变量,以windows为例,就是创建如下的环境变量,把上文得到的KEY粘贴进去就行了。

4 测试程序

然后我们可以编写一个测试程序,注意需要安装dashscope先,可以看到程序里是不需要设置api-key的。当然也可以不配置环境变量,把api-key设置在程序里,但是这种方法不太安全,不建议这么做。

python 复制代码
# Refer to the document for workspace information: https://help.aliyun.com/document_detail/2746874.html

# 测试千问接口
# 可以测试下环境有没有配置好

from http import HTTPStatus
import dashscope

def call_with_stream():
    messages = [
        {'role': 'user', 'content': '苏州天气如何'}]
    responses = dashscope.Generation.call("qwen-turbo",
                                          messages=messages,
                                          result_format='message',  # set the result to be "message"  format.
                                          stream=True,  # set streaming output
                                          incremental_output=True  # get streaming output incrementally
                                          )
    for response in responses:
        if response.status_code == HTTPStatus.OK:
            print(response.output.choices[0]['message']['content'], end='\n')
        else:
            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                response.request_id, response.status_code,
                response.code, response.message
            ))


if __name__ == '__main__':
    call_with_stream()
相关推荐
会Tk矩阵群控的小木3 分钟前
基于Python的iMessage短信群发与社媒多账号统一管理系统实现
开发语言·windows·python·新媒体运营·开源软件·个人开发
小七-七牛开发者24 分钟前
AI Agent 的 4 个工程关键词:Prompt、Context、Loop、Harness 到底是什么?
ai·大模型·agent·token·context·loop·codex·harness
质造者29 分钟前
LangChain + Ollama + Tavily 实现旅游问答系统
linux·人工智能·python·langchain·rag
伊布拉西莫42 分钟前
【流畅的Python】第20章:并发执行器 — 学习笔记
笔记·python·学习
IT策士1 小时前
Redis 从入门到精通:Python 操作 Redis
redis·python·bootstrap
编码者卢布1 小时前
【Azure AI Search】 searchMode=any 和 searchMode=all 有什么区别?
人工智能·python·flask
Samooyou1 小时前
大模型微调(Fine Tuning)
人工智能·python·ai·语言模型
qq_8573058191 小时前
python语法
开发语言·python·算法
AI行业学习1 小时前
CC-Switch v3.16.1 官方下载 | 安装配置详细教程【2026.6.10】
java·开发语言·vue.js·python·mysql·eclipse·html
早起CaiCai2 小时前
【Pytorch 实践1】手写数字
人工智能·pytorch·python