chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,10 @@
|
||||
# Install torch/torchvision for the target CPU/CUDA/MPS platform separately.
|
||||
# Versions below match the WeMM model card and the validated service environment.
|
||||
transformers==5.2.0
|
||||
qwen-vl-utils==0.0.14
|
||||
accelerate>=1.1.0,<2
|
||||
fastapi>=0.115,<1
|
||||
pydantic>=2,<3
|
||||
uvicorn>=0.30,<1
|
||||
pillow>=10,<13
|
||||
httpx>=0.27,<1
|
||||
@@ -0,0 +1,367 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Serve WeMM text/image embeddings with the official Transformers model code."""
|
||||
import argparse
|
||||
import base64
|
||||
import binascii
|
||||
import hmac
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import Depends, FastAPI, Header, HTTPException
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
from pydantic import BaseModel, ConfigDict, Field, StrictInt
|
||||
|
||||
log = logging.getLogger("uvicorn.error")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
model: str = "tencent/WeMM-Embedding-2B"
|
||||
served_model_name: str | None = None
|
||||
revision: str | None = None
|
||||
device: str = "auto"
|
||||
dtype: str = "auto"
|
||||
max_tokens: int = 8192
|
||||
max_pixels: int = 1048576
|
||||
max_batch_size: int = 32
|
||||
cpu_threads: int = 8
|
||||
local_files_only: bool = False
|
||||
api_key: str = ""
|
||||
|
||||
def __post_init__(self):
|
||||
if min(self.max_tokens, self.max_pixels, self.max_batch_size, self.cpu_threads) <= 0:
|
||||
raise ValueError("Resource limits and cpu_threads must be positive.")
|
||||
if self.max_pixels < 65536:
|
||||
raise ValueError("max_pixels must be at least 65536 (the model processor's minimum).")
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
if self.served_model_name:
|
||||
return self.served_model_name
|
||||
return Path(self.model).name if Path(self.model).is_dir() else self.model
|
||||
|
||||
|
||||
class StrictModel(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class TextPart(StrictModel):
|
||||
type: Literal["text"]
|
||||
text: str
|
||||
|
||||
|
||||
class ImageURL(StrictModel):
|
||||
url: str = Field(max_length=16 * 1024 * 1024)
|
||||
detail: Literal["auto", "low", "high"] = "auto"
|
||||
|
||||
|
||||
class ImagePart(StrictModel):
|
||||
type: Literal["image_url"]
|
||||
image_url: ImageURL
|
||||
|
||||
|
||||
class Message(StrictModel):
|
||||
role: Literal["system", "user", "assistant"]
|
||||
content: str | list[TextPart | ImagePart] = Field(min_length=1)
|
||||
|
||||
|
||||
class EmbedRequest(StrictModel):
|
||||
model: str
|
||||
input: str | list[str] | None = None
|
||||
messages: list[Message] | list[list[Message]] | None = None
|
||||
dimensions: StrictInt | None = None
|
||||
encoding_format: Literal["float", "base64"] = "float"
|
||||
user: str | None = None
|
||||
|
||||
|
||||
def bad(message):
|
||||
raise HTTPException(400, message)
|
||||
|
||||
|
||||
def parse_inputs(request, settings, dimensions, default_dimension):
|
||||
if request.model != settings.name:
|
||||
raise HTTPException(404, f"Model not found: {request.model}")
|
||||
dimension = default_dimension if request.dimensions is None else request.dimensions
|
||||
if dimension not in dimensions:
|
||||
bad(f"dimensions must be one of {dimensions}.")
|
||||
if (request.input is None) == (request.messages is None):
|
||||
bad("Supply exactly one of input or messages.")
|
||||
if request.input is not None:
|
||||
texts = [request.input] if isinstance(request.input, str) else request.input
|
||||
conversations = [[Message(role="user", content=[TextPart(type="text", text=text)])] for text in texts]
|
||||
else:
|
||||
messages = request.messages
|
||||
conversations = [messages] if messages and isinstance(messages[0], Message) else messages
|
||||
if not conversations or len(conversations) > settings.max_batch_size:
|
||||
bad(f"Supply between 1 and {settings.max_batch_size} independent inputs.")
|
||||
for conversation in conversations:
|
||||
if not conversation:
|
||||
bad("Each conversation must contain at least one message.")
|
||||
chars, images = 0, 0
|
||||
for message in conversation:
|
||||
parts = message.content
|
||||
if isinstance(parts, str):
|
||||
chars += len(parts)
|
||||
continue
|
||||
for part in parts:
|
||||
if isinstance(part, TextPart):
|
||||
chars += len(part.text)
|
||||
else:
|
||||
images += 1
|
||||
if not part.image_url.url.startswith("data:image/") or ";base64," not in part.image_url.url:
|
||||
bad("Images must be base64 data:image/... URLs; network URLs and file paths are unsupported.")
|
||||
if images > 5:
|
||||
bad("At most 5 images per conversation.")
|
||||
if chars > settings.max_tokens * 16:
|
||||
bad("Text exceeds the character guard; reduce the input size.")
|
||||
return conversations, dimension
|
||||
|
||||
|
||||
def decode_image(url):
|
||||
try:
|
||||
data = base64.b64decode(url.split(";base64,", 1)[1], validate=True)
|
||||
with Image.open(io.BytesIO(data)) as image:
|
||||
if image.width * image.height > 40_000_000:
|
||||
bad("Image exceeds 40 megapixels before resizing.")
|
||||
# Preserve alpha until qwen-vl-utils composites RGBA onto white,
|
||||
# matching the official inference path instead of exposing hidden RGB.
|
||||
return image.copy()
|
||||
except (ValueError, binascii.Error, UnidentifiedImageError, OSError, Image.DecompressionBombError):
|
||||
bad("Invalid image data.")
|
||||
|
||||
|
||||
def prepare_messages(conversation, max_pixels):
|
||||
messages = []
|
||||
for message in conversation:
|
||||
parts = message.content
|
||||
if isinstance(parts, str):
|
||||
parts = [TextPart(type="text", text=parts)]
|
||||
content = []
|
||||
for part in parts:
|
||||
if isinstance(part, TextPart):
|
||||
content.append({"type": "text", "text": part.text})
|
||||
else:
|
||||
content.append({"type": "image", "image": decode_image(part.image_url.url), "max_pixels": max_pixels})
|
||||
messages.append({"role": message.role, "content": content})
|
||||
return messages
|
||||
|
||||
|
||||
def resolve_device_dtype(torch, settings):
|
||||
name = settings.device
|
||||
if name == "auto":
|
||||
name = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
||||
device = torch.device(name)
|
||||
if device.type not in ("cuda", "mps", "cpu"):
|
||||
raise ValueError("device must be auto, cpu, mps, cuda or cuda:N.")
|
||||
if device.type == "cuda":
|
||||
if not torch.cuda.is_available():
|
||||
raise ValueError("CUDA is unavailable; install a CUDA-enabled PyTorch build or select another device.")
|
||||
if device.index is not None and device.index >= torch.cuda.device_count():
|
||||
raise ValueError("The requested CUDA device does not exist.")
|
||||
with torch.cuda.device(device):
|
||||
bf16 = torch.cuda.is_bf16_supported()
|
||||
elif device.type == "mps":
|
||||
if not torch.backends.mps.is_available():
|
||||
raise ValueError("MPS is unavailable on this machine.")
|
||||
bf16 = True
|
||||
else:
|
||||
bf16 = False
|
||||
dtype = settings.dtype
|
||||
if dtype == "auto":
|
||||
dtype = "float32" if device.type == "cpu" else "bfloat16" if bf16 else "float16"
|
||||
if device.type == "cuda" and dtype == "bfloat16" and not bf16:
|
||||
raise ValueError("This CUDA device does not support bfloat16; use --dtype float16.")
|
||||
return device, getattr(torch, dtype)
|
||||
|
||||
|
||||
class Engine:
|
||||
def __init__(self, settings):
|
||||
# Import after CLI setup so --mps-fallback takes effect before torch loads.
|
||||
import torch
|
||||
from transformers import AutoConfig, AutoModel, AutoProcessor
|
||||
self.settings, self.torch = settings, torch
|
||||
self.device, self.dtype = resolve_device_dtype(torch, settings)
|
||||
self.lock = threading.Lock()
|
||||
torch.set_num_threads(settings.cpu_threads)
|
||||
options = {"trust_remote_code": True, "local_files_only": settings.local_files_only}
|
||||
if settings.revision:
|
||||
options["revision"] = settings.revision
|
||||
config = AutoConfig.from_pretrained(settings.model, **options)
|
||||
self.dimensions = tuple(getattr(config, "matryoshka_dimensions", ()))
|
||||
self.default_dimension = config.text_config.hidden_size
|
||||
if not self.dimensions or self.default_dimension not in self.dimensions:
|
||||
raise ValueError("The model must provide WeMM matryoshka_dimensions including its hidden size.")
|
||||
self.processor = AutoProcessor.from_pretrained(settings.model, **options)
|
||||
self.model = AutoModel.from_pretrained(
|
||||
settings.model, dtype=self.dtype, attn_implementation="sdpa", **options,
|
||||
).to(self.device).eval()
|
||||
if not callable(getattr(self.model, "embedding", None)):
|
||||
raise ValueError("The model must expose WeMM's embedding() method.")
|
||||
|
||||
def encode(self, conversations, dimension):
|
||||
from qwen_vl_utils import process_vision_info
|
||||
torch = self.torch
|
||||
vectors, total_tokens = [], 0
|
||||
# WeMM's model stores rope_deltas. Keep independent conversations serialized,
|
||||
# and decode images only for the current input rather than the entire batch.
|
||||
with self.lock, torch.inference_mode():
|
||||
for conversation in conversations:
|
||||
messages = prepare_messages(conversation, self.settings.max_pixels)
|
||||
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
|
||||
images, _ = process_vision_info(messages, image_patch_size=16)
|
||||
inputs = self.processor(text=text, images=images, return_tensors="pt")
|
||||
count = inputs["input_ids"].shape[-1]
|
||||
if count > self.settings.max_tokens:
|
||||
bad(f"Input has {count} tokens; limit is {self.settings.max_tokens}. Input was not truncated.")
|
||||
last = self.processor.tokenizer.convert_ids_to_tokens(inputs["input_ids"][0, -1].item())
|
||||
if last != "<embedding>":
|
||||
raise RuntimeError("Processor did not append the official <embedding> token.")
|
||||
inputs = inputs.to(self.device)
|
||||
# Reset explicitly as well, so older official code revisions are safe
|
||||
# when a text request follows a multimodal request.
|
||||
self.model.model.rope_deltas = None
|
||||
vector = self.model.embedding(**inputs, use_cache=False).float()
|
||||
vector = torch.nn.functional.normalize(vector[:, :dimension], dim=-1).cpu()[0]
|
||||
if vector.numel() != dimension or not torch.isfinite(vector).all() or vector.norm() < 0.99:
|
||||
raise RuntimeError("Invalid embedding output.")
|
||||
vectors.append(vector)
|
||||
total_tokens += count
|
||||
del inputs, images, messages
|
||||
return vectors, total_tokens
|
||||
|
||||
|
||||
class BodyLimit:
|
||||
"""Bound JSON bodies, including chunked requests, before parsing/base64 decoding."""
|
||||
def __init__(self, app, limit=32 * 1024 * 1024):
|
||||
self.app, self.limit = app, limit
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
if scope["type"] != "http" or scope["method"] != "POST":
|
||||
return await self.app(scope, receive, send)
|
||||
chunks, size = [], 0
|
||||
while True:
|
||||
message = await receive()
|
||||
if message["type"] == "http.disconnect":
|
||||
return
|
||||
chunk = message.get("body", b"")
|
||||
size += len(chunk)
|
||||
if size > self.limit:
|
||||
response = JSONResponse({"error": {"message": "Request body exceeds 32 MiB."}}, status_code=413)
|
||||
return await response(scope, receive, send)
|
||||
chunks.append(chunk)
|
||||
if not message.get("more_body", False):
|
||||
break
|
||||
body = b"".join(chunks)
|
||||
|
||||
async def replay():
|
||||
nonlocal body
|
||||
if body is not None:
|
||||
value, body = body, None
|
||||
return {"type": "http.request", "body": value, "more_body": False}
|
||||
return await receive()
|
||||
|
||||
await self.app(scope, replay, send)
|
||||
|
||||
|
||||
def create_app(settings, engine_factory=Engine):
|
||||
@asynccontextmanager
|
||||
async def lifespan(app):
|
||||
started = time.monotonic()
|
||||
app.state.engine = engine_factory(settings)
|
||||
log.info("Loaded %s on %s/%s in %.1fs", settings.name, app.state.engine.device,
|
||||
app.state.engine.dtype, time.monotonic() - started)
|
||||
yield
|
||||
del app.state.engine
|
||||
|
||||
app = FastAPI(title="WeMM Transformers embedding service", lifespan=lifespan)
|
||||
app.add_middleware(BodyLimit)
|
||||
|
||||
def authorize(authorization: str | None = Header(default=None)):
|
||||
expected = "Bearer " + settings.api_key
|
||||
if settings.api_key and not hmac.compare_digest((authorization or "").encode(), expected.encode()):
|
||||
raise HTTPException(401, "Invalid API key.", headers={"WWW-Authenticate": "Bearer"})
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def api_error(_request, exc):
|
||||
return JSONResponse(status_code=exc.status_code, headers=exc.headers,
|
||||
content={"error": {"message": exc.detail, "code": exc.status_code}})
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_error(_request, exc):
|
||||
return JSONResponse(status_code=400, content={"error": {
|
||||
"message": "Invalid request: " + exc.errors()[0]["msg"], "code": 400,
|
||||
}})
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
engine = app.state.engine
|
||||
return {"status": "ok", "model": settings.name, "backend": "transformers",
|
||||
"device": str(engine.device), "dtype": str(engine.dtype), "revision": settings.revision,
|
||||
"dimensions": engine.dimensions, "default_dimension": engine.default_dimension,
|
||||
"max_tokens": settings.max_tokens, "max_image_pixels": settings.max_pixels}
|
||||
|
||||
@app.get("/v1/models", dependencies=[Depends(authorize)])
|
||||
def models():
|
||||
return {"object": "list", "data": [{"id": settings.name, "object": "model", "created": 0, "owned_by": "tencent"}]}
|
||||
|
||||
@app.post("/v1/embeddings", dependencies=[Depends(authorize)])
|
||||
def embeddings(request: EmbedRequest):
|
||||
engine = app.state.engine
|
||||
conversations, dimension = parse_inputs(request, settings, engine.dimensions, engine.default_dimension)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
vectors, tokens = engine.encode(conversations, dimension)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception:
|
||||
log.exception("WeMM inference failed")
|
||||
raise HTTPException(500, "WeMM inference failed; inspect the server log.") from None
|
||||
data = []
|
||||
for index, vector in enumerate(vectors):
|
||||
value = (base64.b64encode(vector.numpy().astype("<f4").tobytes()).decode()
|
||||
if request.encoding_format == "base64" else vector.tolist())
|
||||
data.append({"object": "embedding", "index": index, "embedding": value})
|
||||
log.info("Embedded %d inputs, %d tokens, %d dimensions in %.2fs", len(data), tokens,
|
||||
dimension, time.monotonic() - started)
|
||||
return {"object": "list", "data": data, "model": settings.name,
|
||||
"usage": {"prompt_tokens": tokens, "total_tokens": tokens}}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--model", default=Settings.model, help="WeMM Hugging Face ID or model directory")
|
||||
parser.add_argument("--served-model-name", help="API model ID; defaults to the repo ID or directory basename")
|
||||
parser.add_argument("--revision", help="Hugging Face revision (prefer a commit SHA for reproducibility)")
|
||||
parser.add_argument("--device", default="auto", help="auto, cuda, cuda:N, mps or cpu")
|
||||
parser.add_argument("--dtype", choices=("auto", "bfloat16", "float16", "float32"), default="auto")
|
||||
parser.add_argument("--max-tokens", type=int, default=8192)
|
||||
parser.add_argument("--max-pixels", type=int, default=1048576)
|
||||
parser.add_argument("--max-batch-size", type=int, default=32)
|
||||
parser.add_argument("--cpu-threads", type=int, default=8)
|
||||
parser.add_argument("--local-files-only", action="store_true")
|
||||
parser.add_argument("--mps-fallback", action="store_true", help="Allow PyTorch CPU fallback for unsupported MPS ops")
|
||||
parser.add_argument("--host", default="127.0.0.1")
|
||||
parser.add_argument("--port", type=int, default=18080)
|
||||
args = vars(parser.parse_args())
|
||||
host, port = args.pop("host"), args.pop("port")
|
||||
if args.pop("mps_fallback"):
|
||||
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
||||
settings = Settings(**args, api_key=os.environ.get("WEMM_API_KEY", ""))
|
||||
import uvicorn
|
||||
# A single process owns the model; do not add workers without separate GPUs.
|
||||
uvicorn.run(create_app(settings), host=host, port=port, workers=1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check a running WeMM service with actual text and image inference."""
|
||||
import argparse
|
||||
import base64
|
||||
import io
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import time
|
||||
|
||||
import httpx
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
|
||||
def dot(a, b):
|
||||
return sum(x * y for x, y in zip(a, b))
|
||||
|
||||
|
||||
def text_message(text):
|
||||
return [{"role": "user", "content": [{"type": "text", "text": text}]}]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://127.0.0.1:18080", help="Service root URL, without /v1")
|
||||
parser.add_argument("--timeout", type=float, default=180)
|
||||
args = parser.parse_args()
|
||||
headers = {"Authorization": "Bearer " + os.environ["WEMM_API_KEY"]} if os.environ.get("WEMM_API_KEY") else {}
|
||||
results = []
|
||||
with httpx.Client(base_url=args.base_url.rstrip("/"), timeout=args.timeout, headers=headers) as client:
|
||||
health_response = client.get("/health")
|
||||
health_response.raise_for_status()
|
||||
health = health_response.json()
|
||||
model = health["model"]
|
||||
default_dimension = health["default_dimension"]
|
||||
response = client.get("/v1/models")
|
||||
response.raise_for_status()
|
||||
assert model in [row["id"] for row in response.json()["data"]]
|
||||
|
||||
def run(name, **payload):
|
||||
started = time.monotonic()
|
||||
response = client.post("/v1/embeddings", json={"model": model, **payload})
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
vectors = [row["embedding"] for row in data["data"]]
|
||||
assert [row["index"] for row in data["data"]] == list(range(len(vectors)))
|
||||
for vector in vectors:
|
||||
assert len(vector) == payload.get("dimensions", default_dimension)
|
||||
assert all(math.isfinite(x) for x in vector)
|
||||
assert abs(math.sqrt(dot(vector, vector)) - 1) < 1e-4
|
||||
results.append({"test": name, "seconds": round(time.monotonic() - started, 3),
|
||||
"vectors": len(vectors), "dimension": len(vectors[0])})
|
||||
print(json.dumps(results[-1]), flush=True)
|
||||
return vectors
|
||||
|
||||
texts = ["A red square on a white background.", "A blue circle on a black background."]
|
||||
text_vectors = run("batched text messages", messages=[text_message(text) for text in texts])
|
||||
images = []
|
||||
for color, shape, background in [("red", "rectangle", "white"), ("blue", "ellipse", "black")]:
|
||||
image = Image.new("RGB", (256, 256), background)
|
||||
getattr(ImageDraw.Draw(image), shape)((40, 40, 216, 216), fill=color)
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, format="PNG")
|
||||
images.append([{"role": "user", "content": [{"type": "image_url", "image_url": {
|
||||
"url": "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode(),
|
||||
}}]}])
|
||||
image_vectors = run("batched image messages", messages=images)
|
||||
scores = [[dot(text, image) for image in image_vectors] for text in text_vectors]
|
||||
assert scores[0][0] > scores[0][1] and scores[1][1] > scores[1][0], scores
|
||||
repeated = run("text after image state isolation", messages=text_message(texts[0]))[0]
|
||||
assert dot(repeated, text_vectors[0]) > 0.9999
|
||||
dimension = min(health["dimensions"])
|
||||
short = run("Matryoshka dimensions", messages=text_message(texts[0]), dimensions=dimension)[0]
|
||||
prefix = text_vectors[0][:dimension]
|
||||
norm = math.sqrt(dot(prefix, prefix))
|
||||
assert dot(short, [value / norm for value in prefix]) > 0.9999
|
||||
raw = run("raw text compatibility", input=texts[0])[0]
|
||||
assert dot(raw, text_vectors[0]) > 0.9999
|
||||
for payload in ({"input": "hello", "dimensions": 3}, {"input": "a", "messages": []},
|
||||
{"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {
|
||||
"url": "https://example.com/image.png",
|
||||
}}]}]}):
|
||||
response = client.post("/v1/embeddings", json={"model": model, **payload})
|
||||
assert response.status_code == 400, response.text
|
||||
print(json.dumps({"status": "passed", "health": health, "tests": results, "text_image_scores": scores}), flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,169 @@
|
||||
"""Contract tests without downloading weights: python -m unittest discover -s scripts/wemm-transformers."""
|
||||
import asyncio
|
||||
import base64
|
||||
import io
|
||||
import struct
|
||||
import unittest
|
||||
from contextlib import nullcontext
|
||||
from unittest.mock import patch
|
||||
|
||||
import torch
|
||||
from fastapi import HTTPException
|
||||
from fastapi.testclient import TestClient
|
||||
from PIL import Image
|
||||
from serve import BodyLimit, Message, Settings, create_app, decode_image, prepare_messages, resolve_device_dtype
|
||||
|
||||
|
||||
class FakeEngine:
|
||||
device, dtype = "cpu", "float32"
|
||||
|
||||
def __init__(self, settings, dimensions=(64, 128, 256, 512, 1024, 2048)):
|
||||
self.dimensions = dimensions
|
||||
self.default_dimension = dimensions[-1]
|
||||
self.calls = []
|
||||
|
||||
def encode(self, conversations, dimension):
|
||||
self.calls.append(conversations)
|
||||
vectors = []
|
||||
for index, _ in enumerate(conversations):
|
||||
vector = torch.zeros(dimension)
|
||||
vector[index] = 1
|
||||
vectors.append(vector)
|
||||
return vectors, len(conversations) * 10
|
||||
|
||||
|
||||
class ServiceTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.settings = Settings()
|
||||
self.engine = FakeEngine(self.settings)
|
||||
self.client = self.enterContext(TestClient(create_app(self.settings, lambda _: self.engine)))
|
||||
|
||||
def post(self, **payload):
|
||||
return self.client.post("/v1/embeddings", json={"model": self.settings.name, **payload})
|
||||
|
||||
def test_input_and_single_or_batched_messages(self):
|
||||
message = {"role": "user", "content": [{"type": "text", "text": "hello"}]}
|
||||
for payload, count in [({"input": "hello"}, 1), ({"input": ["a", "b"]}, 2),
|
||||
({"messages": [message]}, 1), ({"messages": [[message], [message]]}, 2)]:
|
||||
with self.subTest(payload=payload):
|
||||
response = self.post(**payload)
|
||||
self.assertEqual(response.status_code, 200, response.text)
|
||||
data = response.json()["data"]
|
||||
self.assertEqual([x["index"] for x in data], list(range(count)))
|
||||
self.assertTrue(all(len(x["embedding"]) == 2048 for x in data))
|
||||
self.assertEqual([x["embedding"][i] for i, x in enumerate(data)], [1] * count)
|
||||
|
||||
def test_all_model_sizes_use_their_own_default_and_allowed_dimensions(self):
|
||||
for size, dimensions in [("2B", (64, 128, 256, 512, 1024, 2048)),
|
||||
("4B", (64, 128, 256, 512, 1024, 2560)),
|
||||
("9B", (64, 128, 256, 512, 1024, 2048, 4096))]:
|
||||
settings = Settings(model="tencent/WeMM-Embedding-" + size)
|
||||
with self.subTest(size=size), TestClient(create_app(settings, lambda s: FakeEngine(s, dimensions))) as client:
|
||||
for requested in (None, 1024):
|
||||
payload = {"model": settings.name, "input": "hello"}
|
||||
if requested is not None:
|
||||
payload["dimensions"] = requested
|
||||
response = client.post("/v1/embeddings", json=payload)
|
||||
self.assertEqual(response.status_code, 200, response.text)
|
||||
self.assertEqual(len(response.json()["data"][0]["embedding"]), requested or dimensions[-1])
|
||||
payload["dimensions"] = 2049
|
||||
self.assertEqual(client.post("/v1/embeddings", json=payload).status_code, 400)
|
||||
self.assertEqual(client.get("/health").json()["default_dimension"], dimensions[-1])
|
||||
|
||||
def test_base64_is_little_endian_float32(self):
|
||||
response = self.post(input="hello", dimensions=64, encoding_format="base64")
|
||||
vector = struct.unpack("<64f", base64.b64decode(response.json()["data"][0]["embedding"]))
|
||||
self.assertEqual(vector, (1,) + (0,) * 63)
|
||||
|
||||
def test_invalid_requests_never_reach_inference(self):
|
||||
for payload in [dict(input="a", messages=[]), dict(input=[]), dict(messages=[[]]),
|
||||
dict(input="a", dimensions=True), dict(input="a", dimensions=0),
|
||||
dict(input="a", temperature=0), dict(input=["a"] * 33),
|
||||
dict(messages=[{"role": "user", "content": [{"type": "video", "video": "x"}]}]),
|
||||
dict(messages=[{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "file:///etc/passwd"}}]}])]:
|
||||
with self.subTest(payload=payload):
|
||||
self.assertEqual(self.post(**payload).status_code, 400)
|
||||
self.assertEqual(self.post(model="wrong", input="a").status_code, 404)
|
||||
self.assertEqual(self.engine.calls, [])
|
||||
|
||||
def test_authentication_on_models_and_embeddings(self):
|
||||
settings = Settings(api_key="test-only-key")
|
||||
with TestClient(create_app(settings, lambda _: self.engine)) as client:
|
||||
self.assertEqual(client.get("/health").status_code, 200)
|
||||
self.assertEqual(client.get("/v1/models").status_code, 401)
|
||||
for token, status in [("Bearer wrong", 401), ("Bearer test-only-key", 200)]:
|
||||
response = client.post("/v1/embeddings", json={"model": settings.name, "input": "a"},
|
||||
headers={"Authorization": token})
|
||||
self.assertEqual(response.status_code, status)
|
||||
self.assertEqual(len(self.engine.calls), 1)
|
||||
|
||||
def test_image_decoding_and_invalid_bytes(self):
|
||||
buffer = io.BytesIO()
|
||||
Image.new("RGB", (64, 64), "red").save(buffer, format="PNG")
|
||||
uri = "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
|
||||
self.assertEqual(decode_image(uri).size, (64, 64))
|
||||
for data in ["!invalid!", base64.b64encode(b"not an image").decode()]:
|
||||
with self.assertRaises(HTTPException) as error:
|
||||
decode_image("data:image/png;base64," + data)
|
||||
self.assertEqual(error.exception.status_code, 400)
|
||||
|
||||
def test_transparent_images_match_official_white_background(self):
|
||||
from qwen_vl_utils import process_vision_info
|
||||
image = Image.new("RGBA", (256, 256), (0, 0, 0, 0))
|
||||
image.putpixel((128, 128), (255, 0, 0, 128))
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, format="PNG")
|
||||
uri = "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
|
||||
conversation = [Message.model_validate({"role": "user", "content": [
|
||||
{"type": "image_url", "image_url": {"url": uri}},
|
||||
]})]
|
||||
actual, _ = process_vision_info(prepare_messages(conversation, 1048576), image_patch_size=16)
|
||||
expected, _ = process_vision_info([{"role": "user", "content": [
|
||||
{"type": "image", "image": image, "max_pixels": 1048576},
|
||||
]}], image_patch_size=16)
|
||||
self.assertEqual(actual[0].getpixel((0, 0)), (255, 255, 255))
|
||||
self.assertEqual(actual[0].getpixel((128, 128)), (255, 127, 127))
|
||||
self.assertEqual(actual[0].tobytes(), expected[0].tobytes())
|
||||
|
||||
def test_cpu_defaults_and_explicit_unavailable_device(self):
|
||||
with patch.object(torch.cuda, "is_available", return_value=False), patch.object(torch.backends.mps, "is_available", return_value=False):
|
||||
device, dtype = resolve_device_dtype(torch, Settings())
|
||||
self.assertEqual(str(device), "cpu")
|
||||
self.assertEqual(dtype, torch.float32)
|
||||
for requested in ("cuda", "mps"):
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_device_dtype(torch, Settings(device=requested))
|
||||
|
||||
def test_cuda_auto_dtype_and_explicit_bf16_guard(self):
|
||||
with patch.object(torch.cuda, "is_available", return_value=True), patch.object(torch.cuda, "device", return_value=nullcontext()):
|
||||
for supported, expected in [(True, torch.bfloat16), (False, torch.float16)]:
|
||||
with patch.object(torch.cuda, "is_bf16_supported", return_value=supported):
|
||||
device, dtype = resolve_device_dtype(torch, Settings())
|
||||
self.assertEqual(str(device), "cuda")
|
||||
self.assertEqual(dtype, expected)
|
||||
if not supported:
|
||||
with self.assertRaises(ValueError):
|
||||
resolve_device_dtype(torch, Settings(device="cuda", dtype="bfloat16"))
|
||||
|
||||
def test_body_limit_counts_chunked_bodies(self):
|
||||
async def run(chunks):
|
||||
received, sent = [], []
|
||||
events = iter({"type": "http.request", "body": chunk, "more_body": i < len(chunks) - 1}
|
||||
for i, chunk in enumerate(chunks))
|
||||
async def receive():
|
||||
return next(events)
|
||||
async def send(event):
|
||||
sent.append(event)
|
||||
async def downstream(scope, receive, send):
|
||||
received.append((await receive())["body"])
|
||||
await BodyLimit(downstream, limit=4)({"type": "http", "method": "POST"}, receive, send)
|
||||
return received, sent
|
||||
received, _ = asyncio.run(run([b"12", b"34"]))
|
||||
self.assertEqual(received, [b"1234"])
|
||||
received, sent = asyncio.run(run([b"12", b"345"]))
|
||||
self.assertEqual(received, [])
|
||||
self.assertEqual(sent[0]["status"], 413)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user