3import { db as allValsDb } from "https://esm.town/v/sqlite/db?v=9";
4import { blob } from "https://esm.town/v/std/blob";
5import OpenAI from "npm:openai";
6
7const dimensions = 1536;
11 await client.connect();
12
13 const openai = new OpenAI();
14 const queryEmbedding = (await openai.embeddings.create({
15 model: "text-embedding-3-small",
16 input: query,
5import { db as allValsDb } from "https://esm.town/v/sqlite/db?v=9";
6import { blob } from "https://esm.town/v/std/blob";
7import OpenAI from "npm:openai";
8import { truncateMessage } from "npm:openai-tokens";
9
10// CREATE TABLE vals_embeddings (id TEXT PRIMARY KEY, embedding VECTOR(1536));
41 }
42
43 const openai = new OpenAI();
44 for (const newValsBatch of newValsBatches) {
45 await Promise.all([...Array(newValsBatch.length).keys()].map(async (valIndex) => {
47 const code = getValCode(val);
48
49 const embedding = await openai.embeddings.create({
50 model: "text-embedding-3-small",
51 input: truncateMessage(code, "text-embedding-3-small"),
2import { fileToDataURL } from "https://esm.town/v/stevekrouse/fileToDataURL";
3import { modifyImage } from "https://esm.town/v/stevekrouse/modifyImage";
4import { chat } from "https://esm.town/v/stevekrouse/openai";
5import { Hono } from "npm:hono@3";
6
122 event.preventDefault();
123 const resumeContent = document.getElementById('resumeContent').value;
124 const apiKey = '${Deno.env.get("OPENAI_API_KEY")}';
125 const spinner = document.getElementById('spinner');
126 const jsonOutput = document.getElementById('jsonOutput');
1import { email } from "https://esm.town/v/std/email?v=11";
2import { OpenAI } from "npm:openai";
3
4let location = "brooklyn ny";
8).then(r => r.json());
9
10const openai = new OpenAI();
11let chatCompletion = await openai.chat.completions.create({
12 messages: [{
13 role: "user",
1# ChatGPT Implemented in Val Town
2
3Demonstrated how to use assistants and threads with the OpenAI SDK and how to stream the response with Server-Sent Events.
4
5<p align=center>
1/** @jsxImportSource https://esm.sh/react */
2import OpenAI from "npm:openai";
3import { renderToString } from "npm:react-dom/server";
4
5// This uses by personal API key, you'll need to provide your own if
6// you fork this. We'll be adding support to the std/openai lib soon!
7const openai = new OpenAI();
8import { Hono } from "npm:hono@3";
9
38 });
39
40 // Setup the SSE connection and stream back the response. OpenAI handles determining
41 // which message is the correct response based on what was last read from the
42 // thread. This is likely vulnerable to race conditions.
58const app = new Hono();
59app.get("/", async (c) => {
60 const thread = await openai.beta.threads.create();
61 const assistant = await openai.beta.assistants.create({
62 name: "",
63 instructions:
114app.post("/post-message", async (c) => {
115 let message = await c.req.text();
116 await openai.beta.threads.messages.create(
117 c.req.query("threadId"),
118 { role: "user", content: message },
132 ));
133 };
134 const run = openai.beta.threads.runs.stream(threadId, {
135 assistant_id: assistantId,
136 // Make sure we only display messages we haven't seen yet.
1/** @jsxImportSource https://esm.sh/react */
2import { OpenAI } from "https://esm.town/v/std/openai";
3import { Hono } from "npm:hono@3";
4import { renderToString } from "npm:react-dom/server";
5
6const openai = new OpenAI();
7
8const jsxResponse = (jsx) => {
62 let thread, assistant;
63 try {
64 thread = await openai.chat.completions.create({
65 model: "gpt-3.5-turbo", // Use a faster model
66 messages: [{ role: "system", content: "Start a new thread" }],
67 });
68 assistant = await openai.chat.completions.create({
69 model: "gpt-3.5-turbo", // Use a faster model
70 messages: [{
128 let message = await c.req.text();
129 try {
130 await openai.chat.completions.create({
131 model: "gpt-3.5-turbo", // Use a faster model
132 messages: [{ role: "user", content: message }],
150 ));
151 };
152 const run = openai.chat.completions.stream({
153 model: "gpt-3.5-turbo", // Use a faster model
154 messages: [{ role: "user", content: "Continue the conversation" }],
1# ChatGPT Implemented in Val Town
2
3Demonstrated how to use assistants and threads with the OpenAI SDK and how to stream the response with Server-Sent Events.
4
5<p align=center>
1# ChatGPT Implemented in Val Town
2
3Demonstrated how to use assistants and threads with the OpenAI SDK and how to stream the response with Server-Sent Events