1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import "jsr:@std/dotenv/load"; // needed for deno run; not req for smallweb or valtown
import { Hono } from "npm:hono@3";
import { cors } from "npm:hono/cors";
import { createOpenAI } from "npm:@ai-sdk/openai";
import { createAnthropic } from "npm:@ai-sdk/anthropic@0.0.48";
import { google, createGoogleGenerativeAI } from 'npm:@ai-sdk/google';
import { mistral, createMistral } from "npm:@ai-sdk/mistral";
import { generateText, streamText, generateObject, embed } from "npm:ai";
// import { z } from "npm:zod";
// import { nanoid } from 'npm:nanoid';
import stringHash from 'npm:string-hash';
import { blobby } from "https://esm.town/v/@yawnxyz/blobby";
const URL = "https://yawnxyz-coversheetai.web.val.run"
const taskPrefix = "task::ai:"
const app = new Hono();
app.use('*', cors({
origin: '*',
allowMethods: ['GET', 'POST'],
allowHeaders: ['Content-Type'],
}));
const anthropic = createAnthropic({
// apiKey = Deno.env.get("ANTHROPIC_API_KEY");
apiKey: Deno.env.get("ANTHROPIC_API_KEY_COVERSHEET")
});
const openai = createOpenAI({
// apiKey = Deno.env.get("OPENAI_API_KEY");
apiKey: Deno.env.get("OPENAI_API_KEY_COVERSHEET")
});
const groq = createOpenAI({
baseURL: 'https://api.groq.com/openai/v1',
apiKey: Deno.env.get("GROQ_API_KEY"),
});
const perplexity = createOpenAI({
apiKey: Deno.env.get("PERPLEXITY_API_KEY") ?? '',
baseURL: 'https://api.perplexity.ai/',
});
const googleProvider = createGoogleGenerativeAI({
apiKey: Deno.env.get("GOOGLE_GENERATIVE_AI_API_KEY"),
});
class ModelProvider {
constructor(options = {}) {
this.id = options.id || 'new-model-provider';
this.chars = options.chars || {};
this.system = options.system || 'You are a helpful assistant';
this.memories = options.memories || [];
this.messages = options.messages || [];
this.defaultProvider = options.provider || 'openai';
this.defaultModel = options.model;
this.defaultMaxTokens = options.maxTokens;
this.defaultTemperature = options.temperature;
this.defaultStreaming = options.streaming || false;
this.defaultSchema = options.schema;
this.defaultTools = options.tools;
this.defaultUseMessages = options.useMessages || false;
this.defaultUseMemory = options.useMemory || false;
this.defaultUseChars = options.useChars || false;
this.defaultGetUpdatedChars = options.getUpdatedChars || false;
this.memoryPrompt = options.memoryPrompt || 'Summarize the key points from the current conversation, focusing on the most important and relevant details for future reference.';
this.charsPrompt = options.charsPrompt || 'Based on the current conversation, describe how the characters state, emotions, inventory, or ideas might change.';
}
async gen({
prompt,
messages,
memories,
chars,
provider = this.defaultProvider,
model = this.defaultModel,
maxTokens = this.defaultMaxTokens,
temperature = this.defaultTemperature,
streaming = this.defaultStreaming,
schema = this.defaultSchema,
system = this.system,
tools = this.defaultTools,
useMessages = this.defaultUseMessages,
useChars = this.defaultUseChars,
useMemory = this.defaultUseMemory,
getUpdatedChars = this.defaultGetUpdatedChars,
...additionalSettings
}) {
if (useMessages) {
this.useMessages = useMessages;
if (messages && messages.length > 0) {
this.messages.push(...messages);
}
if (prompt) {
this.messages.push({ role: 'user', content: prompt });