-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathollamaService.js
95 lines (84 loc) · 3.32 KB
/
ollamaService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const axios = require('axios');
const config = require('../config/config');
class OllamaService {
constructor() {
this.apiUrl = config.ollama.apiUrl;
this.model = config.ollama.model;
this.client = axios.create({
timeout: 300000 // 5 Minuten Timeout
});
}
async analyzeDocument(content, existingTags) {
try {
const prompt = this._buildPrompt(content, existingTags);
const response = await this.client.post(`${this.apiUrl}/api/generate`, {
model: this.model,
prompt: prompt,
system: `
You are a document analyzer. Your task is to analyze documents and extract relevant information. You do not ask back questions.
YOU MUST: Analyze the document content and extract the following information into this structured JSON format and only this format!: {
"title": "xxxxx",
"correspondent": "xxxxxxxx",
"tags": ["Tag1", "Tag2", "Tag3", "Tag4"],
"document_date": "YYYY-MM-DD",
"language": "en/de/es/..."
}
ALWAYS USE THE INFORMATION TO FILL OUT THE JSON OBJECT. DO NOT ASK BACK QUESTIONS.
`,
stream: false,
options: {
temperature: 0.7, // Kreativität (0.0 - 1.0)
top_p: 0.9, // Nucleus sampling
repeat_penalty: 1.1 // Verhindert Wiederholungen
}
});
// Prüfe explizit auf Response-Fehler
if (!response.data || !response.data.response) {
console.error('Unexpected Ollama response format:', response);
throw new Error('Invalid response from Ollama API');
}
return this._parseResponse(response.data.response);
} catch (error) {
if (error.code === 'ECONNABORTED') {
console.error('Timeout bei der Ollama-Anfrage:', error);
throw new Error('Die Analyse hat zu lange gedauert. Bitte versuchen Sie es erneut.');
}
console.error('Error analyzing document with Ollama:', error);
throw error;
}
}
_buildPrompt(content) {
if(process.env.USE_PROMPT_TAGS === 'yes') {
//get tags from PROMPT_TAGS (comma separated)
promptTags = process.env.PROMPT_TAGS;
systemPrompt = config.specialPromptPreDefinedTags;
return systemPrompt + '\n\n' + JSON.stringify(content);
}else{
return process.env.SYSTEM_PROMPT + '\n\n' + JSON.stringify(content);
}
}
_parseResponse(response) {
try {
// Find JSON in response using regex
const jsonMatch = response.match(/\{[\s\S]*\}/);
if (!jsonMatch) {
console.warn('No JSON found in response:', response);
return { tags: [], correspondent: null };
}
const jsonStr = jsonMatch[0];
console.log('Extracted JSON:', jsonStr);
// Try to parse the extracted JSON
const result = JSON.parse(jsonStr);
// Validate and return structured data
return {
tags: Array.isArray(result.tags) ? result.tags : [],
correspondent: typeof result.correspondent === 'string' ? result.correspondent : null
};
} catch (error) {
console.error('Error parsing Ollama response:', error);
console.error('Raw response:', response);
return { tags: [], correspondent: null };
}
}
}
module.exports = new OllamaService();