成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringAI正式版1.0發(fā)布!核心內(nèi)容和智能體詳解

人工智能
Spring AI 1.0 的發(fā)布標(biāo)志著企業(yè)級(jí) Java 應(yīng)用程序開發(fā)進(jìn)入了一個(gè)新時(shí)代,使開發(fā)者能夠輕松地將最先進(jìn)的 AI 能力集成到他們的 Spring 應(yīng)用程序中。

在經(jīng)歷了八個(gè)里程碑式的版本之后(M1~M8),Spring AI 1.0 正式版本,終于在 2025 年 5 月 20 日正式發(fā)布了,這是另一個(gè)新高度的里程碑式的版本,標(biāo)志著 Spring 生態(tài)系統(tǒng)正式全面擁抱人工智能技術(shù),并且意味著 Spring AI 將會(huì)給企業(yè)帶來穩(wěn)定 API 支持。

1.核心特性

Spring AI 1.0 的核心是 ChatClient 接口,這是一個(gè)可移植且易于使用的 API,是與 AI 模型交互的主要接口。

它支持調(diào)用 20 多種 AI 模型,從 Anthropic 到 ZhiPu AI,并支持多模態(tài)輸入和輸出(當(dāng)?shù)讓幽P椭С謺r(shí))以及結(jié)構(gòu)化響應(yīng)(通常以 JSON 格式,便于應(yīng)用程序處理輸出)。

1.1 單模型ChatClient使用

在項(xiàng)目中只有一個(gè)模型時(shí),創(chuàng)建全局的 ChatClient:

@RestController
class MyController {

    privatefinal ChatClient chatClient;

    public MyController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/ai")
    String generation(String userInput) {
        returnthis.chatClient.prompt()
            .user(userInput)
            .call()
            .content();
    }
}

1.2 多模型ChatClient使用

在項(xiàng)目中有多個(gè)模型時(shí),為這一個(gè)模型創(chuàng)建全局的 ChatClient:

// Create ChatClient instances programmatically
ChatModel myChatModel = ... // already autoconfigured by Spring Boot
ChatClient chatClient = ChatClient.create(myChatModel);

// Or use the builder for more control
ChatClient.Builder builder = ChatClient.builder(myChatModel);
ChatClient customChatClient = builder
    .defaultSystemPrompt("You are a helpful assistant.")
    .build();

1.3 不同模型類型的ChatClients

當(dāng)項(xiàng)目中有多個(gè)模型時(shí),為每個(gè)模型定義單獨(dú)的 ChatClient:

import org.springframework.ai.chat.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
publicclass ChatClientConfig {

    @Bean
    public ChatClient openAiChatClient(OpenAiChatModel chatModel) {
        return ChatClient.create(chatModel);
    }

    @Bean
    public ChatClient anthropicChatClient(AnthropicChatModel chatModel) {
        return ChatClient.create(chatModel);
    }
}

然后,您可以使用 @Qualifier 指定大模型對(duì)應(yīng)的 ChatClient:

@Configuration
publicclass ChatClientExample {

    @Bean
    CommandLineRunner cli(
            @Qualifier("openAiChatClient") ChatClient openAiChatClient,
            @Qualifier("anthropicChatClient") ChatClient anthropicChatClient) {

        return args -> {
            var scanner = new Scanner(System.in);
            ChatClient chat;

            // Model selection
            System.out.println("\nSelect your AI model:");
            System.out.println("1. OpenAI");
            System.out.println("2. Anthropic");
            System.out.print("Enter your choice (1 or 2): ");

            String choice = scanner.nextLine().trim();

            if (choice.equals("1")) {
                chat = openAiChatClient;
                System.out.println("Using OpenAI model");
            } else {
                chat = anthropicChatClient;
                System.out.println("Using Anthropic model");
            }

            // Use the selected chat client
            System.out.print("\nEnter your question: ");
            String input = scanner.nextLine();
            String response = chat.prompt(input).call().content();
            System.out.println("ASSISTANT: " + response);

            scanner.close();
        };
    }
}

