Spring AI-07.顾问 API(Advisors API)

Spring AI-07.顾问 API(Advisors API)

Spring AI 的顾问(Advisors)API 提供了一种灵活且强大的方式,用于拦截、修改和增强 Spring 应用中由 AI 驱动的交互。通过利用顾问 API,开发者能够创建更复杂、可复用且易于维护的 AI 组件。其核心优势包括封装常见的生成式 AI 模式、转换与大型语言模型(LLM)交互的数据,以及在各种模型和用例中实现可移植性。

以下示例展示了如何通过 ChatClient API 配置现有顾问:

var chatClient = ChatClient.builder(chatModel)
    .defaultAdvisors(
       
08:28:41,486 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - 'File' option has the same value "C:\Users\DELL/logs/nacos/config.log" as that given for appender [CONFIG_LOG_FILE] defined earlier. 08:28:41,486 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. 08:28:41,486 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - For more information, please visit https://logback.qos.ch/codes.html#earlier_fa_collision 08:28:41,487 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - 'File' option has the same value "C:\Users\DELL/logs/nacos/naming.log" as that given for appender [NAMING_LOG_FILE] defined earlier. 08:28:41,487 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. 08:28:41,487 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - For more information, please visit https://logback.qos.ch/codes.html#earlier_fa_collision 08:28:41,487 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - 'File' option has the same value "C:\Users\DELL/logs/nacos/remote.log" as that given for appender [REMOTE_LOG_FILE] defined earlier. 08:28:41,487 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. 08:28:41,487 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - For more information, please visit https://logback.qos.ch/codes.html#earlier_fa_collision 2025-08-23T08:28:41.490+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] com.atguigu.study.HelloLangChain4JApp : Starting HelloLangChain4JApp using Java 21.0.8 with PID 18276 (E:\杨哥mysql\langchain4J-2025.8.22\01code\langchain4j-atguiguV5\langchain4j-01helloworld\target\classes started by DELL in E:\杨哥mysql\langchain4J-2025.8.22\01code\langchain4j-atguiguV5) 2025-08-23T08:28:41.491+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] com.atguigu.study.HelloLangChain4JApp : No active profile set, falling back to 1 default profile: "default" 2025-08-23T08:28:41.980+08:00 WARN 18276 --- [langchain4j-01helloworld] [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.cloud.nacos.NacosConfigAutoConfiguration' of type [com.alibaba.cloud.nacos.NacosConfigAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). The currently created BeanPostProcessor [nacosAnnotationProcessor] is declared through a non-static factory method on that class; consider declaring it as static instead. 2025-08-23T08:28:41.984+08:00 WARN 18276 --- [langchain4j-01helloworld] [ main] trationDelegate$BeanPostProcessorChecker : Bean 'nacosConfigProperties' of type [com.alibaba.cloud.nacos.NacosConfigProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected/applied to a currently created BeanPostProcessor [nacosAnnotationProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE. 08:28:41,990 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - 'File' option has the same value "C:\Users\DELL/logs/nacos/config.log" as that given for appender [CONFIG_LOG_FILE] defined earlier. 08:28:41,990 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. 08:28:41,990 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - For more information, please visit https://logback.qos.ch/codes.html#earlier_fa_collision 08:28:41,991 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - 'File' option has the same value "C:\Users\DELL/logs/nacos/naming.log" as that given for appender [NAMING_LOG_FILE] defined earlier. 08:28:41,991 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. 08:28:41,991 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[NAMING_LOG_FILE] - For more information, please visit https://logback.qos.ch/codes.html#earlier_fa_collision 08:28:41,991 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - 'File' option has the same value "C:\Users\DELL/logs/nacos/remote.log" as that given for appender [REMOTE_LOG_FILE] defined earlier. 08:28:41,991 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. 08:28:41,991 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - For more information, please visit https://logback.qos.ch/codes.html#earlier_fa_collision 08:28:41,993 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Attempted to append to non started appender [CONFIG_LOG_FILE]. 08:28:41,993 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Attempted to append to non started appender [CONFIG_LOG_FILE]. 08:28:41,993 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Attempted to append to non started appender [CONFIG_LOG_FILE]. 08:28:41,993 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Attempted to append to non started appender [CONFIG_LOG_FILE]. 08:28:41,994 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Attempted to append to non started appender [CONFIG_LOG_FILE]. 08:28:41,994 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[CONFIG_LOG_FILE] - Attempted to append to non started appender [CONFIG_LOG_FILE]. 08:28:42,024 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Attempted to append to non started appender [REMOTE_LOG_FILE]. 08:28:42,024 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Attempted to append to non started appender [REMOTE_LOG_FILE]. 08:28:42,024 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Attempted to append to non started appender [REMOTE_LOG_FILE]. 08:28:42,024 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Attempted to append to non started appender [REMOTE_LOG_FILE]. 08:28:42,024 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Attempted to append to non started appender [REMOTE_LOG_FILE]. 08:28:42,024 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[REMOTE_LOG_FILE] - Attempted to append to non started appender [REMOTE_LOG_FILE]. 2025-08-23T08:28:42.029+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success. 2025-08-23T08:28:42.029+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success. 2025-08-23T08:28:42.031+08:00 WARN 18276 --- [langchain4j-01helloworld] [ main] trationDelegate$BeanPostProcessorChecker : Bean 'nacosConfigManager' of type [com.alibaba.cloud.nacos.NacosConfigManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected/applied to a currently created BeanPostProcessor [nacosAnnotationProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE. 2025-08-23T08:28:42.171+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 9001 (http) 2025-08-23T08:28:42.182+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-08-23T08:28:42.182+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.41] 2025-08-23T08:28:42.248+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-08-23T08:28:42.248+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 734 ms 2025-08-23T08:28:42.687+08:00 WARN 18276 --- [langchain4j-01helloworld] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dashscopeRerankModel' defined in class path resource [com/alibaba/cloud/ai/autoconfigure/dashscope/DashScopeAutoConfiguration$DashScopeRerankConfiguration.class]: Failed to instantiate [com.alibaba.cloud.ai.dashscope.rerank.DashScopeRerankModel]: Factory method 'dashscopeRerankModel' threw exception with message: DashScope API key must be set. Use the connection property: spring.ai.dashscope.api-key or spring.ai.dashscope.rerank.api-key property. 2025-08-23T08:28:42.689+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2025-08-23T08:28:42.697+08:00 INFO 18276 --- [langchain4j-01helloworld] [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-08-23T08:28:42.708+08:00 ERROR 18276 --- [langchain4j-01helloworld] [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dashscopeRerankModel' defined in class path resource [com/alibaba/cloud/ai/autoconfigure/dashscope/DashScopeAutoConfiguration$DashScopeRerankConfiguration.class]: Failed to instantiate [com.alibaba.cloud.ai.dashscope.rerank.DashScopeRerankModel]: Factory method 'dashscopeRerankModel' threw exception with message: DashScope API key must be set. Use the connection property: spring.ai.dashscope.api-key or spring.ai.dashscope.rerank.api-key property. at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:657) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:645) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1375) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1205) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:569) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:339) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:373) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.instantiateSingleton(DefaultListableBeanFactory.java:1222) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingleton(DefaultListableBeanFactory.java:1188) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:1123) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:987) ~[spring-context-6.2.7.jar:6.2.7] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:627) ~[spring-context-6.2.7.jar:6.2.7] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.5.0.jar:3.5.0] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753) ~[spring-boot-3.5.0.jar:3.5.0] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.5.0.jar:3.5.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.5.0.jar:3.5.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1362) ~[spring-boot-3.5.0.jar:3.5.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1351) ~[spring-boot-3.5.0.jar:3.5.0] at com.atguigu.study.HelloLangChain4JApp.main(HelloLangChain4JApp.java:12) ~[classes/:na] Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.cloud.ai.dashscope.rerank.DashScopeRerankModel]: Factory method 'dashscopeRerankModel' threw exception with message: DashScope API key must be set. Use the connection property: spring.ai.dashscope.api-key or spring.ai.dashscope.rerank.api-key property. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:199) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:88) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:168) ~[spring-beans-6.2.7.jar:6.2.7] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-6.2.7.jar:6.2.7] ... 21 common frames omitted Caused by: java.lang.IllegalArgumentException: DashScope API key must be set. Use the connection property: spring.ai.dashscope.api-key or spring.ai.dashscope.rerank.api-key property. at org.springframework.util.Assert.hasText(Assert.java:253) ~[spring-core-6.2.7.jar:6.2.7] at com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeConnectionUtils.resolveConnectionProperties(DashScopeConnectionUtils.java:66) ~[spring-ai-alibaba-autoconfigure-1.0.0-M6.1.jar:1.0.0-M6.1] at com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration$DashScopeRerankConfiguration.dashscopeRerankModel(DashScopeAutoConfiguration.java:415) ~[spring-ai-alibaba-autoconfigure-1.0.0-M6.1.jar:1.0.0-M6.1] at com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration$DashScopeRerankConfiguration$$SpringCGLIB$$0.CGLIB$dashscopeRerankModel$0(<generated>) ~[spring-ai-alibaba-autoconfigure-1.0.0-M6.1.jar:1.0.0-M6.1] at com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration$DashScopeRerankConfiguration$$SpringCGLIB$$FastClass$$1.invoke(<generated>) ~[spring-ai-alibaba-autoconfigure-1.0.0-M6.1.jar:1.0.0-M6.1] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) ~[spring-core-6.2.7.jar:6.2.7] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:393) ~[spring-context-6.2.7.jar:6.2.7] at com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration$DashScopeRerankConfiguration$$SpringCGLIB$$0.dashscopeRerankModel(<generated>) ~[spring-ai-alibaba-autoconfigure-1.0.0-M6.1.jar:1.0.0-M6.1] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:171) ~[spring-beans-6.2.7.jar:6.2.7] ... 24 common frames omitted 什么错误怎么解决
08-24
从你提供的日志来看,应用启动失败,主要有以下两个核心错误: --- ### 🔴 错误 1:Logback 日志文件路径冲突(致命错误) #### 日志信息: ``` Collisions detected with FileAppender/RollingAppender instances defined earlier. Aborting. ``` #### 原因分析: Logback 检测到多个 `RollingFileAppender` 使用了相同的日志输出路径,例如: - `C:\Users\DELL/logs/nacos/config.log` - `naming.log` - `remote.log` 这通常是因为在 `logback-spring.xml` 或其他日志配置文件中重复定义了同名或同路径的 Appender。 #### 解决方案: 1. **打开 `logback-spring.xml` 文件**,检查是否重复定义了如下结构: ```xml <appender name="CONFIG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>C:\Users\DELL/logs/nacos/config.log</file> ... </appender> ``` 2. **确保每个 Appender 的 `name` 和 `file` 路径唯一**,或者删除重复的定义。 3. **清理缓存并重启应用**,确认冲突已解决。 --- ### 🔴 错误 2:DashScope API Key 未设置(致命错误) #### 日志信息: ``` DashScope API key must be set. Use the connection property: spring.ai.dashscope.api-key or spring.ai.dashscope.rerank.api-key property. ``` #### 原因分析: 你的应用尝试使用阿里云 DashScope 的 AI 模型(如 rerank 模型),但没有在配置文件中设置对应的 API Key。 #### 解决方案: 1. **获取 DashScope API Key**: - 登录 [阿里云 DashScope 控制台](https://help.aliyun.com/zh/dashscope/developer-reference/quick-start) - 创建或获取已有的 API Key。 2. **在 `application.yml` 或 `application.properties` 中添加配置**: ```yaml spring: ai: dashscope: api-key: your_dashscope_api_key_here ``` 或: ```properties spring.ai.dashscope.api-key=your_dashscope_api_key_here ``` 3. **重启应用**,确保 API Key 被正确加载。 --- ### ⚠️ 警告信息(不影响启动,但建议优化) #### 示例: ``` Bean 'nacosConfigProperties' is not eligible for getting processed by all BeanPostProcessors ``` #### 原因分析: 这类信息是 Spring 的警告,表示某些 Bean 在 BeanPostProcessor 初始化之前就被创建了,导致它们无法被代理或增强。通常不影响功能。 #### 建议: - 如果不需要这些 Bean 被代理,可以忽略。 - 如果需要,可尝试将 Bean 的定义改为 `static` 方法,或调整加载顺序。 --- ### ✅ 总结 | 错误类型 | 是否致命 | 建议操作 | |----------|----------|----------| | Logback 文件冲突 | 是 | 检查并修复 `logback-spring.xml` 中的 Appender 定义 | | DashScope API Key 未设置 | 是 | 配置 API Key | | BeanPostProcessor 警告 | 否 | 可忽略或优化 Bean 定义 | --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员勇哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值