先行一步
VMware 提供培訓和認證,助你快速進步。
瞭解更多在最近的構建有效智慧體研究出版物中,Anthropic 分享了關於構建有效大型語言模型 (LLM) 智慧體的寶貴見解。這項研究的特別之處在於它強調簡單性和可組合性,而非複雜的框架。讓我們探討這些原則如何透過使用Spring AI的功能轉化為實際實現。
雖然模式描述和圖表來源於 Anthropic 的原始出版物,但我們將重點介紹如何使用 Spring AI 的特性實現這些模式,以便實現模型可移植性和結構化輸出。我們建議先閱讀原始論文。
下面討論的模式已在 agentic-patterns 專案中實現。
這項研究出版物對兩類智慧體系統進行了重要的架構區分:
關鍵在於,儘管全自主智慧體可能看起來很有吸引力,但工作流通常能為定義明確的任務提供更好的可預測性和一致性。這與企業對可靠性和可維護性的需求完美契合。
讓我們透過五種基本模式來探討 Spring AI 如何實現這些概念,每種模式都適用於特定的用例:
鏈式工作流模式體現了將複雜任務分解為更簡單、更易管理的步驟的原則。
何時使用
這是 Spring AI 實現中的一個實際示例:
public class ChainWorkflow {
private final ChatClient chatClient;
private final String[] systemPrompts;
// Processes input through a series of prompts, where each step's output
// becomes input for the next step in the chain.
public String chain(String userInput) {
String response = userInput;
for (String prompt : systemPrompts) {
// Combine the system prompt with previous response
String input = String.format("{%s}\n {%s}", prompt, response);
// Process through the LLM and capture output
response = chatClient.prompt(input).call().content();
}
return response;
}
}
此實現演示了幾個關鍵原則:
大型語言模型可以同時處理任務,並透過程式設計方式聚合其輸出。並行化工作流主要有兩種變體:
何時使用
並行化工作流模式展示了對多個大型語言模型 (LLM) 操作進行高效並行處理的能力。該模式特別適用於需要並行執行 LLM 呼叫並自動聚合輸出的場景。
以下是使用並行化工作流的基本示例:
List<String> parallelResponse = new ParallelizationWorkflow(chatClient)
.parallel(
"Analyze how market changes will impact this stakeholder group.",
List.of(
"Customers: ...",
"Employees: ...",
"Investors: ...",
"Suppliers: ..."
),
4
);
此示例演示了並行處理利益相關者分析,其中每個利益相關者群體都同時進行分析。
路由模式實現了智慧任務分配,能夠針對不同型別的輸入進行專門處理。
此模式專為複雜任務設計,其中不同型別的輸入由專門的流程處理效果更佳。它使用大型語言模型分析輸入內容,並將其路由到最合適的專用提示或處理程式。
何時使用
以下是使用路由工作流的基本示例:
@Autowired
private ChatClient chatClient;
// Create the workflow
RoutingWorkflow workflow = new RoutingWorkflow(chatClient);
// Define specialized prompts for different types of input
Map<String, String> routes = Map.of(
"billing", "You are a billing specialist. Help resolve billing issues...",
"technical", "You are a technical support engineer. Help solve technical problems...",
"general", "You are a customer service representative. Help with general inquiries..."
);
// Process input
String input = "My account was charged twice last week";
String response = workflow.route(input, routes);
此模式演示瞭如何在保持控制的同時實現更復雜的智慧體行為:
何時使用
該實現使用 Spring AI 的 ChatClient 進行大型語言模型互動,幷包括:
public class OrchestratorWorkersWorkflow {
public WorkerResponse process(String taskDescription) {
// 1. Orchestrator analyzes task and determines subtasks
OrchestratorResponse orchestratorResponse = // ...
// 2. Workers process subtasks in parallel
List<String> workerResponses = // ...
// 3. Results are combined into final response
return new WorkerResponse(/*...*/);
}
}
ChatClient chatClient = // ... initialize chat client
OrchestratorWorkersWorkflow workflow = new OrchestratorWorkersWorkflow(chatClient);
// Process a task
WorkerResponse response = workflow.process(
"Generate both technical and user-friendly documentation for a REST API endpoint"
);
// Access results
System.out.println("Analysis: " + response.analysis());
System.out.println("Worker Outputs: " + response.workerResponses());
評估器-最佳化器模式實現了雙大型語言模型流程,其中一個模型生成響應,而另一個模型在迭代迴圈中提供評估和反饋,類似於人類作者的潤色過程。該模式由兩個主要元件組成:
何時使用
該實現使用 Spring AI 的 ChatClient 進行大型語言模型互動,幷包括:
public class EvaluatorOptimizerWorkflow {
public RefinedResponse loop(String task) {
// 1. Generate initial solution
Generation generation = generate(task, context);
// 2. Evaluate the solution
EvaluationResponse evaluation = evaluate(generation.response(), task);
// 3. If PASS, return solution
// 4. If NEEDS_IMPROVEMENT, incorporate feedback and generate new solution
// 5. Repeat until satisfactory
return new RefinedResponse(finalSolution, chainOfThought);
}
}
ChatClient chatClient = // ... initialize chat client
EvaluatorOptimizerWorkflow workflow = new EvaluatorOptimizerWorkflow(chatClient);
// Process a task
RefinedResponse response = workflow.loop(
"Create a Java class implementing a thread-safe counter"
);
// Access results
System.out.println("Final Solution: " + response.solution());
System.out.println("Evolution: " + response.chainOfThought());
Spring AI 對這些模式的實現提供了幾個與 Anthropic 建議相符的優勢:
<!-- Easy model switching through dependencies -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
// Type-safe handling of LLM responses
EvaluationResponse response = chatClient.prompt(prompt)
.call()
.entity(EvaluationResponse.class);
基於 Anthropic 的研究和 Spring AI 的實現,以下是構建有效大型語言模型系統的一些關鍵建議:
從簡單開始
設計可靠性
權衡利弊
在本系列的第二部分,我們將探討如何構建更高階的智慧體,將這些基礎模式與複雜特性結合起來:
模式組合
高階智慧體記憶體管理
工具和模型上下文協議 (MCP) 整合
敬請關注這些高階特性的詳細實現和最佳實踐。
VMware Tanzu Platform 10 中的 Tanzu AI Server 由 Spring AI 提供支援,它提供了:
有關使用 Tanzu AI Server 部署 AI 應用的更多資訊,請訪問VMware Tanzu AI 文件。
Anthropic 的研究見解與 Spring AI 的實際實現相結合,為構建有效的基於大型語言模型的系統提供了一個強大的框架。遵循這些模式和原則,開發者可以建立健壯、可維護且高效的 AI 應用,提供實際價值,同時避免不必要的複雜性。
關鍵在於記住,有時最簡單的解決方案也是最有效的。從基本模式開始,徹底理解你的用例,並且只有在能夠明顯提升系統性能或能力時才增加複雜性。