增加多模态模型识别功能。
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import base64
|
||||
import os
|
||||
import time
|
||||
from typing import Any, Dict, List
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://192.168.1.250:11434")
|
||||
DEFAULT_MODEL = os.getenv("VLM_MODEL", "qwen2.5vl:32b")
|
||||
|
||||
|
||||
def _img_to_data_uri(path: str) -> str:
|
||||
with open(path, "rb") as f:
|
||||
b64 = base64.b64encode(f.read()).decode("utf-8")
|
||||
return f"data:image/jpeg;base64,{b64}"
|
||||
|
||||
|
||||
def chat_vision(
|
||||
prompt: str,
|
||||
image_paths: List[str],
|
||||
*,
|
||||
temperature: float = 0.1,
|
||||
options: Dict[str, Any] | None = None,
|
||||
model: str = DEFAULT_MODEL,
|
||||
timeout_sec: int = 120,
|
||||
) -> str:
|
||||
# Logging: request summary before heavy work
|
||||
try:
|
||||
img_sizes = []
|
||||
for p in image_paths:
|
||||
try:
|
||||
img_sizes.append(os.path.getsize(p))
|
||||
except Exception:
|
||||
img_sizes.append(-1)
|
||||
print(
|
||||
f"[Ollama] Prepare chat url={OLLAMA_URL}/api/chat model={model} temp={temperature} "
|
||||
f"images={len(image_paths)} sizes={img_sizes} timeout={timeout_sec}s"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
images = [_img_to_data_uri(p) for p in image_paths]
|
||||
payload: Dict[str, Any] = {
|
||||
"model": model,
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": prompt,
|
||||
"images": images,
|
||||
}
|
||||
],
|
||||
"options": {"temperature": temperature, **(options or {})},
|
||||
"stream": False,
|
||||
}
|
||||
|
||||
t0 = time.time()
|
||||
try:
|
||||
resp = requests.post(f"{OLLAMA_URL}/api/chat", json=payload, timeout=timeout_sec)
|
||||
dt = time.time() - t0
|
||||
print(f"[Ollama] Response status={resp.status_code} elapsed={dt:.2f}s")
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
text = (data.get("message") or {}).get("content", "")
|
||||
_preview = (text[:120]).replace("\n", " ")
|
||||
print(f"[Ollama] Text length={len(text)} preview={_preview}...")
|
||||
return text
|
||||
except Exception as e:
|
||||
dt = time.time() - t0
|
||||
print(f"[Ollama][Error] during request elapsed={dt:.2f}s: {e}")
|
||||
raise
|
||||
|
||||
|
||||
Reference in New Issue
Block a user