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:}
|
||||
|
||||
|
||||
Reference in new issue
Block a user