Python对接阿里AI大模型-通义千问

通义千问是阿里云研发的超大规模语言模型,能够进行多轮对话、文案创作、逻辑推理、多模态理解,并支持多种语言。该模型在2023年4月开始对外测试,并随后通过备案正式向公众开放。通义千问APP已上线各大手机应用市场,用户可以通过APP与模型互动,体验其在创意文案生成、办公助理、学习辅导、日常生活助手等多个场景下的强大功能。作为一款AI智能助手,通义千问可以理解并生成人类语言,帮助用户解决各种问题、提供信息和完成特定任务。

申请API-KEY

前往阿里云模型服务灵积控制台模型服务灵积-总览 (aliyun.com)

https://dashscope.console.aliyun.com/overview

点击去开通开启服务:

Python对接阿里AI大模型-通义千问

然后前往模型服务灵积-API-KEY管理 (aliyun.com)申请API-KEY

https://dashscope.console.aliyun.com/apiKey
Python对接阿里AI大模型-通义千问

代码接入

安装SDK

pip install dashscope

编写代码

import dashscope
from http import HTTPStatus

dashscope.api_key="YOUR_DASHSCOPE_API_KEY"

def call_with_messages():
    messages = [{'role''system''content''You are a helpful assistant.'},
                {'role''user''content''如何做炒西红柿鸡蛋?'}]

    response = dashscope.Generation.call(
        dashscope.Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # set the result to be "message" format.
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
    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_messages()

多轮上下文对话

from http import HTTPStatus
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role

dashscope.api_key="YOUR_DASHSCOPE_API_KEY"

def conversation_with_messages():
    messages = [{'role': Role.SYSTEM, 'content''You are a helpful assistant.'},
                {'role': Role.USER, 'content''如何做西红柿炖牛腩?'}]
    response = Generation.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # set the result to be "message" format.
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
        # append result to messages.
        messages.append({'role': response.output.choices[0]['message']['role'],
                         'content': response.output.choices[0]['message']['content']})
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))
    messages.append({'role': Role.USER, 'content''不放糖可以吗?'})
    # make second round call
    response = Generation.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # set the result to be "message" format.
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
    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__':
    conversation_with_messages()

欢迎大家关注我的公众号,将会为大家推荐更优质的内容!

原文始发于微信公众号(青檬小栈):Python对接阿里AI大模型-通义千问

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/213183.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!