跟踪一个aiService的调用流程
我们构建一个aiService的bean, 触发请求:
@CrossOrigin
@Slf4j
@RestController
class ChatController {
@Resource
private AssistantService assistantService;
@GetMapping("/ai/assistant")
public String assistant(@RequestParam(defaultValue = "What can you do for me?") String message) {
String response = assistantService.chat(message);
log.info("Received message: {}, generated response: {}", message, response);
return response;
}
}
定义AiService:
@AiService
public interface AssistantService {
@SystemMessage(value = {
"you are system assistant, you can help me to do some works in this system.",
"if user want to generate table data, must input the table name and the number of rows."
})
String chat(String message);
}
这里debug调用chat方法的时候实际上debug会走到dev.langchain4j.service.DefaultAiServices#build中的匿名InvocationHandler.invoke()方法

核心业务逻辑就是根据根据构建的aiService信息调用大模型的api:

如果有对当前的aiService定义添加tool工具, 那么会tool工具调用对应的大模型function calling的api, 这样模型会知道何时才会去调用系统内部的工具类.
@AiService如何构建的
实际上就是 langchain4j-spring-boot-starter 的自动装配流程
自动装配
starter主要作用就是确保所有必要的库和配置都被自动包含,从而减少了手动添加依赖和配置的工作量。
主要对 RAG的配置和AiService的配置
启动自动装配: 类 dev.langchain4j.spring.LangChain4jAutoConfig

LangChain4jAutoConfig
@Import注解用于导入其他的配置类,使得这些配置类中的bean定义可以被当前配置类所在的Spring容器中识别和管理。在这里,它导入了AiServicesAutoConfig和RagAutoConfig两个配置类,这意味着这两个配置类中的所有bean定义都将被包含在当前Spring容器中。

最低0.47元/天 解锁文章
4056

被折叠的 条评论
为什么被折叠?



