vue 实现简单AI聊天程序(二): python 对接通义千问API

申请账号流程,参考:https://blog.csdn.net/u012917925/article/details/140794192

登录阿里云,然后找到阿里云百炼,创建一个API KEY

配置环境变量:https://help.aliyun.com/zh/model-studio/developer-reference/configure-api-key-through-environment-variables?spm=a2c4g.11186623.0.0.4e6b2117oJdNbD#bbd6387ea6on0

pip install dashscope

需要配置一个系统环境变量,并且把阿里云百炼里面申请下来的API-KEY配置进去:

export DASHSCOPE_API_KEY="sk-*****1"    //macos的配置方法,windows直接在环境变量里加就行了

先要配置好环境变量,(如果不行,把IDE重启下,它才能重新读取环境变量)然后再测试如下代码的调用:

# 测试千问接口

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()

这边我们使用turbo模型,便宜一点,实测在控制台的效果是这样的:
在这里插入图片描述

下一章我们来对接前端,实现前后端分离的聊天程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦麦大数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值