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" },