Seventh Commit

This commit is contained in:
Mohammad Zwaib
2026-07-10 22:55:00 +02:00
parent 308eeb00af
commit d6153521ca
10 changed files with 47 additions and 43 deletions
BIN
View File
Binary file not shown.
@@ -7,26 +7,24 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.homme.demo.dto.ProcessedWordFileResponseDto;
import com.homme.demo.service.HumbeeService;
@RestController("humbee")
@RestController
@RequestMapping("/knowledge-base")
public class HumbeeController {
@Autowired
private HumbeeService humbeeService;
@GetMapping("/word-links")
public List<ProcessedWordFileResponseDto> getWordLinks() throws Exception {
@GetMapping("/build")
public List<ProcessedWordFileResponseDto> buildKnowledgeBase() throws Exception {
return humbeeService.getWordLinks();
return humbeeService.buildKnowledgeBase();
}
@@ -3,12 +3,10 @@ package com.homme.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.homme.demo.dto.AskResponse;
import com.homme.demo.input.AskRequest;
import com.homme.demo.service.RagService;
@RestController
@@ -19,9 +17,9 @@ public class SearchController {
@PostMapping("/ask")
public AskResponse ask(@RequestBody AskRequest request) {
public AskResponse ask(@RequestParam String question) {
String question = (request == null) ? null : request.getQuestion();
if (question == null || question.isBlank()) {
return AskResponse.builder()
@@ -1,15 +0,0 @@
package com.homme.demo.input;
import lombok.Getter;
import lombok.Setter;
/**
* Request body for the chatbot endpoint: { "question": "..." }.
*/
@Setter
@Getter
public class AskRequest {
private String question;
}
@@ -16,8 +16,8 @@ public class DocumentChunckService {
if(text == null || text.isBlank())
return chunks;
int chunkSize = 6000;
int overlab = 200;
int chunkSize = 1200;
int overlab = 150;
int start = 0;
while(start < text.length()) {
@@ -73,13 +73,13 @@ public class HumbeeService {
.build();
}
public List<ProcessedWordFileResponseDto> getWordLinks() throws Exception{
public List<ProcessedWordFileResponseDto> buildKnowledgeBase() throws Exception{
login();
String json = getDiroctoryJson();
List<String> links = extractWordLinks(json);
int numFiles = 0;
int numFilesResult =0;
ExecutorService pool = Executors.newFixedThreadPool(6);
try {
@@ -87,7 +87,7 @@ public class HumbeeService {
for(String link :links) {
numFiles++;
futures.add(pool.submit(() -> processOneFile(link)));
}
@@ -95,6 +95,7 @@ public class HumbeeService {
for(Future<ProcessedWordFileResponseDto> f: futures) {
numFilesResult++;
result.add(f.get());
}
return result;
@@ -104,6 +105,7 @@ public class HumbeeService {
Thread.currentThread().interrupt();
throw new RuntimeException("Fehler bei paralleler Dateiverarbeitung: " + e.getMessage());
} finally {
System.out.println("$$$$$$$$ ="+links.size()+" numFiles = "+numFiles +" numFilesResult= "+ numFilesResult);
pool.shutdown();
}
@@ -152,7 +154,6 @@ public class HumbeeService {
public void login() {
String loginPath = "/account/login";
sessionCookies.clear();
Map<String, String> requestBody = new HashMap<>();
requestBody.put("email", email);
@@ -165,7 +166,6 @@ public class HumbeeService {
.bodyValue(requestBody)
.exchangeToMono(res -> Mono.just(res))
.block();
System.out.println("response = "+response);
if(response == null) {
throw new RuntimeException("Anmeldung fehlgeschlagen: Keine Antwort vom Server.");
@@ -213,7 +213,7 @@ public class HumbeeService {
}
}
public List<String> extractWordLinks(String json) throws Exception{
List<String> links = new ArrayList<>();
@@ -231,7 +231,8 @@ public class HumbeeService {
for(JsonNode action: quickActions) {
String id = action.path("id").asText();
String href = action.path("href").asText();
if("download".equals(id) && (href.contains(".docx?") || href.contains(".doc?"))) {
if("download".equals(id) && (href.contains(".docx?") || href.contains(".doc?") || href.contains(".txt"))) {
links.add("https://cloud.humbee.de"+href);
}
@@ -278,6 +279,11 @@ public class HumbeeService {
}
}
else if(downloadHref.contains(".txt")){
return decodeBytes(fileBytes);
}
throw new RuntimeException("Nicht unterstützer Dateityp");
}
@@ -287,6 +293,26 @@ public class HumbeeService {
throw new RuntimeException("Nicht angeledet. Bitte zuerst einloggen.");
}
}
public String decodeBytes(byte[] bytes) {
if (bytes == null || bytes.length == 0) return "";
if (bytes.length >= 3 && (bytes[0]&0xFF)==0xEF && (bytes[1]&0xFF)==0xBB && (bytes[2]&0xFF)==0xBF)
return new String(bytes, 3, bytes.length-3, java.nio.charset.StandardCharsets.UTF_8);
if (bytes.length >= 2 && (bytes[0]&0xFF)==0xFF && (bytes[1]&0xFF)==0xFE)
return new String(bytes, 2, bytes.length-2, java.nio.charset.StandardCharsets.UTF_16LE);
if (bytes.length >= 2 && (bytes[0]&0xFF)==0xFE && (bytes[1]&0xFF)==0xFF)
return new String(bytes, 2, bytes.length-2, java.nio.charset.StandardCharsets.UTF_16BE);
int nulls = 0;
for (int i = 1; i < Math.min(bytes.length, 400); i += 2)
if (bytes[i] == 0) nulls++;
if (nulls > 50)
return new String(bytes, java.nio.charset.StandardCharsets.UTF_16LE);
return new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ spring.application.name=Homme
# Load secrets from .env (classpath) so ${DB_PASSWORD}, ${HUMBEE_PASSWORD},
# ${AZURE_EMBEDDING_KEY}, ${OPENAI_API_MISTRAL_KEY} below resolve from it.
# "optional:" => app still starts if .env is absent (e.g. secrets come from real env vars).
#spring.config.import=optional:classpath:/.env[.properties]
spring.config.import=optional:classpath:/.env[.properties]
server.port =9192