世界唯一识别图片免费的大模型api被我发现了!!!代码给你了!!是每天2000次!每天!每天!每天!
评论
收藏

世界唯一识别图片免费的大模型api被我发现了!!!代码给你了!!是每天2000次!每天!每天!每天!

经验分享
再见史蒂夫周
2025-11-11 09:59·浏览量:830
再见史蒂夫周
发布于 2025-11-11 09:55更新于 2025-11-11 09:59830浏览

先注册和绑定阿里账号

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

 


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

选择文本生成

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

参数4:问题(给图片取8-12字的标题,描述下图片内容)

参数5url: https://api-inference.modelscope.cn/v1



import base64
from openai import OpenAI
from PIL import Image
import io

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


def call_vision_api_with_image(image_file, api_key, base_url, model_name, system_prompt, user_question):
  
  
    if not image_file:
        raise ValueError("请提供有效的图片文件")
    if not api_key:
        raise ValueError("请提供有效的API密钥")
    if not base_url:
        raise ValueError("请提供有效的API基础URL")
    if not model_name:
        raise ValueError("请提供有效的模型名称")
    if not user_question:
        raise ValueError("请提供用户问题")
    
    def _encode_image_to_base64(image_file):
        """
        将图片文件转换为base64编码的data URL
        """
        try:
            with Image.open(image_file) as img:
               
                if img.mode != 'RGB':
                    img = img.convert('RGB')
                
              
                buffer = io.BytesIO()
                img.save(buffer, format='JPEG')
                
              
                image_data = base64.b64encode(buffer.getvalue()).decode('utf-8')
                return f"data:image/jpeg;base64,{image_data}"
        except Exception as e:
            raise ValueError(f"图片处理失败: {str(e)}")
    
    def _call_api_with_streaming(client, model_name, messages):
        
        try:
            response = client.chat.completions.create(
                model=model_name,
                messages=messages,
                stream=True
            )
            
           
            full_response = ""
            for chunk in response:
                if chunk.choices[0].delta.content:
                    full_response += chunk.choices[0].delta.content
            
            return full_response
        except Exception as e:
            raise RuntimeError(f"API调用失败: {str(e)}")
    
   
    image_url = _encode_image_to_base64(image_file)
    
   
    try:
        client = OpenAI(
            api_key=api_key,
            base_url=base_url
        )
    except Exception as e:
        raise RuntimeError(f"客户端初始化失败: {str(e)}")
    
  
    messages = [
        {
            "role": "system",
            "content": [
                {"type": "text", "text": system_prompt or "You are a helpful and harmless assistant."}
            ]
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {"url": image_url}
                },
                {
                    "type": "text",
                    "text": user_question
                }
            ]
        }
    ]
    
    
    response_text = _call_api_with_streaming(client, model_name, messages)
    
    if not response_text:
        raise RuntimeError("API返回空响应")
    
    return response_text


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