1import { loadPageContent } from "https://esm.town/v/charlypoly/browserbaseUtils";
2import { OpenAI } from "https://esm.town/v/std/openai?v=4";
3import { z } from "npm:zod";
4import { zodToJsonSchema } from "npm:zod-to-json-schema";
25});
26
27// we create a OpenAI Tool that takes our schema as argument
28const extractContentTool: any = {
29 type: "function",
36};
37
38const openai = new OpenAI();
39
40// We ask OpenAI to extract the content from the given web page.
41// The model will reach out to our `extract_content` tool and
42// by doing so, the model will extract the required data to satisfy
43// the requirement of `extract_content`s argument.
44const completion = await openai.chat.completions.create({
45 model: "gpt-4-turbo",
46 messages: [
55});
56
57// we retrieve the serialized arguments generated by OpenAI
58const result = completion.choices[0].message.tool_calls![0].function.arguments;
59// the serialized arguments are parsed into a valid JavaScript array of objects
1import { OpenAI } from "npm:openai";
2import { zodResponseFormat } from "npm:openai/helpers/zod";
3import { z } from "npm:zod";
4
9});
10
11const client = new OpenAI({ apiKey: Deno.env.get("OPENAI_API_KEY") });
12
13async function main() {
38 }
39 const code = await generateValCode(
40 process.env.VT_OPENAI_KEY,
41 value.description,
42 );
21 try {
22 let resp = await textToImageDalle(
23 process.env.openai,
24 text.replace("/dalle", ""),
25 1,
100 // Configuration
101 const API_CONFIG = {
102 url: "https://willthereader-openaidefiner.web.val.run",
103 method: "POST",
104 mode: "cors",
1# OpenAI ChatGPT helper function
2
3This val uses your OpenAI token if you have one, and the @std/openai if not, so it provides limited OpenAI usage for free.
4
5```ts
6import { chat } from "https://esm.town/v/stevekrouse/openai";
7
8const { content } = await chat("Hello, GPT!");
11
12```ts
13import { chat } from "https://esm.town/v/stevekrouse/openai";
14
15const { content } = await chat(
1import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming, Message } from "npm:@types/openai";
2
3async function getOpenAI() {
4 // if you don't have a key, use our std library version
5 if (Deno.env.get("OPENAI_API_KEY") === undefined) {
6 const { OpenAI } = await import("https://esm.town/v/std/openai");
7 return new OpenAI();
8 } else {
9 const { OpenAI } = await import("npm:openai");
10 return new OpenAI();
11 }
12}
13
14/**
15 * Initiates a chat conversation with OpenAI's GPT model and retrieves the content of the first response.
16 * This function can handle both single string inputs and arrays of message objects.
17 * It supports various GPT models, allowing for flexibility in choosing the model based on the application's needs.
25 options?: Omit<ChatCompletionCreateParamsNonStreaming, "messages">,
26): Promise<ChatCompletion & { content: string }> {
27 const openai = await getOpenAI();
28 const messages = Array.isArray(input) ? input : [{ role: "user", content: input }];
29 const createParams: ChatCompletionCreateParamsNonStreaming = {
33 messages,
34 };
35 const completion = await openai.chat.completions.create(createParams);
36
37 return { ...completion, content: completion.choices[0].message.content };
13* and activity (befriends aliens, goes to the doctor, rides a rollercoaster, bakes a cake for friends)
14
15It uses OpenAI to write a children's bedtime story
16
17* title
21for a "fantastical story about a green whale who rides the bus" or the "spooky story about the tomato fox who explores a cave".
22
23Then using the summary, OpenAI geenrates another prompt to describe the instructions to generate a children's story book image.
24
25That's sent to Fal to generate an image.
15import { generateOpenGraphTags, OpenGraphData } from "https://esm.town/v/dthyresson/generateOpenGraphTags";
16import { ValTownLink } from "https://esm.town/v/dthyresson/viewOnValTownComponent";
17import { chat } from "https://esm.town/v/stevekrouse/openai";
18import * as fal from "npm:@fal-ai/serverless-client";
19
1// This calendar app will allow users to upload a PDF, extract events from it using OpenAI's GPT model,
2// and display them on a big calendar. We'll use react-big-calendar for the calendar component,
3// and pdf.js for PDF parsing. The server will handle PDF processing and event extraction.
121
122async function server(request: Request): Promise<Response> {
123 const { OpenAI } = await import("https://esm.town/v/std/openai");
124 const pdfExtractText = await import("https://esm.town/v/pdebieamzn/pdfExtractText");
125
136 const fullText = await pdfExtractText.default(arrayBuffer);
137
138 const openai = new OpenAI();
139 const completion = await openai.chat.completions.create({
140 messages: [
141 { role: "system", content: "You are a helpful assistant that extracts event information from text. Extract events with their titles, start times, and end times. Format the response as a valid JSON array of objects with 'title', 'start', and 'end' properties. Use ISO 8601 format for dates. Do not include any explanatory text outside the JSON array. Do not use markdown code fences or JSON labels. Consider the provided context when extracting events." },
158 events = JSON.parse(content);
159 } catch (error) {
160 console.error('Error parsing OpenAI response:', error);
161 console.log('Raw response:', completion.choices[0].message.content);
162 return new Response(JSON.stringify({ error: 'Error processing events' }), { status: 500, headers: { 'Content-Type': 'application/json' } });
164
165 if (!Array.isArray(events)) {
166 console.error('Unexpected response format from OpenAI');
167 return new Response(JSON.stringify({ error: 'Unexpected response format' }), { status: 500, headers: { 'Content-Type': 'application/json' } });
168 }