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
1import OpenAI from "https://esm.sh/openai";
2
3function parseValResponse(response: string) {
28 const system = await (await fetch(`${import.meta.url.split("/").slice(0, -1).join("/")}/system_prompt.txt`)).text();
29
30 const openai = new OpenAI({
31 baseURL: "https://openrouter.ai/api/v1",
32 apiKey: Deno.env.get("OPEN_ROUTER_KEY"),
33 });
34 console.log(messages);
35 const completion = await openai.chat.completions.create({
36 model: "deepseek/deepseek-r1",
37 messages: [
287 } else if (url.pathname === "/api/analyze") {
288 if (req.method === "POST") {
289 const { OpenAI } = await import("https://esm.town/v/std/openai");
290 const openai = new OpenAI();
291
292 try {
297 Full Content: ${fullContent}`;
298
299 const completion = await openai.chat.completions.create({
300 messages: [{ role: "user", content: prompt }],
301 model: "gpt-4o-mini",
1import { encode } from "https://deno.land/std@0.203.0/encoding/base64.ts";
2import OpenAI from "https://esm.sh/openai@4.24.1";
3import { email } from "https://esm.town/v/std/email";
4import { pdfText } from "jsr:@pdf/pdftext";
134async function sendRequestToFireworks(prompt, apiKey, model) {
135 try {
136 const client = new OpenAI({
137 baseURL: "https://api.fireworks.ai/inference/v1",
138 apiKey: apiKey,
2
3 export default async function (Request:Request):Promise<Response> {
4 const OpenAIRequest = {
5 messages: [
6 { role:'system', content:'please answer the following question' },
14 method: 'POST',
15 headers:{ 'Content-Type':'application/json' },
16 body: JSON.stringify(OpenAIRequest)
17 });
18
243export default async function server(request: Request): Promise<Response> {
244 if (request.method === "POST") {
245 const { OpenAI } = await import("https://esm.town/v/std/openai");
246 const openai = new OpenAI();
247
248 const body = await request.json();
249 const messages = body.messages || [];
250
251 const completion = await openai.chat.completions.create({
252 messages: messages,
253 model: "gpt-4o-mini",
114 }
115
116 return await OpenAIChatCompletion(Request)
117 }
118
2 import { floatingQuotaTracker } from 'https://esm.town/v/rozek/floatingQuotaTracker'
3
4/**** how long until "OpenAIChatCompletion" becomes available again? ****/
5
6 export default async function (Request:Request):Promise<Response> {
2import { Hono } from "npm:hono@3";
3import { html } from "npm:hono@3/html";
4import { OpenAI } from "npm:openai";
5
6const app = new Hono();
7const openai = new OpenAI({ apiKey: Deno.env.get("OPENAI_API_KEY_VOICE") });
8
9class TranscriptionService {
10 async transcribeAudio(audioFile) {
11 try {
12 const transcription = await openai.audio.transcriptions.create({
13 file: audioFile,
14 model: "whisper-1",
18 return transcription;
19 } catch (error) {
20 console.error("OpenAI API error:", error);
21 throw error;
22 }
403
404 try {
405 const response = await openai.chat.completions.create({
406 model: "gpt-4o-mini",
407 messages: [
421 return c.text(translation);
422 } catch (error) {
423 console.error("OpenAI API error:", error);
424 return c.text("Error occurred during translation", 500);
425 }
436
437 try {
438 const mp3 = await openai.audio.speech.create({
439 model: "tts-1",
440 voice: voice,
448 });
449 } catch (error) {
450 console.error("OpenAI API error:", error);
451 return c.text("Error occurred during speech generation", 500);
452 }
3The app is set up so you can easily have a conversation between two people. The app will translate between the two selected languages, in each voice, as the speakers talk.
4
5Add your OpenAI API Key, and make sure to open in a separate window for Mic to work.