From 29d1e331698a97dd86bb2c7170af1efd1e7fc370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BA=E6=B5=B7=E5=9B=BD?= <404580946@qq.com> Date: Mon, 13 Apr 2026 17:37:31 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E4=BF=AE=E6=94=B9api=20url=EF=BC=8C?= =?UTF-8?q?key=20model=E4=B8=BA=E5=8F=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 20 +++++++++++++++----- env.example | 4 +++- pyproject.toml | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 9cb0635..61a2fed 100644 --- a/app.py +++ b/app.py @@ -25,8 +25,8 @@ app = FastAPI( # OpenRouter API 配置 OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY") -OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions" -MODEL_NAME = os.getenv("OPENROUTER_MODEL", "qwen/qwen3-vl-8b-instruct") # 可通过环境变量配置 +OPENROUTER_API_URL = os.getenv("OPENROUTER_API_URL") +MODEL_NAME = os.getenv("OPENROUTER_MODEL") # 可通过环境变量配置 if not OPENROUTER_API_KEY: raise ValueError("请设置 OPENROUTER_API_KEY 环境变量") @@ -52,6 +52,10 @@ def encode_image_to_base64(image: Image.Image) -> str: async def call_openrouter_api(image_base64: str, user_message: str) -> str: """调用 OpenRouter API""" + print() + print(f"调用 OpenRouter API: {MODEL_NAME}") + print(f"用户消息: {user_message} {OPENROUTER_API_KEY}") + headers = { "Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json", @@ -64,7 +68,7 @@ async def call_openrouter_api(image_base64: str, user_message: str) -> str: "messages": [ { "role": "system", - "content": "你是一位专业的农业病虫害识别专家。请仔细分析用户提供的植物图片,识别可能存在的病虫害问题,并提供详细的诊断信息,包括:1. 病虫害类型和名称 2. 严重程度 3. 可能的病因 4. 防治建议。请用中文回答。" + "content": "你是一位专业的农业病虫害识别专家。请仔细分析用户提供的植物图片,识别可能存在的病虫害问题,并提供详细的诊断信息,包括:1. 植物类型,病虫害类型和名称 2. 严重程度 3. 可能的病因 4. 防治建议。请用中文回答。" }, { "role": "user", @@ -84,13 +88,19 @@ async def call_openrouter_api(image_base64: str, user_message: str) -> str: ], "max_tokens": 2000, "temperature": 0.7, + "chat_template_kwargs": { + "enable_thinking": False + } } + + async with httpx.AsyncClient(timeout=60.0) as client: try: response = await client.post(OPENROUTER_API_URL, json=payload, headers=headers) response.raise_for_status() result = response.json() + print(result) if "choices" in result and len(result["choices"]) > 0: return result["choices"][0]["message"]["content"] @@ -213,8 +223,8 @@ async def identify_pest_disease_base64(request: Base64ImageRequest): if not base64_str: raise ValueError("Base64 字符串为空") image_data = base64.b64decode(base64_str, validate=True) - except binascii.Error as decode_error: - raise ValueError(f"Base64 解码失败: 无效的 base64 格式") + except binascii.Error: + raise ValueError("Base64 解码失败: 无效的 base64 格式") except Exception as decode_error: error_msg = str(decode_error) try: diff --git a/env.example b/env.example index 4705b30..de82c95 100644 --- a/env.example +++ b/env.example @@ -1,7 +1,9 @@ +# OPEN API URL +OPENROUTER_API_URL=https://example.com/v1/chat/completions # OpenRouter API 密钥 # 从 https://openrouter.ai/keys 获取 OPENROUTER_API_KEY=your_openrouter_api_key_here # OpenRouter 模型名称(可选,默认为 qwen/qwen3-vl-8b-instruct) -# OPENROUTER_MODEL=qwen/qwen3-vl-8b-instruct +OPENROUTER_MODEL=qwen/qwen3-vl-8b-instruct diff --git a/pyproject.toml b/pyproject.toml index 02491d1..b358984 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "hm-qwen3-vl" version = "0.1.0" -description = "基于 OpenRouter Qwen3 VL 8B 的病虫害识别 FastAPI 服务" +description = "基于 Qwen3.5 VL 的病虫害识别 FastAPI 服务" readme = "README.md" requires-python = ">=3.8" dependencies = [