File size: 943 Bytes
aa975a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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,   # 关键:与 served-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