-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmodels.ts
More file actions
48 lines (39 loc) · 1.36 KB
/
models.ts
File metadata and controls
48 lines (39 loc) · 1.36 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
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { ChatOpenAI } from '@langchain/openai';
import { ChatOllama } from '@langchain/ollama';
export type ChatProvider = 'ollama' | 'openai';
export type ChatModelWithTools = BaseChatModel & {
bindTools: (tools: any[]) => any;
};
export interface ChatModelOptions {
provider: ChatProvider;
temperature?: number;
modelName?: string;
verbose?: boolean;
}
const chatModelConstructors: Record<ChatProvider, (options: ChatModelOptions) => ChatModelWithTools > = {
openai: ({ modelName, temperature = 0.7, verbose = false }) =>
new ChatOpenAI({
modelName: modelName ?? 'gpt-4o-mini',
temperature,
streaming: true,
openAIApiKey: process.env.OPENAI_API_KEY!,
verbose,
}),
ollama: ({ modelName, temperature = 0.7, verbose = false }) =>
new ChatOllama({
model: modelName ?? 'llama3',
temperature,
verbose,
}),
};
export class ChatModelFactory {
static create(options: ChatModelOptions): ChatModelWithTools {
const { provider } = options;
const constructor = chatModelConstructors[provider];
if (!constructor) {
throw new Error(`Unsupported provider: ${provider}`);
}
return constructor(options);
}
}