94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },
4import type { ApiResponse, RecommendationRequest, UserProfile } from "../shared/types";
5import { drills, initDatabase, initSampleDrills, playlists, sessions, users } from "./database";
6import { generateFootballDrills, generateMusicPlaylist, generateTrainingSession } from "./openai";
7
8// Initialize the Hono app
1import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
2import { OpenAI } from "https://esm.town/v/std/openai";
3import { readFile } from "https://esm.town/v/std/utils@85-main/index.ts";
4
5// Initialize OpenAI client
6const openai = new OpenAI();
7
8// Database setup
50}
51
52// Fallback response generator when OpenAI is not available
53function generateFallbackResponse(query: string, memories: Array<{ content: string; tags?: string }> = []) {
54 if (memories.length === 0) {
71}
72
73// Generate a response using OpenAI, incorporating memories if available
74async function generateResponse(query: string, memories: Array<{ content: string; tags?: string }> = []) {
75 try {
84 : "I don't have any memories stored yet.\n\n";
85
86 console.log(`Sending to OpenAI with ${memories.length} memories in context`);
87
88 const response = await openai.chat.completions.create({
89 model: "gpt-4o-mini",
90 messages: [
102
103 if (!response.choices || response.choices.length === 0) {
104 console.error('OpenAI returned no choices:', response);
105 return generateFallbackResponse(query, memories);
106 }
108 return response.choices[0].message.content || generateFallbackResponse(query, memories);
109 } catch (error) {
110 console.error('Error generating response with OpenAI:', error);
111 return generateFallbackResponse(query, memories);
112 }
18brian@airbnb.com,Brian Chesky
19drew@dropbox.com,Drew Houston
20sam@openai.com,Sam Altman
21tim@apple.com,Tim Cook
22jeff@amazon.com,Jeff Bezos
18brian@airbnb.com,Brian Chesky
19drew@dropbox.com,Drew Houston
20sam@openai.com,Sam Altman
21tim@apple.com,Tim Cook
22jeff@amazon.com,Jeff Bezos
1# AI Agent with Memory
2
3This is an AI agent that can save information to its memory and retrieve it when answering questions. The agent uses SQLite for persistent storage and OpenAI's API for generating responses.
4
5## Features
141. **Database**: Uses SQLite to store memory snippets with content, tags, and timestamps
152. **Memory Search**: Performs simple text matching to find relevant memories
163. **Response Generation**: Uses OpenAI to generate responses that incorporate found memories
174. **Web Interface**: Provides a clean UI for asking questions and saving new memories
18
36- **Backend**: TypeScript with Deno runtime
37- **Database**: SQLite for persistent storage
38- **AI**: OpenAI API for generating responses
39- **Frontend**: HTML, JavaScript, and Twind (Tailwind CSS in JS)
40
88Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
89
90### OpenAI
91
92```ts
93import { OpenAI } from "https://esm.town/v/std/openai";
94const openai = new OpenAI();
95const completion = await openai.chat.completions.create({
96 messages: [
97 { role: "user", content: "Say hello in a creative way" },
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
95
96### OpenAI
97
98```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102 messages: [
103 { role: "user", content: "Say hello in a creative way" },
17- **Backend**: TypeScript, Hono framework for API routing
18- **Database**: Val Town SQLite for data persistence
19- **AI**: OpenAI GPT models for personalized content generation
20
21## How It Works
231. **User Onboarding**: Users create a profile with their football position, skill level, and music preferences
242. **Personalization**: The app uses this information to generate tailored recommendations
253. **AI Integration**: OpenAI models create custom playlists and training programs based on user context
264. **Combined Experience**: Music and football training are seamlessly integrated for an enhanced experience
27
38โโโ backend/
39โ โโโ index.ts # Main HTTP API entry point
40โ โโโ openai.ts # OpenAI integration for recommendations
41โ โโโ database.ts # Database operations
42โโโ frontend/
1export default async function(req: Request): Promise<Response> {
2 // const OPENAI_API_KEY = "your_openai_api_key_here";
3
4 if (!req.url.includes("/v1/chat/completions")) {
9 const body = await req.json();
10
11 // Prepare the request to OpenAI API with streaming enabled
12 const openaiResponse = await fetch("https://api.openai.com/v1/chat/completions", {
13 method: "POST",
14 headers: {
22 });
23
24 if (!openaiResponse.ok) {
25 const errorText = await openaiResponse.text();
26 return new Response(errorText, { status: openaiResponse.status });
27 }
28
29 // Stream the response back to the client
30 return new Response(openaiResponse.body, {
31 headers: {
32 "Content-Type": "text/event-stream",