Perplexica/src/lib/providers/ollama.ts

74 lines
1.8 KiB
TypeScript
Raw Normal View History

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';
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
try {
2025-03-19 16:23:27 +05:30
const res = await axios.get(`${ollamaApiEndpoint}/api/tags`, {
headers: {
'Content-Type': 'application/json',
},
});
2025-03-19 16:23:27 +05:30
const { models } = res.data;
const chatModels: Record<string, ChatModel> = {};
2025-03-19 16:23:27 +05:30
models.forEach((model: any) => {
chatModels[model.model] = {
displayName: model.name,
model: new ChatOllama({
2025-03-19 16:23:27 +05:30
baseUrl: ollamaApiEndpoint,
model: model.model,
temperature: 0.7,
2025-03-19 16:23:27 +05:30
keepAlive: getKeepAlive(),
}),
};
2025-03-19 16:23:27 +05:30
});
return chatModels;
} catch (err) {
2025-03-19 16:23:27 +05:30
console.error(`Error loading Ollama models: ${err}`);
return {};
}
};
2025-03-19 16:23:27 +05:30
export const loadOllamaEmbeddingModels = async () => {
const ollamaApiEndpoint = getOllamaApiEndpoint();
2025-03-19 16:23:27 +05:30
if (!ollamaApiEndpoint) return {};
2024-07-08 15:39:27 +05:30
try {
2025-03-19 16:23:27 +05:30
const res = await axios.get(`${ollamaApiEndpoint}/api/tags`, {
headers: {
'Content-Type': 'application/json',
},
});
2025-03-19 16:23:27 +05:30
const { models } = res.data;
const embeddingModels: Record<string, EmbeddingModel> = {};
2025-03-19 16:23:27 +05:30
models.forEach((model: any) => {
embeddingModels[model.model] = {
displayName: model.name,
model: new OllamaEmbeddings({
2025-03-19 16:23:27 +05:30
baseUrl: ollamaApiEndpoint,
model: model.model,
}),
};
2025-03-19 16:23:27 +05:30
});
2025-03-19 16:23:27 +05:30
return embeddingModels;
} catch (err) {
2025-03-19 16:23:27 +05:30
console.error(`Error loading Ollama embeddings models: ${err}`);
return {};
}
};