当前位置: 首页 > article >正文

python调用硅基流动的视觉语言模型

参考: https://docs.siliconflow.cn/cn/userguide/capabilities/vision   

import base64
import json
from openai import OpenAI
from PIL import Image
import io# 初始化OpenAI客户端
client = OpenAI(api_key="sk-**********",  # 替换为实际API密钥base_url="https://api.siliconflow.cn/v1"
)def convert_image_to_webp_base64(input_image_path: str) -> str:"""将本地图片转换为WebP格式的Base64字符串"""try:with Image.open(input_image_path) as img:# 转换为WebP格式(优化大小)byte_arr = io.BytesIO()img.save(byte_arr, format='WEBP', quality=85)  # 调整质量平衡大小和清晰度byte_arr = byte_arr.getvalue()return base64.b64encode(byte_arr).decode('utf-8')except Exception as e:print(f"图片转换错误: {e}")return None# 1. 转换本地图片
input_image_path = "7125e2e3.jpeg"  # 替换为实际图片路径
base64_image = convert_image_to_webp_base64(input_image_path)if not base64_image:print("图片转换失败,请检查路径和格式")exit()# 2. 创建流式请求
response = client.chat.completions.create(model="Qwen/Qwen2.5-VL-72B-Instruct",messages=[{"role": "user","content": [{"type": "image_url","image_url": {"url": f"data:image/webp;base64,{base64_image}",  # 指定WebP格式"detail": "high"  # 平衡速度与精度}},{"type": "text","text": "使用ocr识别图片内容并输出"  # 替换为你的提示词}]}],stream=True,max_tokens=1000  # 控制响应长度
)# 3. 流式处理响应
print("模型响应:")
full_response = ""
for chunk in response:if chunk.choices[0].delta.content:text_chunk = chunk.choices[0].delta.contentprint(text_chunk, end='', flush=True)full_response += text_chunkprint("\n\n完整响应已接收")

http://www.lryc.cn/news/2398062.html

相关文章:

  • 下载并运行自制RAG框架
  • Rust 学习笔记:Cargo 工作区
  • 颈部的 “异常坚持”
  • Ubuntu22.04安装MinkowskiEngine
  • 【计算机网络】第2章:应用层—应用层协议原理
  • 【Zephyr 系列 6】使用 Zephyr + BLE 打造蓝牙广播与连接系统(STEVAL-IDB011V1 实战)
  • 利用 Scrapy 构建高效网页爬虫:框架解析与实战流程
  • RPG20.创建敌人的初始能力和加载武器
  • P5684 [CSP-J2019 江西] 非回文串 题解
  • 自适应移动平均(Adaptive Moving Average, AMA)
  • Java密码加密存储算法,SpringBoot 实现密码安全存储
  • 使用 Version Catalogs统一配置版本 (Gradle 7.0+ 特性)
  • 涨薪技术|0到1学会性能测试第95课-全链路脚本开发实例
  • C++文件和流基础
  • Spring AI Alibaba + Nacos 动态 MCP Server 代理方案
  • MCP:让AI工具协作变得像聊天一样简单 [特殊字符]
  • C++ Learning string类模拟实现
  • Message=“HalconDotNet.HHandleBase”的类型初始值设定项引发异常
  • AI炼丹日志-27 - Anubis 通过 PoW工作量证明的反爬虫组件 上手指南 原理解析
  • 阿姆达尔定律的演进:古斯塔夫森定律
  • JavaScript极致性能优化全攻略
  • 批量大数据并发处理中的内存安全与高效调度设计(以Qt为例)
  • Transformer核心原理
  • Grafana-State timeline状态时间线
  • 解决CSDN等网站访问不了的问题
  • 【华为云Astro Zero】组装设备管理页面开发(图形拖拽 + 脚本绑定)
  • PopupImageMenuItem 无响应
  • C++ Vector算法精讲与底层探秘:从经典例题到性能优化全解析
  • Flowith,有一种Agent叫无限
  • 系统思考:短期利益与长期系统影响