SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
36.3 KB · 380 lines python
Raw Blame History
1#!/usr/bin/env python32"""Gemini CORE GENERATION live probe (API Atlas). Cheap, logged, sanitized raws in tmp-live/gemini-core/.3Run: .venv/bin/python tmp-live/gemini-core/run_live.py4"""5from __future__ import annotations67import base648import json9import struct10import sys11import time12import zlib13from pathlib import Path1415ROOT = Path(__file__).resolve().parents[2]16sys.path.insert(0, str(ROOT))17from scripts import live  # noqa: E4021819OUT = ROOT / "tmp-live" / "gemini-core"20LITE = "gemini-3.5-flash-lite"21FLASH = "gemini-3.5-flash"22G38 = "gemini-3.8-flash"23PRO = "gemini-3.1-pro-preview"24SUMMARY: dict = {}25PRICES = {LITE: (0.30, 2.50), FLASH: (1.50, 9.00), G38: (0.75, 3.75), PRO: (2.0, 12.0),26          "gemini-embedding-001": (0.15, 0), "gemini-embedding-2": (0.20, 0)}27TOTAL_COST = 0.0282930def cost(model: str, body) -> float:31    if not isinstance(body, dict):32        return 0.033    u = body.get("usageMetadata") or {}34    pi, po = PRICES.get(model, (1.0, 5.0))35    inp = u.get("promptTokenCount", 0) or 036    out = (u.get("candidatesTokenCount", 0) or 0) + (u.get("thoughtsTokenCount", 0) or 0)37    return inp * pi / 1e6 + out * po / 1e6383940def call(name: str, method: str, path: str, body=None, *, model: str = LITE, note: str = "", **kw):41    """Non-stream call; saves raw; prints one summary line."""42    global TOTAL_COST43    t0 = time.time()44    st, resp, hdrs = live.gemini_request(method, path, body, note=f"core-probe {name}", **kw)45    dt = time.time() - t046    c = cost(model, resp)47    TOTAL_COST += c48    live.save_sanitized({"name": name, "request": {"method": method, "path": path, "body": body},49                         "status": st, "elapsed_s": round(dt, 2), "response": resp if not isinstance(resp, bytes) else resp.decode("utf-8", "replace")[:4000],50                         "headers": live.interesting_headers(hdrs)}, OUT / f"{name}.json")51    short = summarize(resp)52    print(f"[{name}] {method} {path.split('?')[0]} -> {st} ({dt:.1f}s) ${c:.6f} :: {short}")53    SUMMARY[name] = {"status": st, "path": path, "elapsed_s": round(dt, 2), "cost": round(c, 6), "short": short}54    return st, resp, hdrs555657def summarize(resp) -> str:58    if isinstance(resp, bytes):59        return f"bytes[{len(resp)}] {resp[:120]!r}"60    if not isinstance(resp, dict):61        return str(resp)[:200]62    if "error" in resp:63        e = resp["error"]64        det = e.get("details")65        return f"ERROR {e.get('code')} {e.get('status')}: {e.get('message','')[:220]}" + (f" details={json.dumps(det)[:200]}" if det else "")66    out = {}67    if "candidates" in resp:68        c0 = resp["candidates"][0]69        parts = c0.get("content", {}).get("parts", [])70        out["text"] = "".join(p.get("text", "") for p in parts if not p.get("thought"))[:60]71        out["thought_parts"] = sum(1 for p in parts if p.get("thought"))72        out["sig"] = [bool(p.get("thoughtSignature")) for p in parts]73        out["finish"] = c0.get("finishReason")74        out["n_cand"] = len(resp["candidates"])75        if c0.get("safetyRatings"):76            out["safetyRatings"] = len(c0["safetyRatings"])77        if c0.get("logprobsResult"):78            out["logprobs"] = True79    for k in ("usageMetadata", "modelVersion", "responseId", "promptFeedback", "totalTokens", "cachedContentTokenCount", "promptTokensDetails", "name", "expireTime", "state"):80        if k in resp:81            out[k] = resp[k]82    if "embedding" in resp:83        out["embedding_len"] = len(resp["embedding"].get("values", []))84        out["usage"] = resp.get("usageMetadata")85    if "embeddings" in resp:86        out["embeddings_lens"] = [len(e.get("values", [])) for e in resp["embeddings"]]87        out["usage"] = resp.get("usageMetadata")88    if "file" in resp:89        f = resp["file"]90        out["file"] = {k: f.get(k) for k in ("name", "uri", "state", "mimeType", "sizeBytes", "expirationTime", "source", "sha256Hash")}91    if "files" in resp:92        out["files_n"] = len(resp["files"])93    if "cachedContents" in resp:94        out["cachedContents_n"] = len(resp["cachedContents"])95    return json.dumps(out, ensure_ascii=False)[:600]969798def stream_call(name: str, path: str, body, *, model: str = LITE):99    """Streams raw lines; saves raw text + analysis."""100    global TOTAL_COST101    t0 = time.time()102    st, resp, hdrs = live.gemini_request("POST", path, body, stream=True, timeout=180, note=f"core-probe {name}")103    if st != 200:104        print(f"[{name}] -> {st} {summarize(resp)}")105        live.save_sanitized({"name": name, "status": st, "response": resp}, OUT / f"{name}.json")106        SUMMARY[name] = {"status": st, "short": summarize(resp)}107        return st, None108    lines = list(resp)109    dt = time.time() - t0110    raw = "\n".join(lines)111    (OUT / f"{name}.raw.txt").write_text(live.mask(raw))112    sse = "alt=sse" in path113    chunks = []114    if sse:115        for ln in lines:116            if ln.startswith("data:"):117                chunks.append(json.loads(ln[5:].strip()))118        other = [ln for ln in lines if ln and not ln.startswith("data:")]119    else:120        text = raw.strip()121        chunks = json.loads(text) if text.startswith("[") else []122        other = []123    last = chunks[-1] if chunks else {}124    c = cost(model, last)125    TOTAL_COST += c126    seq = []127    for ch in chunks:128        cand = (ch.get("candidates") or [{}])[0]129        parts = cand.get("content", {}).get("parts", [])130        seq.append({"parts": [("thought" if p.get("thought") else ("text" if "text" in p else list(p.keys())[0] if p else "empty")) + ("+sig" if p.get("thoughtSignature") else "") for p in parts],131                    "text_len": sum(len(p.get("text", "")) for p in parts), "finishReason": cand.get("finishReason"),132                    "usage": "usageMetadata" in ch, "responseId": ch.get("responseId"), "modelVersion": ch.get("modelVersion")})133    analysis = {"name": name, "status": st, "elapsed_s": round(dt, 2), "content_type": hdrs.get("Content-Type"), "n_lines": len(lines), "n_chunks": len(chunks),134                "first_lines": [live.mask(l)[:160] for l in lines[:4]], "last_lines": [live.mask(l)[:200] for l in lines[-4:]], "non_data_lines_sample": other[:5],135                "chunk_sequence": seq, "final_usage": last.get("usageMetadata"), "headers": live.interesting_headers(hdrs)}136    live.save_sanitized(analysis, OUT / f"{name}.json")137    print(f"[{name}] -> {st} ({dt:.1f}s) ${c:.6f} ct={hdrs.get('Content-Type')} lines={len(lines)} chunks={len(chunks)} seq={json.dumps(seq)[:500]} usage={last.get('usageMetadata')}")138    SUMMARY[name] = {k: analysis[k] for k in ("status", "content_type", "n_lines", "n_chunks", "chunk_sequence", "final_usage")}139    return st, chunks140141142def png_1x1() -> str:143    def chunk(t, d):144        c = struct.pack(">I", len(d)) + t + d145        return c + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)146    raw = b"\x00\xff\x00\x00"  # filter 0 + RGB red147    png = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"")148    return base64.b64encode(png).decode()149150151def minimal_pdf(word: str = "ATLAS") -> bytes:152    content = f"BT /F1 24 Tf 72 720 Td ({word}) Tj ET".encode()153    objs = [b"<< /Type /Catalog /Pages 2 0 R >>",154            b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",155            b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>",156            b"<< /Length " + str(len(content)).encode() + b" >>\nstream\n" + content + b"\nendstream",157            b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"]158    out = b"%PDF-1.4\n"159    offs = []160    for i, o in enumerate(objs, 1):161        offs.append(len(out))162        out += f"{i} 0 obj\n".encode() + o + b"\nendobj\n"163    xref = len(out)164    out += f"xref\n0 {len(objs)+1}\n0000000000 65535 f \n".encode()165    for o in offs:166        out += f"{o:010d} 00000 n \n".encode()167    out += f"trailer\n<< /Size {len(objs)+1} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n".encode()168    return out169170171def gc(model: str, version: str = "v1beta", alt: str | None = None) -> str:172    return f"/{version}/models/{model}:generateContent" + (f"?alt={alt}" if alt else "")173174175U = lambda text: [{"role": "user", "parts": [{"text": text}]}]  # noqa: E731176OK = {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 32}}177178179def main() -> None:180    OUT.mkdir(parents=True, exist_ok=True)181    # ---- (a) minimal + variants182    st, a, hdrs = call("a_minimal", "POST", gc(LITE), OK)183    call("a_v1_minimal", "POST", gc(LITE, version="v1"), OK)184    call("a_alt_sse_nonstream", "POST", gc(LITE, alt="sse"), OK, content_type="application/json")185    call("a_contents_single_object", "POST", gc(LITE), {"contents": {"parts": [{"text": "Reply with OK."}]}, "generationConfig": {"maxOutputTokens": 32}})186    call("a_no_role", "POST", gc(LITE), {"contents": [{"parts": [{"text": "Reply with OK."}]}], "generationConfig": {"maxOutputTokens": 32}})187    call("a_multiturn", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "My name is Atlas. Reply with OK."}]}, {"role": "model", "parts": [{"text": "OK."}]}, {"role": "user", "parts": [{"text": "What is my name? One word."}]}], "generationConfig": {"maxOutputTokens": 16}})188    call("a_role_assistant_invalid", "POST", gc(LITE), {"contents": [{"role": "assistant", "parts": [{"text": "hi"}]}], "generationConfig": {"maxOutputTokens": 8}})189    # thought signature round trip (send back sig from a)190    if st == 200:191        parts = a["candidates"][0]["content"]["parts"]192        call("a_sig_roundtrip", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "Reply with OK."}]}, {"role": "model", "parts": parts}, {"role": "user", "parts": [{"text": "Again, reply with OK."}]}], "generationConfig": {"maxOutputTokens": 16}})193        call("a_sig_bogus", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "Reply with OK."}]}, {"role": "model", "parts": [{"text": "OK.", "thoughtSignature": "bm90LWEtcmVhbC1zaWc="}]}, {"role": "user", "parts": [{"text": "Again, reply with OK."}]}], "generationConfig": {"maxOutputTokens": 16}})194        call("a_sig_skip_validator", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "Reply with OK."}]}, {"role": "model", "parts": [{"text": "OK.", "thoughtSignature": "skip_thought_signature_validator"}]}, {"role": "user", "parts": [{"text": "Again, reply with OK."}]}], "generationConfig": {"maxOutputTokens": 16}})195    # ---- (b) streaming196    stream_call("b_stream_sse", f"/v1beta/models/{LITE}:streamGenerateContent?alt=sse", {"contents": U("Count from 1 to 12 separated by spaces."), "generationConfig": {"maxOutputTokens": 32}})197    stream_call("b_stream_json_array", f"/v1beta/models/{LITE}:streamGenerateContent", {"contents": U("Count from 1 to 12 separated by spaces."), "generationConfig": {"maxOutputTokens": 32}})198    # ---- (c) system instruction + generationConfig199    call("c_system_gencfg", "POST", gc(LITE), {"systemInstruction": {"parts": [{"text": "You only ever answer with the single word OK."}]}, "contents": U("What is 2+2?"),200                                                "generationConfig": {"temperature": 0.2, "topP": 0.9, "topK": 40, "seed": 42, "stopSequences": ["STOP"], "maxOutputTokens": 16, "responseModalities": ["TEXT"]}})201    call("c_system_role_system", "POST", gc(LITE), {"systemInstruction": {"role": "system", "parts": [{"text": "Answer with OK."}]}, "contents": U("Hi"), "generationConfig": {"maxOutputTokens": 8}})202    call("c_system_instruction_snake", "POST", gc(LITE), {"system_instruction": {"parts": [{"text": "Answer with OK."}]}, "contents": U("Hi"), "generation_config": {"max_output_tokens": 8}})203    call("c_candidate_count_2", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"candidateCount": 2, "maxOutputTokens": 8}})204    call("c_logprobs", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"responseLogprobs": True, "logprobs": 2, "maxOutputTokens": 8}})205    call("c_penalties", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"presencePenalty": 0.5, "frequencyPenalty": 0.5, "maxOutputTokens": 8}})206    call("c_temperature_out_of_range", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"temperature": 2.5, "maxOutputTokens": 8}})207    call("c_stop_sequences_6", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"stopSequences": ["a", "b", "c", "d", "e", "f"], "maxOutputTokens": 8}})208    call("c_service_tier_standard", "POST", gc(LITE), {"contents": U("Reply with OK."), "serviceTier": "standard", "generationConfig": {"maxOutputTokens": 8}})209    call("c_service_tier_bogus", "POST", gc(LITE), {"contents": U("Reply with OK."), "serviceTier": "turbo", "generationConfig": {"maxOutputTokens": 8}})210    call("c_labels_store", "POST", gc(LITE), {"contents": U("Reply with OK."), "labels": {"env": "atlas-probe"}, "store": False, "generationConfig": {"maxOutputTokens": 8}})211    call("c_unknown_field", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "bogusField": 1}})212    call("c_media_resolution_text_only", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "mediaResolution": "MEDIA_RESOLUTION_LOW"}})213    call("c_enhanced_civic", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "enableEnhancedCivicAnswers": True}})214    call("c_max_tokens_hit", "POST", gc(LITE), {"contents": U("Write a 300 word essay about rivers."), "generationConfig": {"maxOutputTokens": 8}})215    # ---- (d) structured output216    schema = {"type": "OBJECT", "properties": {"ok": {"type": "BOOLEAN"}, "word": {"type": "STRING"}}, "required": ["ok", "word"], "propertyOrdering": ["word", "ok"]}217    call("d_json_response_schema", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 32, "responseMimeType": "application/json", "responseSchema": schema}})218    jschema = {"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": {"ok": {"type": "boolean"}, "word": {"type": ["string", "null"], "minLength": 1}}, "required": ["ok", "word"], "additionalProperties": False}219    call("d_json_response_json_schema", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 32, "responseMimeType": "application/json", "responseJsonSchema": jschema}})220    call("d_response_format_new", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 32, "responseFormat": {"text": {"mimeType": "APPLICATION_JSON", "schema": {"type": "object", "properties": {"ok": {"type": "boolean"}}, "required": ["ok"]}}}}})221    call("d_enum_mode", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "responseMimeType": "text/x.enum", "responseSchema": {"type": "STRING", "enum": ["OK", "NOT_OK"]}}})222    call("d_json_no_schema", "POST", gc(LITE), {"contents": U("Return a JSON object with key ok=true."), "generationConfig": {"maxOutputTokens": 32, "responseMimeType": "application/json"}})223    call("d_schema_unknown_keyword", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "responseMimeType": "application/json", "responseSchema": {"type": "OBJECT", "properties": {"ok": {"type": "BOOLEAN"}}, "additionalProperties": False}}})224    call("d_schema_without_mime", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "responseSchema": schema}})225    call("d_both_schemas", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "responseMimeType": "application/json", "responseSchema": schema, "responseJsonSchema": jschema}})226    call("d_bad_mime", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8, "responseMimeType": "text/html"}})227    stream_call("d_stream_json", f"/v1beta/models/{LITE}:streamGenerateContent?alt=sse", {"contents": U("List three colors."), "generationConfig": {"maxOutputTokens": 32, "responseMimeType": "application/json", "responseJsonSchema": {"type": "object", "properties": {"colors": {"type": "array", "items": {"type": "string"}}}, "required": ["colors"]}}})228    # ---- (e) thinking229    call("e_flash_thoughts_budget", "POST", gc(FLASH), {"contents": U("What is 17*23? Reply with the number only."), "generationConfig": {"maxOutputTokens": 600, "thinkingConfig": {"includeThoughts": True, "thinkingBudget": 256}}}, model=FLASH)230    call("e_g38_level_low", "POST", gc(G38), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 64, "thinkingConfig": {"thinkingLevel": "low", "includeThoughts": True}}}, model=G38)231    call("e_g38_level_high", "POST", gc(G38), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 256, "thinkingConfig": {"thinkingLevel": "high"}}}, model=G38)232    call("e_g38_level_minimal_unsupported", "POST", gc(G38), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 16, "thinkingConfig": {"thinkingLevel": "minimal"}}}, model=G38)233    call("e_pro_budget_0", "POST", gc(PRO), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 16, "thinkingConfig": {"thinkingBudget": 0}}}, model=PRO)234    call("e_level_and_budget_conflict", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 16, "thinkingConfig": {"thinkingLevel": "low", "thinkingBudget": 100}}})235    call("e_lite_budget_0", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 16, "thinkingConfig": {"thinkingBudget": 0}}})236    call("e_lite_budget_minus1", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 64, "thinkingConfig": {"thinkingBudget": -1, "includeThoughts": True}}})237    call("e_lite_level_minimal", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 16, "thinkingConfig": {"thinkingLevel": "minimal"}}})238    call("e_lite_level_bogus", "POST", gc(LITE), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 16, "thinkingConfig": {"thinkingLevel": "extreme"}}})239    call("e_embedding_model_thinking", "POST", gc("gemini-embedding-001"), {"contents": U("Reply with OK."), "generationConfig": {"maxOutputTokens": 8}}, model="gemini-embedding-001")240    stream_call("e_stream_thoughts", f"/v1beta/models/{FLASH}:streamGenerateContent?alt=sse", {"contents": U("What is 19*21? Reply with the number only."), "generationConfig": {"maxOutputTokens": 500, "thinkingConfig": {"includeThoughts": True, "thinkingBudget": 128}}}, model=FLASH)241    # ---- (f) safety242    cats = ["HARM_CATEGORY_HARASSMENT", "HARM_CATEGORY_HATE_SPEECH", "HARM_CATEGORY_SEXUALLY_EXPLICIT", "HARM_CATEGORY_DANGEROUS_CONTENT"]243    call("f_safety_block_low", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": c, "threshold": "BLOCK_LOW_AND_ABOVE"} for c in cats], "generationConfig": {"maxOutputTokens": 8}})244    call("f_safety_block_none", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": c, "threshold": "BLOCK_NONE"} for c in cats], "generationConfig": {"maxOutputTokens": 8}})245    call("f_safety_off", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": c, "threshold": "OFF"} for c in cats], "generationConfig": {"maxOutputTokens": 8}})246    call("f_safety_civic", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_LOW_AND_ABOVE"}], "generationConfig": {"maxOutputTokens": 8}})247    call("f_safety_jailbreak", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": "HARM_CATEGORY_JAILBREAK", "threshold": "BLOCK_LOW_AND_ABOVE"}], "generationConfig": {"maxOutputTokens": 8}})248    call("f_safety_palm_category", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": "HARM_CATEGORY_TOXICITY", "threshold": "BLOCK_LOW_AND_ABOVE"}], "generationConfig": {"maxOutputTokens": 8}})249    call("f_safety_duplicate", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": cats[0], "threshold": "BLOCK_NONE"}, {"category": cats[0], "threshold": "OFF"}], "generationConfig": {"maxOutputTokens": 8}})250    call("f_safety_bogus", "POST", gc(LITE), {"contents": U("Reply with OK."), "safetySettings": [{"category": "HARM_CATEGORY_FOO", "threshold": "BLOCK_NONE"}], "generationConfig": {"maxOutputTokens": 8}})251    # ---- (g) countTokens252    ct = f"/v1beta/models/{LITE}:countTokens"253    call("g_count_text", "POST", ct, {"contents": U("The quick brown fox jumps over the lazy dog.")})254    call("g_count_wrapper_system_tools", "POST", ct, {"generateContentRequest": {"model": f"models/{LITE}", "contents": U("What is the weather in Paris?"), "systemInstruction": {"parts": [{"text": "You are terse."}]},255                                                                                "tools": [{"functionDeclarations": [{"name": "get_weather", "description": "Get weather", "parameters": {"type": "OBJECT", "properties": {"city": {"type": "STRING"}}, "required": ["city"]}}]}]}})256    call("g_count_wrapper_model_mismatch", "POST", ct, {"generateContentRequest": {"model": f"models/{FLASH}", "contents": U("hi")}})257    call("g_count_top_level_system", "POST", ct, {"contents": U("hi"), "systemInstruction": {"parts": [{"text": "You are terse."}]}})258    call("g_count_both", "POST", ct, {"contents": U("hi"), "generateContentRequest": {"model": f"models/{LITE}", "contents": U("hello there")}})259    call("g_count_png", "POST", ct, {"contents": [{"role": "user", "parts": [{"text": "Describe."}, {"inlineData": {"mimeType": "image/png", "data": png_1x1()}}]}]})260    call("g_count_png_low_res", "POST", ct, {"generateContentRequest": {"model": f"models/{LITE}", "contents": [{"role": "user", "parts": [{"text": "Describe."}, {"inlineData": {"mimeType": "image/png", "data": png_1x1()}}]}], "generationConfig": {"mediaResolution": "MEDIA_RESOLUTION_LOW"}}})261    call("g_count_png_part_res_high", "POST", ct, {"contents": [{"role": "user", "parts": [{"text": "Describe."}, {"inlineData": {"mimeType": "image/png", "data": png_1x1()}, "mediaResolution": {"level": "MEDIA_RESOLUTION_HIGH"}}]}]})262    call("g_count_youtube", "POST", ct, {"contents": [{"role": "user", "parts": [{"text": "Summarize."}, {"fileData": {"fileUri": "https://www.youtube.com/watch?v=9hE5-98ZeCg"}}]}]})263    call("g_count_youtube_clip", "POST", ct, {"contents": [{"role": "user", "parts": [{"text": "Summarize."}, {"fileData": {"fileUri": "https://www.youtube.com/watch?v=9hE5-98ZeCg", "mimeType": "video/*"}, "videoMetadata": {"startOffset": "0s", "endOffset": "10s", "fps": 1}}]}]})264    call("g_count_empty", "POST", ct, {})265    call("g_count_flash", "POST", f"/v1beta/models/{FLASH}:countTokens", {"contents": U("The quick brown fox jumps over the lazy dog.")}, model=FLASH)266    # ---- (h) embeddings267    call("h_embed_001", "POST", "/v1beta/models/gemini-embedding-001:embedContent", {"content": {"parts": [{"text": "Hello world"}]}, "taskType": "SEMANTIC_SIMILARITY", "outputDimensionality": 64}, model="gemini-embedding-001")268    call("h_embed_001_default_dim", "POST", "/v1beta/models/gemini-embedding-001:embedContent", {"content": {"parts": [{"text": "Hello world"}]}}, model="gemini-embedding-001")269    call("h_embed_001_title", "POST", "/v1beta/models/gemini-embedding-001:embedContent", {"content": {"parts": [{"text": "Hello world"}]}, "taskType": "RETRIEVAL_DOCUMENT", "title": "Greeting", "outputDimensionality": 64}, model="gemini-embedding-001")270    call("h_embed_2", "POST", "/v1beta/models/gemini-embedding-2:embedContent", {"content": {"parts": [{"text": "task: classification | query: Hello world"}]}, "outputDimensionality": 64}, model="gemini-embedding-2")271    call("h_embed_2_config", "POST", "/v1beta/models/gemini-embedding-2:embedContent", {"content": {"parts": [{"text": "Hello world"}]}, "embedContentConfig": {"outputDimensionality": 64, "autoTruncate": True}}, model="gemini-embedding-2")272    call("h_embed_2_tasktype", "POST", "/v1beta/models/gemini-embedding-2:embedContent", {"content": {"parts": [{"text": "Hello world"}]}, "taskType": "SEMANTIC_SIMILARITY", "outputDimensionality": 64}, model="gemini-embedding-2")273    call("h_embed_2_multipart_aggregate", "POST", "/v1beta/models/gemini-embedding-2:embedContent", {"content": {"parts": [{"text": "A red pixel"}, {"inlineData": {"mimeType": "image/png", "data": png_1x1()}}]}, "outputDimensionality": 64}, model="gemini-embedding-2")274    call("h_embed_dim_too_big", "POST", "/v1beta/models/gemini-embedding-001:embedContent", {"content": {"parts": [{"text": "Hello"}]}, "outputDimensionality": 4096}, model="gemini-embedding-001")275    call("h_batch_embed_001", "POST", "/v1beta/models/gemini-embedding-001:batchEmbedContents", {"requests": [{"model": "models/gemini-embedding-001", "content": {"parts": [{"text": "Hello"}]}, "outputDimensionality": 64}, {"model": "models/gemini-embedding-001", "content": {"parts": [{"text": "World"}]}, "outputDimensionality": 64}]}, model="gemini-embedding-001")276    call("h_batch_embed_no_model", "POST", "/v1beta/models/gemini-embedding-001:batchEmbedContents", {"requests": [{"content": {"parts": [{"text": "Hello"}]}, "outputDimensionality": 64}]}, model="gemini-embedding-001")277    call("h_embed_with_lite", "POST", f"/v1beta/models/{LITE}:embedContent", {"content": {"parts": [{"text": "Hello"}]}})278    st, op, _ = call("h_async_batch_embed", "POST", "/v1beta/models/gemini-embedding-001:asyncBatchEmbedContent", {"batch": {"displayName": "atlas-core-probe", "inputConfig": {"requests": {"requests": [{"request": {"content": {"parts": [{"text": "Hello"}]}, "outputDimensionality": 64}, "metadata": {"k": "1"}}, {"request": {"content": {"parts": [{"text": "World"}]}, "outputDimensionality": 64}, "metadata": {"k": "2"}}]}}}}, model="gemini-embedding-001")279    if st == 200 and isinstance(op, dict) and op.get("name"):280        time.sleep(3)281        call("h_async_batch_embed_get", "GET", f"/v1beta/{op['name']}", None)282    # ---- (i) cachedContents283    filler = " ".join(f"Section {i}: The Atlas reference corpus documents the public API surface of large language model providers, including endpoints, parameters, streaming events, objects, errors and pricing, verified experimentally with minimal payloads and careful cost control." for i in range(1, 121))284    cc = {"model": f"models/{LITE}", "displayName": "atlas-core-probe", "contents": [{"role": "user", "parts": [{"text": filler}]}], "systemInstruction": {"parts": [{"text": "You answer with the single word OK."}]}, "ttl": "120s"}285    st, cache, _ = call("i_cache_create", "POST", "/v1beta/cachedContents", cc)286    cache_name = cache.get("name") if st == 200 else None287    if cache_name:288        call("i_cache_generate", "POST", gc(LITE), {"contents": U("Reply with OK."), "cachedContent": cache_name, "generationConfig": {"maxOutputTokens": 8}})289        call("i_cache_count_tokens", "POST", ct, {"generateContentRequest": {"model": f"models/{LITE}", "contents": U("Reply with OK."), "cachedContent": cache_name}})290        call("i_cache_generate_wrong_model", "POST", gc(FLASH), {"contents": U("Reply with OK."), "cachedContent": cache_name, "generationConfig": {"maxOutputTokens": 8}}, model=FLASH)291        call("i_cache_generate_with_system", "POST", gc(LITE), {"contents": U("Reply with OK."), "cachedContent": cache_name, "systemInstruction": {"parts": [{"text": "x"}]}, "generationConfig": {"maxOutputTokens": 8}})292        call("i_cache_list", "GET", "/v1beta/cachedContents?pageSize=5", None)293        call("i_cache_get", "GET", f"/v1beta/{cache_name}", None)294        call("i_cache_patch_ttl", "PATCH", f"/v1beta/{cache_name}", {"ttl": "300s"})295        call("i_cache_patch_display_name", "PATCH", f"/v1beta/{cache_name}", {"displayName": "renamed"})296        call("i_cache_patch_update_mask", "PATCH", f"/v1beta/{cache_name}?updateMask=ttl", {"ttl": "240s"})297        call("i_cache_delete", "DELETE", f"/v1beta/{cache_name}", None)298        call("i_cache_get_after_delete", "GET", f"/v1beta/{cache_name}", None)299    call("i_cache_below_min", "POST", "/v1beta/cachedContents", {"model": f"models/{LITE}", "contents": U("Too small to cache."), "ttl": "60s"})300    call("i_cache_expire_time_form", "POST", "/v1beta/cachedContents", {"model": f"models/{LITE}", "contents": U("Too small to cache."), "expireTime": "2030-01-01T00:00:00Z"})301    call("i_cache_no_model", "POST", "/v1beta/cachedContents", {"contents": U("x")})302    # ---- (j) files303    data = b"The secret word in this Atlas probe text file is PELICAN. " * 3304    data = data[:200]305    st, start_body, start_hdrs = live.gemini_request("POST", "/upload/v1beta/files", {"file": {"displayName": "atlas-probe.txt"}},306                                                     extra_headers={"X-Goog-Upload-Protocol": "resumable", "X-Goog-Upload-Command": "start", "X-Goog-Upload-Header-Content-Length": str(len(data)), "X-Goog-Upload-Header-Content-Type": "text/plain"}, note="core-probe j_upload_start")307    lower = {k.lower(): v for k, v in start_hdrs.items()}308    upload_url = lower.get("x-goog-upload-url")309    print(f"[j_upload_start] -> {st} upload_url={'yes' if upload_url else 'no'} hdrs={[k for k in lower if k.startswith('x-goog-upload')]} body={start_body!r}"[:300])310    live.save_sanitized({"status": st, "body": start_body if not isinstance(start_body, bytes) else start_body.decode(), "upload_headers": {k: ("<url-redacted>" if k == "x-goog-upload-url" else v) for k, v in lower.items() if k.startswith("x-goog-upload")}}, OUT / "j_upload_start.json")311    SUMMARY["j_upload_start"] = {"status": st, "upload_headers": [k for k in lower if k.startswith("x-goog-upload")]}312    file_name = file_uri = None313    if upload_url:314        # upload URL is absolute; strip base315        path = upload_url.replace(live.GEMINI_BASE, "")316        st, fb, fh = call("j_upload_finalize", "POST", path, None, data=data, content_type="text/plain", extra_headers={"X-Goog-Upload-Offset": "0", "X-Goog-Upload-Command": "upload, finalize", "Content-Length": str(len(data))})317        if st == 200:318            file_name, file_uri = fb["file"]["name"], fb["file"]["uri"]319            SUMMARY["j_upload_finalize"]["upload_status_header"] = {k: v for k, v in fh.items() if k.lower().startswith("x-goog-upload")}320    if file_name:321        call("j_files_get", "GET", f"/v1beta/{file_name}", None)322        call("j_files_list", "GET", "/v1beta/files?pageSize=3", None)323        call("j_generate_with_file", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "What is the secret word in the file? One word."}, {"fileData": {"fileUri": file_uri, "mimeType": "text/plain"}}]}], "generationConfig": {"maxOutputTokens": 8}})324        call("j_generate_with_file_no_mime", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "Reply with OK."}, {"fileData": {"fileUri": file_uri}}]}], "generationConfig": {"maxOutputTokens": 8}})325        call("j_files_delete", "DELETE", f"/v1beta/{file_name}", None)326        call("j_files_get_after_delete", "GET", f"/v1beta/{file_name}", None)327        call("j_generate_with_deleted_file", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "Reply with OK."}, {"fileData": {"fileUri": file_uri, "mimeType": "text/plain"}}]}], "generationConfig": {"maxOutputTokens": 8}})328    # multipart (simple) upload of a tiny PDF329    pdf = minimal_pdf("ATLAS")330    boundary = "atlas_boundary_7f3"331    meta = json.dumps({"file": {"displayName": "atlas-probe.pdf"}}).encode()332    mp = (f"--{boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n".encode() + meta + f"\r\n--{boundary}\r\nContent-Type: application/pdf\r\n\r\n".encode() + pdf + f"\r\n--{boundary}--\r\n".encode())333    st, pb, _ = call("j_upload_multipart_pdf", "POST", "/upload/v1beta/files?uploadType=multipart", None, data=mp, content_type=f"multipart/related; boundary={boundary}")334    if st == 200:335        pname, puri, pstate = pb["file"]["name"], pb["file"]["uri"], pb["file"].get("state")336        for _ in range(10):337            if pstate == "ACTIVE":338                break339            time.sleep(2)340            st2, g, _ = live.gemini_request("GET", f"/v1beta/{pname}", note="core-probe j_pdf_poll")341            pstate = g.get("state") if isinstance(g, dict) else None342        SUMMARY["j_pdf_state_after_poll"] = pstate343        call("j_generate_with_pdf", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"text": "Which single word is printed in this PDF? Reply with that word only."}, {"fileData": {"fileUri": puri, "mimeType": "application/pdf"}}]}], "generationConfig": {"maxOutputTokens": 8}})344        call("j_count_pdf", "POST", ct, {"contents": [{"role": "user", "parts": [{"text": "x"}, {"fileData": {"fileUri": puri, "mimeType": "application/pdf"}}]}]})345        call("j_files_delete_pdf", "DELETE", f"/v1beta/{pname}", None)346    call("j_files_register", "POST", "/v1beta/files:register", {"uris": ["gs://cloud-samples-data/generative-ai/image/scones.jpg"]})347    call("j_files_get_missing", "GET", "/v1beta/files/does-not-exist-atlas", None)348    call("j_generated_files_list", "GET", "/v1beta/generatedFiles?pageSize=3", None)349    call("j_upload_simple_raw", "POST", "/upload/v1beta/files?uploadType=media", None, data=b"hello raw upload", content_type="text/plain")350    # ---- (k) multimodal inline351    call("k_inline_png", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"inlineData": {"mimeType": "image/png", "data": png_1x1()}}, {"text": "Reply with OK."}]}], "generationConfig": {"maxOutputTokens": 8}})352    call("k_inline_png_low_res", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"inlineData": {"mimeType": "image/png", "data": png_1x1()}}, {"text": "Reply with OK."}]}], "generationConfig": {"maxOutputTokens": 8, "mediaResolution": "MEDIA_RESOLUTION_LOW"}})353    call("k_inline_bad_mime", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"inlineData": {"mimeType": "image/bmp", "data": png_1x1()}}, {"text": "Reply with OK."}]}], "generationConfig": {"maxOutputTokens": 8}})354    call("k_inline_pdf", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"inlineData": {"mimeType": "application/pdf", "data": base64.b64encode(minimal_pdf("ORCHID")).decode()}}, {"text": "Which single word is printed? Reply with that word only."}]}], "generationConfig": {"maxOutputTokens": 8}})355    call("k_inline_text_plain", "POST", gc(LITE), {"contents": [{"role": "user", "parts": [{"inlineData": {"mimeType": "text/plain", "data": base64.b64encode(b"The code word is FALCON.").decode()}}, {"text": "What is the code word? One word."}]}], "generationConfig": {"maxOutputTokens": 8}})356    # ---- (l) legacy PaLM + dynamic + misc errors357    call("l_generateText_bison", "POST", "/v1beta/models/text-bison-001:generateText", {"prompt": {"text": "Reply with OK."}, "maxOutputTokens": 8})358    call("l_generateText_lite", "POST", f"/v1beta/models/{LITE}:generateText", {"prompt": {"text": "Reply with OK."}, "maxOutputTokens": 8})359    call("l_generateMessage_chat_bison", "POST", "/v1beta/models/chat-bison-001:generateMessage", {"prompt": {"messages": [{"content": "Reply with OK."}]}})360    call("l_embedText_gecko", "POST", "/v1beta/models/embedding-gecko-001:embedText", {"text": "hello"})361    call("l_embedText_001", "POST", "/v1beta/models/gemini-embedding-001:embedText", {"text": "hello"})362    call("l_countTextTokens_001", "POST", "/v1beta/models/gemini-embedding-001:countTextTokens", {"prompt": {"text": "hello world"}})363    call("l_countTextTokens_lite", "POST", f"/v1beta/models/{LITE}:countTextTokens", {"prompt": {"text": "hello world"}})364    call("l_countMessageTokens_lite", "POST", f"/v1beta/models/{LITE}:countMessageTokens", {"prompt": {"messages": [{"content": "hello"}]}})365    call("l_generateAnswer_aqa", "POST", "/v1beta/models/aqa:generateAnswer", {"contents": U("What color is the sky?"), "answerStyle": "ABSTRACTIVE", "inlinePassages": {"passages": [{"id": "p1", "content": {"parts": [{"text": "The sky is blue on a clear day."}]}}]}}, model="aqa")366    call("l_dynamic_default", "POST", "/v1beta/dynamic/default:generateContent", OK)367    call("l_dynamic_lite", "POST", f"/v1beta/dynamic/{LITE}:generateContent", OK)368    call("l_model_not_found", "POST", gc("gemini-99-ultra"), OK)369    call("l_missing_contents", "POST", gc(LITE), {"generationConfig": {"maxOutputTokens": 8}})370    call("l_empty_parts", "POST", gc(LITE), {"contents": [{"role": "user", "parts": []}]})371    call("l_models_get_lite", "GET", f"/v1beta/models/{LITE}", None)372    call("l_v1_cached_contents", "GET", "/v1/cachedContents?pageSize=1", None)373    call("l_v1_files", "GET", "/v1/files?pageSize=1", None)374    live.save_sanitized(SUMMARY, OUT / "summary.json")375    print(f"\nTOTAL est cost ${TOTAL_COST:.5f} over {len(SUMMARY)} probes")376377378if __name__ == "__main__":379    main()380