首頁  >  文章  >  科技週邊  >  編程新範式,當Spring Boot遇上OpenAI

編程新範式,當Spring Boot遇上OpenAI

WBOY
WBOY轉載
2024-02-01 21:18:02890瀏覽

2023年,AI技術已成為熱門話題,對各產業產生了巨大影響,程式設計領域尤其如此。人們越來越認識到AI技術的重要性,Spring社群也不例外。

隨著GenAI(General Artificial Intelligence)技術的不斷進步,簡化具備AI功能的應用程式的創建變得至關重要和迫切。在這個背景下,"Spring AI"應運而生,旨在簡化開發AI功能應用程式的過程,使其變得簡單直觀,避免不必要的複雜性。透過"Spring AI",開發者可以更輕鬆地建立具備AI功能的應用程序,將其變得更加易於使用和操作。這不僅有助於提高開發效率,還可以加速AI技術的普及和應用。總之,"Spring AI"為AI應用程式的開發帶來了新的可能性,為開發者提供了更簡單、更直覺的工具和框架。

本文將簡要介紹Spring AI框架以及一些使用該框架的提示工程技巧。開發人員可以透過這些技巧更好地建立提示訊息,充分發揮Spring AI的功能。

1 Spring AI介紹

编程新范式,当Spring Boot遇上OpenAISpring AI由M K Pavan Kumar創建和撰寫

Spring AI是一個旨在簡化AI應用程序開發的項目,它受到了Python項目LangChain和LlamaIndex的啟發。然而,Spring AI並不是簡單的複製。它的核心理念是將生成式AI應用程式開放給各種程式語言的用戶,而不僅限於Python語言的愛好者。這意味著開發人員無需學習Python語言就可以使用他們熟悉的語言來建立AI應用程式。透過Spring AI,開發人員可以更輕鬆地利用AI的強大功能來解決各種問題,無論他們使用的是哪種程式語言。這將促進更廣泛的AI應用程式開發,並為開發人員提供更多靈活性和選擇。

Spring AI的核心目標是提供建立AI驅動應用程式的基本構建塊。這些構建塊具有高度的靈活性,可以輕鬆交換組件,幾乎不需要對程式碼進行任何修改。一個例子是,Spring AI引入了一個名為ChatClient介面的元件,它相容於OpenAI和Azure OpenAI的技術。這使得開發人員可以在不改變程式碼的情況下切換不同的AI服務供應商,從而更方便地進行開發和整合。

Spring AI的核心是為開發基於人工智慧的應用程式提供可靠的建置模組。這些模組具有彈性,使得能夠平滑地交換組件,而無需對編碼進行大量修改。一個範例是Spring AI引入了ChatClient接口,該接口與OpenAI和Azure OpenAI相容,使得開發人員能夠輕鬆地與這兩個平台進行對話。這種相容性使得開發人員能夠根據實際需求選擇適合的平台,而無需重新編寫程式碼。透過Spring AI,開發人員能夠更有效率地建立AI驅動的應用程式。

Spring AI不僅提供基本構建塊,還專注於提供更高級的解決方案。例如,它可以支援「關於自己文件的問答」或「使用文件進行互動式聊天」等典型場景。而隨著應用程式需求的成長,Spring AI計畫與Spring生態系統的其他元件如Spring Integration,Spring Batch和Spring Data等緊密合作,以滿足更複雜的業務需求。

2 建立Spring Boot專案和編寫OpenAI控制器範例

先在IDE中產生Spring Boot項目,在application.properties檔案中保留以下內容:

spring.ai.openai.api-key=<YOUR\_OPENAI\_API\_KEY>

下面編寫名為OpenAIController.java的控制器:

package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}}

3 使用Prompt類別建立提示訊息

提示類別是一個訊息物件序列的結構化持有者,每個訊息都代表提示的一部分。這些訊息在提示中扮演不同的角色和目的,內容也各不相同。包括用戶問題、AI生成的回應以及相關上下文細節等等。這種設置有助於進行複雜和精細的人機交互,因為提示由多個具有特定功能的訊息組成。

@GetMapping("/completion")public String completion(@RequestParam(value = "message") String message){return this.aiClient.generate(message);}

然而,aiClient的generate方法並不僅僅接受純文字作為參數,它也可以接受Prompt類別的物件作為參數,如下所示。現在,這個方法傳回的是AiResponse類型的實例,不是簡單的文字。

@GetMapping("/completion")public AiResponse completion(@RequestParam(value = "message") String message){ PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}"); Prompt prompt = promptTemplate.create(Map.of("query", message)); return this.aiClient.generate(prompt);}

此外,Prompt類別也提供了一個重載的建構函數,可以接受不同角色和意圖的Message類型實例序列作為參數。這樣可以更好地組織和管理提示訊息,方便後續的處理和使用。下面是一個範例程式碼,展示如何使用這個重載建構函數來合併所有內容。

package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.ai.client.Generation;import org.springframework.ai.prompt.Prompt;import org.springframework.ai.prompt.PromptTemplate;import org.springframework.ai.prompt.SystemPromptTemplate;import org.springframework.ai.prompt.messages.Message;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}@GetMapping("/completion")public List<Generation> completion(@RequestParam(value = "message") String message) {String systemPrompt = """You are a helpful AI assistant that helps people translate given text from english to french.Your name is TranslateProYou should reply to the user's request with your name and also in the style of a professional.""";SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);Message systemMessage = systemPromptTemplate.createMessage();PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}");Message userMessage = promptTemplate.createMessage(Map.of("query", message));Prompt prompt = new Prompt(List.of(systemMessage, userMessage));return this.aiClient.generate(prompt).getGenerations();}}

4 測試應用程式

可以使用市場上任何可用的開放工具來測試應用程序,例如postman、insomnia和Httpie等等。

编程新范式,当Spring Boot遇上OpenAI 圖片

#

以上是編程新範式,當Spring Boot遇上OpenAI的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除