import axios from 'axios';
import { CONFIG } from '../config.js';
import { LocalLLMInput, ToolResponse } from '../validation.js';
export async function executeOllamaLLM(input: LocalLLMInput): Promise<ToolResponse> {
const { model, prompt, temperature = 0.7, stream = false } = input;
try {
// Use /api/chat endpoint by default, can be configured via environment
const endpoint = process.env.OLLAMA_USE_CHAT === 'false' ? '/api/generate' : '/api/chat';
const url = `${CONFIG.OLLAMA_BASE_URL}${endpoint}`;
let requestBody: any;
if (endpoint === '/api/chat') {
requestBody = {
model,
messages: [{ role: 'user', content: prompt }],
stream,
options: { temperature }
};
} else {
requestBody = {
model,
prompt,
stream,
options: { temperature }
};
}
const startTime = Date.now();
const response = await axios.post(url, requestBody, {
timeout: 60000,
headers: { 'Content-Type': 'application/json' }
});
const latencyMs = Date.now() - startTime;
let text: string;
if (endpoint === '/api/chat' && response.data.message) {
text = response.data.message.content;
} else if (response.data.response) {
text = response.data.response;
} else {
throw new Error('Unexpected response format from Ollama');
}
const result = {
text,
model,
latencyMs,
tokens: response.data.eval_count || 0
};
return {
content: [{
type: 'text' as const,
text: JSON.stringify(result, null, 2)
}]
};
} catch (error: any) {
const errorMessage = error.response?.data?.error || error.message || 'Unknown error';
return {
content: [{
type: 'text' as const,
text: `Error calling Ollama: ${errorMessage}`
}]
};
}
}