配置阿里云千问大模型--环境变量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()
相关推荐
lunzi_fly6 分钟前
【学习笔记】《Python编程 从入门到实践》第6章:字典创建、遍历与嵌套用法详解
python·python 小白学习
柒和远方1 小时前
LeetCode 452. 用最少数量的箭引爆气球 —— 区间贪心经典:排序 + 扫描一箭穿心
javascript·python·算法
winfredzhang2 小时前
用 Python + wxPython 做一个个人健康饮食管理工具:从记录三餐到综合生活建议
python·wxpython·deepseek·生活习惯管理
Irissgwe2 小时前
十、LangGraph能力详解:工作流的常见模式
python·langchain·ai编程·工作流·langgraph
Merlyn103 小时前
【栈】155. 最小栈
python·算法
SilentSamsara3 小时前
NumPy 进阶:广播机制、ufunc 与向量化计算的工程实践
开发语言·python·青少年编程·性能优化·numpy
林爷万福3 小时前
机器学习在光谱分析中的应用:Python实现
人工智能·python·机器学习
编程探索者小陈3 小时前
接口自动化三件套:JSON Schema 校验 + logging 日志 + Allure 测试报告
开发语言·python
godspeed_lucip3 小时前
LLM和Agent——专题6:Multi Agent 入门(3)
人工智能·python
如此这般英俊3 小时前
手搓Claude Code-第二章 tool_use
人工智能·python·ai·语言模型