mirror of
https://github.com/zadam/trilium.git
synced 2025-11-09 06:45:49 +01:00
I'm 100% going to have to destroy this commit later
This commit is contained in:
@@ -32,43 +32,201 @@ export class OllamaService extends BaseAIService {
|
||||
try {
|
||||
const endpoint = `${baseUrl.replace(/\/+$/, '')}/api/chat`;
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages: formattedMessages,
|
||||
options: {
|
||||
temperature,
|
||||
}
|
||||
})
|
||||
});
|
||||
// Determine if we should stream the response
|
||||
const shouldStream = opts.stream === true;
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text();
|
||||
throw new Error(`Ollama API error: ${response.status} ${response.statusText} - ${errorBody}`);
|
||||
}
|
||||
if (shouldStream) {
|
||||
// Handle streaming response
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages: formattedMessages,
|
||||
stream: true,
|
||||
options: {
|
||||
temperature,
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
text: data.message?.content || "No response from Ollama",
|
||||
model: data.model || model,
|
||||
provider: this.getName(),
|
||||
usage: {
|
||||
// Ollama doesn't provide token usage in the same format
|
||||
totalTokens: data.eval_count || data.prompt_eval_count || 0
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text();
|
||||
throw new Error(`Ollama API error: ${response.status} ${response.statusText} - ${errorBody}`);
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Ollama service error:', error);
|
||||
throw error;
|
||||
|
||||
// For streaming, we return an object that has a callback for handling the stream
|
||||
return {
|
||||
text: "", // Initial empty text that will be built up
|
||||
model: model,
|
||||
provider: this.getName(),
|
||||
usage: {
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 0
|
||||
},
|
||||
stream: async (callback) => {
|
||||
if (!response.body) {
|
||||
throw new Error("No response body from Ollama");
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
let fullText = "";
|
||||
let partialLine = "";
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
// Convert the chunk to text
|
||||
const chunk = new TextDecoder().decode(value);
|
||||
partialLine += chunk;
|
||||
|
||||
// Split by lines and process each complete JSON object
|
||||
const lines = partialLine.split('\n');
|
||||
|
||||
// Process all complete lines except the last one (which might be incomplete)
|
||||
for (let i = 0; i < lines.length - 1; i++) {
|
||||
const line = lines[i].trim();
|
||||
if (!line) continue;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(line);
|
||||
console.log("Streaming chunk received:", data);
|
||||
|
||||
if (data.message && data.message.content) {
|
||||
// Extract just the new content
|
||||
const newContent = data.message.content;
|
||||
// Add to full text
|
||||
fullText += newContent;
|
||||
// Call the callback with the new content
|
||||
await callback({
|
||||
text: newContent,
|
||||
done: false
|
||||
});
|
||||
}
|
||||
|
||||
if (data.done) {
|
||||
// Final message in the stream
|
||||
await callback({
|
||||
text: "",
|
||||
done: true,
|
||||
usage: {
|
||||
promptTokens: data.prompt_eval_count || 0,
|
||||
completionTokens: data.eval_count || 0,
|
||||
totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0)
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error parsing JSON from Ollama stream:", err, "Line:", line);
|
||||
}
|
||||
}
|
||||
|
||||
// Keep the potentially incomplete last line for the next iteration
|
||||
partialLine = lines[lines.length - 1];
|
||||
}
|
||||
|
||||
// Handle any remaining content in partialLine
|
||||
if (partialLine.trim()) {
|
||||
try {
|
||||
const data = JSON.parse(partialLine.trim());
|
||||
if (data.message && data.message.content) {
|
||||
fullText += data.message.content;
|
||||
await callback({
|
||||
text: data.message.content,
|
||||
done: false
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error parsing final JSON from Ollama stream:", err);
|
||||
}
|
||||
}
|
||||
|
||||
return fullText;
|
||||
} catch (err) {
|
||||
console.error("Error reading Ollama stream:", err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
} else {
|
||||
// Non-streaming response - explicitly request JSON format
|
||||
console.log("Sending to Ollama with formatted messages:", JSON.stringify(formattedMessages, null, 2));
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages: formattedMessages,
|
||||
stream: false,
|
||||
options: {
|
||||
temperature,
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text();
|
||||
throw new Error(`Ollama API error: ${response.status} ${response.statusText} - ${errorBody}`);
|
||||
}
|
||||
|
||||
const rawResponseText = await response.text();
|
||||
console.log("Raw response from Ollama:", rawResponseText);
|
||||
|
||||
let data;
|
||||
|
||||
try {
|
||||
data = JSON.parse(rawResponseText);
|
||||
console.log("Parsed Ollama response:", JSON.stringify(data, null, 2));
|
||||
} catch (err: any) {
|
||||
console.error("Error parsing JSON response from Ollama:", err);
|
||||
console.error("Raw response:", rawResponseText);
|
||||
throw new Error(`Failed to parse Ollama response as JSON: ${err.message}`);
|
||||
}
|
||||
|
||||
// Check for empty or JSON object responses
|
||||
const content = data.message?.content || '';
|
||||
let finalResponseText = content;
|
||||
|
||||
if (content === '{}' || content === '{ }' || content === '{ }') {
|
||||
finalResponseText = "I don't have information about that in my notes.";
|
||||
} else if (!content.trim()) {
|
||||
finalResponseText = "No response was generated. Please try asking a different question.";
|
||||
}
|
||||
|
||||
return {
|
||||
text: finalResponseText,
|
||||
model: data.model || model,
|
||||
provider: this.getName(),
|
||||
usage: {
|
||||
promptTokens: data.prompt_eval_count || 0,
|
||||
completionTokens: data.eval_count || 0,
|
||||
totalTokens: (data.prompt_eval_count || 0) + (data.eval_count || 0)
|
||||
}
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Ollama service error:", error);
|
||||
throw new Error(`Ollama service error: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
private formatMessages(messages: Message[], systemPrompt: string): any[] {
|
||||
console.log("Input messages for formatting:", JSON.stringify(messages, null, 2));
|
||||
|
||||
// Check if there are any messages with empty content
|
||||
const emptyMessages = messages.filter(msg => !msg.content || msg.content === "Empty message");
|
||||
if (emptyMessages.length > 0) {
|
||||
console.warn("Found messages with empty content:", emptyMessages);
|
||||
}
|
||||
|
||||
// Add system message if it doesn't exist
|
||||
const hasSystemMessage = messages.some(m => m.role === 'system');
|
||||
let resultMessages = [...messages];
|
||||
@@ -80,6 +238,21 @@ export class OllamaService extends BaseAIService {
|
||||
});
|
||||
}
|
||||
|
||||
// Validate each message has content
|
||||
resultMessages = resultMessages.map(msg => {
|
||||
// Ensure each message has a valid content
|
||||
if (!msg.content || typeof msg.content !== 'string') {
|
||||
console.warn(`Message with role ${msg.role} has invalid content:`, msg.content);
|
||||
return {
|
||||
...msg,
|
||||
content: msg.content || "Empty message"
|
||||
};
|
||||
}
|
||||
return msg;
|
||||
});
|
||||
|
||||
console.log("Formatted messages for Ollama:", JSON.stringify(resultMessages, null, 2));
|
||||
|
||||
// Ollama uses the same format as OpenAI for messages
|
||||
return resultMessages;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user