Skip to content

Commit 7b348bc

Browse files
committed
lite: support llm conat server endpoint
1 parent f05e19a commit 7b348bc

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/packages/lite/hub/llm.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
This is the lightweight analogue of @cocalc/server/llm
3+
*/
4+
5+
import {
6+
evaluateWithLangChain,
7+
evaluateOllama,
8+
heuristicNumTokens,
9+
} from "@cocalc/ai/llm";
10+
import { init as initConatLLM } from "@cocalc/conat/llm/server";
11+
import { isOllamaLLM } from "@cocalc/util/db-schema/llm-utils";
12+
import { listRows } from "./sqlite/database";
13+
14+
function buildContext() {
15+
const settings: Record<string, any> = {};
16+
for (const row of listRows("server_settings") as {
17+
name?: string;
18+
value?: any;
19+
}[]) {
20+
if (row.name) {
21+
settings[row.name] = row.value;
22+
}
23+
}
24+
25+
return {
26+
settings: {
27+
openai_api_key: settings.openai_api_key,
28+
google_vertexai_key: settings.google_vertexai_key,
29+
anthropic_api_key: settings.anthropic_api_key,
30+
mistral_api_key: settings.mistral_api_key,
31+
},
32+
mode: "user" as const,
33+
tokenCounter: heuristicNumTokens,
34+
};
35+
}
36+
37+
export async function init(): Promise<void> {
38+
await initConatLLM(async (opts: any) => {
39+
const context = buildContext();
40+
if (isOllamaLLM(opts.model)) {
41+
return evaluateOllama(opts);
42+
}
43+
return evaluateWithLangChain(opts, context);
44+
});
45+
}

src/packages/lite/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { localPathFileserver } from "@cocalc/backend/conat/files/local-path";
2828
import { init as initBugCounter } from "@cocalc/project/bug-counter";
2929
import { init as initChangefeeds } from "./hub/changefeeds";
3030
import { init as initHubApi } from "./hub/api";
31+
import { init as initLLM } from "./hub/llm";
3132
import { account_id } from "@cocalc/backend/data";
3233
import { init as initRemote } from "./remote";
3334
import { getAuthToken } from "./auth-token";
@@ -96,6 +97,9 @@ export async function main(): Promise<number> {
9697
logger.debug("start changefeed server");
9798
initChangefeeds({ client: conatClient });
9899

100+
logger.debug("start llm conat server");
101+
await initLLM();
102+
99103
const path = process.cwd();
100104

101105
logger.debug("start hub api");

src/packages/lite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dependencies": {
3333
"@cocalc/assets": "workspace:*",
3434
"@cocalc/backend": "workspace:*",
35+
"@cocalc/ai": "workspace:*",
3536
"@cocalc/conat": "workspace:*",
3637
"@cocalc/lite": "workspace:*",
3738
"@cocalc/project": "workspace:*",

src/packages/pnpm-lock.yaml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)