配置阿里云千问大模型--环境变量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()
相关推荐
FL171713142 小时前
Pytorch保存pt和pkl
人工智能·pytorch·python
爱学习的小道长4 小时前
进程、线程、协程三者的区别和联系
python·ubuntu
L-李俊漩4 小时前
MMN-MnnLlmChat 启动顺序解析
开发语言·python·mnn
大雷神5 小时前
HarmonyOS 横竖屏切换与响应式布局实战指南
python·深度学习·harmonyos
钅日 勿 XiName5 小时前
一小时速通pytorch之训练分类器(四)(完结)
人工智能·pytorch·python
青瓷程序设计5 小时前
水果识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
*才华有限公司*6 小时前
基于BERT的文本分类模型训练全流程:从环境搭建到显存优化实战
python
Lxinccode7 小时前
python(59) : 多线程调用大模型ocr提取图片文本
开发语言·python·图片提取文字·批量提取文件·多线程ocr
梁辰兴7 小时前
PyCharm使用了Conda的虚拟环境创建的的Python项目,下载库(包)到该项目的虚拟环境中
python·pycharm·conda·错误·异常·异常报错
自由日记7 小时前
python简单线性回归
开发语言·python·线性回归