SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
14 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.4 KB · 28 lines python
Raw Blame History
1#!/usr/bin/env python32"""Interactions API tools: `tools: [{"type":"function", …}]` + `generation_config.tool_choice` → status "requires_action" with a3function_call step → answer with a `function_result` input chained via previous_interaction_id.4STATUS: LIVE_VERIFIED 2026-09-18 (gemini-3.5-flash-lite, tmp-live/gemini-tools/h6*.json). Observed: top-level `tool_choice` → 4005"Unknown parameter 'tool_choice'" (it lives in generation_config); function_call step = {id:"call_…", type, name, arguments};6function_result must be {type, name, call_id, result:[{type:"text", text}]}; the follow-up returned steps [thought, model_output].7`{"type":"google_search"}` returned 429 RESOURCE_EXHAUSTED on this key (h7) — same restriction as generateContent googleSearch.8Run: .venv/bin/python examples/gemini/interactions/interactions_tools.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"15TOOLS = [{"type": "function", "name": "get_weather", "description": "Get the weather for a city.",16          "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}]17st, r, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Weather in Paris?", "tools": TOOLS,18                                                          "generation_config": {"tool_choice": {"allowed_tools": {"mode": "any", "tools": ["get_weather"]}}, "max_output_tokens": 64}},19                          note="interactions_tools.py function tool")20fc = next(s for s in r["steps"] if s["type"] == "function_call")21print("turn 1:", st, r["status"], json.dumps(fc))22st, r2, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "previous_interaction_id": r["id"], "tools": TOOLS,23                                                           "input": [{"type": "function_result", "name": fc["name"], "call_id": fc["id"], "result": [{"type": "text", "text": json.dumps({"temperature_c": 18})}]}],24                                                           "generation_config": {"max_output_tokens": 32}}, note="interactions_tools.py function_result")25print("turn 2:", st, r2["status"], " ".join(c["text"] for s in r2["steps"] if s["type"] == "model_output" for c in s["content"] if c["type"] == "text"))26for i in (r["id"], r2["id"]):27    gemini_request("DELETE", f"/v1beta/interactions/{i}", note="interactions_tools.py cleanup")28