Spring AI 與 NVIDIA LLM API

工程 | Christian Tzolov | 2024 年 8 月 20 日 | ...

Spring AI 現在支援 NVIDIA 的大型語言模型 API,提供了與各種模型的整合。透過利用 NVIDIA 的與 OpenAI 相容的 API,Spring AI 使開發者能夠透過熟悉的Spring AI API 使用 NVIDIA 的 LLM。

SpringAI-NVIDIA-API-5

我們將探討如何配置和使用 Spring AI OpenAI 聊天客戶端來連線 NVIDIA LLM API。

  • 演示應用程式程式碼可在 nvidia-llm GitHub 倉庫中找到。
  • SpringAI / NVIDIA 整合的文件

前提條件

  • 建立具有足夠積分的 NVIDIA 賬戶。
  • 從 NVIDIA 提供的模型中選擇您偏好的LLM 模型。例如下方截圖中的 meta/llama-3.1-70b-instruct
  • 從模型頁面獲取您所選模型的 API 金鑰。

NVIDIA-API-KEYS

依賴項

要開始使用,請將 Spring AI OpenAI Starter 新增到您的專案中。對於 Maven,將其新增到 pom.xml 中:

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

對於 Gradle,將其新增到 build.gradle 中:

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

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

配置 Spring AI

要將 NVIDIA LLM API 與 Spring AI 一起使用,我們需要配置 OpenAI 客戶端指向 NVIDIA LLM API 端點並使用 NVIDIA 特定的模型。

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

export SPRING_AI_OPENAI_API_KEY=<NVIDIA_API_KEY>
export SPRING_AI_OPENAI_BASE_URL=https://integrate.api.nvidia.com
export SPRING_AI_OPENAI_CHAT_OPTIONS_MODEL=meta/llama-3.1-70b-instruct
export SPRING_AI_OPENAI_EMBEDDING_ENABLED=false
export SPRING_AI_OPENAI_CHAT_OPTIONS_MAX_TOKENS=2048

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

spring.ai.openai.api-key=<NVIDIA_API_KEY>
spring.ai.openai.base-url=https://integrate.api.nvidia.com
spring.ai.openai.chat.options.model=meta/llama-3.1-70b-instruct

# The NVIDIA LLM API doesn't support embeddings.
spring.ai.openai.embedding.enabled=false
# The NVIDIA LLM API requires this parameter to be set explicitly or error will be thrown.
spring.ai.openai.chat.options.max-tokens=2048

要點

  • api-key 設定為您的 NVIDIA API 金鑰。
  • base-url 設定為 NVIDIA 的 LLM API 端點:https://integrate.api.nvidia.com
  • model 設定為 NVIDIA LLM API 上可用的模型之一。
  • NVIDIA LLM API 要求明確設定 max-tokens,否則會丟擲伺服器錯誤。
  • 由於 NVIDIA LLM API 僅提供 LLM 功能,我們可以停用 embedding 端點:embedding.enabled=false

檢視參考文件以獲取完整的配置屬性列表。

程式碼示例

現在我們已經配置了 Spring AI 使用 NVIDIA LLM API,讓我們看一個如何在應用程式中使用它的簡單示例。

@RestController
public class ChatController {

    private final ChatClient chatClient;

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

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

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

ChatController.java 示例中,我們建立了一個帶有兩個端點的簡單 REST 控制器:

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

工具/函式呼叫

選擇支援工具/函式的模型時,NVIDIA LLM API 端點支援工具/函式呼叫。

SpringAI-NVIDIA-FuncitonCalling

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

查詢更多關於 SpringAI/OpenAI 函式呼叫支援的資訊。

工具示例

以下是使用 Spring AI 進行工具/函式呼叫的簡單示例:

@SpringBootApplication
public class NvidiaLlmApplication {

	public static void main(String[] args) {
		SpringApplication.run(NvidiaLlmApplication.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);
		}
	}
}

NvidiaLlmApplication.java 示例中,當模型需要天氣資訊時,它會自動呼叫 weatherFunction bean,然後該 bean 可以獲取即時天氣資料。期望的響應如下:

阿姆斯特丹目前的天氣是 20 攝氏度,巴黎目前的天氣是 25 攝氏度。

要點

將 NVIDIA LLM API 與 Spring AI 一起使用時,請牢記以下幾點:

  • 模型選擇:NVIDIA 提供來自各種提供商的廣泛模型。為您的用例選擇合適的模型。
  • API 相容性:NVIDIA LLM API 設計為與 OpenAI API 相容,這使得與 Spring AI 整合變得容易。
  • 效能:NVIDIA 的 LLM API 針對高效能推理進行了最佳化。您可能會注意到響應速度有所提高,特別是對於大型模型。
  • 專業模型:NVIDIA 提供針對不同任務(例如程式碼補全、數學問題和通用聊天)的專業模型。為您的特定需求選擇最合適的模型。
  • API 限制:注意與您的 NVIDIA API 金鑰相關的任何速率限制或使用配額。

參考資料

有關更多資訊,請檢視 Spring AI 和 OpenAI 參考文件。

結論

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

在您探索此整合時,請記住隨時關注 Spring AI 和 NVIDIA LLM API 的最新文件,因為功能和模型的可用性可能會隨時間演變。

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

祝您程式設計愉快,盡享 NVIDIA LLM API 為您的 AI 驅動 Spring 應用程式帶來的速度和能力!

訂閱 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您快速提升。

瞭解更多

獲得支援

Tanzu Spring 在一個簡單的訂閱中提供對 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案。

瞭解更多

即將舉辦的活動

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

檢視全部