blob: 0ae3bbd87c3ff27fe0431766d35b422476bfe236 (
plain)
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
|
import { App } from "../app";
import { Log } from "../util/log";
import { createAnthropic } from "@ai-sdk/anthropic";
import type { LanguageModel, Provider } from "ai";
import { generateText, NoSuchModelError } from "ai";
export namespace LLM {
const log = Log.create({ service: "llm" });
export class ModelNotFoundError extends Error {
constructor(public readonly model: string) {
super();
}
}
const state = App.state("llm", async (app) => {
const providers: Provider[] = [];
if (process.env["ANTHROPIC_API_KEY"] || app.config.providers?.anthropic) {
log.info("loaded anthropic");
const provider = createAnthropic({
apiKey: app.config.providers?.anthropic?.apiKey,
baseURL: app.config.providers?.anthropic?.baseURL,
headers: app.config.providers?.anthropic?.headers,
});
providers.push(provider);
}
return {
models: new Map<string, LanguageModel>(),
providers,
};
});
export async function providers() {
return state().then((state) => state.providers);
}
export async function findModel(model: string) {
const s = await state();
if (s.models.has(model)) {
return s.models.get(model)!;
}
log.info("loading", { model });
for (const provider of s.providers) {
try {
const match = provider.languageModel(model);
log.info("found", { model });
s.models.set(model, match);
return match;
} catch (e) {
if (e instanceof NoSuchModelError) continue;
throw e;
}
}
throw new ModelNotFoundError(model);
}
}
|