当前位置: 首页 > news >正文

在 Spring Boot 中调用 AnythingLLM 的发消息接口

 整体逻辑:

自建系统的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.propertiesapplication.yml 中配置 API 的基础信息:

properties

# application.properties

anythingllm.api.url=https://api.anythingllm.com/v1/messages

anythingllm.api.key=your_api_key_here


3. 创建 API 调用服务

使用 RestTemplateWebClient 调用 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);

});

}


注意事项

  1. API 认证 :根据 AnythingLLM 的文档调整认证方式(如 API Key、OAuth 等)。
  2. 请求体格式 :确保请求体的 JSON 结构与 API 要求一致(如字段名称、嵌套结构)。
  3. 超时配置 :如果接口响应较慢,需配置 RestTemplateWebClient 的超时时间。
  4. 日志记录 :建议在调用过程中添加日志,方便调试和监控。

如果需要更具体的实现(如文件上传、复杂参数),请提供 AnythingLLM 的 API 文档细节!

知识库文件上传

相关文章:

  • 如何确保爬虫遵守Shopee的使用条款?
  • uniapp实现全局拖拽按钮
  • 力扣算法Hot100——75. 颜色分类
  • GaussDB 资源管理指南:冻结、解冻、释放与生命周期控制
  • Node.js 中使用 RabbitMQ
  • 【Golang】go如何通过atomic原子操作来确保数据一致性
  • AFFiNE:下一代开源全能知识库工具,重新定义协作与创作
  • 深入理解JVM类加载机制:从原理到实践
  • 拓扑排序——117. 软件构建
  • 持续升级的电子实验记录本系统,更加好用、安全
  • 数据操作 + 数据预处理
  • JavaScript中的闭包:解锁函数的神秘力量
  • Linux--软硬链接、动静态库
  • Javascript 中事件环以及宏任务微任务详细介绍
  • VS2019 快捷键及各项功能汇总
  • 【GNN】0.环境配置
  • 【Pandas】pandas Index str
  • Quartus + VScode 实现模块化流水灯
  • 【Dive Into Stable Diffusion v3.5】1:开源项目正式发布——深入探索SDv3.5模型全参/LoRA/RLHF训练
  • DAPO:一个开源的大规模大型语言模型LLM强化学习系统
  • 十年磨一剑!上海科学家首次揭示宿主识别肠道菌群调控免疫新机制
  • 商务部新闻发言人就出口管制管控名单答记者问
  • 浙能集团原董事长童亚辉被查,还是杭州市书法家协会主席
  • 科技部等七部门:优先支持取得关键核心技术突破的科技型企业上市融资
  • 杭勇已任常州市政协党组成员,此前任常州市委常委、秘书长
  • 人才争夺战,二三线城市和一线城市拼什么?洛阳官方调研剖析