2024-12-26 19:02:20 +05:30
|
|
|
import axios from 'axios';
|
2025-03-19 16:23:27 +05:30
|
|
|
import { getKeepAlive, getOllamaApiEndpoint } from '../config';
|
|
|
|
|
import { ChatModel, EmbeddingModel } from '.';
|
|
|
|
|
import { ChatOllama } from '@langchain/community/chat_models/ollama';
|
|
|
|
|
import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama';
|
2024-07-06 14:19:33 +05:30
|
|
|
|
|
|
|
|
export const loadOllamaChatModels = async () => {
|
2025-03-19 16:23:27 +05:30
|
|
|
const ollamaApiEndpoint = getOllamaApiEndpoint();
|
2024-11-23 15:04:19 +05:30
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
if (!ollamaApiEndpoint) return {};
|
2024-07-08 15:39:27 +05:30
|
|
|
|
2024-07-06 14:19:33 +05:30
|
|
|
try {
|
2025-03-19 16:23:27 +05:30
|
|
|
const res = await axios.get(`${ollamaApiEndpoint}/api/tags`, {
|
2024-07-06 14:19:33 +05:30
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
const { models } = res.data;
|
|
|
|
|
|
|
|
|
|
const chatModels: Record<string, ChatModel> = {};
|
2024-07-06 14:19:33 +05:30
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
models.forEach((model: any) => {
|
|
|
|
|
chatModels[model.model] = {
|
2024-09-24 22:34:43 +05:30
|
|
|
displayName: model.name,
|
|
|
|
|
model: new ChatOllama({
|
2025-03-19 16:23:27 +05:30
|
|
|
baseUrl: ollamaApiEndpoint,
|
2024-09-24 22:34:43 +05:30
|
|
|
model: model.model,
|
|
|
|
|
temperature: 0.7,
|
2025-03-19 16:23:27 +05:30
|
|
|
keepAlive: getKeepAlive(),
|
2024-09-24 22:34:43 +05:30
|
|
|
}),
|
|
|
|
|
};
|
2025-03-19 16:23:27 +05:30
|
|
|
});
|
2024-07-06 14:19:33 +05:30
|
|
|
|
|
|
|
|
return chatModels;
|
|
|
|
|
} catch (err) {
|
2025-03-19 16:23:27 +05:30
|
|
|
console.error(`Error loading Ollama models: ${err}`);
|
2024-07-06 14:19:33 +05:30
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
export const loadOllamaEmbeddingModels = async () => {
|
|
|
|
|
const ollamaApiEndpoint = getOllamaApiEndpoint();
|
2024-07-06 14:19:33 +05:30
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
if (!ollamaApiEndpoint) return {};
|
2024-07-08 15:39:27 +05:30
|
|
|
|
2024-07-06 14:19:33 +05:30
|
|
|
try {
|
2025-03-19 16:23:27 +05:30
|
|
|
const res = await axios.get(`${ollamaApiEndpoint}/api/tags`, {
|
2024-07-06 14:19:33 +05:30
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
const { models } = res.data;
|
|
|
|
|
|
|
|
|
|
const embeddingModels: Record<string, EmbeddingModel> = {};
|
2024-07-06 14:19:33 +05:30
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
models.forEach((model: any) => {
|
|
|
|
|
embeddingModels[model.model] = {
|
2024-09-24 22:34:43 +05:30
|
|
|
displayName: model.name,
|
|
|
|
|
model: new OllamaEmbeddings({
|
2025-03-19 16:23:27 +05:30
|
|
|
baseUrl: ollamaApiEndpoint,
|
2024-09-24 22:34:43 +05:30
|
|
|
model: model.model,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2025-03-19 16:23:27 +05:30
|
|
|
});
|
2024-09-24 22:34:43 +05:30
|
|
|
|
2025-03-19 16:23:27 +05:30
|
|
|
return embeddingModels;
|
2024-07-06 14:19:33 +05:30
|
|
|
} catch (err) {
|
2025-03-19 16:23:27 +05:30
|
|
|
console.error(`Error loading Ollama embeddings models: ${err}`);
|
2024-07-06 14:19:33 +05:30
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|