-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
74 lines (63 loc) · 2.04 KB
/
Copy pathhandler.ts
File metadata and controls
74 lines (63 loc) · 2.04 KB
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
import './instrumentation';
import { ChatPromptTemplate } from '@langchain/core/prompts';
import { StringOutputParser } from '@langchain/core/output_parsers';
import { CallbackHandler } from 'langfuse-langchain';
import { selectLlm, logger } from '@llm-ts-example/common-backend';
export async function handle(
sessionId: string,
{ question, model: modelType }: { question: string, model?: string },
output: NodeJS.WritableStream,
) {
const langfuse = {
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
secretKey: process.env.LANGFUSE_SECRET_KEY,
};
const { platform, model, modelName } = selectLlm(modelType);
const qaPrpmpt = `Answer the user's question to the best of your ability.
However, please keep your answers brief and in the same language as the question.
{question}`;
const prompt = ChatPromptTemplate.fromMessages([
['system', qaPrpmpt],
['human', 'question'],
]);
try {
// Initialize Langfuse callback handler
const langfuseHandler = langfuse.publicKey && langfuse.secretKey ? new CallbackHandler({
sessionId,
flushInterval: 0,
flushAt: 1,
tags: [modelName],
}) : undefined;
logger.debug(`Langfuse: ${langfuseHandler ? 'enable' : 'disable'}`);
const chain = prompt.pipe(model).pipe(new StringOutputParser()).withConfig({
tags: [modelName],
});
const stream = await chain.streamEvents(
{
question,
},
{
version: 'v2',
configurable: {
sessionId,
},
callbacks: langfuseHandler ? [langfuseHandler] : [],
},
);
for await (const sEvent of stream) {
logger.trace('event', sEvent);
if (sEvent.event === 'on_chat_model_stream') {
const chunk = sEvent.data.chunk;
if (platform === 'aws') {
output.write(chunk.content ?? '');
} else {
output.write(chunk.text ?? '');
}
}
}
output.write('\n');
} catch (e) {
logger.error('', JSON.parse(JSON.stringify(e)));
output.write(`Error: ${(e as Error).message}\n`);
}
}