#!/usr/bin/env python3 """OpenAI-API tests against a served lfm3-vl-2b: image by http URL, two images, base64 data URL (what Open WebUI sends), text only, image + tool call, and the rejections (video, > 4 images). python3 tests/vl/api_test.py PORT LABEL [IMAGE_BASE_URL] -> results/vl/api_LABEL.json IMAGE_BASE_URL: where tests/vl/images is served over http for the server (default http://172.17.0.1:8765). """ import base64, json, sys, time, urllib.error, urllib.request port, label = int(sys.argv[1]), sys.argv[2] base = sys.argv[3] if len(sys.argv) > 3 else "http://172.17.0.1:8765" URL = f"http://127.0.0.1:{port}/v1/chat/completions" IMG = "tests/vl/images" def data_url(path, mime): return f"data:{mime};base64," + base64.b64encode(open(f"{IMG}/{path}", "rb").read()).decode() def img(url): return {"type": "image_url", "image_url": {"url": url}} def chat(content, max_tokens=4096, tools=None, expect_error=False): # no sampling params: the server applies the checkpoint's generation_config.json # (temperature 0.1, top_k 50, repetition_penalty 1.1), as for an Open WebUI request; fixed seed body = {"model": "lfm3-vl-2b", "messages": [{"role": "user", "content": content}], "max_tokens": max_tokens, "seed": 0} if tools: body["tools"] = tools req = urllib.request.Request(URL, json.dumps(body).encode(), {"Content-Type": "application/json"}) t0 = time.time() try: r = json.loads(urllib.request.urlopen(req, timeout=600).read()) except urllib.error.HTTPError as e: return {"http_status": e.code, "error": json.loads(e.read()).get("error", {}).get("message", "")[:400], "expected_error": expect_error} m = r["choices"][0]["message"] return {"http_status": 200, "expected_error": expect_error, "seconds": round(time.time() - t0, 2), "prompt_tokens": r["usage"]["prompt_tokens"], "completion_tokens": r["usage"]["completion_tokens"], "finish_reason": r["choices"][0]["finish_reason"], "content": m.get("content"), "reasoning": (m.get("reasoning") or "")[:600], "tool_calls": [tc["function"] for tc in (m.get("tool_calls") or [])]} TOOLS = [{"type": "function", "function": { "name": "search_web", "description": "Search the web.", "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}}}] TESTS = { "one_image_url": lambda: chat([img(f"{base}/photo.jpeg"), {"type": "text", "text": "Which newspaper is this and what is the main headline?"}]), "two_images": lambda: chat([img(f"{base}/photo.jpeg"), img(f"{base}/wide_strip.png"), {"type": "text", "text": "Describe the first image in one sentence and the second image in one sentence."}]), "base64_data_url": lambda: chat([{"type": "text", "text": "What kind of document is this? What are the column headers of the first table?"}, img(data_url("screenshot.png", "image/png"))]), "text_only": lambda: chat("What is the capital of Australia? Answer in one sentence."), "image_tool_call": lambda: chat([img(data_url("photo.jpeg", "image/jpeg")), {"type": "text", "text": "Find out more about the newspaper in this image: search the web for its name."}], tools=TOOLS), "video_rejected": lambda: chat([{"type": "video_url", "video_url": {"url": f"{base}/clip.mp4"}}, {"type": "text", "text": "What happens?"}], expect_error=True), "five_images_rejected": lambda: chat([img(data_url("small.png", "image/png"))] * 5 + [{"type": "text", "text": "How many images?"}], expect_error=True), } out = {} for name, fn in TESTS.items(): out[name] = fn() r = out[name] ok = (r["http_status"] != 200) == r["expected_error"] print(f"{'OK ' if ok else 'BAD'} {name}: status {r['http_status']}" + (f", {r['prompt_tokens']} prompt tok, {r['completion_tokens']} out, {r['seconds']} s, finish {r['finish_reason']}" if r["http_status"] == 200 else f": {r['error'][:200]}"), flush=True) if r["http_status"] == 200: print(" content:", repr((r["content"] or "")[:300]), "| tool_calls:", r["tool_calls"], flush=True) json.dump(out, open(f"results/vl/api_{label}.json", "w"), indent=1, ensure_ascii=False)