2024-07-06 14:19:33 +05:30
|
|
|
import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama';
|
2024-11-20 19:11:47 +05:30
|
|
|
import { getKeepAlive, getOllamaApiEndpoint } from '../../config';
|
2024-07-06 14:19:33 +05:30
|
|
|
import logger from '../../utils/logger';
|
|
|
|
|
import { ChatOllama } from '@langchain/community/chat_models/ollama';
|
2024-12-26 19:02:20 +05:30
|
|
|
import axios from 'axios';
|
2024-07-06 14:19:33 +05:30
|
|
|
|
|
|
|
|
export const loadOllamaChatModels = async () => {
|
|
|
|
|
const ollamaEndpoint = getOllamaApiEndpoint();
|
2024-11-20 19:11:47 +05:30
|
|
|
const keepAlive = getKeepAlive();
|
2024-11-23 15:04:19 +05:30
|
|
|
|
2024-07-08 15:39:27 +05:30
|
|
|
if (!ollamaEndpoint) return {};
|
|
|
|
|
|
2024-07-06 14:19:33 +05:30
|
|
|
try {
|
2024-12-26 19:02:20 +05:30
|
|
|
const response = await axios.get(`${ollamaEndpoint}/api/tags`, {
|
2024-07-06 14:19:33 +05:30
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-26 19:02:20 +05:30
|
|
|
const { models: ollamaModels } = response.data;
|
2024-07-06 14:19:33 +05:30
|
|
|
|
|
|
|
|
const chatModels = ollamaModels.reduce((acc, model) => {
|
2024-09-24 22:34:43 +05:30
|
|
|
acc[model.model] = {
|
|
|
|
|
displayName: model.name,
|
|
|
|
|
model: new ChatOllama({
|
|
|
|
|
baseUrl: ollamaEndpoint,
|
|
|
|
|
model: model.model,
|
|
|
|
|
temperature: 0.7,
|
2024-11-23 15:04:19 +05:30
|
|
|
keepAlive: keepAlive,
|
2024-09-24 22:34:43 +05:30
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-06 14:19:33 +05:30
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
return chatModels;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`Error loading Ollama models: ${err}`);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-08 15:39:27 +05:30
|
|
|
export const loadOllamaEmbeddingsModels = async () => {
|
2024-07-06 14:19:33 +05:30
|
|
|
const ollamaEndpoint = getOllamaApiEndpoint();
|
|
|
|
|
|
2024-07-08 15:39:27 +05:30
|
|
|
if (!ollamaEndpoint) return {};
|
|
|
|
|
|
2024-07-06 14:19:33 +05:30
|
|
|
try {
|
2024-12-26 19:02:20 +05:30
|
|
|
const response = await axios.get(`${ollamaEndpoint}/api/tags`, {
|
2024-07-06 14:19:33 +05:30
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-26 19:02:20 +05:30
|
|
|
const { models: ollamaModels } = response.data;
|
2024-07-06 14:19:33 +05:30
|
|
|
|
|
|
|
|
const embeddingsModels = ollamaModels.reduce((acc, model) => {
|
2024-09-24 22:34:43 +05:30
|
|
|
acc[model.model] = {
|
|
|
|
|
displayName: model.name,
|
|
|
|
|
model: new OllamaEmbeddings({
|
|
|
|
|
baseUrl: ollamaEndpoint,
|
|
|
|
|
model: model.model,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-06 14:19:33 +05:30
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
return embeddingsModels;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`Error loading Ollama embeddings model: ${err}`);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|