2.主要功能亮點(diǎn)

  • 檢索增強(qiáng)生成(RAG):Spring AI 提供了便攜式向量存儲(chǔ)抽象,支持 20 種不同的向量數(shù)據(jù)庫,從 Azure Cosmos DB 到 Weaviate,像常見的 Cassandra、PostgreSQL/PGVector、MongoDB Atlas、Milvus、Pinecone 和 Redis 等向量數(shù)據(jù)庫存儲(chǔ)都是支持的。還包括一個(gè)輕量級(jí)、可配置的 ETL 框架,用于將數(shù)據(jù)導(dǎo)入向量存儲(chǔ)。
  • 對(duì)話記憶:通過 ChatMemory 接口管理消息的存儲(chǔ)和檢索,支持 JDBC、Cassandra 和 Neo4j 等持久化存儲(chǔ)。
  • 工具調(diào)用:通過 @Tool 注解可以輕松定義工具,讓 AI 模型能夠獲取外部信息或執(zhí)行實(shí)際動(dòng)作。
  • 評(píng)估與測(cè)試:提供 Evaluator 接口和內(nèi)置的 RelevancyEvaluator、FactCheckingEvaluator,幫助開發(fā)者評(píng)估 AI 生成內(nèi)容的準(zhǔn)確性和相關(guān)性。
  • 可觀測(cè)性:與 Micrometer 集成,提供模型延遲、令牌使用情況等關(guān)鍵指標(biāo)的詳細(xì)遙測(cè)數(shù)據(jù)。

3.模型上下文協(xié)議(MCP)支持

Spring AI 1.0 全面支持 Model Context Protocol (MCP),這是一個(gè)標(biāo)準(zhǔn)化協(xié)議,使 AI 模型能夠與外部工具、提示和資源進(jìn)行交互。Spring AI 提供了客戶端和服務(wù)器端的 MCP支持,簡化了 MCP 工具的使用和創(chuàng)建。

最簡單的 MCP 自定義服務(wù)器端實(shí)現(xiàn):

@Service
publicclass WeatherService {

    @Tool(description = "Get weather information by city name")
    public String getWeather(String cityName) {
        // 偽代碼
        return"The weather in " + cityName + " is 21°C and sunny.";
    }
}

@SpringBootApplication
publicclass McpServerApplication {

    privatestaticfinal Logger logger = LoggerFactory.getLogger(McpServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(McpServerApplication.class, args);
    }

@Bean
public ToolCallbackProvider weatherTools(WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
 }
}

最簡單的 MCP 客戶端核心代碼實(shí)現(xiàn):

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
publicclass ClientController {
    @Autowired
    private ChatClient chatClient;

    @RequestMapping("/chat")
    public String chat(@RequestParam(value = "msg",defaultValue = "今天天氣如何?") String msg) {
        String response = chatClient.prompt()
        .user(msg)
        .call()
        .content();
        System.out.println("響應(yīng)結(jié)果: " + response);
        return response;
    }
}

4.AI Agent(智能體)支持

AI Agent 的核心是“利用 AI 模型與其環(huán)境交互,以解決用戶定義的任務(wù)”。有效的 AI Agent 將規(guī)劃、記憶和作相結(jié)合,以完成用戶分配的任務(wù)。

Spring AI 1.0 支持兩種主要類型的 Agent:

  • 工作流驅(qū)動(dòng)代理:通過預(yù)定義路徑編排 LLM 和工具,一種更可控的 Agents 實(shí)現(xiàn)方法,其中 LLM 和工具通過預(yù)定義的路徑進(jìn)行編排。這些工作流是規(guī)范性的,可指導(dǎo) AI 完成既定的作序列以實(shí)現(xiàn)可預(yù)測(cè)的結(jié)果。
  • 自主驅(qū)動(dòng)代理:允許 LLM 自主規(guī)劃和執(zhí)行處理步驟。這種方式代理將自己決定要調(diào)用的路徑,決定使用哪些工具以及以什么順序使用。

