配置阿里云千问大模型--环境变量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()
相关推荐
维爱智VR认知学能9 小时前
VR抗干扰训练调度器:视听干扰等级分层注入与安全上限熔断实现
python·软件工程·架构设计
prince_zxill10 小时前
百度 Unlimited-SVG:1024 个 token 看懂整张图,0.5B 参数把它写成 SVG 代码
python·百度·svg·多模态·图像转矢量
zc.z12 小时前
Context Engineering:深度学习知识点
python·prompt·上下文工程
5G微创业12 小时前
短视频去水印接口怎么对接?一篇讲清 GET 参数、返回字段与多语言示例
python·小程序·api·短视频·去水印
布语world12 小时前
某东pc滑块分析
前端·javascript·网络·python·安全
m0_6464299712 小时前
Python pytest 单元测试基础
python
DeepVisionary13 小时前
从1秒多压到0.39秒,Gemini 4 Live靠先应声再干活,把语音延迟降了3倍
python·自动化
Web3&Basketball13 小时前
Playwright实战:27B网页智能体评测3步
python·大模型·浏览器自动化·playwright·网页智能体·评测口径·molmoweb
zc.z13 小时前
RAG (一):从底层原理、Naive RAG 到企业级项目开发与面试实战
人工智能·python·rag
吴名氏.13 小时前
全栈跃迁:Java 后端 27 讲【7】MyBatisPlus进阶与慢SQL治理
java·开发语言·数据库·sql·大模型·sql调优