# SPDX-License-Identifier: Apache-2.0 """Quickstart against a running server (tt serve changh95/embeddinggemma-2): text retrieval with task prompts, Matryoshka truncation, and an image / audio query from the shipped samples. Standard library only.""" import base64 import json import math import pathlib import urllib.request URL = "http://localhost:20000" SAMPLES = pathlib.Path(__file__).resolve().parent.parent / "code" / "tt_embgemma" / "samples" def post(route, payload): req = urllib.request.Request(URL + route, data=json.dumps(payload).encode(), headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=300) as r: return json.loads(r.read()) def cos(a, b): return sum(x * y for x, y in zip(a, b)) / math.sqrt(sum(x * x for x in a) * sum(y * y for y in b)) docs = ["The northern lights are caused by charged particles from the sun.", "Giant pandas feed almost entirely on bamboo.", "In Python, sorted(my_list) returns a new sorted list."] q = post("/v1/embeddings", {"input": "What causes the northern lights?", "prompt_name": "SearchQuery", "dimensions": 256})["data"][0]["embedding"] d = [e["embedding"] for e in post("/v1/embeddings", {"input": docs, "prompt_name": "Document", "dimensions": 256})["data"]] print("query vs docs:", [round(cos(q, x), 4) for x in d]) img = base64.b64encode((SAMPLES / "checker.png").read_bytes()).decode() wav = base64.b64encode((SAMPLES / "speech_16k.wav").read_bytes()).decode() r = post("/embed", {"inputs": [{"image": img}, {"audio": wav}]}) texts = post("/v1/embeddings", {"input": ["a black and white checkerboard pattern", "Hello, this is a short reference recording."], "prompt_name": "SearchQuery"})["data"] for name, e in zip(("image", "audio"), r["embeddings"]): print(name, "vs captions:", [round(cos(e, t["embedding"]), 4) for t in texts])