之前分享了,使用程序调用文心一言。但是很快文心一言就要收费了。阿里的提供了暂时免费版的基础模型,效果还算可以。所以再分享一下,如何使用程序来调用通义千问的模型。



整体很简单,分三步:导入依赖;获取ApiKey;使用程序调用模型。



体感:使用简单,效果还可以,但是速度很慢!






如何使用程序调用通义千问_java



安装

支持两种方式,python和java

python 导入依赖

pip install dashscope

java安装:安装DashScope SDK_模型服务灵积(DashScope)-阿里云帮助中心

获取API-KEY

如何开通DashScope并创建API-KEY_模型服务灵积(DashScope)-阿里云帮助中心

代码测试

python代码

import dashscope
import time
from http import HTTPStatus
# 替换api key
dashscope.api_key="sk-f7a06881b8814bdebd19252d47ccc6f1"

def sample_sync_call():
    start_time = time.time()  # 记录开始时间
    prompt_text = '用土豆可以做什么饭'
    resp = dashscope.Generation.call(
        model='qwen-max',
        prompt=prompt_text
    )
    end_time = time.time()  # 记录结束时间
    # The response status_code is HTTPStatus.OK indicate success,
    # otherwise indicate request is failed, you can get error code
    # and message from code and message.
    if resp.status_code == HTTPStatus.OK:
        print(resp.output)  # The output text
        print(resp.usage)  # The usage information
    else:
        print(resp.code)  # The error code.
        print(resp.message)  # The error message.

      # 计算并打印调用时间
    print("调用时间:", end_time - start_time, "秒")


sample_sync_call()