Spring AI 與 Groq - 極速 AI 推理引擎

工程技術 | Christian Tzolov | 2024 年 7 月 31 日 | ...

更快捷的資訊處理不僅能提供資訊,還能改變我們的認知和創新方式。

Spring AI,一個用於將 AI 功能整合到 Spring 應用中的強大框架,現已支援Groq - 一個極速 AI 推理引擎,支援工具/函式呼叫。

利用 Groq 與 OpenAI 相容的 API,Spring AI 透過調整其現有的OpenAI 聊天客戶端實現了無縫整合。這種方法使開發者能夠透過熟悉的 Spring AI API 利用 Groq 的高效能模型。

spring-ai-groq-integration

我們將探討如何配置和使用 Spring AI OpenAI 聊天客戶端連線 Groq。有關詳細資訊,請查閱 Spring AI Groq 文件和相關的測試

Groq API 金鑰

要與 Groq 互動,您需要從https://console.groq.com/keys獲取 Groq API 金鑰。

依賴項

將 Spring AI OpenAI Starter 新增到您的專案。

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

對於 Gradle,將其新增到您的 build.gradle 檔案中

dependencies {
  implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}

確保您已新增 Spring 的Milestone 和 Snapshot 倉庫並新增Spring AI BOM

配置 Spring AI 以使用 Groq

要將 Groq 與 Spring AI 一起使用,我們需要配置 OpenAI 客戶端以指向 Groq 的 API 端點並使用 Groq 專有模型。

將以下環境變數新增到您的專案

export SPRING_AI_OPENAI_API_KEY=<INSERT GROQ API KEY HERE>  
export SPRING_AI_OPENAI_BASE_URL=https://api.groq.com/openai  
export SPRING_AI_OPENAI_CHAT_OPTIONS_MODEL=llama3-70b-8192

或者,您可以將這些新增到您的 application.properties 檔案中

spring.ai.openai.api-key=<GROQ_API_KEY>
spring.ai.openai.base-url=https://api.groq.com/openai
spring.ai.openai.chat.options.model=llama3-70b-8192
spring.ai.openai.chat.options.temperature=0.7

關鍵點

  • api-key 設定為您的Groq 金鑰之一。
  • base-url 設定為 Groq 的 API 端點:https://api.groq.com/openai
  • model 設定為 Groq 的可用模型之一。

有關完整的配置屬性列表,請查閱Groq 聊天屬性文件。

程式碼示例

現在我們已經配置 Spring AI 使用 Groq,讓我們看一個如何在您的應用中使用它的簡單示例。

@RestController
public class ChatController {

  private final ChatClient chatClient;

  @Autowired
  public ChatController(ChatClient.Builder builder) {
      this.chatClient = builder.build();
  }

  @GetMapping("/ai/generate")
  public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
     String response = chatClient.prompt().user(message).call().content();
     return Map.of("generation", response);
  }

  @GetMapping("/ai/generateStream")
  public Flux<String> generateStream(@RequestParam(value = "message", 
        defaultValue = "Tell me a joke") String message) {
      return chatClient.prompt().user(message).stream().content();
  }
}

在這個示例中,我們建立了一個簡單的 REST 控制器,包含兩個端點:

  • /ai/generate: 為給定提示生成一個響應。
  • /ai/generateStream: 以流式方式傳輸響應,這對於較長的輸出或即時互動非常有用。

工具/函式

Groq API 端點在選擇支援工具/函式的模型時支援工具/函式呼叫

spring-ai-groq-functions-2

您可以在 ChatModel 中註冊自定義 Java 函式,並讓提供的 Groq 模型智慧地選擇輸出一個包含呼叫一個或多個已註冊函式的引數的 JSON 物件。這是一種將大型語言模型 (LLM) 功能與外部工具和 API 連線起來的強大技術。

工具示例

以下是使用 Spring AI 呼叫 Groq 函式的一個簡單示例:

@SpringBootApplication
public class GroqApplication {

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

	@Bean
	CommandLineRunner runner(ChatClient.Builder chatClientBuilder) {
		return args -> {
			var chatClient = chatClientBuilder.build();

			var response = chatClient.prompt()
				.user("What is the weather in Amsterdam and Paris?")
				.functions("weatherFunction") // reference by bean name.
				.call()
				.content();

			System.out.println(response);
		};
	}

	@Bean
	@Description("Get the weather in location")
	public Function<WeatherRequest, WeatherResponse> weatherFunction() {
		return new MockWeatherService();
	}

	public static class MockWeatherService implements Function<WeatherRequest, WeatherResponse> {

		public record WeatherRequest(String location, String unit) {}
		public record WeatherResponse(double temp, String unit) {}

		@Override
		public WeatherResponse apply(WeatherRequest request) {
			double temperature = request.location().contains("Amsterdam") ? 20 : 25;
			return new WeatherResponse(temperature, request.unit);
		}
	}
}

在此示例中,當模型需要天氣資訊時,它將自動呼叫 weatherFunction bean,該 bean 隨後可以獲取即時天氣資料。

預期的響應示例如下:“阿姆斯特丹目前的天氣為 20 攝氏度,巴黎目前的天氣為 25 攝氏度。”

閱讀更多關於 OpenAI 函式呼叫的資訊。

主要注意事項

將 Groq 與 Spring AI 一起使用時,請記住以下幾點:

  • 工具/函式呼叫:Groq支援工具/函式呼叫。檢視推薦使用的模型。
  • API 相容性:Groq API 與 OpenAI API 並非完全相容。請注意行為或功能上的潛在差異。
  • 模型選擇:確保您使用的是 Groq 專有的模型之一。
  • 多模態限制:目前,Groq 不支援多模態訊息。
  • 效能:Groq 以其快速推理時間而聞名。與其他提供商相比,您可能會注意到響應速度有所提高,特別是對於大型模型。

結論

將 Groq 與 Spring AI 整合,為希望在其 Spring 應用中利用高效能 AI 模型的開發者帶來了新的可能性。透過重用 OpenAI 客戶端,Spring AI 使得在不同 AI 提供商之間切換變得簡單直觀,讓您可以為特定需求選擇最佳解決方案。

在探索這種整合時,請記住隨時查閱Spring AIGroq的最新文件,因為功能和相容性可能會隨時間變化。

我們鼓勵您嘗試不同的 Groq 模型,並比較它們的效能和輸出,以便找到最適合您的用例的模型。

程式設計愉快,享受 Groq 為您的 AI 驅動的 Spring 應用帶來的速度和能力吧!

訂閱 Spring 通訊

透過 Spring 通訊保持聯絡

訂閱

保持領先

VMware 提供培訓和認證,助您加速發展。

瞭解更多

獲取支援

Tanzu Spring 透過一個簡單的訂閱,即可提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視全部