[ Python使用通义千问 ]


在python中使用通义千问-阿里云

官方api

api-key管理页面

注册

pip install dashscope

配置环境变量

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

运行

import random
from http import HTTPStatus
import dashscope


dashscope.api_key = "sk-4607ca0bb63d4646b8aeaca909091a9f"

def call_with_messages():
    messages = [{"role": "user", "content": "用萝卜、土豆、茄子做饭,给我个菜谱"}]
    response = dashscope.Generation.call(
        "qwen1.5-72b-chat",
        messages=messages,
        # set the random seed, optional, default to 1234 if not set
        seed=random.randint(1, 10000),
        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()