Search
Code1,776
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, // Verify the webhook. const OPENAI_SIGNING_SECRET = Deno.env.get("OPENAI_SIGNING_SECRET"); if (!OPENAI_SIGNING_SECRET) { console.error("π΄ webhook secret not configured"); } const webhook = new Webhook(OPENAI_SIGNING_SECRET); const bodyStr = await c.req.text();**Hello Realtime** is a OpenAI Realtime app that supports both WebRTC and SIP(telephone) users. You can access the app via WebRTC atIf you remix the app, you'll just need to pop in your own `OPENAI_API_KEY` (from[platform.openai.com](https://platform.openai.com)), and if you want SIP, the`OPENAI_SIGNING_SECRET`. - creates WebRTC offer - `/rtc` endpoint handles SDP negotiation with OpenAI - observer established to monitor sessionconst INSTRUCTIONS = ` Greet the user in English, and thank them for trying the new OpenAI Realtime API. Give them a brief summary based on the list below, and then ask if they have any questions. - improved handling of alphanumerics (eg, properly understanding credit card and phone numbers) - support for the OpenAI Prompts API - support for MCP-based tools**Hello Realtime** is a OpenAI Realtime app that supports both WebRTC and SIP(telephone) users. You can access the app via WebRTC atIf you remix the app, you'll just need to pop in your own `OPENAI_API_KEY` (from[platform.openai.com](https://platform.openai.com)), and if you want SIP, the`OPENAI_SIGNING_SECRET`. - creates WebRTC offer - `/rtc` endpoint handles SDP negotiation with OpenAI - observer established to monitor sessionconst REALTIME_BASE_URL = "https://api.openai.com/v1/realtime";const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");if (!OPENAI_API_KEY) { throw new Error("π΄ OpenAI API key not configured");} const obj: Record<string, string> = { Authorization: `Bearer ${OPENAI_API_KEY}`, }; // Verify the webhook. const OPENAI_SIGNING_SECRET = Deno.env.get("OPENAI_SIGNING_SECRET"); if (!OPENAI_SIGNING_SECRET) { console.error("π΄ webhook secret not configured"); } const webhook = new Webhook(OPENAI_SIGNING_SECRET); const bodyStr = await c.req.text();const REALTIME_BASE_URL = "https://api.openai.com/v1/realtime";const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");if (!OPENAI_API_KEY) { throw new Error("π΄ OpenAI API key not configured");} const obj: Record<string, string> = { Authorization: `Bearer ${OPENAI_API_KEY}`, };* Make a folder for the temporary vals that get generated, take the ID from the URL, and put it in `tempValsParentFolderId`.* If you want to use OpenAI models you need to set the `OPENAI_API_KEY` [env var](https://www.val.town/settings/environment-variables).* If you want to use Anthropic models you need to set the `ANTHROPIC_API_KEY` [env var](https://www.val.town/settings/environment-variables).# OpenAI - [Docs β](https://docs.val.town/std/openai)Use OpenAI's chat completion API with[`std/openai`](https://www.val.town/v/std/openai). This integration enablesaccess to OpenAI's language models without needing to acquire API keys.This SDK is powered by[our openapiproxy](https://www.val.town/x/std/openaiproxy).```ts title="Example" valimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [While our wrapper simplifies the integration of OpenAI, there are a fewlimitations to keep in mind:1. Create your own API key on [OpenAI's website](https://platform.openai.com/api-keys)2. Create an [environment variable](https://www.val.town/settings/environment-variables?adding=true) named `OPENAI_API_KEY`3. Use the `OpenAI` client from `npm:openai`:```ts title="Example" valimport { OpenAI } from "npm:openai";const openai = new OpenAI();```[π Edit docs](https://github.com/val-town/val-town-docs/edit/main/src/content/docs/std/openai.mdx)- `AGENT_API_KEY` (This is a secure token that you choose to secure the agent.tsx POST endpoint)- `OPENAI_API_KEY` (An OpenAI API Key)- `EXA_API_KEY` (Optional, though needed if you use the web search tool)- `AGENT_API_KEY` (This is a secure token that you choose to secure the agent.tsx POST endpoint)- `OPENAI_API_KEY` (An OpenAI API Key)- `EXA_API_KEY` (Optional, though needed if you use the web search tool)import { anthropic } from "npm:@ai-sdk/anthropic";import { openai } from "npm:@ai-sdk/openai";import { generateText, streamText } from "npm:ai"; const model = Deno.env.get("ANTHROPIC_API_KEY") ? anthropic("claude-3-7-sonnet-latest") : openai("gpt-4.1");import { anthropic } from "npm:@ai-sdk/anthropic";import { openai } from "npm:@ai-sdk/openai";import { generateText, streamText } from "npm:ai"; const model = Deno.env.get("ANTHROPIC_API_KEY") ? anthropic("claude-3-7-sonnet-latest") : openai("gpt-4.1");import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";/** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. */export class OpenAI { private rawOpenAIClient: RawOpenAI; /** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. * constructor(options: Omit<ClientOptions, "baseURL" | "apiKey" | "organization"> = {}) { this.rawOpenAIClient = new RawOpenAI({ ...options, baseURL: "https://std-openaiproxy.web.val.run/v1", apiKey: Deno.env.get("valtown"), get chat() { return this.rawOpenAIClient.chat; } return { chat: this.rawOpenAIClient.beta.chat, };