取得領先
VMware 提供培訓和認證,以加速您的進度。
了解更多Spring AI 現在支援 NVIDIA 的大型語言模型 API,提供與各種 模型 的整合。透過利用 NVIDIA 與 OpenAI 相容的 API,Spring AI 讓開發人員可以使用 NVIDIA 的 LLM,透過熟悉的 Spring AI API。
我們將探討如何設定和使用 Spring AI OpenAI 聊天客戶端來連接 NVIDIA LLM API。
meta/llama-3.1-70b-instruct
。若要開始使用,請將 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。
若要將 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.commodel
設定為 NVIDIA LLM API 上可用的模型之一。max-tokens
,否則會擲回伺服器錯誤。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 端點支援工具/函式呼叫。
您可以將自訂 Java 函式註冊到您的 ChatModel,並讓提供的 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,然後可以取得即時天氣資料。預期的回應看起來像這樣
阿姆斯特丹目前的天氣是攝氏 20 度,而巴黎目前的天氣是攝氏 25 度。
將 NVIDIA LLM API 與 Spring AI 搭配使用時,請記住以下幾點
如需更多資訊,請查看 Spring AI 和 OpenAI 參考文件。
將 NVIDIA LLM API 與 Spring AI 整合,為希望在其 Spring 應用程式中利用高效能 AI 模型的開發人員開啟了新的可能性。透過重新利用 OpenAI 客戶端,Spring AI 可以輕鬆地在不同的 AI 提供者之間切換,讓您可以為您的特定需求選擇最佳解決方案。
在您探索此整合時,請記住隨時了解 Spring AI 和 NVIDIA LLM API 的最新文件,因為功能和模型可用性可能會隨著時間的推移而演變。
我們鼓勵您嘗試不同的模型,並比較它們的效能和輸出,以找到最適合您使用案例的模型。
祝您編碼愉快,並享受 NVIDIA LLM API 為您的 AI 驅動 Spring 應用程式帶來的速度和功能!