1import { blob } from "https://esm.town/v/std/blob?v=12";
2import OpenAI from "npm:openai";
3
4const openai = new OpenAI();
5
6const getCacheKey = (url: string): string => {
90 let pageResult = "";
91
92 // // 2. Do one OpenAI inference to expand that URL to a longer page description
93 const pageDescriptionStream = await openai.chat.completions.create({
94 model: "gpt-4o",
95 messages: [{
127
128 // 3. Generate the page
129 const stream = await openai.chat.completions.create({
130 model: "gpt-4o",
131 messages: [{
1import OpenAI from "npm:openai";
2const openai = new OpenAI();
3import process from "node:process";
4
5// Define our assistant.
6const assistant = await openai.beta.assistants.create({
7 name: "Val Tutor",
8 instructions: `You are a personal Val tutor.
14
15// Create a thread to chat in.
16const thread = await openai.beta.threads.create();
17
18// These are the messages we'll send to the assistant.
44 }, 100);
45
46 const message = await openai.beta.threads.messages.create(
47 thread.id,
48 { role: "user", content: messages[i] },
49 );
50
51 const run = openai.beta.threads.runs.stream(thread.id, {
52 assistant_id: assistant.id,
53 // Make sure we only display messages we haven't seen yet.
1import { blob } from "https://esm.town/v/std/blob?v=12";
2import OpenAI from "npm:openai";
3
4const openai = new OpenAI();
5
6const getCacheKey = (url: string): string => {
90 let pageResult = "";
91
92 // // 2. Do one OpenAI inference to expand that URL to a longer page description
93 const pageDescriptionStream = await openai.chat.completions.create({
94 model: "gpt-4o",
95 messages: [{
127
128 // 3. Generate the page
129 const stream = await openai.chat.completions.create({
130 model: "gpt-4o",
131 messages: [{
1
2
3Migrated from folder: Archive/openaistreaminghtml
1null
2
3Migrated from folder: Archive/openaiStreamingDemo
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# OpenAI Streaming - Assistant and Threads
2
3An example of using OpenAI to stream back a chat with an assistant. This example sends two messages to the assistant and streams back the responses when they come in.
4
5Example response:
4import { fileToDataURL } from "https://esm.town/v/stevekrouse/fileToDataURL";
5import { modifyImage } from "https://esm.town/v/stevekrouse/modifyImage";
6import { chat } from "https://esm.town/v/stevekrouse/openai";
7import { Hono } from "npm:hono@3";
8
1# OpenAI Proxy Metrics
2
3We write openAI usage data to a `openai_usage` sqlite table. This script val is imported into the openai proxy. Use this val to run administrative scripts: https://www.val.town/v/std/OpenAIUsageScript
4
5Migrated from folder: openai/OpenAIUsage
109 <path stroke-linecap="round" stroke-linejoin="round" d="M20.25 8.511c.884.284 1.5 1.128 1.5 2.097v4.286c0 1.136-.847 2.1-1.98 2.193-.34.027-.68.052-1.02.072v3.091l-3-3c-1.354 0-2.694-.055-4.02-.163a2.115 2.115 0 0 1-.825-.242m9.345-8.334a2.126 2.126 0 0 0-.476-.095 48.64 48.64 0 0 0-8.048 0c-1.131.094-1.976 1.057-1.976 2.192v4.286c0 .837.46 1.58 1.155 1.951m9.345-8.334V6.637c0-1.621-1.152-3.026-2.76-3.235A48.455 48.455 0 0 0 11.25 3c-2.115 0-4.198.137-6.24.402-1.608.209-2.76 1.614-2.76 3.235v6.226c0 1.621 1.152 3.026 2.76 3.235.577.075 1.157.14 1.74.194V21l4.155-4.155"></path>
110 </svg>
111 <div class="font-semibold group-hover:text-black">OpenAI chat completion</div>
112 </button>
113 <button class="group bg-gray-50 border-gray-300 flex items-center gap-2 overflow-hidden rounded border p-3 text-left transition-colors hover:border-blue-500 hover:shadow-md">