71```
72
73### OpenAI Integration
74```typescript
75import { OpenAI } from "https://esm.town/v/std/openai";
76const openai = new OpenAI();
77const completion = await openai.chat.completions.create({
78 messages: [{ role: "user", content: "Say hello" }],
79 model: "gpt-4o-mini",
83
84 <library>
85 ## OpenAI
86
87 Val Town includes a free, proxied OpenAI:
88
89 ```ts
90 import { OpenAI } from "https://esm.town/v/std/openai";
91 const openai = new OpenAI();
92 const completion = await openai.chat.completions.create({
93 messages: [
94 { role: "user", content: "Say hello in a creative way" },
99 ```
100
101 OpenAI only works on the server. If the val includes client-side code, use dynamic imports to import this module in the server function, e.g.:
102 `const { OpenAI } = await import "https://esm.town/v/std/openai");`
103 </library>
104
143Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
144
145### OpenAI
146```ts
147import { OpenAI } from "npm:openai";
148const openai = new OpenAI();
149const completion = await openai.chat.completions.create({
150 messages: [
151 { role: "user", content: "Say hello in a creative way" },
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3// --- TYPE DEFINITIONS ---
372
373export default async function(req: Request): Promise<Response> {
374 const openai = new OpenAI();
375 const url = new URL(req.url);
376 const CORS_HEADERS = {
411 }
412
413 const completion = await openai.chat.completions.create({
414 model,
415 messages: [{ role: "system", content: prompt }, { role: "user", content: userContent }],
143Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
144
145### OpenAI
146```ts
147import { OpenAI } from "npm:openai";
148const openai = new OpenAI();
149const completion = await openai.chat.completions.create({
150 messages: [
151 { role: "user", content: "Say hello in a creative way" },
86Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
87
88### OpenAI
89
90```ts
91import { OpenAI } from "https://esm.town/v/std/openai";
92const openai = new OpenAI();
93const completion = await openai.chat.completions.create({
94 messages: [
95 { role: "user", content: "Say hello in a creative way" },
83
84 <library>
85 ## OpenAI
86
87 Val Town includes a free, proxied OpenAI:
88
89 ```ts
90 import { OpenAI } from "https://esm.town/v/std/openai";
91 const openai = new OpenAI();
92 const completion = await openai.chat.completions.create({
93 messages: [
94 { role: "user", content: "Say hello in a creative way" },
99 ```
100
101 OpenAI only works on the server. If the val includes client-side code, use dynamic imports to import this module in the server function, e.g.:
102 `const { OpenAI } = await import "https://esm.town/v/std/openai");`
103 </library>
104
71```
72
73### OpenAI Integration
74```typescript
75import { OpenAI } from "https://esm.town/v/std/openai";
76const openai = new OpenAI();
77const completion = await openai.chat.completions.create({
78 messages: [{ role: "user", content: "Say hello" }],
79 model: "gpt-4o-mini",
11 console.log("Received:", message, "from", from);
12
13 // Get OpenAI API key
14 const openaiKey = Deno.env.get("OPENAI_API_KEY")!;
15
16 // 1. Ask OpenAI for a response
17 let aiResponse = "Sorry, something went wrong.";
18 try {
19 const openaiRes = await fetch("https://api.openai.com/v1/chat/completions", {
20 method: "POST",
21 headers: {
22 Authorization: `Bearer ${openaiKey}`,
23 "Content-Type": "application/json",
24 },
39 });
40
41 const data = await openaiRes.json();
42 aiResponse = data.choices?.[0]?.message?.content ?? aiResponse;
43 } catch (err) {
44 console.error("OpenAI error:", err);
45 }
46
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },