{"id":10021025,"date":"2025-10-10T21:08:02","date_gmt":"2025-10-10T13:08:02","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021025"},"modified":"2025-10-10T21:08:02","modified_gmt":"2025-10-10T13:08:02","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-spring-boot-%e5%ba%94%e7%94%a8%e4%b8%ad%e9%85%8d%e7%bd%ae%e5%a4%9a%e4%b8%aa-spring-ai-%e7%9a%84-llm-%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021025","title":{"rendered":"\u5982\u4f55\u5728 Spring Boot \u5e94\u7528\u4e2d\u914d\u7f6e\u591a\u4e2a Spring AI \u7684 LLM \u5ba2\u6237\u7aef"},"content":{"rendered":"<h2>1. \u6982\u8ff0<\/h2>\n<p>\u8d8a\u6765\u8d8a\u591a\u7684\u73b0\u4ee3\u5e94\u7528\u5f00\u59cb\u96c6\u6210\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\uff0c\u4ee5\u6784\u5efa\u66f4\u667a\u80fd\u7684\u529f\u80fd\u3002\u5982\u4f55\u4f7f\u7528Spring AI\u5feb\u901f\u6574\u5408LLM\u80fd\u529b\u5230\u81ea\u5df1\u7684Spring Boot\u5e94\u7528\uff0c\u5728\u4e4b\u524d\u7684\u535a\u6587\u4e2d\u6709\u8fc7\u5f88\u591a\u7bc7\u5173\u4e8e<a href=\"https:\/\/link.segmentfault.com\/?enc=bxLAwgup9Firy1g9x%2B3uxw%3D%3D.4ijsP7SVS5lX6eugzDu4ukbWR7rovzCBEKBMIbHVkhs%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u4f7f\u7528Spring AI\u4f7f\u7528\u4e0d\u540c\u4f9b\u5e94\u5546LLM\u7684\u6574\u5408\u6848\u4f8b<\/a>\u3002\u867d\u7136\u4e00\u4e2a LLM \u80fd\u80dc\u4efb\u591a\u79cd\u4efb\u52a1\uff0c\u4f46\u53ea\u4f9d\u8d56\u5355\u4e00\u6a21\u578b\u5e76\u4e0d\u603b\u662f\u6700\u4f18\u3002<\/p>\n<p>\u4e0d\u540c\u6a21\u578b\u5404\u6709\u4fa7\u91cd\uff1a\u6709\u7684\u64c5\u957f\u6280\u672f\u5206\u6790\uff0c\u6709\u7684\u66f4\u9002\u5408\u521b\u610f\u5199\u4f5c\u3002\u7b80\u5355\u4efb\u52a1\u66f4\u9002\u5408\u8f7b\u91cf\u3001\u6027\u4ef7\u6bd4\u9ad8\u7684\u6a21\u578b\uff1b\u590d\u6742\u4efb\u52a1\u5219\u4ea4\u7ed9\u66f4\u5f3a\u5927\u7684\u6a21\u578b\u3002<\/p>\n<p>\u672c\u6587\u5c06\u6f14\u793a\u5982\u4f55\u501f\u52a9 Spring AI\uff0c\u5728 Spring Boot \u5e94\u7528\u4e2d\u96c6\u6210\u591a\u4e2a LLM\u3002<\/p>\n<p>\u6211\u4eec\u65e2\u4f1a\u914d\u7f6e\u6765\u81ea\u4e0d\u540c\u4f9b\u5e94\u5546\u7684\u6a21\u578b\uff0c\u4e5f\u4f1a\u914d\u7f6e\u540c\u4e00\u4f9b\u5e94\u5546\u4e0b\u7684\u591a\u4e2a\u6a21\u578b\u3002\u968f\u540e\u57fa\u4e8e\u8fd9\u4e9b\u914d\u7f6e\uff0c\u6784\u5efa\u4e00\u4e2a\u5177\u5907\u5f39\u6027\u7684\u804a\u5929\u673a\u5668\u4eba\uff0c\u5728\u6545\u969c\u65f6\u53ef\u81ea\u52a8\u5728\u6a21\u578b\u95f4\u5207\u6362\u3002<\/p>\n<h2>2. \u914d\u7f6e\u4e0d\u540c\u4f9b\u5e94\u5546\u7684 LLM<\/h2>\n<p>\u6211\u4eec\u5148\u5728\u5e94\u7528\u4e2d\u914d\u7f6e\u6765\u81ea\u4e0d\u540c\u4f9b\u5e94\u5546\u7684\u4e24\u4e2a LLM\u3002<\/p>\n<p>\u5728\u672c\u6587\u793a\u4f8b\u4e2d\uff0c<strong>\u6211\u4eec\u5c06\u4f7f\u7528 OpenAI \u548c Anthropic \u4f5c\u4e3a AI \u6a21\u578b\u63d0\u4f9b\u5546<\/strong>\u3002<\/p>\n<h3>2.1. \u914d\u7f6e\u4e3b LLM<\/h3>\n<p>\u6211\u4eec\u5148\u5c06\u4e00\u4e2a OpenAI \u6a21\u578b\u914d\u7f6e\u4e3a\u4e3b LLM\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u9879\u76ee\u7684 <code>pom.xml<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u6240\u9700\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.ai&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-ai-starter-model-openai&lt;\/artifactId&gt;\n    &lt;version&gt;1.0.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u8be5 <a href=\"https:\/\/link.segmentfault.com\/?enc=p%2Bn6ncq20ypVcolymBC%2FUw%3D%3D.GEx0iCVxRvLC1wJm7DtNVGUPVZ%2BHnn5ormoVVAgNf4utk%2Blg%2FUNGAxV%2Ffa31zxC8ybZnACNhvHaF7kEgYDH4E0DRhmCNDEpC7uCL3FJcBpgwcZa4hdJKweSoekhTIqfG\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">OpenAI Starter \u4f9d\u8d56<\/a> \u662f\u5bf9 <a href=\"https:\/\/link.segmentfault.com\/?enc=RfBjJL0o0Cl6%2BW35tEjlfQ%3D%3D.Qoou7DBqbKDZwpe294ptQje7ePHBDnLe%2B4Sh8zYSvt899qwiSXy51OPI7arSbWQlzIOSgWXRR7Bb7olrW1i8jg%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">OpenAI Chat Completions API<\/a> \u7684\u5c01\u88c5\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u5728\u5e94\u7528\u4e2d\u4e0e OpenAI \u6a21\u578b\u4ea4\u4e92\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u5728 <code>application.yaml<\/code> \u4e2d\u914d\u7f6e\u6211\u4eec\u7684 OpenAI API Key \u548c\u804a\u5929\u6a21\u578b\uff1a<\/p>\n<pre><code class=\"yaml\">spring:\n  ai:\n    open-ai:\n      api-key: ${OPENAI_API_KEY}\n      chat:\n        options:\n          model: ${PRIMARY_LLM}\n          temperature: 1<\/code><\/pre>\n<p>\u6211\u4eec\u4f7f\u7528 <code>${}<\/code> \u5c5e\u6027\u5360\u4f4d\u7b26\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u52a0\u8f7d\u5c5e\u6027\u503c\u3002\u53e6\u5916\uff0c\u6211\u4eec\u5c06\u6e29\u5ea6\u8bbe\u7f6e\u4e3a <code>1<\/code>\uff0c\u56e0\u4e3a\u8f83\u65b0\u7684 OpenAI \u6a21\u578b<a href=\"https:\/\/link.segmentfault.com\/?enc=OV%2BCMGL5OSEaNgzOi3eQHQ%3D%3D.6tq%2B5%2FiIl7N8PULmwu1Ifw0xhgADMe7ftfSaKE1tnEC%2FFLd4TDp6kPafTtrayB9RhYIQ6BvdV03CwzjPEKjI%2Bg3tTSoNKQsC%2BoIy%2BSvF53o%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u53ea\u63a5\u53d7\u8fd9\u4e2a\u9ed8\u8ba4\u503c<\/a>\u3002<\/p>\n<p>\u5728\u5b8c\u6210\u4e0a\u8ff0\u5c5e\u6027\u914d\u7f6e\u540e\uff0c<strong>Spring AI \u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a <code>OpenAiChatModel<\/code> \u7c7b\u578b\u7684 bean\u3002\u6211\u4eec\u4f7f\u7528\u5b83\u6765\u5b9a\u4e49\u4e00\u4e2a <code>ChatClient<\/code> bean\uff0c\u4f5c\u4e3a\u4e0e LLM \u4ea4\u4e92\u7684\u4e3b\u8981\u5165\u53e3<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">@Configuration\nclass ChatbotConfiguration {\n\n    @Bean\n    @Primary\n    ChatClient primaryChatClient(OpenAiChatModel chatModel) {\n        return ChatClient.create(chatModel);\n    }\n}<\/code><\/pre>\n<p>\u5728 <code>ChatbotConfiguration<\/code> \u7c7b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>OpenAiChatModel<\/code> bean \u521b\u5efa\u4e86\u4e3b LLM \u7684 <code>ChatClient<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u4f7f\u7528 <code>@Primary<\/code> \u6ce8\u89e3\u6807\u8bb0\u8be5 bean\u3002\u5f53\u5728\u7ec4\u4ef6\u4e2d\u6ce8\u5165 <code>ChatClient<\/code> \u4e14\u672a\u4f7f\u7528 <code>Qualifier<\/code> \u65f6\uff0cSpring Boot \u4f1a\u81ea\u52a8\u6ce8\u5165\u5b83\u3002<\/p>\n<h3>2.2. \u914d\u7f6e\u6b21\u7ea7 LLM<\/h3>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u914d\u7f6e\u4e00\u4e2a\u6765\u81ea Anthropic \u7684\u6a21\u578b\u4f5c\u4e3a\u6b21\u7ea7 LLM\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728 <code>pom.xml<\/code> \u4e2d\u6dfb\u52a0 <a href=\"https:\/\/link.segmentfault.com\/?enc=%2BkPGdJn01GW9v4zvvUCyFg%3D%3D.Fn1Yx8ZEyMeorz4ndOaRpjkAG4hn8kMaKfnysd0szOmM91%2FyWrD2OP6s1Hs9u4Wyke9f%2BPPalUbQTHKyATRmn7q%2F9ql7Dc0R3M67YZQ140oLBF2cHDmfkLTrTI4MRYHw\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Anthropic Starter \u4f9d\u8d56<\/a>\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.ai&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-ai-starter-model-anthropic&lt;\/artifactId&gt;\n    &lt;version&gt;1.0.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u8be5\u4f9d\u8d56\u662f\u5bf9 <a href=\"https:\/\/link.segmentfault.com\/?enc=Chzfh5I28vgPhl3hGH8cKg%3D%3D.b7omUAvnGdCmsNJKLBZQWa8WOUIJT7YJHfPfbnRF%2F2iWVR1kH260Tv6Gk729VRjU\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Anthropic Message API<\/a> \u7684\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u4e0e Anthropic \u6a21\u578b\u5efa\u7acb\u8fde\u63a5\u5e76\u4ea4\u4e92\u6240\u9700\u7684\u7c7b\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u4e3a\u6b21\u7ea7\u6a21\u578b\u5b9a\u4e49\u914d\u7f6e\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"yaml\">spring:\n  ai:\n    anthropic:\n      api-key: ${ANTHROPIC_API_KEY}\n      chat:\n        options:\n          model: ${SECONDARY_LLM}<\/code><\/pre>\n<p>\u4e0e\u4e3b LLM \u7684\u914d\u7f6e\u7c7b\u4f3c\uff0c\u6211\u4eec\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u52a0\u8f7d <a href=\"https:\/\/link.segmentfault.com\/?enc=1Fi6MbpAW7H7DaeZ8FztKw%3D%3D.RLQnKcxa3aeMQvbRyH%2BYlcnVgc%2BG30wolCe0gZOtEHv%2FaOXtMAE3npr66DG0%2BEeW\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Anthropic API Key<\/a> \u548c\u6a21\u578b ID\u3002<\/p>\n<p>\u6700\u540e\uff0c<strong>\u4e3a\u6b21\u7ea7\u6a21\u578b\u521b\u5efa\u4e00\u4e2a\u4e13\u7528\u7684 <code>ChatClient<\/code> bean<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">@Bean\nChatClient secondaryChatClient(AnthropicChatModel chatModel) {\n    return ChatClient.create(chatModel);\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528 Spring AI \u81ea\u52a8\u914d\u7f6e\u7684 <code>AnthropicChatModel<\/code> bean \u521b\u5efa\u4e86 <code>secondaryChatClient<\/code>\u3002<\/p>\n<h2>3. \u914d\u7f6e\u540c\u4e00\u4f9b\u5e94\u5546\u7684\u591a\u4e2a LLM<\/h2>\n<p>\u5f88\u591a\u65f6\u5019\uff0c<strong>\u6211\u4eec\u9700\u8981\u914d\u7f6e\u7684\u591a\u4e2a LLM \u53ef\u80fd\u6765\u81ea\u540c\u4e00 AI \u4f9b\u5e94\u5546<\/strong>\u3002<\/p>\n<p><strong>Spring AI \u5e76\u4e0d\u539f\u751f\u652f\u6301\u8fd9\u79cd\u573a\u666f\uff0c\u5176\u81ea\u52a8\u914d\u7f6e\u6bcf\u4e2a\u4f9b\u5e94\u5546\u53ea\u4f1a\u521b\u5efa\u4e00\u4e2a <code>ChatModel<\/code> bean<\/strong>\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u989d\u5916\u7684\u6a21\u578b\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u5b9a\u4e49 <code>ChatModel<\/code> bean\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5177\u4f53\u8fc7\u7a0b\uff0c\u5e76<strong>\u5728\u5e94\u7528\u4e2d\u914d\u7f6e\u7b2c\u4e8c\u4e2a Anthropic \u6a21\u578b<\/strong>\uff1a<\/p>\n<pre><code class=\"yaml\">spring:\n  ai:\n    anthropic:\n      chat:\n        options:\n          tertiary-model: ${TERTIARY_LLM}<\/code><\/pre>\n<p>\u5728 <code>application.yaml<\/code> \u7684 Anthropic \u914d\u7f6e\u4e0b\uff0c<strong>\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5c5e\u6027\u6765\u4fdd\u5b58\u7b2c\u4e09\u4e2a\uff08tertiary\uff09LLM \u7684\u6a21\u578b\u540d\u79f0<\/strong>\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u4e3a\u7b2c\u4e09\u4e2a LLM \u5b9a\u4e49\u5fc5\u8981\u7684 bean\uff1a<\/p>\n<pre><code class=\"java\">@Bean\nChatModel tertiaryChatModel(\n    AnthropicApi anthropicApi,\n    AnthropicChatModel anthropicChatModel,\n    @Value(&quot;${spring.ai.anthropic.chat.options.tertiary-model}&quot;) String tertiaryModelName\n) {\n    AnthropicChatOptions chatOptions = anthropicChatModel.getDefaultOptions().copy();\n    chatOptions.setModel(tertiaryModelName);\n    return AnthropicChatModel.builder()\n      .anthropicApi(anthropicApi)\n      .defaultOptions(chatOptions)\n      .build();\n}\n\n@Bean\nChatClient tertiaryChatClient(@Qualifier(&quot;tertiaryChatModel&quot;) ChatModel tertiaryChatModel) {\n    return ChatClient.create(tertiaryChatModel);\n}<\/code><\/pre>\n<p>\u9996\u5148\uff0c\u4e3a\u521b\u5efa\u81ea\u5b9a\u4e49\u7684 <code>ChatModel<\/code> bean\uff0c\u6211\u4eec\u6ce8\u5165\u81ea\u52a8\u914d\u7f6e\u7684 <code>AnthropicApi<\/code> bean\u3001\u7528\u4e8e\u521b\u5efa\u6b21\u7ea7 LLM \u7684\u9ed8\u8ba4 <code>AnthropicChatModel<\/code> bean\uff0c\u5e76\u901a\u8fc7 <code>@Value<\/code> \u6ce8\u5165\u7b2c\u4e09\u4e2a\u6a21\u578b\u7684\u540d\u79f0\u5c5e\u6027\u3002<\/p>\n<p><strong>\u6211\u4eec\u590d\u5236\u73b0\u6709 <code>AnthropicChatModel<\/code> \u7684\u9ed8\u8ba4\u9009\u9879\uff0c\u5e76\u4ec5\u8986\u76d6\u5176\u4e2d\u7684\u6a21\u578b\u540d\u79f0<\/strong>\u3002<\/p>\n<p>\u8be5\u8bbe\u7f6e\u5047\u5b9a\u4e24\u4e2a Anthropic \u6a21\u578b\u5171\u4eab\u540c\u4e00\u4e2a API Key \u53ca\u5176\u4ed6\u914d\u7f6e\u3002<strong>\u5982\u679c\u9700\u8981\u4e0d\u540c\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49 <code>AnthropicChatOptions<\/code><\/strong>\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 <code>tertiaryChatModel<\/code> \u5728\u914d\u7f6e\u7c7b\u4e2d\u521b\u5efa\u7b2c\u4e09\u4e2a <code>ChatClient<\/code> bean\u3002<\/p>\n<h2>4. \u63a2\u7d22\u4e00\u4e2a\u5b9e\u7528\u7528\u4f8b<\/h2>\n<p>\u5728\u5b8c\u6210\u591a\u6a21\u578b\u914d\u7f6e\u540e\uff0c\u8ba9\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u5b9e\u7528\u7528\u4f8b\u3002<strong>\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a\u5177\u5907\u5f39\u6027\u7684\u804a\u5929\u673a\u5668\u4eba\uff0c\u5f53\u4e3b\u6a21\u578b\u51fa\u73b0\u6545\u969c\u65f6\u53ef\u6309\u987a\u5e8f\u81ea\u52a8\u56de\u9000\u5230\u66ff\u4ee3\u6a21\u578b<\/strong>\u3002<\/p>\n<h3>4.1. \u6784\u5efa\u5177\u5907\u5f39\u6027\u7684\u804a\u5929\u673a\u5668\u4eba<\/h3>\n<p>\u4e3a\u5b9e\u73b0\u56de\u9000\u903b\u8f91\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Spring Retry\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 <code>ChatbotService<\/code> \u7c7b\uff0c\u5e76\u6ce8\u5165\u6211\u4eec\u5b9a\u4e49\u7684\u4e09\u4e2a <code>ChatClient<\/code>\u3002\u63a5\u7740\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5165\u53e3\u65b9\u6cd5\u4f7f\u7528\u4e3b LLM\uff1a<\/p>\n<pre><code class=\"java\">@Retryable(retryFor = Exception.class, maxAttempts = 3)\nString chat(String prompt) {\n    logger.debug(&quot;Attempting to process prompt '{}' with primary LLM. Attempt #{}&quot;,\n        prompt, RetrySynchronizationManager.getContext().getRetryCount() + 1);\n    return primaryChatClient\n      .prompt(prompt)\n      .call()\n      .content();\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<strong>\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4f7f\u7528 <code>primaryChatClient<\/code> \u7684 <code>chat()<\/code> \u65b9\u6cd5<\/strong>\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528 <code>@Retryable<\/code> \u6ce8\u89e3\uff0c\u5728\u9047\u5230\u4efb\u610f Exception \u65f6\u6700\u591a\u91cd\u8bd5\u4e09\u6b21\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u5b9a\u4e49\u4e00\u4e2a\u6062\u590d\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">@Recover\nString chat(Exception exception, String prompt) {\n    logger.warn(&quot;Primary LLM failure. Error received: {}&quot;, exception.getMessage());\n    logger.debug(&quot;Attempting to process prompt '{}' with secondary LLM&quot;, prompt);\n    try {\n        return secondaryChatClient\n          .prompt(prompt)\n          .call()\n          .content();\n    } catch (Exception e) {\n        logger.warn(&quot;Secondary LLM failure: {}&quot;, e.getMessage());\n        logger.debug(&quot;Attempting to process prompt '{}' with tertiary LLM&quot;, prompt);\n        return tertiaryChatClient\n          .prompt(prompt)\n          .call()\n          .content();\n    }\n}<\/code><\/pre>\n<p><strong>\u4f7f\u7528 <code>@Recover<\/code> \u6ce8\u89e3\u6807\u8bb0\u7684\u91cd\u8f7d <code>chat()<\/code> \u65b9\u6cd5\u5c06\u4f5c\u4e3a\u539f\u59cb <code>chat()<\/code> \u65b9\u6cd5\u5931\u8d25\u5e76\u8017\u5c3d\u91cd\u8bd5\u540e\u7684\u56de\u9000\u5904\u7406<\/strong>\u3002<\/p>\n<p>\u6211\u4eec\u9996\u5148\u5c1d\u8bd5\u901a\u8fc7 <code>secondaryChatClient<\/code> \u83b7\u53d6\u54cd\u5e94\uff1b\u5982\u679c\u4ecd\u5931\u8d25\uff0c\u5219\u6700\u540e\u518d\u5c1d\u8bd5\u4f7f\u7528 <code>tertiaryChatClient<\/code>\u3002<\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u7b80\u5355\u7684 try-catch \u5b9e\u73b0\uff0c\u56e0\u4e3a Spring Retry \u6bcf\u4e2a\u65b9\u6cd5\u7b7e\u540d\u53ea\u5141\u8bb8\u4e00\u4e2a\u6062\u590d\u65b9\u6cd5\u3002\u4f46\u5728\u751f\u4ea7\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u5e94\u8003\u8651\u4f7f\u7528\u66f4\u5b8c\u5584\u7684\u65b9\u6848\uff0c\u4f8b\u5982 Resilience4j\u3002<\/p>\n<p>\u5728\u5b8c\u6210\u670d\u52a1\u5c42\u5b9e\u73b0\u540e\uff0c<strong>\u6211\u4eec\u518d\u5bf9\u5916\u66b4\u9732\u4e00\u4e2a REST API<\/strong>\uff1a<\/p>\n<pre><code class=\"java\">@PostMapping(&quot;\/api\/chatbot\/chat&quot;)\nChatResponse chat(@RequestBody ChatRequest request) {\n    String response = chatbotService.chat(request.prompt);\n    return new ChatResponse(response);\n}\n\nrecord ChatRequest(String prompt) {}\nrecord ChatResponse(String response) {}<\/code><\/pre>\n<p>\u8fd9\u91cc\u5b9a\u4e49\u4e86\u4e00\u4e2a POST \u63a5\u53e3 <code>\/api\/chatbot\/chat<\/code>\uff0c\u63a5\u6536 <code>prompt<\/code>\uff0c\u5c06\u5176\u4f20\u9012\u5230\u670d\u52a1\u5c42\uff0c\u6700\u540e\u628a <code>response<\/code> \u5305\u88c5\u5728 <code>ChatResponse<\/code> record \u4e2d\u8fd4\u56de\u3002<\/p>\n<h3>4.2. \u6d4b\u8bd5\u6211\u4eec\u7684\u804a\u5929\u673a\u5668\u4eba<\/h3>\n<p>\u6700\u540e\uff0c<strong>\u6211\u4eec\u6765\u6d4b\u8bd5\u804a\u5929\u673a\u5668\u4eba\uff0c\u9a8c\u8bc1\u56de\u9000\u673a\u5236\u662f\u5426\u6b63\u5e38\u5de5\u4f5c<\/strong>\u3002<\/p>\n<p>\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u542f\u52a8\u5e94\u7528\uff1a\u4e3a\u4e3b\u3001\u6b21\u7ea7 LLM \u8bbe\u7f6e\u65e0\u6548\u6a21\u578b\u540d\u79f0\uff0c\u540c\u65f6\u4e3a\u7b2c\u4e09\u4e2a LLM \u8bbe\u7f6e\u4e00\u4e2a\u6709\u6548\u7684\u6a21\u578b\u540d\u79f0\uff1a<\/p>\n<pre><code class=\"bash\">OPENAI_API_KEY=.... \/\nANTHROPIC_API_KEY=.... \/\nPRIMARY_LLM=gpt-100 \/\nSECONDARY_LLM=claude-opus-200 \/\nTERTIARY_LLM=claude-3-haiku-20240307 \/\nmvn spring-boot:run<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u547d\u4ee4\u4e2d\uff0c<strong><code>gpt-100<\/code> \u548c <code>claude-opus-200<\/code> \u662f\u65e0\u6548\u7684\u6a21\u578b\u540d\u79f0\uff0c\u4f1a\u5bfc\u81f4 API \u9519\u8bef\uff1b\u800c <a href=\"https:\/\/link.segmentfault.com\/?enc=EOqFsa6H4PdXn9UBCxeDNw%3D%3D.g7ZrgndZVnGIqS4Bj9XilaNshD%2FpU1lW%2F1JC%2BipJjlGvGXSQ97VKwXZNkqVY1wAeK0m2kc1QKD1wmT1XBgYPm1c%2BojLYztmaGe7UsorHdU8l1ZHgQOpEu99VWLSZ9Wud3SUpah5UyriLXmY1Cf2fcw%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">claude-3-haiku-20240307<\/a> \u662f Anthropic \u63d0\u4f9b\u7684\u6709\u6548\u6a21\u578b<\/strong>\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u4f7f\u7528 HTTPie CLI \u8c03\u7528\u63a5\u53e3\uff0c\u4e0e\u804a\u5929\u673a\u5668\u4eba\u4ea4\u4e92\uff1a<\/p>\n<pre><code class=\"bash\">http POST :8080\/api\/chatbot\/chat prompt=&quot;What is the capital of France?&quot;<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u5411\u804a\u5929\u673a\u5668\u4eba\u53d1\u9001\u4e00\u4e2a\u7b80\u5355\u7684\u63d0\u793a\u8bcd\uff0c\u770b\u770b\u8fd4\u56de\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"java\">{\n    &quot;response&quot;: &quot;The capital of France is Paris.&quot;\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u5c3d\u7ba1\u4e3b\u3001\u6b21\u7ea7 LLM \u7684\u914d\u7f6e\u4e3a\u65e0\u6548\u6a21\u578b\uff0c\u804a\u5929\u673a\u5668\u4eba\u4ecd\u8fd4\u56de\u4e86\u6b63\u786e\u54cd\u5e94\uff0c\u8fd9\u9a8c\u8bc1\u4e86\u7cfb\u7edf\u6210\u529f\u56de\u9000\u5230\u4e86\u7b2c\u4e09\u4e2a LLM<\/strong>\u3002<\/p>\n<p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u770b\u5230\u56de\u9000\u903b\u8f91\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b\u5e94\u7528\u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"bash\">[2025-09-30 12:56:03] [DEBUG] [com.baeldung.multillm.ChatbotService] - Attempting to process prompt 'What is the capital of France?' with primary LLM. Attempt #1\n[2025-09-30 12:56:05] [DEBUG] [com.baeldung.multillm.ChatbotService] - Attempting to process prompt 'What is the capital of France?' with primary LLM. Attempt #2\n[2025-09-30 12:56:06] [DEBUG] [com.baeldung.multillm.ChatbotService] - Attempting to process prompt 'What is the capital of France?' with primary LLM. Attempt #3\n[2025-09-30 12:56:07] [WARN] [com.baeldung.multillm.ChatbotService] - Primary LLM failure. Error received: HTTP 404 - {\n    &quot;error&quot;: {\n        &quot;message&quot;: &quot;The model `gpt-100` does not exist or you do not have access to it.&quot;,\n        &quot;type&quot;: &quot;invalid_request_error&quot;,\n        &quot;param&quot;: null,\n        &quot;code&quot;: &quot;model_not_found&quot;\n    }\n}\n[2025-09-30 12:56:07] [DEBUG] [com.baeldung.multillm.ChatbotService] - Attempting to process prompt 'What is the capital of France?' with secondary LLM\n[2025-09-30 12:56:07] [WARN] [com.baeldung.multillm.ChatbotService] - Secondary LLM failure: HTTP 404 - {&quot;type&quot;:&quot;error&quot;,&quot;error&quot;:{&quot;type&quot;:&quot;not_found_error&quot;,&quot;message&quot;:&quot;model: claude-opus-200&quot;},&quot;request_id&quot;:&quot;req_011CTeBrAY8rstsSPiJyv3sj&quot;}\n[2025-09-30 12:56:07] [DEBUG] [com.baeldung.multillm.ChatbotService] - Attempting to process prompt 'What is the capital of France?' with tertiary LLM<\/code><\/pre>\n<p>\u65e5\u5fd7\u6e05\u6670\u5730\u5c55\u793a\u4e86\u8bf7\u6c42\u7684\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u770b\u5230\uff0c\u4e3b LLM \u8fde\u7eed\u4e09\u6b21\u5c1d\u8bd5\u5931\u8d25\uff1b\u968f\u540e\u670d\u52a1\u5c1d\u8bd5\u4f7f\u7528\u6b21\u7ea7 LLM\uff0c\u4ecd\u7136\u5931\u8d25\uff1b\u6700\u7ec8\u8c03\u7528\u7b2c\u4e09\u4e2a LLM \u5904\u7406\u63d0\u793a\u8bcd\u5e76\u8fd4\u56de\u4e86\u6211\u4eec\u770b\u5230\u7684\u54cd\u5e94<\/strong>\u3002<\/p>\n<p>\u8fd9\u8868\u660e\u56de\u9000\u673a\u5236\u6309\u8bbe\u8ba1\u6b63\u5e38\u5de5\u4f5c\uff0c\u5373\u4f7f\u591a\u4e2a LLM \u540c\u65f6\u5931\u8d25\uff0c\u804a\u5929\u673a\u5668\u4eba\u4ecd\u4fdd\u6301\u53ef\u7528\u3002<\/p>\n<h2>5. \u5c0f\u7ed3<\/h2>\n<p>\u672c\u6587\u63a2\u8ba8\u4e86\u5982\u4f55\u5728\u5355\u4e2a Spring AI \u5e94\u7528\u4e2d\u96c6\u6210\u591a\u4e2a LLM\u3002\u9996\u5148\uff0c\u6211\u4eec\u6f14\u793a\u4e86 Spring AI \u7684\u62bd\u8c61\u5c42\u5982\u4f55\u7b80\u5316\u6765\u81ea\u4e0d\u540c\u4f9b\u5e94\u5546\uff08\u5982 OpenAI \u4e0e Anthropic\uff09\u7684\u6a21\u578b\u914d\u7f6e\u3002\u968f\u540e\uff0c\u6211\u4eec\u89e3\u51b3\u4e86\u66f4\u590d\u6742\u7684\u573a\u666f\uff1a\u5728\u540c\u4e00\u4f9b\u5e94\u5546\u4e0b\u914d\u7f6e\u591a\u4e2a\u6a21\u578b\uff0c\u5e76\u5728 Spring AI \u7684\u81ea\u52a8\u914d\u7f6e\u4e0d\u591f\u7528\u65f6\u521b\u5efa\u81ea\u5b9a\u4e49 bean\u3002\u6700\u540e\uff0c\u6211\u4eec\u5229\u7528\u591a\u6a21\u578b\u914d\u7f6e\u6784\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u9ad8\u53ef\u7528\u6027\u7684\u5f39\u6027\u804a\u5929\u673a\u5668\u4eba\u3002\u501f\u52a9 Spring Retry\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u7ea7\u8054\u56de\u9000\u6a21\u5f0f\uff0c\u5728\u53d1\u751f\u6545\u969c\u65f6\u53ef\u5728\u4e0d\u540c LLM \u95f4\u81ea\u52a8\u5207\u6362\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047313551\">https:\/\/segmentfault.com\/a\/1190000047313551<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6982\u8ff0 \u8d8a\u6765\u8d8a\u591a\u7684\u73b0\u4ee3\u5e94\u7528\u5f00\u59cb\u96c6\u6210\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\uff0c\u4ee5\u6784\u5efa\u66f4\u667a\u80fd\u7684\u529f\u80fd\u3002\u5982\u4f55\u4f7f\u7528Spring AI\u5feb [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021025","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021025"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021025\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}