SpanishPanda_botmain.tsx5 matches
1import { OpenAI } from "https://esm.town/v/std/openai";
2import { telegramSendMessage } from "https://esm.town/v/vtdocs/telegramSendMessage?v=5";
3import {
15console.log(`received: ${text}`)
16if (text) {
17const response = await translateToSpanishWithOpenAI(text);
18console.log(`translated to: ${response}`);
19ctx.reply(response);
333435async function translateToSpanishWithOpenAI(text: string) {
36const openai = new OpenAI();
37const completion = await openai.chat.completions.create({
38messages: [
39{
2* Legal AI Document Analysis (Single Val Version with PDF Upload & Dashboard Style)
3* Ingests documents (URL, Text, PDF Upload), takes a user-defined legal task query,
4* and uses a Legal AI Agent (via OpenAI) to analyze the content.
5* The Legal AI Agent outputs a standardized JSON structure.
6* Uses 'npm:pdf.js-extract' for direct PDF text extraction within the Val.
620// --- Main Request Handler (Server Code) ---
621export default async function(req: Request) {
622const { OpenAI } = await import("https://esm.town/v/std/openai");
623// Zod is imported globally via import statement at the top of the script
624const { fetch } = await import("https://esm.town/v/std/fetch"); // Using std/fetch
676}
677678// --- Helper Function: Call OpenAI API ---
679async function callOpenAI(
680openai: OpenAI,
681systemPrompt: string,
682userMessage: string,
686log.push({ agent, type: "step", message: CONFIG.statusMessages.aiAnalysisInProgress(model) });
687try {
688const response = await openai.chat.completions.create({
689messages: [{ role: "system", content: systemPrompt }, { role: "user", content: userMessage }],
690temperature: 0.2, // REFACTOR: Slightly lower for more precision
732message: `AI response was not valid JSON. Raw: ${content.substring(0, 250)}...`,
733});
734console.error("OpenAI JSON Parse Error:", parseError, "Raw Content:", content);
735return {
736error: "AI_JSON_PARSE_ERROR",
740}
741} catch (error) {
742console.error(`OpenAI API call failed for model ${model}. Error:`, error);
743let errorMessage = `Error communicating with AI model (${model}).`;
744if (error.message) { errorMessage += ` Details: ${error.message}`; }
762},
763): Promise<{ finalResult?: LegalAIResponse | object; log: LogEntry[] }> {
764const openai = new OpenAI();
765766log.push({ agent: "System", type: "step", message: "Legal AI analysis workflow initiated." });
878.replace("%%AI_MODEL_USED%%", input.aiModel);
879880const aiResponseOrError = await callOpenAI(openai, finalSystemPrompt, truncatedText, input.aiModel);
881882if (typeof aiResponseOrError === "object" && (aiResponseOrError as any).error) {
883// Error already logged by callOpenAI
884log.push({ agent: "System", type: "final", message: "Workflow finished with AI processing errors." });
885return { finalResult: aiResponseOrError, log };
886}
887// At this point, Zod validation passed in callOpenAI, so it's LegalAIResponse
888const aiResponse = aiResponseOrError as LegalAIResponse;
889
2import { Hono } from "npm:hono@3";
3import { cors } from "npm:hono/cors";
4import { createOpenAI } from "npm:@ai-sdk/openai";
5import { createAnthropic } from "npm:@ai-sdk/anthropic@0.0.48";
6import { google, createGoogleGenerativeAI } from 'npm:@ai-sdk/google';
30});
3132const openai = createOpenAI({
33// apiKey = Deno.env.get("OPENAI_API_KEY");
34apiKey: Deno.env.get("OPENAI_API_KEY_COVERSHEET")
35});
363738const groq = createOpenAI({
39baseURL: 'https://api.groq.com/openai/v1',
40apiKey: Deno.env.get("GROQ_API_KEY"),
41});
4243const perplexity = createOpenAI({
44apiKey: Deno.env.get("PERPLEXITY_API_KEY") ?? '',
45baseURL: 'https://api.perplexity.ai/',
57this.memories = options.memories || [];
58this.messages = options.messages || [];
59this.defaultProvider = options.provider || 'openai';
60this.defaultModel = options.model;
61this.defaultMaxTokens = options.maxTokens;
122let result;
123switch (provider) {
124case 'openai':
125result = await this.generateOpenAIResponse({ model, prompt, maxTokens, temperature, streaming, schema, system, messages, tools, ...additionalSettings });
126break;
127case 'anthropic':
171}
172173async generateOpenAIResponse({ model, prompt, maxTokens, temperature, streaming, schema, system, messages, tools, embed, value, dimensions, user, ...additionalSettings }) {
174const modelId = model || 'gpt-3.5-turbo';
175176if (embed) {
177let result = await this.generateOpenAIEmbedding({ model, value, dimensions, user });
178// console.log('embed!', result)
179return result
181182const options = {
183model: openai(modelId),
184system,
185temperature,
235}
236237async generateOpenAIEmbedding({ model, value, dimensions, user }) {
238const modelId = model || 'text-embedding-3-large';
239const options = {
240model: openai.embedding(modelId, {
241dimensions,
242user,
491492app.get('/generate', async (c) => {
493const provider = c.req.query('provider') || 'openai';
494const model = c.req.query('model');
495const prompt = c.req.query('prompt');
523console.log("post/generate", { mode: 'post/generate', prompt, provider, model });
524const response = await modelProvider.gen({
525provider: provider || 'openai',
526model,
527prompt,
voicemessagesvoicenotes.ts3 matches
1import { type Context, Hono } from "https://esm.sh/hono@3.11.7";
2import { blob } from "https://esm.town/v/std/blob";
3import { OpenAI } from "https://esm.town/v/std/openai";
4import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
5import Groq from "npm:groq-sdk";
1617const app = new Hono();
18const openai = new OpenAI();
1920// Get all voice notes (for admin/dashboard)
153async function transcribeAudio(voiceNoteId: string, audioBuffer: ArrayBuffer) {
154try {
155// Convert ArrayBuffer to File for OpenAI
156const audioFile = new File([audioBuffer], "audio.webm", { type: "audio/webm" });
157
Towniesystem_prompt.txt4 matches
88Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
8990### OpenAI
9192```ts
93import { OpenAI } from "https://esm.town/v/std/openai";
94const openai = new OpenAI();
95const completion = await openai.chat.completions.create({
96messages: [
97{ role: "user", content: "Say hello in a creative way" },
Townie.cursorrules4 matches
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
9596### OpenAI
9798```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102messages: [
103{ role: "user", content: "Say hello in a creative way" },
Towniesystem_prompt.txt4 matches
88Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
8990### OpenAI
9192```ts
93import { OpenAI } from "https://esm.town/v/std/openai";
94const openai = new OpenAI();
95const completion = await openai.chat.completions.create({
96messages: [
97{ role: "user", content: "Say hello in a creative way" },
Townie.cursorrules4 matches
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
9596### OpenAI
9798```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102messages: [
103{ role: "user", content: "Say hello in a creative way" },
Towniesystem_prompt.txt4 matches
88Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
8990### OpenAI
9192```ts
93import { OpenAI } from "https://esm.town/v/std/openai";
94const openai = new OpenAI();
95const completion = await openai.chat.completions.create({
96messages: [
97{ role: "user", content: "Say hello in a creative way" },
Townie.cursorrules4 matches
94Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
9596### OpenAI
9798```ts
99import { OpenAI } from "https://esm.town/v/std/openai";
100const openai = new OpenAI();
101const completion = await openai.chat.completions.create({
102messages: [
103{ role: "user", content: "Say hello in a creative way" },