-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudChat.ts
63 lines (55 loc) · 1.57 KB
/
cloudChat.ts
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
import { ChatOpenAI } from "@langchain/openai";
import { StringOutputParser } from "langchain/schema/output_parser";
import {
AIMessage,
HumanMessage,
SystemMessage,
} from "@langchain/core/messages";
import { HistoryMessage } from "../prompt";
import { configuration } from "../utils/configuration";
import { getSuppabaseClient } from "../auth/supabaseClient";
type Parameters = {
temperature: number;
n_predict: number;
controller?: AbortController;
};
export const sendChatRequestCloud = async (
history: HistoryMessage[],
parameters: Parameters
) => {
const supabase = getSuppabaseClient();
const session = await supabase.auth.getSession();
const apiKey = session.data?.session?.access_token;
if (!apiKey) {
throw new Error("No API key found");
}
const model = new ChatOpenAI({
maxRetries: 0,
openAIApiKey: apiKey,
configuration: {
baseURL: configuration.get("cloud.endpoint"),
},
temperature: parameters.temperature,
maxTokens: parameters.n_predict,
});
const parser = new StringOutputParser();
const messages = history.map((message) => {
switch (message.role) {
case "ai":
return new AIMessage(message.content);
case "user":
return new HumanMessage(message.content);
case "system":
return new SystemMessage(message.content);
default:
return new HumanMessage(message.content);
}
});
const stream = await model.pipe(parser).stream(messages, {
configurable: {
signal: parameters.controller,
},
maxConcurrency: 1,
});
return stream;
};