3import { basicAuth } from "https://esm.town/v/pomdtr/basicAuth?v=62";
4import { fetchText } from "https://esm.town/v/stevekrouse/fetchText";
5import { chat } from "https://esm.town/v/stevekrouse/openai";
6
7export default basicAuth(async (req) => {
132 await email({ subject: "Subject line", text: "Body of message" });
133
134 // OpenAI
135 import { OpenAI } from "https://esm.town/v/std/openai";
136 const openai = new OpenAI();
137 const completion = await openai.chat.completions.create({
138 messages: [
139 { role: "user", content: "Say hello in a creative way" },
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 { Hono } from "npm:hono@3";
3import OpenAI from "npm:openai";
4import { renderToString } from "npm:react-dom/server";
5
43 });
44};
45const openai = new OpenAI();
46
47const app = new Hono();
48app.get("/", async (c) => {
49 const thread = await openai.beta.threads.create();
50 const assistant = await openai.beta.assistants.create({
51 name: "",
52 instructions:
105 const message = c.req.query("message");
106
107 await openai.beta.threads.messages.create(
108 threadId,
109 { role: "user", content: message },
117 ));
118 };
119 const run = openai.beta.threads.runs.stream(threadId, {
120 assistant_id: assistantId,
121 // Make sure we only display messages we haven't seen yet.
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
10</p>
11
12**⚠️ Note: Requires your own OpenAI API key to get this to run in a fork**
13
14Migrated from folder: Archive/chatGPT
1import { getTweets } from "https://esm.town/v/geoffreylitt/getTweets";
2import { email } from "https://esm.town/v/std/email?v=12";
3import { OpenAI } from "https://esm.town/v/std/openai?v=4";
4import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
5import { twitterSearch } from "https://esm.town/v/stevekrouse/twitterSearch";
25];
26
27const openai = new OpenAI();
28
29export async function twitterAlert({ lastRunAt }: Interval) {
37
38 async function filterTweets(tweets) {
39 const completion = await openai.chat.completions.create({
40 messages: [
41 {
1import { getTweets } from "https://esm.town/v/geoffreylitt/getTweets";
2import { email } from "https://esm.town/v/std/email?v=12";
3import { OpenAI } from "https://esm.town/v/std/openai?v=4";
4import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
5import { twitterSearch } from "https://esm.town/v/stevekrouse/twitterSearch";
25];
26
27const openai = new OpenAI();
28
29export async function twitterAlert({ lastRunAt }: Interval) {
37
38 async function filterTweets(tweets) {
39 const completion = await openai.chat.completions.create({
40 messages: [
41 {
2import { cors } from "npm:hono/cors";
3import { embed, embedMany } from "npm:ai";
4import { openai } from "npm:@ai-sdk/openai";
5import lunr from "https://cdn.skypack.dev/lunr";
6
13}));
14
15openai.apiKey = Deno.env.get("OPENAI_API_KEY");
16
17class SemanticSearch {
55 async getEmbedding(text, modelName) {
56 const { embedding } = await embed({
57 model: openai.embedding(modelName),
58 value: text,
59 });
63 async getEmbeddings(texts, modelName) {
64 const { embeddings } = await embedMany({
65 model: openai.embedding(modelName),
66 values: texts,
67 });
1import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
2import { runVal } from "https://esm.town/v/std/runVal";
3import { OpenAI } from "https://esm.town/v/std/openai";
4const openai = new OpenAI();
5
6export const generateValCode = async (
19 \`\`\`
20 `;
21 const response = await openai.chat.completions.create({
22 model: "gpt-4o",
23 messages: [
1// import { openaiChatCompletion } from "https://esm.town/v/andreterron/openaiChatCompletion";
2import { OpenAI } from "https://esm.town/v/std/openai";
3const openai = new OpenAI();
4
5export const generateValCode = async (
20 \`\`\`
21 `;
22 const response = await openai.chat.completions.create({
23 openaiKey: key,
24 organization: org,
25 body: {
4export let generateValCodeAPI = (description: string) =>
5 generateValCode(
6 process.env.OPENAI_API_KEY,
7 description,
8 );