Search
Code3,249
* * A GET request to this Val will display a UI to view and search stored tags. * * It utilizes the LLM-Safe Fork of @std/sqlite (adjust import if necessary). * Assumes 'openai' secret is set in Val Town environment variables. * * Last Updated: 2025-05-08 (SQLite refactor and Glassmorphic UI) */export default async function(req: Request) { // --- Dynamic Imports (Inside Handler for Val Town) --- const { OpenAI } = await import("https://esm.town/v/std/openai"); const val_fetch = (await import("https://esm.town/v/std/fetch")).fetch; const { PDFExtract } = await import("npm:pdf.js-extract"); } // --- Helper Function: Call OpenAI API (Unchanged from previous) --- async function callOpenAI( openai: OpenAI, systemPrompt: string, userMessage: string, ): Promise<{ role: "assistant" | "system"; content: string | object }> { try { const response = await openai.chat.completions.create({ model, messages: [{ role: "system", content: systemPrompt }, { role: "user", content: userMessage }], return { role: "assistant", content }; } catch (error) { console.error(`OpenAI call failed (ExpectJSON: ${expectJson}):`, error); let msg = "Error communicating with AI."; if (error.message) msg += ` Details: ${error.message}`; if ((error as any).status === 401) msg = "OpenAI Auth Error. Check your 'openai' secret in Val Town."; if ((error as any).status === 429) msg = "OpenAI Rate Limit Exceeded."; // Consider more specific error handling or re-throwing return { role: "system", content: msg }; // Or return an actual Error object / throw log: LogEntry[], ): Promise<{ finalLog: LogEntry[]; extractedTagsCount: number; documentIdentifier: string | null }> { const openai = new OpenAI(); // Ensure your OpenAI secret is set in Val Town log.push({ agent: "System", type: "step", message: "Workflow started." }); log.push({ agent: "System", type: "step", message: "Extracting searchable tags with LLM..." }); const tagAgent = "Autism Tag Extraction Agent"; const llmResponse = await callOpenAI(openai, autismTagExtractionSystemPrompt, textToAnalyze, "gpt-4o", true); let extractedTags: string[] = []; type: "error", message: `LLM call failed or returned unexpected format.`, details: llmResponse.content, // content here will be the error message from callOpenAI }); }
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.### OpenAI```tsimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [ { role: "user", content: "Say hello in a creative way" },
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.### OpenAI```tsimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({messages: [{ role: "user", content: "Say hello in a creative way" },
import { email } from "https://esm.town/v/std/email";import { extractValInfo } from "https://esm.town/v/stevekrouse/extractValInfo";import { OpenAI } from "npm:openai";function stripHtmlBackticks(html: string): string {export default async function(e: Email) { const openai = new OpenAI(); console.log(`from: ${e.from} to: ${e.to} subject: ${e.subject}, cc: ${e.cc}, bcc: ${e.bcc}`); } const summary = await openai.chat.completions.create({ messages: [ {
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.### OpenAI```tsimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [ { role: "user", content: "Say hello in a creative way" },
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.### OpenAI```tsimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({messages: [{ role: "user", content: "Say hello in a creative way" },
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.### OpenAI```tsimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [ { role: "user", content: "Say hello in a creative way" },
}, { "title": "An Introduction to OpenAI fine-tuning", "slug": "an-introduction-to-openai-fine-tuning", "link": "/blog/an-introduction-to-openai-fine-tuning", "description": "How to customize OpenAI to your liking", "pubDate": "Fri, 25 Aug 2023 00:00:00 GMT", "author": "Steve Krouse", "slug": "val-town-newsletter-16", "link": "/blog/val-town-newsletter-16", "description": "Our seed round, growing team, Codeium completions, @std/openai, and more", "pubDate": "Mon, 22 Apr 2024 00:00:00 GMT", "author": "Steve Krouse",
/** @jsxImportSource https://esm.sh/react@18.2.0 */import OpenAI from "https://esm.sh/openai@4.28.4";import { createRoot } from "https://esm.sh/react-dom@18.2.0/client";import React, { useEffect, useRef, useState } from "https://esm.sh/react@18.2.0"; if (request.method === "POST" && new URL(request.url).pathname === "/chat") { try { const openai = new OpenAI({ apiKey: "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", console.log("Received messages:", JSON.stringify(messages, null, 2)); // Prepare messages for OpenAI API const formattedMessages = [ { role: "system", content: CHARACTER_DESCRIPTION }, console.log("Formatted messages:", JSON.stringify(formattedMessages, null, 2)); const completion = await openai.chat.completions.create({ messages: formattedMessages, model: "gpt-3.5-turbo", }); console.log("OpenAI Response:", JSON.stringify(completion.choices[0].message, null, 2)); return Response.json({
// Store the API key as a constant or use environment variableconst OPENAI_API_KEY = "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"; : "You are an expert guide helping users accelerate their learning and mastery of skills. Provide concise, actionable insights that help people learn faster and more effectively."); const response = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { "Authorization": `Bearer ${OPENAI_API_KEY}`, "Content-Type": "application/json", },
reconsumeralization
import { OpenAI } from "https://esm.town/v/std/openai";
import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
/**
* Practical Implementation of Collective Content Intelligence
* Bridging advanced AI with collaborative content creation
*/
exp
kwhinnery_openai
lost1991
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
No docs found