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" },
680// --- SERVER-SIDE LOGIC ---
681export default async function(req: Request) {
682 const { OpenAI } = await import("https://esm.town/v/std/openai");
683 const { PDFExtract } = await import("npm:pdf.js-extract");
684
694 const action = url.searchParams.get("action");
695 const sourceUrl = import.meta.url.replace("esm.town", "val.town");
696 const openai = new OpenAI();
697 const MAX_TEXT_ANALYZE = 30000;
698
722 agentName: string,
723 ): Promise<object> {
724 log.push({ agent: agentName, type: "step", message: `Calling OpenAI gpt-4o...` });
725 try {
726 const response = await openai.chat.completions.create({
727 model: "gpt-4o",
728 messages: [{ role: "system", content: systemPrompt }, { role: "user", content: userMessage }],
852
853export default async function(req: Request) {
854 const { OpenAI } = await import("https://esm.town/v/std/openai");
855 const { PDFExtract } = await import("npm:pdf.js-extract");
856
866 const action = url.searchParams.get("action");
867 const sourceUrl = import.meta.url.replace("esm.town", "val.town");
868 const openai = new OpenAI();
869 const MAX_TEXT_SUGGEST = 20000;
870 const MAX_TEXT_ANALYZE = 30000;
895 agentName: string,
896 ): Promise<object> {
897 log.push({ agent: agentName, type: "step", message: `Calling OpenAI gpt-4o...` });
898 try {
899 const response = await openai.chat.completions.create({
900 model: "gpt-4o",
901 messages: [{ role: "system", content: systemPrompt }, { role: "user", content: userMessage }],
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" },
1import { Hono } from "https://esm.sh/hono@3.11.7";
2import { OpenAI } from "https://esm.town/v/std/openai";
3import { readFile } from "https://esm.town/v/std/utils/index.ts";
4import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
20});
21
22// Initialize OpenAI
23const openai = new OpenAI();
24
25// Initialize cache on startup with error handling
87 }
88
89 console.log(`Fetching fresh data from OpenAI for: "${plantName}"`);
90 // If not cached, fetch from OpenAI
91 const prompt = `Please provide detailed information about the plant "${plantName}" in the following JSON format. Be specific and accurate:
92
104Only return the JSON object, no additional text.`;
105
106 const completion = await openai.chat.completions.create({
107 messages: [
108 { role: "user", content: prompt }
116
117 if (!responseText) {
118 return c.json({ error: "No response from OpenAI" }, 500);
119 }
120
128 }
129
130 // Parse the JSON response from OpenAI
131 const plantInfo: PlantInfo = JSON.parse(cleanedResponse);
132
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" },