6
7// โโโโโโโโ Val.town/Backend Imports โโโโโโโโ //
8import { OpenAI } from "https://esm.town/v/std/openai"; // For backend API call
9import { html } from "https://esm.town/v/stevekrouse/html"; // For serving HTML
10
444async function runImprovementPipeline(
445 originalText: string,
446 openai: OpenAI,
447): Promise<{ improvedText: string; coreIntentSummary: string }> {
448 console.log("Pipeline Step 1: Starting Intent Extraction for text length:", originalText.length);
450 try {
451 const intentPrompt = createIntentExtractionPrompt(originalText);
452 const intentCompletion = await openai.chat.completions.create({
453 model: AI_MODEL,
454 messages: [{ role: "user", content: intentPrompt }],
467 let improvedText = originalText;
468 try {
469 const mainCompletion = await openai.chat.completions.create({
470 model: AI_MODEL,
471 messages: [{ role: "user", content: mainImprovementPrompt }],
488export default async function server(req: Request): Promise<Response> {
489 const url = new URL(req.url);
490 const apiKey = Deno.env.get("OPENAI_API_KEY");
491
492 if (!apiKey) {
493 // ... (API key error handling remains the same) ...
494 const errorMsg = "Server configuration error: OPENAI_API_KEY secret not set.";
495 console.error(errorMsg);
496 if (req.method === "POST") return Response.json({ error: errorMsg }, { status: 500 });
497 return html(`<html><body><h1>Configuration Error</h1><p>${errorMsg}</p></body></html>`, { status: 500 });
498 }
499 const openai = new OpenAI({ apiKey: apiKey });
500
501 // --- API Endpoint (POST) ---
508
509 // Run the pipeline
510 const { improvedText, coreIntentSummary } = await runImprovementPipeline(text, openai);
511
512 // Return results
100Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
101
102### OpenAI
103```ts
104import { OpenAI } from "https://esm.town/v/std/openai";
105const openai = new OpenAI();
106const completion = await openai.chat.completions.create({
107 messages: [
108 { role: "user", content: "Say hello in a creative way" },
100Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
101
102### OpenAI
103```ts
104import { OpenAI } from "https://esm.town/v/std/openai";
105const openai = new OpenAI();
106const completion = await openai.chat.completions.create({
107 messages: [
108 { role: "user", content: "Say hello in a creative way" },
100Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
101
102### OpenAI
103```ts
104import { OpenAI } from "https://esm.town/v/std/openai";
105const openai = new OpenAI();
106const completion = await openai.chat.completions.create({
107 messages: [
108 { role: "user", content: "Say hello in a creative way" },
100Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
101
102### OpenAI
103```ts
104import { OpenAI } from "https://esm.town/v/std/openai";
105const openai = new OpenAI();
106const completion = await openai.chat.completions.create({
107 messages: [
108 { role: "user", content: "Say hello in a creative way" },
6import React from "https://esm.sh/react@18.2.0";
7import { blob } from "https://esm.town/v/std/blob";
8import { OpenAI } from "https://esm.town/v/std/openai";
9import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
10
305export default async function server(request: Request): Promise<Response> {
306 const KEY = "fortuitousSalmonFox";
307 const openai = new OpenAI();
308 const url = new URL(request.url);
309
6import React from "https://esm.sh/react@18.2.0";
7import { blob } from "https://esm.town/v/std/blob";
8import { OpenAI } from "https://esm.town/v/std/openai";
9import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
10
305export default async function server(request: Request): Promise<Response> {
306 const KEY = "Girlswithjuice";
307 const openai = new OpenAI();
308 const url = new URL(request.url);
309
6import React from "https://esm.sh/react@18.2.0";
7import { blob } from "https://esm.town/v/std/blob";
8import { OpenAI } from "https://esm.town/v/std/openai";
9import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
10
305export default async function server(request: Request): Promise<Response> {
306 const KEY = "lustrousApricotParakeet";
307 const openai = new OpenAI();
308 const url = new URL(request.url);
309
198 },
199 {
200 "title": "An Introduction to OpenAI fine-tuning",
201 "slug": "an-introduction-to-openai-fine-tuning",
202 "link": "/blog/an-introduction-to-openai-fine-tuning",
203 "description": "How to customize OpenAI to your liking",
204 "pubDate": "Fri, 25 Aug 2023 00:00:00 GMT",
205 "author": "Steve Krouse",
417 "slug": "val-town-newsletter-16",
418 "link": "/blog/val-town-newsletter-16",
419 "description": "Our seed round, growing team, Codeium completions, @std/openai, and more",
420 "pubDate": "Mon, 22 Apr 2024 00:00:00 GMT",
421 "author": "Steve Krouse",
19async function generateMotivation(userProgress) {
20 try {
21 const { OpenAI } = await import("https://esm.town/v/std/openai");
22 const openai = new OpenAI();
23
24 const motivationPrompt = `Generate a highly personalized, inspiring message for someone who has ${userProgress.totalHabits} habits, ${userProgress.completedHabits} completed habits, and an average streak of ${userProgress.averageStreak} days. Make it motivational and specific.`;
25
26 const response = await openai.chat.completions.create({
27 model: "gpt-4o-mini",
28 messages: [{ role: "user", content: motivationPrompt }],