NVIDIA Chat
NVIDIA LLM API 是一個代理 AI 推論引擎,提供來自 各種供應商 的廣泛模型。
Spring AI 透過重複使用現有的 OpenAI 用戶端與 NVIDIA LLM API 整合。為此,您需要將 base-url 設定為 integrate.api.nvidia.com
,選擇提供的 LLM 模型 之一,並取得其 api-key
。

NVIDIA LLM API 需要明確設定 max-tokens 參數,否則會拋出伺服器錯誤。 |
請查看 NvidiaWithOpenAiChatModelIT.java 測試,以取得將 NVIDIA LLM API 與 Spring AI 搭配使用的範例。
先決條件
-
建立具有足夠額度的 NVIDIA 帳戶。
-
選擇要使用的 LLM 模型。例如,以下螢幕擷取畫面中的
meta/llama-3.1-70b-instruct
。 -
從選取模型的頁面中,您可以取得用於存取此模型的
api-key
。

自動配置
Spring AI 為 OpenAI Chat Client 提供 Spring Boot 自動配置。若要啟用它,請將下列相依性新增至專案的 Maven pom.xml
檔案
<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 AI BOM 新增至您的建置檔案。 |
Chat 屬性
重試屬性
前綴 spring.ai.retry
用作屬性前綴,可讓您設定 OpenAI chat 模型的重試機制。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.retry.max-attempts |
最大重試次數。 |
10 |
spring.ai.retry.backoff.initial-interval |
指數輪詢政策的初始睡眠持續時間。 |
2 秒。 |
spring.ai.retry.backoff.multiplier |
輪詢間隔乘數。 |
5 |
spring.ai.retry.backoff.max-interval |
最大輪詢持續時間。 |
3 分鐘。 |
spring.ai.retry.on-client-errors |
若為 false,則擲回 NonTransientAiException,且不嘗試重試 |
false |
spring.ai.retry.exclude-on-http-codes |
不應觸發重試的 HTTP 狀態代碼清單 (例如,擲回 NonTransientAiException)。 |
空白 |
spring.ai.retry.on-http-codes |
應觸發重試的 HTTP 狀態代碼清單 (例如,擲回 TransientAiException)。 |
空白 |
連線屬性
前綴 spring.ai.openai
用作屬性前綴,可讓您連線至 OpenAI。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.openai.base-url |
要連線的 URL。必須設定為 |
- |
spring.ai.openai.api-key |
NVIDIA API 金鑰 |
- |
組態屬性
前綴 spring.ai.openai.chat
是屬性前綴,可讓您設定 OpenAI 的 chat 模型實作。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.openai.chat.enabled |
啟用 OpenAI chat 模型。 |
true |
spring.ai.openai.chat.base-url |
選擇性覆寫 spring.ai.openai.base-url 以提供 chat 特定 URL。必須設定為 |
- |
spring.ai.openai.chat.api-key |
選擇性覆寫 spring.ai.openai.api-key 以提供 chat 特定 API 金鑰 |
- |
spring.ai.openai.chat.options.model |
要使用的 NVIDIA LLM 模型 |
- |
spring.ai.openai.chat.options.temperature |
要使用的取樣溫度,以控制產生的完成項目的顯著創造力。值越高,輸出會越隨機,而值越低,結果會越集中且具決定性。不建議針對相同的完成項目要求修改 temperature 和 top_p,因為這兩個設定的互動難以預測。 |
0.8 |
spring.ai.openai.chat.options.frequencyPenalty |
介於 -2.0 和 2.0 之間的數字。正值會根據新 tokens 在目前文字中的現有頻率來懲罰新 tokens,從而降低模型逐字重複相同行的可能性。 |
0.0f |
spring.ai.openai.chat.options.maxTokens |
要在 chat 完成項目中產生的最大 tokens 數。輸入 tokens 和產生的 tokens 的總長度受模型內容長度限制。 |
注意:NVIDIA LLM API 需要明確設定 |
spring.ai.openai.chat.options.n |
要為每個輸入訊息產生多少個 chat 完成選項。請注意,您將根據所有選項中產生的 tokens 數量收費。將 n 保留為 1 可將成本降至最低。 |
1 |
spring.ai.openai.chat.options.presencePenalty |
介於 -2.0 和 2.0 之間的數字。正值會根據新 tokens 是否已出現在目前文字中來懲罰新 tokens,從而提高模型談論新主題的可能性。 |
- |
spring.ai.openai.chat.options.responseFormat |
指定模型必須輸出的格式的物件。設定為 |
- |
spring.ai.openai.chat.options.seed |
此功能為 Beta 版。如果指定,我們的系統將盡力以決定性的方式取樣,以便使用相同種子和參數的重複要求應傳回相同的結果。 |
- |
spring.ai.openai.chat.options.stop |
API 將停止產生更多 tokens 的最多 4 個序列。 |
- |
spring.ai.openai.chat.options.topP |
一種使用 temperature 取樣的替代方案,稱為核取樣,其中模型會考慮具有 top_p 機率質量的 tokens 結果。因此 0.1 表示僅考慮包含前 10% 機率質量的 tokens。我們通常建議變更此項或 temperature,但不要同時變更兩者。 |
- |
spring.ai.openai.chat.options.tools |
模型可能呼叫的工具清單。目前,僅支援函式作為工具。使用此選項可提供模型可能為其產生 JSON 輸入的函式清單。 |
- |
spring.ai.openai.chat.options.toolChoice |
控制模型呼叫哪個 (如果有的話) 函式。none 表示模型不會呼叫函式,而是產生訊息。auto 表示模型可以在產生訊息或呼叫函式之間選擇。透過 {"type: "function", "function": {"name": "my_function"}} 指定特定函式會強制模型呼叫該函式。當沒有函式時,none 是預設值。如果存在函式,auto 是預設值。 |
- |
spring.ai.openai.chat.options.user |
代表您的終端使用者的唯一識別碼,可協助 OpenAI 監控和偵測濫用行為。 |
- |
spring.ai.openai.chat.options.functions |
函式清單,依其名稱識別,以在單一提示要求中啟用函式呼叫。具有這些名稱的函式必須存在於 functionCallbacks 登錄中。 |
- |
spring.ai.openai.chat.options.stream-usage |
(僅限串流) 設定為新增額外的區塊,其中包含整個要求的 token 使用率統計資訊。此區塊的 |
false |
spring.ai.openai.chat.options.proxy-tool-calls |
若為 true,Spring AI 將不會在內部處理函式呼叫,而是將其代理至用戶端。然後,用戶端有責任處理函式呼叫、將其分派至適當的函式,並傳回結果。若為 false (預設值),Spring AI 將在內部處理函式呼叫。僅適用於支援函式呼叫的 chat 模型 |
false |
前綴為 spring.ai.openai.chat.options 的所有屬性都可以在執行階段覆寫,方法是將要求特定的 執行階段選項 新增至 Prompt 呼叫。 |
執行階段選項
OpenAiChatOptions.java 提供模型組態,例如要使用的模型、temperature、frequency penalty 等。
在啟動時,可以使用 OpenAiChatModel(api, options)
建構函式或 spring.ai.openai.chat.options.*
屬性來組態預設選項。
在執行階段,您可以將新的要求特定選項新增至 Prompt
呼叫,以覆寫預設選項。例如,覆寫特定要求的預設模型和 temperature
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder()
.withModel("mixtral-8x7b-32768")
.withTemperature(0.4)
.build()
));
除了模型特定的 OpenAiChatOptions,您可以使用可攜式 ChatOptions 執行個體,使用 ChatOptionsBuilder#builder() 建立。 |
函式呼叫
當選取支援工具/函式呼叫的模型時,NVIDIA LLM API 支援工具/函式呼叫。

