領先一步
VMware 提供培訓和認證,助力您加速進步。
瞭解更多我們很高興宣佈釋出 Spring AI 1.0.0 里程碑 6。為了慶祝此釋出,我們建立了一個特別的AI 生成音樂播放列表,以增強您的部落格閱讀和編碼體驗!
與往常一樣,此版本包含多項新功能和錯誤修復。我們繼續從設計的角度審閱了程式碼庫。雖然我們試圖透過在一個釋出週期內棄用方法和類來使過渡順利,但我們知道存在一些破壞性變更,也可能存在一些我們不知道的變更,所以請大家諒解。有關詳細資訊,請參閱本文底部的破壞性變更部分。
函式呼叫的整體設計和功能集有了顯著改進,現在採用了更普遍的術語“工具呼叫”。非常感謝 Thomas Vitale 推動了這些改進。
現在有幾種定義工具的方式
@Tool
和 @ToolParam
註解的宣告式方法工具以下是使用 @Tool
註解建立獲取當前日期和時間的工具的示例。
import java.time.LocalDateTime;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.context.i18n.LocaleContextHolder;
class DateTimeTools {
@Tool(description = "Get the current date and time in the user's timezone")
String getCurrentDateTime() {
return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
}
}
// Using the tool with ChatClient
String response = ChatClient.create(chatModel)
.prompt("What day is tomorrow?")
.tools(new DateTimeTools())
.call()
.content();
當模型需要知道當前日期和時間時,它會自動請求呼叫該工具。ChatClient 在內部處理工具執行並將結果返回給模型,然後模型使用此資訊生成最終響應。JSON Schema 是使用類和工具註解自動生成的。
關於棄用,org.springframework.ai.model.function
包中的所有內容都已被棄用,新的介面和類可在 org.springframework.ai.tool
包中找到。
相關 API 已更新
FunctionCallingOptions
更名為 ToolCallingChatOptions
ChatClient.builder().defaultFunctions()
更名為 ChatClient.builder().defaultTools()
ChatClient.functions()
更名為 ChatClient.tools()
請參閱工具遷移指南瞭解更多資訊。
工具呼叫功能還有其他多項改進,請查閱工具參考文件瞭解更多資訊。
與其說是“十月驚喜”,不如說是去年的“十一月驚喜”:Model Context Protocol 釋出了,並受到了 AI 社群的廣泛好評。
簡而言之,Model Context Protocol (MCP) 提供了一種統一的方式,將 AI 模型連線到不同的資料來源和工具,使整合變得無縫且一致。它幫助您在大語言模型 (LLMs) 的基礎上構建代理和複雜的工作流。由於 LLMs 經常需要與資料和工具整合,MCP 提供了
spring-ai-mcp 實驗專案於去年 11 月啟動,並一直在發展。Spring AI 團隊已與 David Soria Parra 以及 Anthropic 的其他成員合作,將該實驗專案納入官方的 MCP Java SDK。MCP Java SDK 具有以下核心功能
還有多種傳輸選項
請查閱 MCP Java SDK 文件,瞭解有關 SDK 入門的更多資訊,並訪問 MCP Java SDK GitHub 倉庫以提交問題並參與討論。
核心元件已移至 Anthropic Java SDK,與 Spring AI 的整合為開發人員提供了更輕鬆的體驗,可以利用 Spring Boot 自動配置建立客戶端和伺服器實現。
這裡有一個小的客戶端示例,展示瞭如何在簡單的聊天機器人應用程式中使用 Brave Search API
@SpringBootApplication
public class Application {
@Bean
public CommandLineRunner predefinedQuestions(
ChatClient.Builder chatClientBuilder,
ToolCallbackProvider tools,
ConfigurableApplicationContext context) {
return args -> {
var chatClient = chatClientBuilder
.defaultTools(tools)
.build();
String question = "Does Spring AI support the Model Context Protocol?";
System.out.println("ASSISTANT: " +
chatClient.prompt(question).call().content());
};
}
}
配置很簡單
spring.ai.mcp.client.stdio.enabled=true
spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-servers-config.json
伺服器配置是
{
"mcpServers": {
"brave-search": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-brave-search"
],
"env": {
}
}
}
}
在您的依賴配置中,匯入 Spring AI Bom 並新增 Spring Boot 客戶端啟動器,此處顯示的是 Maven 配置。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
MCP 是一個很大的主題。參考文件包含更多資訊,並且有多個示例
VectorStore API 已得到改進,儘管伴隨了一些破壞性變更。
VectorStore 介面的 delete 方法已簡化為 void 操作,移除了之前的 Optional
現在您可以根據元資料條件而不是僅僅根據文件 ID 刪除文件。
以下是一個示例
// Create and add documents to the store
Document bgDocument = new Document("content",
Map.of("country", "BG", "year", 2020));
Document nlDocument = new Document("content",
Map.of("country", "NL", "year", 2021));
vectorStore.add(List.of(bgDocument, nlDocument));
// Delete documents using string filter expression
vectorStore.delete("country == 'BG'");
// Or use the Expression-based API
Filter.Expression expr = // ... your filter expression
vectorStore.delete(expr);
此功能已在所有向量儲存提供商中實現,包括 Chroma、Elasticsearch、PostgreSQL、Weaviate、Redis、Milvus 等。
新的 getNativeClient()
API 允許開發人員在需要時訪問底層的原生客戶端實現
WeaviateVectorStore vectorStore = // get vector store
Optional<WeaviateClient> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
WeaviateClient client = nativeClient.get();
// Use native client capabilities
}
SimpleVectorStore 實現現在支援使用 Spring Expression Language (SpEL) 進行元資料過濾
PostgreSQL 向量儲存實現已得到改進,可以更靈活地處理不同的 ID 列型別。它現在支援以下型別,而不是強制 UUID 作為唯一的 Primary Key 型別:
這使得與現有資料庫 schema 和不同的 ID 管理策略的整合更加容易。
這些模型現已替換為 Amazon Bedrock Converse API,後者更靈活,並支援使用相同的 API 呼叫不同的模型。
每個人都在談論代理。我們近期不會構建代理框架,因為 Anthropic 的部落格文章 “構建高效代理” 引起了團隊的強烈共鳴。
摘自部落格文章
在過去的一年裡,我們與數十個在各行業構建大語言模型 (LLM) 代理的團隊合作。最成功的實現並非使用了複雜的框架或專用庫。相反,它們是使用簡單、可組合的模式構建的。
代理系統的兩個主要類別是工作流和代理
Spring AI 已經提供了“構建塊:增強型 LLM”——這是一個透過檢索、工具和記憶體等增強功能得到加強的 LLM。利用這個構建塊,部落格文章描述了幾種構建高效代理的工作流
有關這些模式的詳細實現,請參閱 Spring AI 的 使用 Spring AI 構建高效代理(第一部分) 和配套的示例。
以下是第一個模式的示例
在本示例中,我們將建立一個鏈式工作流,透過以下步驟處理商業報告:
以下是使用 Spring AI 實現此模式的方法
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// Sample business report with various metrics in natural language
String report = """
Q3 Performance Summary:
Our customer satisfaction score rose to 92 points this quarter.
Revenue grew by 45% compared to last year.
Market share is now at 23% in our primary market.
Customer churn decreased to 5% from 8%.
New user acquisition cost is $43 per user.
Product adoption rate increased to 78%.
Employee satisfaction is at 87 points.
Operating margin improved to 34%.
""";
@Bean
public CommandLineRunner commandLineRunner(ChatClient.Builder chatClientBuilder) {
return args -> {
new ChainWorkflow(chatClientBuilder.build()).chain(report);
};
}
}
/**
* Implements a prompt chaining workflow that breaks down complex tasks into a sequence
* of simpler LLM calls, where each step's output feeds into the next step.
*/
public class ChainWorkflow {
/**
* System prompts that define each transformation step in the chain.
* Each prompt acts as a gate that validates and transforms the output
* before proceeding to the next step.
*/
private static final String[] CHAIN_PROMPTS = {
// Step 1: Extract numerical values
"""
Extract only the numerical values and their associated metrics from the text.
Format each as 'value: metric' on a new line.
Example format:
92: customer satisfaction
45%: revenue growth""",
// Step 2: Standardize to percentages
"""
Convert all numerical values to percentages where possible.
If not a percentage or points, convert to decimal (e.g., 92 points -> 92%).
Keep one number per line.
Example format:
92%: customer satisfaction
45%: revenue growth""",
// Step 3: Sort in descending order
"""
Sort all lines in descending order by numerical value.
Keep the format 'value: metric' on each line.
Example:
92%: customer satisfaction
87%: employee satisfaction""",
// Step 4: Format as markdown
"""
Format the sorted data as a markdown table with columns:
| Metric | Value |
|:--|--:|
| Customer Satisfaction | 92% |"""
};
private final ChatClient chatClient;
public ChainWorkflow(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String chain(String userInput) {
String response = userInput;
for (String prompt : CHAIN_PROMPTS) {
response = chatClient.prompt(
String.format("{%s}\n{%s}", prompt, response)
).call().content();
}
return response;
}
}
關鍵的資料流是每一步的輸出都成為下一步的輸入。對於我們的示例報告,資料流如下:
完整的原始碼以及其他代理模式的實現可在 spring-ai-examples 倉庫和 使用 Spring AI 構建高效代理(第一部分) 中找到。
許多貢獻者進行了廣泛的重構、錯誤修復和文件增強。如果您的 PR 尚未被處理,請耐心等待,我們會盡快處理。感謝以下貢獻者:
本次釋出包含幾項破壞性變更,這是我們持續改進 API 設計的結果。主要變更包括
org.springframework.ai.model.function
更改為 org.springframework.ai.tool
)有關破壞性變更的完整列表和詳細的升級說明,請參閱參考文件中的升級說明。