雖然完全自主代理的靈活性很有吸引力,但工作流為定義明確的任務(wù)提供了更好的可預(yù)測(cè)性和一致性。具體使用哪種類型,取決于您的具體要求和風(fēng)險(xiǎn)承受能力。

讓我們看看 Spring AI 如何通過五種基本模式來實(shí)現(xiàn)這些概念,每種模式都服務(wù)于特定的用例:

4.1 Chain 工作流模式

該模式將復(fù)雜任務(wù)分解為一系列步驟,其中每個(gè) LLM 調(diào)用都會(huì)處理前一個(gè) LLM 調(diào)用的輸出。

Chain Workflow 模式體現(xiàn)了將復(fù)雜任務(wù)分解為更簡單、更易于管理的步驟的原則。

圖片圖片

使用場(chǎng)景

  • 具有明確順序步驟的任務(wù)。
  • 當(dāng)您想用延遲換取更高的準(zhǔn)確性時(shí)。
  • 當(dāng)每個(gè)步驟都基于上一步的輸出時(shí)。

以下是 Spring AI 實(shí)現(xiàn)中的一個(gè)實(shí)際示例:

public class ChainWorkflow {
    privatefinal ChatClient chatClient;
    privatefinal String[] systemPrompts;

    public String chain(String userInput) {
        String response = userInput;
        for (String prompt : systemPrompts) {
            String input = String.format("{%s}\n {%s}", prompt, response);
            response = chatClient.prompt(input).call().content();
        }
        return response;
    }
}

此實(shí)現(xiàn)演示了幾個(gè)關(guān)鍵原則:

  • 每個(gè)步驟都有重點(diǎn)。
  • 一個(gè)步驟的輸出成為下一個(gè)步驟的輸入。
  • 該鏈易于擴(kuò)展和維護(hù)。

4.2 并行化工作流

LLM 可以同時(shí)處理任務(wù),并以編程方式聚合其輸出。

圖片

使用場(chǎng)景

  • 處理大量相似但獨(dú)立的項(xiàng)目。
  • 需要多個(gè)獨(dú)立視角的任務(wù)。
  • 當(dāng)處理時(shí)間至關(guān)重要且任務(wù)可并行化時(shí)。

簡單代碼實(shí)現(xiàn):

List<String> parallelResponse = new ParallelizationWorkflow(chatClient)
.parallel(
    "Analyze how market changes will impact this stakeholder group.",
    List.of(
        "Customers: ...",
        "Employees: ...",
        "Investors: ...",
        "Suppliers: ..."
    ),
    4
);

4.3 路由工作流

路由模式實(shí)現(xiàn)了智能任務(wù)分配,從而支持對(duì)不同類型的輸入進(jìn)行專門處理。

圖片

使用場(chǎng)景

  • 具有不同輸入類別的復(fù)雜任務(wù)。
  • 當(dāng)不同的輸入需要專門處理時(shí)。
  • 何時(shí)可以準(zhǔn)確處理分類。

簡單代碼實(shí)現(xiàn):

@Autowired
private ChatClient chatClient;

RoutingWorkflow workflow = new RoutingWorkflow(chatClient);

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..."
);

String input = "My account was charged twice last week";
String response = workflow.route(input, routes);

4.4 編排器

圖片

使用場(chǎng)景

  • 無法預(yù)先預(yù)測(cè)子任務(wù)的復(fù)雜任務(wù)。
  • 需要不同方法或觀點(diǎn)的任務(wù)。
  • 需要適應(yīng)性問題解決的情況。

簡單實(shí)現(xiàn)代碼:

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);

WorkerResponse response = workflow.process(
    "Generate both technical and user-friendly documentation for a REST API endpoint"
);

System.out.println("Analysis: " + response.analysis());
System.out.println("Worker Outputs: " + response.workerResponses());

4.5 評(píng)估器-優(yōu)化器

圖片圖片

使用場(chǎng)景

  • 存在明確的評(píng)估標(biāo)準(zhǔn)。
  • 迭代優(yōu)化提供可衡量的價(jià)值。
  • 任務(wù)受益于多輪批評(píng)。
