Perplexica/src/websocket/connectionManager.ts

25 lines
655 B
TypeScript
Raw Normal View History

2024-04-09 16:21:05 +05:30
import { WebSocket } from 'ws';
import { handleMessage } from './messageHandler';
2024-04-20 09:32:19 +05:30
import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
import { getOpenaiApiKey } from '../config';
2024-04-09 16:21:05 +05:30
export const handleConnection = (ws: WebSocket) => {
2024-04-20 09:32:19 +05:30
const llm = new ChatOpenAI({
temperature: 0.7,
openAIApiKey: getOpenaiApiKey(),
});
const embeddings = new OpenAIEmbeddings({
openAIApiKey: getOpenaiApiKey(),
modelName: 'text-embedding-3-large',
});
2024-04-09 16:21:05 +05:30
ws.on(
'message',
2024-04-20 09:32:19 +05:30
async (message) =>
await handleMessage(message.toString(), ws, llm, embeddings),
2024-04-09 16:21:05 +05:30
);
ws.on('close', () => console.log('Connection closed'));
};