Perplexica/src/lib/services/ollamaService.ts

45 lines
1.3 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
import { env } from '../../config/env';
export class OllamaService {
2025-01-05 14:16:31 -07:00
private static readonly baseUrl = env.ollama.url;
private static readonly model = env.ollama.model;
static async complete(prompt: string): Promise<string> {
try {
const response = await axios.post(`${this.baseUrl}/api/generate`, {
model: this.model,
prompt: prompt,
stream: false
});
2025-01-05 14:16:31 -07:00
if (response.data?.response) {
return response.data.response;
}
2025-01-05 14:16:31 -07:00
throw new Error('No response from Ollama');
} catch (error) {
console.error('Ollama error:', error);
throw error;
}
2025-01-05 14:16:31 -07:00
}
2025-01-05 14:16:31 -07:00
static async chat(messages: { role: 'user' | 'assistant'; content: string }[]): Promise<string> {
try {
const response = await axios.post(`${this.baseUrl}/api/chat`, {
model: this.model,
messages: messages,
stream: false
});
if (response.data?.message?.content) {
return response.data.message.content;
}
throw new Error('No response from Ollama chat');
} catch (error) {
console.error('Ollama chat error:', error);
throw error;
}
}
}