public class EvaluatorOptimizerWorkflow {
    public RefinedResponse loop(String task) {
        Generation generation = generate(task, context);
        EvaluationResponse evaluation = evaluate(generation.response(), task);
        return new RefinedResponse(finalSolution, chainOfThought);
    }
}

使用示例:

ChatClient chatClient = // ... initialize chat client
EvaluatorOptimizerWorkflow workflow = new EvaluatorOptimizerWorkflow(chatClient);

RefinedResponse response = workflow.loop(
    "Create a Java class implementing a thread-safe counter"
);

System.out.println("Final Solution: " + response.solution());
System.out.println("Evolution: " + response.chainOfThought());

5.開始使用SpringAI

開發(fā)者可以通過 Maven 中央倉庫獲取 Spring AI 1.0 的所有組件。使用提供的 bom 導(dǎo)入依賴:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>1.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

也可以在 Spring Initializr 網(wǎng)站上創(chuàng)建 1.0 GA 應(yīng)用程序,并參考參考文檔中的"Getting Started"部分。

小結(jié)

Spring AI 1.0 的發(fā)布標(biāo)志著企業(yè)級(jí) Java 應(yīng)用程序開發(fā)進(jìn)入了一個(gè)新時(shí)代,使開發(fā)者能夠輕松地將最先進(jìn)的 AI 能力集成到他們的 Spring 應(yīng)用程序中。


責(zé)任編輯:武曉燕 來源: Java和磊哥
相關(guān)推薦

2013-06-24 09:11:22

Windows 8.1

2014-12-09 09:41:46

谷歌Android Stu

2009-12-01 09:41:57

NetBeans 6.

2011-09-07 16:24:01

Windows Int

2021-08-26 16:51:51

谷歌PixelAndroid

2009-06-08 13:29:36

NetBeans 6.NetBeans ID

2025-05-16 16:00:15

Spring AI大模型) 人工智能

2012-01-13 10:20:32

Windows 8微軟

2016-01-05 13:41:59

物聯(lián)網(wǎng)內(nèi)容硬件

2011-07-01 14:12:57

SEO

2010-06-01 14:16:09

PylonsPython

2010-04-08 09:51:37

Windows 7 S發(fā)布日期

2009-02-22 09:25:40

Vista RC SP正式版發(fā)布日期

2023-01-24 16:44:49

iOSiOS 16.3蘋果

2009-12-02 11:05:26

Spring 3.0

2011-05-19 09:45:05

Opera 11.11

2011-09-14 13:28:36

PostgreSQL

2011-12-07 10:26:11

2011-12-12 09:56:19

CentOS 6.1發(fā)布

2011-01-26 09:37:49

LibreOffice
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 亚洲一区二区在线 | 成人久久久久久久久 | www.99久久.com | 久久亚洲春色中文字幕久久久 | 一级免费黄色 | 爱爱免费视频 | 精品二区 | 午夜视频一区二区三区 | 国产精品九九九 | 久久久综合色 | 日韩一区二区三区在线视频 | 一级日批片 | 97精品国产97久久久久久免费 | 日韩和的一区二区 | 91亚洲精品久久久电影 | 欧美精品一区在线观看 | v亚洲 | 亚洲网站在线观看 | 国产美女精品视频 | 在线观看视频福利 | 欧美 日韩 综合 | 成人久久网| 久久草在线视频 | 精品久久久久国产免费第一页 | 亚洲美女视频 | 亚洲欧美激情精品一区二区 | 天天干干 | 99国产精品久久久 | av在线免费不卡 | 欧美乱人伦视频 | 日韩精品一区二区三区四区视频 | 亚洲欧美日韩精品久久亚洲区 | 国产在线精品一区二区 | 国产在视频一区二区三区吞精 | 欧美福利网站 | 伊人网91 | 成人二区 | 一区二区电影网 | 91香蕉视频在线观看 | 一级毛片免费完整视频 | 日韩有码一区 |