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%
2.9 KB · 38 lines python
Raw Blame History
1#!/usr/bin/env python32"""Interactions API basics (REST): create → stream (SSE) → chain with previous_interaction_id → GET → DELETE; store=false variant.3STATUS: LIVE_VERIFIED 2026-09-18 (gemini-3.5-flash-lite, tmp-live/gemini-tools/h1..h9*.json, < $0.001).4Observed: response keys {id, object:"interaction", status:"completed", model, created, updated, service_tier, steps[], usage};5steps = [{type:"thought", signature}, {type:"model_output", content:[{type:"text", text}]}]; SSE events in order:6interaction.created, interaction.status_update, step.start, step.delta, step.stop (per step), interaction.completed;7store=false → no `id` in the response; GET after DELETE → 404 "Requested entity was not found"; GET /v1beta/interactions (list) → 404.8Run: .venv/bin/python examples/gemini/interactions/interactions_basic.py9"""10import json, sys11from pathlib import Path12sys.path.insert(0, str(Path(__file__).resolve().parents[3]))13from scripts.live import gemini_request14MODEL = "gemini-3.5-flash-lite"1516st, r, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Reply with OK.", "generation_config": {"max_output_tokens": 16}}, note="interactions_basic.py create")17iid = r["id"]18print("create:", st, r["status"], [s["type"] for s in r["steps"]], "usage:", r["usage"]["total_tokens"])1920st, lines, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Reply with OK.", "stream": True, "previous_interaction_id": iid,21                                                              "generation_config": {"max_output_tokens": 16}}, stream=True, note="interactions_basic.py stream")22events = [json.loads(ln[5:]) for ln in lines if ln.startswith("data:") and ln[5:].strip() != "[DONE]"]   # terminal frame: event: done / data: [DONE]23print("stream:", st, [e["event_type"] for e in events])24print("  deltas:", [e["delta"] for e in events if e["event_type"] == "step.delta" and e["delta"]["type"] == "text"])25iid2 = events[-1]["interaction"]["id"]2627st, r3, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "What did I ask you first? 5 words.", "previous_interaction_id": iid2,28                                                           "generation_config": {"max_output_tokens": 24}}, note="interactions_basic.py chain")29print("chain:", st, " ".join(c["text"] for s in r3["steps"] if s["type"] == "model_output" for c in s["content"] if c["type"] == "text"))3031st, g, _ = gemini_request("GET", f"/v1beta/interactions/{iid}", note="interactions_basic.py get")32print("get:", st, sorted(g.keys()))33st, r5, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Reply with OK.", "store": False, "generation_config": {"max_output_tokens": 8}}, note="interactions_basic.py store=false")34print("store=false:", st, "id present:", "id" in r5)35for i in (iid, iid2, r3["id"]):36    st, _, _ = gemini_request("DELETE", f"/v1beta/interactions/{i}", note="interactions_basic.py delete")37    print("delete", i[:10] + "…", st)38