配置阿里云千问大模型--环境变量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()
相关推荐
2601_966949652 分钟前
五档盘口数据对量化交易有什么价值?从信号判断到策略风险的完整分析
开发语言·人工智能·python·数据分析·量化·股票数据·quantdash
cxhello13 分钟前
消费者活着、心跳正常、日志干净,但它七天没拉过一条消息
python·kafka
砚底藏山河16 分钟前
【量化纯GET实战 #11】移动均线 MA:5 日 20 日 60 日怎么算怎么用
java·python·金融·maven
小吴学不废Java19 分钟前
Python 基础语法
开发语言·python
weixin_4334176721 分钟前
阿里视频大模型wan2.7‑t2v,可以生成视频
人工智能·python·音视频
sukioe34 分钟前
一张图、两把锁:AI 物流履约平台的确定性边界设计
人工智能·python·ai·langchain
2601_962203511 小时前
【SpringBoot3】面向切面 AspectJ AOP 使用详解
开发语言·前端·python
程序员大雄学编程1 小时前
微积分43. 无穷积分入门:从概念到Python实战可视化
开发语言·python·学习·微积分
测试老哥1 小时前
Postman获取token并设置token依赖步骤
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
郝学胜-神的一滴1 小时前
C++11 工程级应用 06:自己造一个类似Python的Range迭代器
开发语言·c++·windows·python·程序人生·microsoft