欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > vue 实现简单AI聊天程序(二): python 对接通义千问API

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

2024/10/25 19:20:25 来源:https://blog.csdn.net/roccreed/article/details/141568997  浏览:    关键词: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 dashscopedef 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 outputincremental_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模型,便宜一点,实测在控制台的效果是这样的:
在这里插入图片描述

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com