阿里云百炼免费大模型Python SDK

 

一、账号设置

  1. 注册账号:如果没有阿里云账号,您需要先注册阿里云账号。

    说明

    使用RAM账号时,请由主账号所有者将其添加至主账号空间(授予访客/管理员角色)。详情请参见用户管理

  2. 开通百炼:使用阿里云主账号前往百炼控制台,如果页面顶部显示以下消息,您需要开通百炼的模型服务,以获得免费额度。如果未显示该消息,则表示您已经开通。

    image

    说明

    如果开通服务时提示“您尚未进行实名认证”,请先进行实名认证

  3. 获取API Key:前往API-KEY页面,单击创建我的API-KEY然后在已创建的API Key操作列,单击查看,获取API KEY,用于通过API调用大模型。

    说明

    创建新的API Key时,归属业务空间推荐选择主账号空间。使用子空间API Key需由主账号管理员为对应子空间开通模型授权(如本文使用通义千问-Plus模型),详情请参见授权子业务空间模型调用和部署

 

二、Python SDK

 

import requests
import json
import time,datetime
import os
os.system('') #转义不生效时使用

class ALIAI:

    _api_root = "https://dashscope.aliyuncs.com/compatible-mode/v1/"
    
    def __init__(self, api_key):
        self.API_KEY = api_key
    
    def chat_completions(self, model, prompt):
        data = {
            "model": model,
            "messages": [{"role": "user", "content": prompt}]
        }

        url = f"{self._api_root}chat/completions"
        response = self.http_request_v2(url, method="POST", headers=self.headers(), params=json.dumps(data))
        return response

    # 生成headers头
    def headers(self, params=None):
        headers = {}
        headers['Content-Type'] = 'application/json'
        headers['Authorization'] = f"Bearer {self.API_KEY}"
        return headers
        
    def http_request_v2(self, url, method="GET", headers={}, params=None, max_retries=2, timeout=3):
        headers = headers or {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'}
        for attempts in range(1, max_retries + 1):
            try:
                if method == "GET":
                    response = requests.get(url, headers=headers, timeout=timeout)
                elif method in ("POST", "DELETE"):
                    response = requests.post(url, data=params, headers=headers, timeout=timeout)
                    # response = requests.post(url, json= params, headers=headers, timeout=maxseconds)
                # response.raise_for_status()
                # print(response.text)
                return response.json()

            except Exception as e:
                print([datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), attempts, url, method, params, repr(e)])
                time.sleep(timeout * attempts)


if __name__ == '__main__':
    # 使用示例
    api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
    client = ALIAI(api_key)
 
    prompt = "1+1=?"
    
    models = [
    
        # "qwq-plus", #only support stream mode
        # "qwq-32b",  #only support stream mode
        
        "qwen2.5-1.5b-instruct",
        "qwen2.5-0.5b-instruct",
        
        "qwen2-1.5b-instruct",
        "qwen2-0.5b-instruct",
        
        "qwen1.5-1.8b-chat",
        "qwen1.5-0.5b-chat",
        "qwen-1.8b-chat",

        "qwen2.5-math-1.5b-instruct",
        "qwen2-math-1.5b-instruct",
        
        "qwen2.5-coder-1.5b-instruct",
        "qwen2.5-coder-0.5b-instruct",
        "qwen2.5-coder-3b-instruct",
        
        "deepseek-r1-distill-qwen-1.5b",

        # "deepseek-r1-distill-llama-8b",  #实际 无法使用
        # "deepseek-r1-distill-llama-70b",  #实际 无法使用

        "llama3.3-70b-instruct",

        "qwen2-vl-7b-instruct",
        "qwen2-vl-2b-instruct",
        ]
    for model in models:    # 遍历models列表
        try:
            print()
            print(f" - Model: {model}")

            response = client.chat_completions(model, prompt)
            print(response)

            if 'error' in response:
                # message = result['error']['message']
                message = response['error']
                print(f"\033[91m错误\033[0m, MSG: {message}")
            else:
                message = response["choices"][0]["message"]["content"].strip()
                print(f"\033[92m正常\033[0m", message.strip())
                # print(f"SUCCESS {model}")
                # print(result["choices"][0]["message"]["content"].strip())
        except Exception as e:
            print(f"\033[35m异常\033[0m, MSG: {e}")
            # print(f"except", e)
            # print {"error": str(e)}

 

 

 

posted @ 2025-03-11 15:50  方倍工作室  阅读(311)  评论(0)    收藏  举报