NVIDIA 聊天
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 测试用例,了解如何在 Spring AI 中使用 NVIDIA LLM API 的示例。
先决条件
-
创建拥有足够积分的 NVIDIA 账户。
-
选择一个要使用的 LLM 模型。例如,下图中的
meta/llama-3.1-70b-instruct
。 -
在所选模型的页面上,您可以获取访问该模型的
api-key
。

自动配置
Spring AI 自动配置、starter 模块的 artifact 名称发生了重大变化。更多信息请参考升级说明。 |
Spring AI 为 OpenAI 聊天客户端提供了 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml
文件中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
或添加到您的 Gradle build.gradle
构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
请参阅依赖管理部分,将 Spring AI BOM 添加到您的构建文件。 |
聊天属性
重试属性
前缀 spring.ai.retry
用作属性前缀,允许您配置 OpenAI 聊天模型的重试机制。
属性 | 描述 | 默认值 |
---|---|---|
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 的聊天模型实现。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.openai.chat.enabled (已移除且不再有效) |
启用 OpenAI 聊天模型。 |
true |
spring.ai.model.chat |
启用 OpenAI 聊天模型。 |
openai |
spring.ai.openai.chat.base-url |
可选地覆盖 |
- |
spring.ai.openai.chat.api-key |
可选地覆盖 |
- |
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 之间的数字。正值会根据新 token 在文本中已出现的频率来惩罚新 token,从而降低模型逐字重复同一行的可能性。 |
0.0f |
spring.ai.openai.chat.options.maxTokens |
在聊天补全中生成的最大 token 数。输入 token 和生成 token 的总长度受限于模型的上下文长度。 |
注意:NVIDIA LLM API 要求显式设置 |
spring.ai.openai.chat.options.n |
为每个输入消息生成多少个聊天补全选项。请注意,您将根据所有选项生成的 token 总数计费。将 n 设为 1 以最小化成本。 |
1 |
spring.ai.openai.chat.options.presencePenalty |
介于 -2.0 和 2.0 之间的数字。正值会根据新 token 是否已出现在文本中来惩罚新 token,从而增加模型谈论新主题的可能性。 |
- |
spring.ai.openai.chat.options.responseFormat |
一个对象,指定模型必须输出的格式。设置为 |
- |
spring.ai.openai.chat.options.seed |
此功能处于 Beta 阶段。如果指定,我们的系统将尽最大努力进行确定性采样,以便使用相同的种子和参数重复请求时应返回相同的结果。 |
- |
spring.ai.openai.chat.options.stop |
API 将停止生成后续 token 的最多 4 个序列。 |
- |
spring.ai.openai.chat.options.topP |
一种替代温度采样的核采样(nucleus sampling)方法,模型会考虑 top_p 概率质量的 token 结果。因此,0.1 意味着只考虑构成前 10% 概率质量的 token。我们通常建议修改此参数或 temperature,但不要同时修改两者。 |
- |
spring.ai.openai.chat.options.tools |
模型可以调用的工具列表。目前,仅支持函数作为工具。使用此参数提供模型可以为其生成 JSON 输入的函数列表。 |
- |
spring.ai.openai.chat.options.toolChoice |
控制模型调用哪个(如果存在)函数。 |
- |
spring.ai.openai.chat.options.user |
代表您的终端用户的唯一标识符,这有助于 OpenAI 监控和检测滥用行为。 |
- |
spring.ai.openai.chat.options.functions |
函数列表,按名称标识,用于在单个 prompt 请求中启用函数调用。具有这些名称的函数必须存在于 |
- |
spring.ai.openai.chat.options.stream-usage |
(仅用于流式传输)设置为添加一个额外的块,其中包含整个请求的 token 使用统计信息。此块的 |
false |
spring.ai.openai.chat.options.proxy-tool-calls |
如果为 true,Spring AI 将不内部处理函数调用,而是将其代理给客户端。然后由客户端负责处理函数调用,将其分派给适当的函数,并返回结果。如果为 false(默认值),Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型。 |
false |
所有带有 spring.ai.openai.chat.options 前缀的属性都可以在运行时通过在 Prompt 调用中添加请求特定的运行时选项来覆盖。 |
运行时选项
OpenAiChatOptions.java
提供模型配置,例如要使用的模型、温度、频率惩罚等。
在启动时,可以通过 OpenAiChatModel(api, options)
构造函数或 spring.ai.openai.chat.options.*
属性配置默认选项。
在运行时,您可以通过在 Prompt
调用中添加新的、请求特定的选项来覆盖默认选项。例如,为特定请求覆盖默认模型和温度
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder()
.model("mixtral-8x7b-32768")
.temperature(0.4)
.build()
));
除了模型特定的 OpenAiChatOptions 外,您还可以使用一个可移植的 ChatOptions 实例,该实例使用 ChatOptions#builder() 创建。 |
函数调用
NVIDIA LLM API 在选择支持它的模型时支持工具/函数调用。

您可以在 ChatModel 中注册自定义 Java 函数,让提供的模型智能地选择输出一个包含调用一个或多个已注册函数所需参数的 JSON 对象。这是一种将 LLM 功能与外部工具和 API 连接起来的强大技术。
工具示例
以下是一个关于如何在 Spring AI 中使用 NVIDIA LLM API 函数调用的简单示例
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,然后该 bean 可以获取实时天气数据。预期的响应如下所示:“阿姆斯特丹目前天气为 20 摄氏度,巴黎目前天气为 25 摄氏度。”
阅读更多关于 OpenAI 函数调用的内容。
示例 Controller
创建一个新的 Spring Boot 项目,并将 spring-ai-starter-model-openai
添加到您的 pom(或 gradle)依赖项中。
在 src/main/resources
目录下添加一个 application.properties
文件,用于启用和配置 OpenAI 聊天模型
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-tokens 参数,否则将抛出服务器错误。 |
这是一个使用聊天模型生成文本的简单 @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);
}
}