mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-23 07:33:53 +08:00
update:智控台,完成mcp拍照识图
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import json
|
||||
import copy
|
||||
from aiohttp import web
|
||||
from config.logger import setup_logging
|
||||
from core.utils.util import get_vision_url, is_valid_image_file
|
||||
from core.utils.vllm import create_instance
|
||||
from config.config_loader import get_private_config_from_api
|
||||
from core.utils.auth import AuthToken
|
||||
import base64
|
||||
from typing import Tuple, Optional
|
||||
|
||||
TAG = __name__
|
||||
|
||||
@@ -15,20 +19,45 @@ class VisionHandler:
|
||||
def __init__(self, config: dict):
|
||||
self.config = config
|
||||
self.logger = setup_logging()
|
||||
# 初始化认证工具
|
||||
self.auth = AuthToken(config["server"]["auth_key"])
|
||||
|
||||
def _create_error_response(self, message: str) -> dict:
|
||||
"""创建统一的错误响应格式"""
|
||||
return {"success": False, "message": message}
|
||||
|
||||
def _verify_auth_token(self, request) -> Tuple[bool, Optional[str]]:
|
||||
"""验证认证token"""
|
||||
auth_header = request.headers.get("Authorization", "")
|
||||
if not auth_header.startswith("Bearer "):
|
||||
return False, None
|
||||
|
||||
token = auth_header[7:] # 移除"Bearer "前缀
|
||||
return self.auth.verify_token(token)
|
||||
|
||||
async def handle_post(self, request):
|
||||
"""处理 MCP Vision POST 请求"""
|
||||
try:
|
||||
# 验证token
|
||||
is_valid, token_device_id = self._verify_auth_token(request)
|
||||
if not is_valid:
|
||||
return web.Response(
|
||||
text=json.dumps(
|
||||
self._create_error_response("无效的认证token或token已过期")
|
||||
),
|
||||
content_type="application/json",
|
||||
status=401,
|
||||
)
|
||||
|
||||
# 获取请求头信息
|
||||
device_id = request.headers.get("Device-Id", "")
|
||||
client_id = request.headers.get("Client-Id", "")
|
||||
self.logger.bind(tag=TAG).debug(f"Device-Id: {device_id}")
|
||||
self.logger.bind(tag=TAG).debug(f"Client-Id: {client_id}")
|
||||
|
||||
if device_id != token_device_id:
|
||||
return web.Response(
|
||||
text=json.dumps(self._create_error_response("设备ID与token不匹配")),
|
||||
content_type="application/json",
|
||||
status=401,
|
||||
)
|
||||
# 解析multipart/form-data请求
|
||||
reader = await request.multipart()
|
||||
|
||||
@@ -64,9 +93,34 @@ class VisionHandler:
|
||||
# 将图片转换为base64编码
|
||||
image_base64 = base64.b64encode(image_data).decode("utf-8")
|
||||
|
||||
vllm_config = self.config["VLLM"]["ChatGLMVLLM"]
|
||||
provider = create_instance("openai", vllm_config)
|
||||
response = provider.response(question, image_base64)
|
||||
# 如果开启了智控台,则从智控台获取模型配置
|
||||
current_config = copy.deepcopy(self.config)
|
||||
read_config_from_api = current_config.get("read_config_from_api", False)
|
||||
if read_config_from_api:
|
||||
current_config = get_private_config_from_api(
|
||||
current_config,
|
||||
device_id,
|
||||
client_id,
|
||||
)
|
||||
|
||||
select_vllm_module = current_config["selected_module"].get("VLLM")
|
||||
if not select_vllm_module:
|
||||
raise ValueError("您还未设置默认的视觉分析模块")
|
||||
|
||||
vllm_type = (
|
||||
select_vllm_module
|
||||
if "type" not in current_config["VLLM"][select_vllm_module]
|
||||
else current_config["VLLM"][select_vllm_module]["type"]
|
||||
)
|
||||
|
||||
if not vllm_type:
|
||||
raise ValueError(f"无法找到VLLM模块对应的供应器{vllm_type}")
|
||||
|
||||
vllm = create_instance(
|
||||
vllm_type, current_config["VLLM"][select_vllm_module]
|
||||
)
|
||||
|
||||
response = vllm.response(question, image_base64)
|
||||
|
||||
return_json = {
|
||||
"success": True,
|
||||
@@ -99,7 +153,13 @@ class VisionHandler:
|
||||
"""处理 MCP Vision GET 请求"""
|
||||
try:
|
||||
vision_explain = get_vision_url(self.config)
|
||||
message = f"MCP Vision 接口运行正常,视觉解释接口地址是:{vision_explain}"
|
||||
if vision_explain and len(vision_explain) > 0 and "null" != vision_explain:
|
||||
message = (
|
||||
f"MCP Vision 接口运行正常,视觉解释接口地址是:{vision_explain}"
|
||||
)
|
||||
else:
|
||||
message = "MCP Vision 接口运行不正常,请打开data目录下的.config.yaml文件,找到【server.vision_explain】,设置好地址"
|
||||
|
||||
response = web.Response(text=message, content_type="text/plain")
|
||||
except Exception as e:
|
||||
self.logger.bind(tag=TAG).error(f"MCP Vision GET请求异常: {e}")
|
||||
|
||||
Reference in New Issue
Block a user