您可以向 ChatModel 註冊自訂 Java 函式,並讓提供的模型智慧地選擇輸出包含引數的 JSON 物件,以呼叫一個或多個已註冊的函式。這是一種強大的技術,可將 LLM 功能與外部工具和 API 連接。
工具範例
以下是如何將 NVIDIA LLM API 函式呼叫與 Spring AI 搭配使用的簡單範例
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
spring.ai.openai.chat.options.max-tokens=2048
@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);
}
}
}
在此範例中,當模型需要天氣資訊時,它會自動呼叫 weatherFunction
bean,然後可以擷取即時天氣資料。預期的回應如下所示:「阿姆斯特丹目前的天氣為攝氏 20 度,巴黎目前的天氣為攝氏 25 度。」
深入瞭解 OpenAI 函式呼叫。
範例控制器
建立 新的 Spring Boot 專案,並將 spring-ai-openai-spring-boot-starter
新增至您的 pom (或 gradle) 相依性。
在 src/main/resources
目錄下新增 application.properties
檔案,以啟用和組態 OpenAi chat 模型
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, so we need to disable it.
spring.ai.openai.embedding.enabled=false
# The NVIDIA LLM API requires this parameter to be set explicitly or server internal error will be thrown.
spring.ai.openai.chat.options.max-tokens=2048
將 api-key 取代為您的 NVIDIA 認證。 |
NVIDIA LLM API 需要明確設定 max-token 參數,否則會擲回伺服器錯誤。 |
以下是使用 chat 模型進行文字產生的簡單 @Controller
類別範例。
@RestController
public class ChatController {
private final OpenAiChatModel chatModel;
@Autowired
public ChatController(OpenAiChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/generate")
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", this.chatModel.call(message));
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return this.chatModel.stream(prompt);
}
}