# ai

Language models over each provider's own HTTP API, behind one surface:
Anthropic's Messages API, OpenAI's Responses API, any OpenAI-style Chat
Completions API, and Google's Gemini API. Turns, streams (into the page, to
a fn, or out of a route as server-sent events), tools that are the
program's own typed fns, answers read into a shape, retrieval, and
`AiChat`. The whole of it is [docs/ai.md](../../docs/ai.md).

```text
use "pkg:ai/ai.mote"          // aiChat, aiStreamTo, aiRun, aiObject, aiEmbed, …
use "pkg:ai/chat.mote"        // AiChat (takes ai.mote itself)
use "pkg:ai/retrieval.mote"   // aiChunks, aiIndex, aiSearch, Pinecone, Qdrant, aiRerank
use "pkg:ai/eval.mote"        // for tests: aiSays, aiCalls, aiGrade, aiJudge

let r: Recipe = aiObject({messages: [{role: "user", text: "A soup"}]}, schema(Recipe))?
fn aiChatTools(): AiTool[] = [aiToolFor("weather", schema(weatherIn))]
fn aiTool(name: string, input: json): string      // the program's tools
```

Settings: `AI_KEY` (a secret, read by `fn aiKey(provider)`, which a program
of several providers declares itself); `AI_PROVIDER` (`anthropic`,
`responses`, `openai`, `gemini`; `bedrock` with the aws extension, `vertex`
with the gcloud extension), `AI_MODEL`, `AI_API`; `AI_RETRIES`,
`AI_CACHE`, `AI_RECORD` / `AI_REPLAY`, `AI_BUDGET_TOKENS` /
`AI_BUDGET_DOLLARS`, `AI_PRICES`; `AI_EMBED`; `PINECONE_HOST`, `QDRANT_URL`,
`AI_RERANK`. The default models are in one fn, `aiDefaultModel` in
`ai.mote`.

| File | |
|---|---|
| `ai.mote` | The shapes, the calls, sending (retries on 429 and 529), the cache, recordings, costs and budgets (`AiSpend`), embeddings. |
| `anthropic.mote` | Anthropic's request, answer and stream; batches. |
| `openai.mote` | Chat Completions and the Responses API, each plain and streamed; pictures, speech, moderation, files, batches. |
| `gemini.mote` | Gemini's request, answer and stream; files. |
| `chat.mote` | `AiChat`, `aiChatSay`, and what the program may say of it. |
| `retrieval.mote` | Chunks, the vector store (`AiVector`), Pinecone, Qdrant, reranking. |
| `eval.mote` | Answers in each provider's shape for `given net`, `aiGrade`, `aiJudge`. |
| `tests.mote` | Its tests: `use "pkg:ai/tests.mote"`, then `mote test`. |

**What is covered**, week to week: a turn, a stream, a tool loop with each
input checked against its schema, structured output, images and documents
(bytes, an address, a provider's file), tool choice, effort, thinking
shown, Anthropic's prompt caching and citations, token counts, batches
(Anthropic, OpenAI), files (all three), embeddings (OpenAI, Gemini, Voyage,
Cohere, local), pictures and speech (OpenAI), moderation (OpenAI), costs per
turn and per person per day, budgets, a response cache by the request's
hash, recorded replies, chunking, an exact vector store in the database
(fine to some tens of thousands of vectors), Pinecone, Qdrant, reranking.

**Left out on purpose**: OpenAI's Realtime API and Gemini's Live API (the
back end's WebSocket client, `ws.open`, is there now: what remains is the
sessions themselves, audio in and out, and their events, in `openai.mote`
and `gemini.mote`);
Anthropic's server tools and Managed Agents; OpenAI's Assistants API (retired
for Responses) and fine-tuning; Gemini's explicit caches and batches;
picture making at Google; providers' own tokenizers.

**Not yet run against any service**: only against stand-ins (`tests/all.sh
real_svc_ai real_svc_ai2`) and `given net`. docs/ai.md says how to check
each with a real key.
