?整體邏輯:
自建系統的web UI界面調用接口:
1.SpringBoot接口:/anything/chatMessageAnything
2.調用anythingLLM - 調用知識庫+deepseek r1 .
?
Windows Installation ~ AnythingLLMhttps://docs.anythingllm.com/installation-desktop/windows
http://localhost:3001/api/docs/http://localhost:3001/api/docs/
要在 Spring Boot 中調用 AnythingLLM 的發消息接口,可以通過以下步驟實現。假設你已經了解 AnythingLLM 的 API 文檔(如端點、認證方式、請求參數等),以下是通用實現方案:
1. 添加依賴
在 pom.xml
中添加 Spring Web
依賴(如果項目中沒有):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. 配置 AnythingLLM API 參數
在 application.properties
或 application.yml
中配置 API 的基礎信息:
properties
# application.properties
anythingllm.api.url=https://api.anythingllm.com/v1/messages
anythingllm.api.key=your_api_key_here
3. 創建 API 調用服務
使用 RestTemplate
或 WebClient
調用 AnythingLLM 的接口。以下是兩種實現方式:
方式一:使用 RestTemplate
java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class AnythingLLMService {
private final RestTemplate restTemplate;
private final String apiUrl;
private final String apiKey;
public AnythingLLMService(
RestTemplate restTemplate,
@Value("${anythingllm.api.url}") String apiUrl,
@Value("${anythingllm.api.key}") String apiKey) {
this.restTemplate = restTemplate;
this.apiUrl = apiUrl;
this.apiKey = apiKey;
}
public ResponseEntity<String> sendMessage(String messageContent) {
// 設置請求頭(包含 API Key)
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + apiKey);
headers.set("Content-Type", "application/json");
// 構建請求體
String requestBody = "{\"content\": \"" + messageContent + "\"}";
// 發送 POST 請求
HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
return restTemplate.exchange(
apiUrl,
HttpMethod.POST,
request,
String.class
);
}
}
方式二:使用 WebClient(推薦響應式編程)
java
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class AnythingLLMService {
private final WebClient webClient;
private final String apiUrl;
private final String apiKey;
public AnythingLLMService(
WebClient.Builder webClientBuilder,
@Value("${anythingllm.api.url}") String apiUrl,
@Value("${anythingllm.api.key}") String apiKey) {
this.webClient = webClientBuilder.baseUrl(apiUrl).build();
this.apiUrl = apiUrl;
this.apiKey = apiKey;
}
public Mono<String> sendMessage(String messageContent) {
return webClient.post()
.uri("/")
.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue("{\"content\": \"" + messageContent + "\"}")
.retrieve()
.bodyToMono(String.class);
}
}
4. 控制器層(可選)
如果需要通過 HTTP 接口觸發消息發送,可以添加一個 Controller:
java
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/api/messages")
public class MessageController {
private final AnythingLLMService anythingLLMService;
public MessageController(AnythingLLMService anythingLLMService) {
this.anythingLLMService = anythingLLMService;
}
@PostMapping
public Mono<String> sendMessage(@RequestBody String messageContent) {
return anythingLLMService.sendMessage(messageContent);
}
}
5. 異常處理
添加全局異常處理,捕獲 API 調用中的錯誤:
java
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(HttpClientErrorException.class)
public ResponseEntity<String> handleHttpClientError(HttpClientErrorException ex) {
return ResponseEntity.status(ex.getStatusCode())
.body("API調用失敗: " + ex.getResponseBodyAsString());
}
}
6. 測試調用
使用 RestTemplate 測試
java
@SpringBootTest
public class AnythingLLMServiceTest {
@Autowired
private AnythingLLMService anythingLLMService;
@Test
public void testSendMessage() {
ResponseEntity<String> response = anythingLLMService.sendMessage("Hello, AnythingLLM!");
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}
使用 WebClient 測試
java
@Test
public void testSendMessage() {
anythingLLMService.sendMessage("Hello, AnythingLLM!")
.subscribe(response -> {
assertNotNull(response);
System.out.println("API Response: " + response);
});
}
注意事項
- API 認證 :根據 AnythingLLM 的文檔調整認證方式(如 API Key、OAuth 等)。
- 請求體格式 :確保請求體的 JSON 結構與 API 要求一致(如字段名稱、嵌套結構)。
- 超時配置 :如果接口響應較慢,需配置
RestTemplate
或WebClient
的超時時間。 - 日志記錄 :建議在調用過程中添加日志,方便調試和監控。
如果需要更具體的實現(如文件上傳、復雜參數),請提供 AnythingLLM 的 API 文檔細節!