【大模型】通义千问API接口本地调用

本文主要讲解了如何应用阿里云的"通义千问"进行本地API接口调用。

1. 账号设置

在这里插入图片描述

2. 安装依赖

pip install -U openai

3. 调用样例

import os
from openai import OpenAI

try:
    client = OpenAI(
        # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",        
        # api_key=os.getenv("DASHSCOPE_API_KEY"),    # 通过配置环境变量设置api_key
        api_key="sk-xxxxxxxxxxxxxxxxxxx",   # 直接使用api_key      
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",    
        )

    completion = client.chat.completions.create(
        model="qwen-plus",  # 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models        
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},            
            {'role': 'user', 'content': '你是谁?'}
            ]
    )
    print(completion.choices[0].message.content)
except Exception as e:
    print(f"错误信息:{e}")
    print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")

4. 免费额度

新注册的账号都有一定的免费额度,免费额度查询方式:https://help.aliyun.com/zh/model-studio/new-free-quota?spm=a2c4g.11186623.help-menu-2400256.d_4_1.40d02562KHeyL7

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值