Repository navigation
Expand file tree
/
Copy pathdigest.php
More file actions
137 lines (110 loc) · 3.44 KB
/
Copy pathdigest.php
File metadata and controls
137 lines (110 loc) · 3.44 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
// digest.php – erzeugt täglichen News-Digest
date_default_timezone_set('Europe/Berlin');
// Helper: Log mit Zeitstempel hh:mm:ss
function logMsg($msg) {
echo date('H:i:s') . " $msg\n";
}
// Modell definieren
$model = "llama3.2:latest";
// Sicherheitsprüfung
if (!isset($model) || empty($model)) {
logMsg("Fehler: Kein Modell definiert.");
exit(1);
}
// Ollama-Host (zentrale Variable – wird überall verwendet)
$ollamaHost = "http://100.92.3.18:11434";
// Adresse für Ollama Strato: 172.16.6.4
// Adresse für Ollama Dockfish: 100.92.3.18
// Prüfen, ob Ollama erreichbar ist und Modell existiert
$ch = curl_init("$ollamaHost/api/tags");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5
]);
$tagResponse = curl_exec($ch);
if ($tagResponse === false) {
logMsg("Fehler: Ollama nicht erreichbar - " . curl_error($ch));
curl_close($ch);
exit(1);
}
curl_close($ch);
$data = json_decode($tagResponse, true);
$models = array_column($data['models'] ?? [], 'name');
if (!in_array($model, $models)) {
logMsg("Fehler: Modell '$model' nicht gefunden.");
exit(1);
}
$newsFile = __DIR__ . '/data/news.json';
if (!file_exists($newsFile)) {
logMsg("Fehler: news.json nicht gefunden.");
exit(1);
}
$newsData = json_decode(file_get_contents($newsFile), true);
$articles = $newsData['articles'] ?? [];
$since = time() - 86400; // letzte 24 Stunden
// 1. Nur Artikel der letzten 24h
$filtered = array_filter($articles, function($a) use ($since) {
return strtotime($a['published'] ?? '') >= $since;
});
// 2. Maximal 2 Artikel pro Quelle
$grouped = [];
foreach ($filtered as $a) {
$source = strtolower(trim($a['source'] ?? 'unbekannt'));
if (!isset($grouped[$source])) {
$grouped[$source] = [];
}
if (count($grouped[$source]) < 2) {
$grouped[$source][] = $a;
}
}
// 3. LLM-Input bauen
$llmInput = "";
foreach ($grouped as $source => $items) {
foreach ($items as $a) {
$llmInput .= "Quelle: {$a['source']}\n";
$llmInput .= "{$a['title']}\n";
$llmInput .= "{$a['snippet']}\n\n";
}
}
if (empty($llmInput)) {
logMsg("Keine aktuellen Artikel gefunden.");
exit(0);
}
$prompt = "Erstelle eine sachliche Zusammenfassung der wichtigsten Themen aus diesen Meldungen.\n"
. "Ordne nach Relevanz. Ignoriere Sport und Klatsch.\n"
. "Kennzeichne innenpolitisch, außenpolitisch und wirtschaftlich.\n\n"
. $llmInput;
// 4. Ollama ansprechen – AUSSCHLIESSLICH über Variable $ollamaHost
$ch = curl_init("$ollamaHost/api/generate");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
"model" => $model,
"prompt" => $prompt,
"stream" => false
])
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
logMsg("Fehler: Ollama API returned HTTP $httpCode");
exit(1);
}
$result = json_decode($response, true)['response'] ?? null;
if ($result === null) {
logMsg("Fehler: Keine gültige Antwort von Ollama.");
exit(1);
}
// 5. Speichern
$today = date('Y-m-d');
$digestPath = __DIR__ . "/data/digest-$today.txt";
if (file_put_contents($digestPath, $result) === false) {
logMsg("Fehler: Konnte Digest nicht speichern.");
exit(1);
}
logMsg("Digest erstellt: digest-$today.txt");
exit(0);