add local llm
This commit is contained in:
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}")
|
||||
private String anthropicModel;
|
||||
|
||||
@Value("${api.ollama.url}")
|
||||
private String ollamaUrl;
|
||||
|
||||
@Value("${api.ollama.model}")
|
||||
private String ollamaModel;
|
||||
|
||||
private final OkHttpClient httpClient;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final MediaType jsonMediaType = MediaType.parse("application/json");
|
||||
@@ -52,6 +58,8 @@ public class LLMService {
|
||||
return analyzeWithOpenAI(prompt);
|
||||
case "anthropic":
|
||||
return analyzeWithAnthropic(prompt);
|
||||
case "ollama":
|
||||
return analyzeWithOllama(prompt);
|
||||
default:
|
||||
return new LLMAnalysis("", 0, "", "Unknown provider: " + provider);
|
||||
}
|
||||
@@ -127,6 +135,44 @@ public class LLMService {
|
||||
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) {
|
||||
if (openaiApiKey == null || openaiApiKey.isBlank()) {
|
||||
return new LLMAnalysis("", 0, "", "OpenAI API Key missing");
|
||||
|
||||
@@ -17,6 +17,9 @@ api:
|
||||
anthropic:
|
||||
key: ${ANTHROPIC_API_KEY:}
|
||||
model: ${ANTHROPIC_MODEL:claude-sonnet-4-5}
|
||||
ollama:
|
||||
url: ${OLLAMA_URL:http://192.169.178.64:11434}
|
||||
model: ${OLLAMA_MODEL:gemma2:27b}
|
||||
news:
|
||||
key: ${NEWS_API_KEY:}
|
||||
|
||||
|
||||
Generated
+1669
File diff suppressed because it is too large.
Load diff
@@ -24,6 +24,9 @@ const expanded = ref(false)
|
||||
<span class="badge badge-warning">
|
||||
Anthropic: {{ result.llmAnalysisAnthropic?.prediction || 'N/A' }}
|
||||
</span>
|
||||
<span class="badge badge-success">
|
||||
Ollama: {{ result.llmAnalysisOllama?.prediction || 'N/A' }}
|
||||
</span>
|
||||
</div>
|
||||
<button class="expand-btn">
|
||||
{{ expanded ? '▼' : '▶' }}
|
||||
@@ -169,6 +172,35 @@ const expanded = ref(false)
|
||||
<div class="text-muted">Analysis not available</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>
|
||||
|
||||
<!-- News & Sentiment -->
|
||||
|
||||
@@ -50,6 +50,7 @@ export interface AnalysisResult {
|
||||
socialPosts: string[]
|
||||
llmAnalysisOpenai: LLMAnalysis
|
||||
llmAnalysisAnthropic: LLMAnalysis
|
||||
llmAnalysisOllama?: LLMAnalysis
|
||||
analyzedAt: string
|
||||
date: string
|
||||
targetDate: string
|
||||
|
||||
@@ -138,6 +138,7 @@ async function handleStop() {
|
||||
<th>Change</th>
|
||||
<th>OpenAI</th>
|
||||
<th>Anthropic</th>
|
||||
<th>Ollama</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -176,6 +177,13 @@ async function handleStop() {
|
||||
</span>
|
||||
<span v-else class="text-muted">N/A</span>
|
||||
</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>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -61,7 +61,7 @@ const tableData = computed(() => {
|
||||
isin,
|
||||
symbol,
|
||||
latestResult: null,
|
||||
accuracy: { openaiPct: 0, anthropicPct: 0, total: 0 }
|
||||
accuracy: { openaiPct: 0, anthropicPct: 0, ollamaPct: 0, total: 0 }
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -106,7 +106,7 @@ const filteredData = computed(() => {
|
||||
} else if (sortBy.value === 'symbol') {
|
||||
return a.symbol.localeCompare(b.symbol)
|
||||
} 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
|
||||
})
|
||||
@@ -122,6 +122,7 @@ const selectedDetails = computed(() => {
|
||||
function calculateAccuracy(results: AnalysisResult[]) {
|
||||
let openaiCorrect = 0
|
||||
let anthropicCorrect = 0
|
||||
let ollamaCorrect = 0
|
||||
let total = 0
|
||||
|
||||
for (const result of results) {
|
||||
@@ -133,23 +134,37 @@ function calculateAccuracy(results: AnalysisResult[]) {
|
||||
// OpenAI
|
||||
const openaiPred = result.llmAnalysisOpenai?.prediction?.toUpperCase() || ''
|
||||
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++
|
||||
}
|
||||
|
||||
// Anthropic
|
||||
const anthropicPred = result.llmAnalysisAnthropic?.prediction?.toUpperCase() || ''
|
||||
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++
|
||||
}
|
||||
|
||||
// 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 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() {
|
||||
@@ -304,6 +319,7 @@ function getJobStatus(isin: string) {
|
||||
<th>Target Date</th>
|
||||
<th>Latest Prediction (OpenAI)</th>
|
||||
<th>Latest Prediction (Anthropic)</th>
|
||||
<th>Latest Prediction (Ollama)</th>
|
||||
<th>Accuracy</th>
|
||||
<th>History Size</th>
|
||||
<th>Action</th>
|
||||
@@ -333,10 +349,17 @@ function getJobStatus(isin: string) {
|
||||
</span>
|
||||
<span v-else class="text-muted">N/A</span>
|
||||
</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>
|
||||
<div class="flex flex-col gap-1" style="font-size: 0.75rem;">
|
||||
<span class="text-info">OpenAI: {{ item.accuracy.openaiPct }}%</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>
|
||||
</div>
|
||||
</td>
|
||||
@@ -376,6 +399,9 @@ function getJobStatus(isin: string) {
|
||||
<span class="badge badge-warning">
|
||||
Anthropic Accuracy: {{ calculateAccuracy(selectedDetails).anthropicPct }}%
|
||||
</span>
|
||||
<span class="badge badge-success">
|
||||
Ollama Accuracy: {{ calculateAccuracy(selectedDetails).ollamaPct }}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -389,6 +415,7 @@ function getJobStatus(isin: string) {
|
||||
<th>Real Price</th>
|
||||
<th>OpenAI</th>
|
||||
<th>Anthropic</th>
|
||||
<th>Ollama</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -437,6 +464,18 @@ function getJobStatus(isin: string) {
|
||||
</div>
|
||||
<div v-else class="text-muted">N/A</div>
|
||||
</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>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
Reference in new issue
Block a user