領先一步
VMware 提供培訓和認證,助您快速提升。
瞭解更多Spring AI 現在支援 NVIDIA 的大型語言模型 API,提供了與各種模型的整合。透過利用 NVIDIA 的與 OpenAI 相容的 API,Spring AI 使開發者能夠透過熟悉的Spring AI API 使用 NVIDIA 的 LLM。
我們將探討如何配置和使用 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 端點支援工具/函式呼叫。
您可以使用 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 一起使用時,請牢記以下幾點:
有關更多資訊,請檢視 Spring AI 和 OpenAI 參考文件。
將 NVIDIA LLM API 與 Spring AI 整合,為希望在其 Spring 應用程式中利用高效能 AI 模型的開發者開闢了新的可能性。透過重用 OpenAI 客戶端,Spring AI 使得在不同 AI 提供商之間切換變得簡單直接,讓您可以為您的特定需求選擇最佳解決方案。
在您探索此整合時,請記住隨時關注 Spring AI 和 NVIDIA LLM API 的最新文件,因為功能和模型的可用性可能會隨時間演變。
我們鼓勵您嘗試不同的模型,比較它們的效能和輸出,以便找到最適合您用例的模型。
祝您程式設計愉快,盡享 NVIDIA LLM API 為您的 AI 驅動 Spring 應用程式帶來的速度和能力!