2024-04-20 11:18:52 +05:30
|
|
|
import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
|
|
|
|
import { ChatOllama } from '@langchain/community/chat_models/ollama';
|
|
|
|
import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama';
|
|
|
|
import { getOllamaApiEndpoint, getOpenaiApiKey } from '../config';
|
|
|
|
|
|
|
|
export const getAvailableProviders = async () => {
|
|
|
|
const openAIApiKey = getOpenaiApiKey();
|
|
|
|
const ollamaEndpoint = getOllamaApiEndpoint();
|
|
|
|
|
|
|
|
const models = {};
|
|
|
|
|
|
|
|
if (openAIApiKey) {
|
2024-04-21 20:52:47 +05:30
|
|
|
try {
|
|
|
|
models['openai'] = {
|
|
|
|
'gpt-3.5-turbo': new ChatOpenAI({
|
|
|
|
openAIApiKey,
|
|
|
|
modelName: 'gpt-3.5-turbo',
|
|
|
|
temperature: 0.7,
|
|
|
|
}),
|
|
|
|
'gpt-4': new ChatOpenAI({
|
|
|
|
openAIApiKey,
|
|
|
|
modelName: 'gpt-4',
|
|
|
|
temperature: 0.7,
|
2024-04-29 10:49:15 +02:00
|
|
|
}),
|
|
|
|
'gpt-4-turbo': new ChatOpenAI({
|
|
|
|
openAIApiKey,
|
|
|
|
modelName: 'gpt-4-turbo',
|
|
|
|
temperature: 0.7,
|
2024-04-21 20:52:47 +05:30
|
|
|
}),
|
|
|
|
embeddings: new OpenAIEmbeddings({
|
|
|
|
openAIApiKey,
|
|
|
|
modelName: 'text-embedding-3-large',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`Error loading OpenAI models: ${err}`);
|
|
|
|
}
|
2024-04-20 11:18:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (ollamaEndpoint) {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`${ollamaEndpoint}/api/tags`);
|
|
|
|
|
|
|
|
const { models: ollamaModels } = (await response.json()) as any;
|
|
|
|
|
|
|
|
models['ollama'] = ollamaModels.reduce((acc, model) => {
|
|
|
|
acc[model.model] = new ChatOllama({
|
|
|
|
baseUrl: ollamaEndpoint,
|
|
|
|
model: model.model,
|
|
|
|
temperature: 0.7,
|
|
|
|
});
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
if (Object.keys(models['ollama']).length > 0) {
|
|
|
|
models['ollama']['embeddings'] = new OllamaEmbeddings({
|
|
|
|
baseUrl: ollamaEndpoint,
|
|
|
|
model: models['ollama'][Object.keys(models['ollama'])[0]].model,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2024-04-21 20:52:47 +05:30
|
|
|
console.log(`Error loading Ollama models: ${err}`);
|
2024-04-20 11:18:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return models;
|
|
|
|
};
|