yawnxyz-ai.web.val.run
Readme

An http and class wrapper for Vercel's AI SDK

Usage:

  • Groq: https://yawnxyz-ai.web.val.run/generate?prompt="tell me a beer joke"&provider=groq&model=llama3-8b-8192
  • Perplexity: https://yawnxyz-ai.web.val.run/generate?prompt="what's the latest phage directory capsid & tail article about?"&provider=perplexity
  • Mistral: https://yawnxyz-ai.web.val.run/generate?prompt="tell me a joke?"&provider=mistral&model="mistral-small-latest"
async function calculateEmbeddings(text) {
    const url = `https://yawnxyz-ai.web.val.run/generate?embed=true&value=${encodeURIComponent(text)}`;
    
    try {
        const response = await fetch(url);
        const data = await response.json();
        return data;
    } catch (error) {
        console.error('Error calculating embeddings:', error);
        return null;
    }
}
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 { 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";
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 || '';
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 });
prompt = null;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v213
July 15, 2024