免费千问大模型每天2000次请求,老哥求你了,用下吧
评论
收藏

免费千问大模型每天2000次请求,老哥求你了,用下吧

经验分享
再见史蒂夫周
2025-11-11 09:31·浏览量:510
再见史蒂夫周
发布于 2025-11-11 09:31510浏览

先注册和绑定阿里账号

参数1:apikey https://modelscope.cn/my/myaccesstoken


参数2:模型库 https://www.modelscope.cn/models 推荐模型 Qwen/Qwen2.5-Coder-32B-Instruct(可直接输入)

选择文本生成

参数3:系统人设(可不输入)

参数4:问题



from openai import OpenAI

from typing import *
try:
    from xbot.app.logging import trace as print
except:
    from xbot import print


def call_modelscope_api(api_key, model_id, question, system_content):

    
    if not api_key:
        raise ValueError("API密钥不能为空")
    if not model_id:
        raise ValueError("模型ID不能为空")
    if not question:
        raise ValueError("问题内容不能为空")
    
    def _create_client_and_request(api_key, model_id, question, system_content):
      
        try:
            client = OpenAI(
                api_key=api_key,
                base_url="https://api-inference.modelscope.cn/v1/"
            )
            

            messages = []
            if system_content:
                messages.append({
                    'role': 'system',
                    'content': system_content
                })
            messages.append({
                'role': 'user',
                'content': question
            })
            
            response = client.chat.completions.create(
                model=model_id,
                messages=messages,
                stream=True
            )
            
         
            answer = ""
            for chunk in response:
                if chunk.choices[0].delta.content:
                    answer += chunk.choices[0].delta.content
            
            return answer
            
        except Exception as e:
            raise Exception(f"调用ModelScope API失败: {str(e)}")
    

    if not system_content:
        system_content = "You are a helpful assistant."
    
    answer = _create_client_and_request(api_key, model_id, question, system_content)
    
    return answer


收藏9
全部评论1
最新
发布评论
评论