add local llm

This commit is contained in:
eduard committed 2025-12-12 16:34:29 +01:00
1 parent 3568453103
commit 88272b19a8
7 files changed
+1803 -5

No files matched your search

@@ -30,6 +30,12 @@ public class LLMService {
@Value("${api.anthropic.model}") @Value("${api.anthropic.model}")
private String anthropicModel; private String anthropicModel;
@Value("${api.ollama.url}")
private String ollamaUrl;
@Value("${api.ollama.model}")
private String ollamaModel;
private final OkHttpClient httpClient; private final OkHttpClient httpClient;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
private final MediaType jsonMediaType = MediaType.parse("application/json"); private final MediaType jsonMediaType = MediaType.parse("application/json");
@@ -52,6 +58,8 @@ public class LLMService {
return analyzeWithOpenAI(prompt); return analyzeWithOpenAI(prompt);
case "anthropic": case "anthropic":
return analyzeWithAnthropic(prompt); return analyzeWithAnthropic(prompt);
case "ollama":
return analyzeWithOllama(prompt);
default: default:
return new LLMAnalysis("", 0, "", "Unknown provider: " + provider); return new LLMAnalysis("", 0, "", "Unknown provider: " + provider);
} }
@@ -127,6 +135,44 @@ public class LLMService {
String.join("\n", socialData)); String.join("\n", socialData));
} }
private LLMAnalysis analyzeWithOllama(String prompt) {
if (ollamaUrl == null || ollamaUrl.isBlank()) {
return new LLMAnalysis("", 0, "", "Ollama URL missing");
}
try {
Map<String, Object> requestMap = Map.of(
"model", ollamaModel,
"stream", false,
"format", "json",
"messages", List.of(
Map.of("role", "user", "content", prompt)));
String requestBodyString = objectMapper.writeValueAsString(requestMap);
RequestBody requestBody = RequestBody.create(requestBodyString, jsonMediaType);
Request request = new Request.Builder()
.url(ollamaUrl + "/api/chat")
.post(requestBody)
.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful() || response.body() == null) {
return new LLMAnalysis("", 0, "", "Ollama API error: " + response.code());
}
JsonNode json = objectMapper.readTree(response.body().string());
JsonNode message = json.get("message");
if (message != null && message.has("content")) {
return parseLLMResponse(message.get("content").asText());
}
return new LLMAnalysis("", 0, "", "No response from Ollama");
}
} catch (Exception e) {
return new LLMAnalysis("", 0, "", "Error during Ollama analysis: " + e.getMessage());
}
}
private LLMAnalysis analyzeWithOpenAI(String prompt) { private LLMAnalysis analyzeWithOpenAI(String prompt) {
if (openaiApiKey == null || openaiApiKey.isBlank()) { if (openaiApiKey == null || openaiApiKey.isBlank()) {
return new LLMAnalysis("", 0, "", "OpenAI API Key missing"); return new LLMAnalysis("", 0, "", "OpenAI API Key missing");
@@ -17,6 +17,9 @@ api:
anthropic: anthropic:
key: ${ANTHROPIC_API_KEY:} key: ${ANTHROPIC_API_KEY:}
model: ${ANTHROPIC_MODEL:claude-sonnet-4-5} model: ${ANTHROPIC_MODEL:claude-sonnet-4-5}
ollama:
url: ${OLLAMA_URL:http://192.169.178.64:11434}
model: ${OLLAMA_MODEL:gemma2:27b}
news: news:
key: ${NEWS_API_KEY:} key: ${NEWS_API_KEY:}
+1669
View File
File diff suppressed because it is too large. Load diff
+32
View File
@@ -24,6 +24,9 @@ const expanded = ref(false)
<span class="badge badge-warning"> <span class="badge badge-warning">
Anthropic: {{ result.llmAnalysisAnthropic?.prediction || 'N/A' }} Anthropic: {{ result.llmAnalysisAnthropic?.prediction || 'N/A' }}
</span> </span>
<span class="badge badge-success">
Ollama: {{ result.llmAnalysisOllama?.prediction || 'N/A' }}
</span>
</div> </div>
<button class="expand-btn"> <button class="expand-btn">
{{ expanded ? '▼' : '▶' }} {{ expanded ? '▼' : '▶' }}
@@ -169,6 +172,35 @@ const expanded = ref(false)
<div class="text-muted">Analysis not available</div> <div class="text-muted">Analysis not available</div>
</div> </div>
</div> </div>
<!-- Ollama -->
<div class="analysis-box mt-2">
<div class="analysis-header">
<span class="badge badge-success">Ollama (Gemma 2 27B)</span>
</div>
<div class="analysis-content" v-if="result.llmAnalysisOllama">
<div class="flex gap-2 mb-2">
<div class="metric-card" style="flex: 1;">
<div class="metric-label">Prediction</div>
<div class="metric-value" style="font-size: 1.1rem;">
{{ result.llmAnalysisOllama.prediction }}
</div>
</div>
<div class="metric-card" style="flex: 1;">
<div class="metric-label">Probability</div>
<div class="metric-value" style="font-size: 1.1rem;">
{{ result.llmAnalysisOllama.probability }}%
</div>
</div>
</div>
<div class="reasoning">
<strong>Reasoning:</strong> {{ result.llmAnalysisOllama.reasoning }}
</div>
</div>
<div class="analysis-content" v-else>
<div class="text-muted">Analysis not available</div>
</div>
</div>
</div> </div>
<!-- News & Sentiment --> <!-- News & Sentiment -->
+1
View File
@@ -50,6 +50,7 @@ export interface AnalysisResult {
socialPosts: string[] socialPosts: string[]
llmAnalysisOpenai: LLMAnalysis llmAnalysisOpenai: LLMAnalysis
llmAnalysisAnthropic: LLMAnalysis llmAnalysisAnthropic: LLMAnalysis
llmAnalysisOllama?: LLMAnalysis
analyzedAt: string analyzedAt: string
date: string date: string
targetDate: string targetDate: string
+8
View File
@@ -138,6 +138,7 @@ async function handleStop() {
<th>Change</th> <th>Change</th>
<th>OpenAI</th> <th>OpenAI</th>
<th>Anthropic</th> <th>Anthropic</th>
<th>Ollama</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@@ -176,6 +177,13 @@ async function handleStop() {
</span> </span>
<span v-else class="text-muted">N/A</span> <span v-else class="text-muted">N/A</span>
</td> </td>
<td>
<span v-if="result.llmAnalysisOllama" class="badge badge-success">
{{ result.llmAnalysisOllama.prediction }}
({{ result.llmAnalysisOllama.probability }}%)
</span>
<span v-else class="text-muted">N/A</span>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
+44 -5
View File
@@ -61,7 +61,7 @@ const tableData = computed(() => {
isin, isin,
symbol, symbol,
latestResult: null, latestResult: null,
accuracy: { openaiPct: 0, anthropicPct: 0, total: 0 } accuracy: { openaiPct: 0, anthropicPct: 0, ollamaPct: 0, total: 0 }
}) })
return return
} }
@@ -106,7 +106,7 @@ const filteredData = computed(() => {
} else if (sortBy.value === 'symbol') { } else if (sortBy.value === 'symbol') {
return a.symbol.localeCompare(b.symbol) return a.symbol.localeCompare(b.symbol)
} else if (sortBy.value === 'accuracy') { } else if (sortBy.value === 'accuracy') {
return (b.accuracy.openaiPct + b.accuracy.anthropicPct) - (a.accuracy.openaiPct + a.accuracy.anthropicPct) return (b.accuracy.openaiPct + b.accuracy.anthropicPct + b.accuracy.ollamaPct) - (a.accuracy.openaiPct + a.accuracy.anthropicPct + a.accuracy.ollamaPct)
} }
return 0 return 0
}) })
@@ -122,6 +122,7 @@ const selectedDetails = computed(() => {
function calculateAccuracy(results: AnalysisResult[]) { function calculateAccuracy(results: AnalysisResult[]) {
let openaiCorrect = 0 let openaiCorrect = 0
let anthropicCorrect = 0 let anthropicCorrect = 0
let ollamaCorrect = 0
let total = 0 let total = 0
for (const result of results) { for (const result of results) {
@@ -133,23 +134,37 @@ function calculateAccuracy(results: AnalysisResult[]) {
// OpenAI // OpenAI
const openaiPred = result.llmAnalysisOpenai?.prediction?.toUpperCase() || '' const openaiPred = result.llmAnalysisOpenai?.prediction?.toUpperCase() || ''
if ((openaiPred.includes('BULLISH') && endPrice > startPrice) || if ((openaiPred.includes('BULLISH') && endPrice > startPrice) ||
(openaiPred.includes('BEARISH') && endPrice < startPrice)) { (openaiPred.includes('BEARISH') && endPrice < startPrice) ||
(openaiPred.includes('RISE') && endPrice > startPrice) ||
(openaiPred.includes('FALL') && endPrice < startPrice)) {
openaiCorrect++ openaiCorrect++
} }
// Anthropic // Anthropic
const anthropicPred = result.llmAnalysisAnthropic?.prediction?.toUpperCase() || '' const anthropicPred = result.llmAnalysisAnthropic?.prediction?.toUpperCase() || ''
if ((anthropicPred.includes('BULLISH') && endPrice > startPrice) || if ((anthropicPred.includes('BULLISH') && endPrice > startPrice) ||
(anthropicPred.includes('BEARISH') && endPrice < startPrice)) { (anthropicPred.includes('BEARISH') && endPrice < startPrice) ||
(anthropicPred.includes('RISE') && endPrice > startPrice) ||
(anthropicPred.includes('FALL') && endPrice < startPrice)) {
anthropicCorrect++ anthropicCorrect++
} }
// Ollama
const ollamaPred = result.llmAnalysisOllama?.prediction?.toUpperCase() || ''
if ((ollamaPred.includes('BULLISH') && endPrice > startPrice) ||
(ollamaPred.includes('BEARISH') && endPrice < startPrice) ||
(ollamaPred.includes('RISE') && endPrice > startPrice) ||
(ollamaPred.includes('FALL') && endPrice < startPrice)) {
ollamaCorrect++
}
} }
} }
const openaiPct = total > 0 ? Math.round((openaiCorrect / total) * 100) : 0 const openaiPct = total > 0 ? Math.round((openaiCorrect / total) * 100) : 0
const anthropicPct = total > 0 ? Math.round((anthropicCorrect / total) * 100) : 0 const anthropicPct = total > 0 ? Math.round((anthropicCorrect / total) * 100) : 0
const ollamaPct = total > 0 ? Math.round((ollamaCorrect / total) * 100) : 0
return { openaiPct, anthropicPct, total } return { openaiPct, anthropicPct, ollamaPct, total }
} }
async function handleDeleteByDateRange() { async function handleDeleteByDateRange() {
@@ -304,6 +319,7 @@ function getJobStatus(isin: string) {
<th>Target Date</th> <th>Target Date</th>
<th>Latest Prediction (OpenAI)</th> <th>Latest Prediction (OpenAI)</th>
<th>Latest Prediction (Anthropic)</th> <th>Latest Prediction (Anthropic)</th>
<th>Latest Prediction (Ollama)</th>
<th>Accuracy</th> <th>Accuracy</th>
<th>History Size</th> <th>History Size</th>
<th>Action</th> <th>Action</th>
@@ -333,10 +349,17 @@ function getJobStatus(isin: string) {
</span> </span>
<span v-else class="text-muted">N/A</span> <span v-else class="text-muted">N/A</span>
</td> </td>
<td>
<span v-if="item.latestResult?.llmAnalysisOllama" class="badge badge-success">
{{ item.latestResult.llmAnalysisOllama.prediction }}
</span>
<span v-else class="text-muted">N/A</span>
</td>
<td> <td>
<div class="flex flex-col gap-1" style="font-size: 0.75rem;"> <div class="flex flex-col gap-1" style="font-size: 0.75rem;">
<span class="text-info">OpenAI: {{ item.accuracy.openaiPct }}%</span> <span class="text-info">OpenAI: {{ item.accuracy.openaiPct }}%</span>
<span class="text-warning">Anthropic: {{ item.accuracy.anthropicPct }}%</span> <span class="text-warning">Anthropic: {{ item.accuracy.anthropicPct }}%</span>
<span class="text-success">Ollama: {{ item.accuracy.ollamaPct }}%</span>
<span class="text-muted" style="font-size: 0.7rem;">({{ item.accuracy.total }} verified)</span> <span class="text-muted" style="font-size: 0.7rem;">({{ item.accuracy.total }} verified)</span>
</div> </div>
</td> </td>
@@ -376,6 +399,9 @@ function getJobStatus(isin: string) {
<span class="badge badge-warning"> <span class="badge badge-warning">
Anthropic Accuracy: {{ calculateAccuracy(selectedDetails).anthropicPct }}% Anthropic Accuracy: {{ calculateAccuracy(selectedDetails).anthropicPct }}%
</span> </span>
<span class="badge badge-success">
Ollama Accuracy: {{ calculateAccuracy(selectedDetails).ollamaPct }}%
</span>
</div> </div>
</div> </div>
@@ -389,6 +415,7 @@ function getJobStatus(isin: string) {
<th>Real Price</th> <th>Real Price</th>
<th>OpenAI</th> <th>OpenAI</th>
<th>Anthropic</th> <th>Anthropic</th>
<th>Ollama</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@@ -437,6 +464,18 @@ function getJobStatus(isin: string) {
</div> </div>
<div v-else class="text-muted">N/A</div> <div v-else class="text-muted">N/A</div>
</td> </td>
<td>
<div v-if="result.llmAnalysisOllama">
<span class="badge badge-success">
{{ result.llmAnalysisOllama.prediction }}
</span>
<span class="text-muted"> ⏳</span>
</div>
<div v-if="result.llmAnalysisOllama" class="text-muted" style="font-size: 0.75rem; margin-top: 0.25rem;">
{{ result.llmAnalysisOllama.reasoning.substring(0, 100) }}...
</div>
<div v-else class="text-muted">N/A</div>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>