Search
const INSTRUCTIONS = ` Greet the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{model}} model. Give them a very brief summary of the benefits of the Realtime API based on the details below, When the user says goodbye or you think the call is over, say a brief goodbye and then invoke the end_call function. --- The new ${MODEL} model is the first Realtime API model with built-in reasoning capability, which makes it more accurate and reliable than ever. Tool calling is much more robust, including a major reduction in hallucinated tool calls.`;const HANGUP_TOOL = { type: "function", name: "end_call", description: ` Use this function to hang up the call when the user says goodbye or otherwise indicates they are about to end the call.`,// Builds the declarative session configuration for a Realtime API session.export function makeSession(modelOverride?: string) { const model = modelOverride || MODEL;// AgentHandler implements the agent runtime behavior (tool calling, etc).class AgentHandler implements ObserverHandler {// Creates the runtime handler for the session.export function createHandler(client: ObserverClient, callId: string) { return new AgentHandler(client, callId);You can chat with llms over email, the email thread functions as memory. The biggest thing is that you can instantly create a chatlike interface with llms. Pair that with back end data and functions and you got something really powerful.### Toolings* Llms can uses [tools](https://platform.openai.com/docs/guides/function-calling), meaning you can make this an agent and a whole lot more useful. import { OpenAI } from "npm:openai";const openai = new OpenAI();const functionExpression = await openai.chat.completions.create({ "messages": [ ], "functions": [ {});console.log(functionExpression);// TODO pull out function call and initial messagelet args = functionExpression.choices[0].message.function_call.arguments;let functionCallResult = { "temperature": "22", "unit": "celsius", "description": "Sunny" };const result = await openai.chat.completions.create({ "messages": [ "content": null, "function_call": { "name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}" }, }, { "role": "function", "name": "get_current_weather", "content": JSON.stringify(functionCallResult), }, ], "functions": [ {Migrated from folder: External_APIs/openai/function_calling/gpt4FunctionCallingExampleBuilt on the OpenAI Realtime API (WebRTC) with server-side function calling.Mashes up patterns from B[Browser] -->|SDP offer| RTC[/rtc/] RTC -->|WebRTC| OAI[OpenAI Realtime API] RTC -->|spawn| OBS[/observer/]- **Browser** opens a WebRTC peer connection to OpenAI via `/rtc`. The dashboard itself renders inside a sandboxed iframe pointing at `/api/view`.- **`/rtc`** forwards the SDP offer to OpenAI and kicks off the observer for the resulting call id (fire-and-forget).- **`/observer/:callId`** opens a server-side websocket to the same Realtime session. When the model emits `response.function_call_arguments.done`, the observer dispatches the tool against SQLite and posts a `function_call_output` back, then triggers a new response.- **Frontend polls `/api/current`** every 1.5s. When the active view name or│ ├── api.ts # REST for views/versions/rules│ └── utils.ts # OpenAI URL/header helpers└── database/1. Set `OPENAI_API_KEY` in this val's env (the GA Realtime API key from [platform.openai.com](https://platform.openai.com)).2. Open the val's HTTP endpoint. The Welcome view is seeded on first request.// elaine/contracts.py's own recursive vocabulary, serialized by// elaine/api/catalog.py::_value_contract_record()) into an OpenAI/Mistral// function-calling-compatible JSON Schema, so a tool's `parameters` are// derived from the live catalog contract instead of hand-written text that// MEAN_TOOL (teaching/plainChat.ts) doesn't do today, and what this is for.export function valueContractToJsonSchema(contract: ValueContractRecord): Record<string, unknown> { if (contract.kind === "scalar") {export function buildToolDefinitionFromCatalog( toolName: string,Val Town's standard-library integrations — `std/openai`, `std/fetch` — are not platform magic. Each one is just **two vals**:1. **An SDK val** (a plain script val) that users import via `https://esm.town/v/...`. It wraps your API in a nice function and authenticates the *user* to your proxy.2. **A proxy val** (an HTTP val) that verifies who's calling, applies limits, attaches **your** provider API key, and forwards the request upstream.Anyone can ship one of these from a regular Val Town account. No changes to Val Town itself are needed — which means a partner can ship a sponsored function (e.g. `sent-dm/sms`) entirely in userspace.The SDK's only jobs: expose a clean function, and pass along the caller's identity. Every Val Town user has a `valtown` environment variable containing their own API token — the SDK sends it as a bearer token to your proxy. The user never sees or needs *your* upstream API key.export async function doTheThing(input: string): Promise<Result> { const token = Deno.env.get("valtown");- **`std/fetch` style** — a plain function, as above. Best for simple APIs.- **`std/openai` style** — subclass an existing SDK and point its `baseURL` at your proxy, so users get the full upstream SDK surface for free:```tsimport { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";export class OpenAI extends RawOpenAI { constructor(options: ClientOptions = {}) { ...options, baseURL: "https://std-openaiproxy.web.val.run/v1", apiKey: Deno.env.get("valtown"),```tsasync function verifyToken(token: string): Promise<{ id: string; username: string; tier: string } | null> { const res = await fetch("https://api.val.town/v1/me", {export default async function handler(req: Request): Promise<Response> { const authHeader = req.headers.get("Authorization");export async function checkRateLimit(userId: string, max: number, windowSeconds: number) { const result = await sqlite.execute({export async function logUsage(userId: string, username: string, statusCode: number | null) { await sqlite.execute({Tips from `std/openaiproxy`, which serves every Val Town user:async function forward(req: Request, pathname: string): Promise<Response> { if (!allowedPathnames.includes(pathname)) {- **Strip identifying headers** from the upstream response before returning it.- **Validate/rewrite the request body** if some parameters shouldn't be user-controllable (e.g. `std/openaiproxy` rewrites `model` for free-tier users).```tsexport default async function handler(req: Request): Promise<Response> { try {const INSTRUCTIONS = ` Greet the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{model}} model. Give them a very brief summary of the benefits of the Realtime API based on the details below, When the user says goodbye or you think the call is over, say a brief goodbye and then invoke the end_call function. --- The new ${MODEL} model is the first Realtime API model with built-in reasoning capability, which makes it more accurate and reliable than ever. Tool calling is much more robust, including a major reduction in hallucinated tool calls.`;const HANGUP_TOOL = { type: "function", name: "end_call", description: ` Use this function to hang up the call when the user says goodbye or otherwise indicates they are about to end the call.`,// Builds the declarative session configuration for a Realtime API session.export function makeSession(modelOverride?: string) { const model = modelOverride || MODEL;// AgentHandler implements the agent runtime behavior (tool calling, etc).class AgentHandler implements ObserverHandler {// Creates the runtime handler for the session.export function createHandler(client: ObserverClient, callId: string) { return new AgentHandler(client, callId);const INSTRUCTIONS = ` Greet the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{model}} model. Give them a very brief summary of the benefits of the Realtime API based on the details below, When the user says goodbye or you think the call is over, say a brief goodbye and then invoke the end_call function. --- The new ${MODEL} model is the first Realtime API model with built-in reasoning capability, which makes it more accurate and reliable than ever. Tool calling is much more robust, including a major reduction in hallucinated tool calls.`;const HANGUP_TOOL = { type: "function", name: "end_call", description: ` Use this function to hang up the call when the user says goodbye or otherwise indicates they are about to end the call.`,// Builds the declarative session configuration for a Realtime API session.export function makeSession(modelOverride?: string) { const model = modelOverride || MODEL;// AgentHandler implements the agent runtime behavior (tool calling, etc).class AgentHandler implements ObserverHandler {// Creates the runtime handler for the session.export function createHandler(client: ObserverClient, callId: string) { return new AgentHandler(client, callId);// Configure the timer with the 🕒 icon in the top right.// Sends a "good morning" email with a weather update using OpenAI tool calling.import { OpenAI } from "https://esm.town/v/std/openai";import { email } from "https://esm.town/v/std/email";const openai = new OpenAI();async function getCurrentWeather(location: string): Promise<string> { const WEATHER_API_KEY = process.env.WEATHER_API_KEY ?? "";async function sendEmail(subject: string, content: string): Promise<string> { await email({ subject, text: content });// --- Tool definitions for OpenAI ---const tools: OpenAI["beta"]["chat"]["completions"]["parse"] extends never ? never : any[] = [ { type: "function", function: { name: "get_current_weather", { type: "function", function: { name: "send_email",async function runAgent(userMessage: string) { const messages: any[] = [ // Loop until the model stops calling tools for (let i = 0; i < 5; i++) { const response = await openai.chat.completions.create({ model: "gpt-4o-mini", for (const toolCall of assistantMessage.tool_calls) { const args = JSON.parse(toolCall.function.arguments); let result: string; switch (toolCall.function.name) { case "get_current_weather": default: result = `Unknown tool: ${toolCall.function.name}`; } console.log(`Tool [${toolCall.function.name}]:`, result);export default async function scheduledHandler() { await runAgent(Built on the OpenAI Realtime API (WebRTC) with server-side function calling.Mashes up patterns from B[Browser] -->|SDP offer| RTC[/rtc/] RTC -->|WebRTC| OAI[OpenAI Realtime API] RTC -->|spawn| OBS[/observer/]- **Browser** opens a WebRTC peer connection to OpenAI via `/rtc`. The dashboard itself renders inside a sandboxed iframe pointing at `/api/view`.- **`/rtc`** forwards the SDP offer to OpenAI and kicks off the observer for the resulting call id (fire-and-forget).- **`/observer/:callId`** opens a server-side websocket to the same Realtime session. When the model emits `response.function_call_arguments.done`, the observer dispatches the tool against SQLite and posts a `function_call_output` back, then triggers a new response.- **Frontend polls `/api/current`** every 1.5s. When the active view name or│ ├── api.ts # REST for views/versions/rules│ └── utils.ts # OpenAI URL/header helpers└── database/1. Set `OPENAI_API_KEY` in this val's env (the GA Realtime API key from [platform.openai.com](https://platform.openai.com)).2. Open the val's HTTP endpoint. The Welcome view is seeded on first request.# OpenAI-Compatible API Proxy v2.0A production-ready, non-streaming backend that exposes OpenAI-compatible endpoints for both **OpenAI** and **Anthropic Claude** models — including full **tool_calls / function calling** support.OpenAI models work out-of-the-box via Val Town's built-in proxy (no key needed).### `GET /api/v1/models`Returns all available models in OpenAI format. No auth required.### OpenAI models (built-in, no key needed)### Non-streaming chat (OpenAI)```bash### Tool calling (Claude)```bash "tools": [{ "type": "function", "function": { "name": "get_weather",### Multi-turn tool calling (returning tool result)```bash {"role": "user", "content": "Whats the weather in Beijing?"}, {"role": "assistant", "content": null, "tool_calls": [{"id": "call_123", "type": "function", "function": {"name": "get_weather", "arguments": "{\"city\":\"Beijing\"}"}}]}, {"role": "tool", "tool_call_id": "call_123", "content": "{\"temp\": 22, \"condition\": \"Sunny\"}"}### Python (OpenAI SDK)```pythonfrom openai import OpenAIclient = OpenAI( api_key="any-token",tools = [{ "type": "function", "function": { "name": "get_weather",if msg.tool_calls: print("Tool called:", msg.tool_calls[0].function.name) print("Arguments:", msg.tool_calls[0].function.arguments)```- **Streaming is disabled** — set `stream: false` or omit it entirely.- **tool_choice mapping**: `"auto"` → Anthropic `auto`, `"required"` → Anthropic `any`, `{"type":"function","function":{"name":"..."}}` → Anthropic `tool` type.- **system messages** are correctly lifted to Anthropic's top-level `system` parameter.const INSTRUCTIONS = ` Greet the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{model}} model. Give them a very brief summary of the benefits of the Realtime API based on the details below, When the user says goodbye or you think the call is over, say a brief goodbye and then invoke the end_call function. --- The new gpt-realtime-1.5 model offers more reliable instruction following, tool calling, and multilingual accuracy. Specifically, the model delivers a +5% intelligence lift on Big Bench Audio, which measures reasoning ability, as well as +10.23% on alphanumeric transcription and +7% on instruction following in internal evals.const HANGUP_TOOL = { type: "function", name: "end_call", description: ` Use this function to hang up the call when the user says goodbye or otherwise indicates they are about to end the call.`,// Builds the declarative session configuration for a Realtime API session.export function makeSession(modelOverride?: string) { const model = modelOverride || MODEL;// AgentHandler implements the agent runtime behavior (tool calling, etc).class AgentHandler implements ObserverHandler {// Creates the runtime handler for the session.export function createHandler(client: ObserverClient, callId: string) { return new AgentHandler(client, callId);const INSTRUCTIONS = ` Greet the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{model}} model. Give them a very brief summary of the benefits of the Realtime API based on the details below, When the user says goodbye or you think the call is over, say a brief goodbye and then invoke the end_call function. --- The new gpt-realtime-1.5 model offers more reliable instruction following, tool calling, and multilingual accuracy. Specifically, the model delivers a +5% intelligence lift on Big Bench Audio, which measures reasoning ability, as well as +10.23% on alphanumeric transcription and +7% on instruction following in internal evals.const HANGUP_TOOL = { type: "function", name: "end_call", description: ` Use this function to hang up the call when the user says goodbye or otherwise indicates they are about to end the call.`,// Builds the declarative session configuration for a Realtime API session.export function makeSession(modelOverride?: string) { const model = modelOverride || MODEL;// AgentHandler implements the agent runtime behavior (tool calling, etc).class AgentHandler implements ObserverHandler {// Creates the runtime handler for the session.export function createHandler(client: ObserverClient, callId: string) { return new AgentHandler(client, callId);const INSTRUCTIONS = ` Greet the user in English and tell them that they're using the OpenAI Realtime API, powered by the {{model}} model. Give them a very brief summary of the benefits of the Realtime API based on the details below, When the user says goodbye or you think the call is over, say a brief goodbye and then invoke the end_call function. --- The new gpt-realtime-1.5 model offers more reliable instruction following, tool calling, and multilingual accuracy. Specifically, the model delivers a +5% intelligence lift on Big Bench Audio, which measures reasoning ability, as well as +10.23% on alphanumeric transcription and +7% on instruction following in internal evals.const HANGUP_TOOL = { type: "function", name: "end_call", description: ` Use this function to hang up the call when the user says goodbye or otherwise indicates they are about to end the call.`,// Builds the declarative session configuration for a Realtime API session.export function makeSession(modelOverride?: string) { const model = modelOverride || MODEL;// AgentHandler implements the agent runtime behavior (tool calling, etc).class AgentHandler implements ObserverHandler {// Creates the runtime handler for the session.export function createHandler(client: ObserverClient, callId: string) { return new AgentHandler(client, callId);Vals
No vals found
Users
No users found