| import base64, requests, json, time |
| from typing import List, Dict, Any |
|
|
| def to_jpeg_base64(img_rgb) -> str: |
| import cv2 |
| bgr = img_rgb[:, :, ::-1] |
| ok, enc = cv2.imencode('.jpg', bgr, [int(cv2.IMWRITE_JPEG_QUALITY), 90]) |
| if not ok: raise RuntimeError('JPEG 编码失败') |
| return base64.b64encode(enc.tobytes()).decode('utf-8') |
|
|
| def call_vllm(api_base: str, model_name: str, messages: List[Dict[str,Any]], |
| temperature: float=0.2, max_tokens: int=1024, timeout=300): |
| url = f"{api_base}/chat/completions" |
| payload = { |
| "model": model_name, |
| "messages": messages, |
| "temperature": temperature, |
| "max_tokens": max_tokens |
| } |
| t0 = time.time() |
| r = requests.post(url, json=payload, timeout=timeout) |
| r.raise_for_status() |
| data = r.json() |
| txt = data['choices'][0]['message']['content'] |
| return txt, (time.time()-t0), data |
|
|