Search

4,005 results found for β€œopenai” (9047ms)

Code
3,901

# hello-realtime-video
Hello Realtime is a complete OpenAI Realtime application that supports WebRTC
users. You can access the app via WebRTC at
https://hello-realtime-video.val.run.
websocket interface.
If you remix the app, you'll just need to pop in your own OPENAI_API_KEY (from
platform.openai.com).
observer.post("/:callId", async (c) => {
const callId = c.req.param("callId");
const url = `wss://api.openai.com/v1/realtime?call_id=${callId}`;
const ws = new WebSocket(url, { headers: makeHeaders() });
ws.on("open", () => {
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>OpenAI Realtime API Voice Agent</title>
<style>
:root {
This script allows you to:
- Send emails to OpenAI. The text will be treated as the prompt
- Parse PDF attachments and include their contents in the AI's analysis.
- Get response directly to your inbox.
1. Copy this Val and save it as an Email Val (choose Val type in top-right corner of editor)
2. Add your OpenAI API key to line 8 (or use an environment variable: https://docs.val.town/refe
3. Copy the email address of the Val (click 3 dots in top-right > Copy > Copy email address)
// main controller function
export async function emailValHandler(receivedEmail) {
const openaiUrl = "https://api.openai.com/v1/chat/completions";
const apiKey = Deno.env.get("OPENAI_KEY"); // replace this entire line with your OpenAI API ke
const model = "gpt-4o-mini";
if (!apiKey) {
throw new Error(
"OPENAI_KEY environment variable is not set. Please set it or replace this line with your
);
}
const prompt = generatePrompt(receivedEmail, pdfTexts, emailText);
// step 4: send prompt to openai
const openaiResponse = await sendRequestToOpenAI(prompt, openaiUrl, apiKey, model);
// log the openai response
console.log("openai response:", openaiResponse);
// step 5: send the response back via email
await sendResponseByEmail(receivedEmail.from, openaiResponse);
console.log("response email sent.");
}
// helper function to generate a prompt for openai
function generatePrompt(email, pdfTexts, emailText) {
// extract the first name from the 'from' field if it exists
}
// helper function to send a request to openai
async function sendRequestToOpenAI(prompt, openaiUrl, apiKey, model) {
try {
// prepare the openai messages payload
const messages = [
{
};
// send the request to openai
const response = await fetch(openaiUrl, {
method: "POST",
body: JSON.stringify(body),
// parse the response
const data = await response.json();
return data.choices[0]?.message?.content || "no response from openai.";
} catch (err) {
console.error("error in sendRequestToOpenAI:", err);
return "error processing your request.";
}
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to cre
### OpenAI
```ts
import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "user", content: "Say hello in a creative way" },
const { query } = await request.json();
const { OpenAI } = await import("https://esm.town/v/std/openai");
const openai = new OpenAI();
const response = await openai.chat.completions.create({
messages: [
{
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");
}
export function makeHeaders(contentType?: string) {
const obj: Record<string, string> = {
Authorization: `Bearer ${OPENAI_API_KEY}`,
};
if (contentType) obj["Content-Type"] = contentType;
sip.post("/", async (c) => {
// Verify the webhook.
const OPENAI_SIGNING_SECRET = Deno.env.get("OPENAI_SIGNING_SECRET");
if (!OPENAI_SIGNING_SECRET) {
console.error("πŸ”΄ webhook secret not configured");
return c.text("Internal error", 500);
}
const webhook = new Webhook(OPENAI_SIGNING_SECRET);
const bodyStr = await c.req.text();
let callId: string | undefined;
# hello-realtime
**Hello Realtime** is a OpenAI Realtime app that supports both WebRTC and SIP
(telephone) users. You can access the app via WebRTC at
[hello-realtime.val.run](https://hello-realtime.val.run), or via SIP by calling
server-side websocket interface.
If 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`.
## Architecture
- Browser connects to frontend
- creates WebRTC offer
- `/rtc` endpoint handles SDP negotiation with OpenAI
- observer established to monitor session
2. **SIP Flow**: