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测试 有关将 NVIDIA LLM API 与 Spring AI 结合使用的示例。
前提条件
-
创建具有足够积分的 NVIDIA 帐户。
-
选择要使用的 LLM 模型。例如,
meta/llama-3.1-70b-instruct
在下面的屏幕截图中。 -
在所选模型的页面中,您可以获取
api-key
用于访问此模型。

自动配置
Spring AI 自动配置、入门模块的工件名称发生了重大变化。有关更多信息,请参阅升级说明。 |
Spring AI 为 OpenAI 聊天客户端提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
或 Gradlebuild.gradle
构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。 |
聊天属性
重试属性
前缀spring.ai.retry
用作属性前缀,用于配置 OpenAI 聊天模型的重试机制。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.retry.max次尝试 |
重试尝试的最大次数。 |
10 |
spring.ai.retry.backoff.初始间隔 |
指数退避策略的初始睡眠持续时间。 |
2 秒。 |
spring.ai.retry.backoff.乘数 |
回退间隔乘数。 |
5 |
spring.ai.retry.backoff.max间隔 |
最大回退持续时间。 |
3分钟 |
spring.ai.retry.on-client-错误 |
如果为 false,则引发 NonTransientAiException,并且不要尝试重试 |
false |
spring.ai.retry.exclude-on-http-codes |
不应触发重试的 HTTP 状态代码列表(例如,引发 NonTransientAiException)。 |
empty |
spring.ai.retry.on-http-代码 |
应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)。 |
empty |
连接属性
前缀spring.ai.openai
用作允许您连接到 OpenAI 的属性前缀。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.openai.base-url |
要连接到的 URL。必须设置为 |
- |
spring.ai.openai.api-key |
NVIDIA API 密钥 |
- |
配置属性
现在,通过前缀为 要启用,spring.ai.model.chat=openai(默认启用) 要禁用,spring.ai.model.chat=none(或任何与 openai 不匹配的值) 进行此更改是为了允许配置多个模型。 |
前缀spring.ai.openai.chat
是属性前缀,用于配置 OpenAI 的聊天模型实现。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.openai.chat.enabled(已删除且不再有效) |
启用 OpenAI 聊天模型。 |
true |
spring.ai.model.聊天 |
启用 OpenAI 聊天模型。 |
OpenAI |
spring.ai.openai.chat.base-url |
可选覆盖spring.ai.openai.base-url以提供特定于聊天的url。必须设置为 |
- |
spring.ai.openai.chat.api-key |
可选覆盖spring.ai.openai.api-key以提供特定于聊天的api-key |
- |
spring.ai.openai.chat.options.model |
要使用的 NVIDIA LLM 模型 |
- |
spring.ai.openai.chat.options.temperature |
要使用的采样温度,用于控制生成的完成的表观创造力。较高的值将使输出更加随机,而较低的值将使结果更加集中和确定。不建议为相同的完成请求修改温度和top_p,因为这两个设置的交互很难预测。 |
0.8 |
spring.ai.openai.chat.options.frequency惩罚 |
数字介于 -2.0 和 2.0 之间。正值会根据新标记迄今为止在文本中的现有频率来惩罚它们,从而降低模型逐字重复同一行的可能性。 |
0.0华氏度 |
spring.ai.openai.chat.options.maxTokens |
聊天补全时要生成的最大Tokens数。输入标记和生成标记的总长度受模型上下文长度的限制。 |
注意:NVIDIA LLM API 需要 |
spring.ai.openai.chat.options.n |
要为每条输入消息生成多少个聊天补全选项。请注意,您将根据所有选项中生成的Tokens数量付费。将 n 保持为 1 以最大限度地降低成本。 |
1 |
spring.ai.openai.chat.options.presence惩罚 |
数字介于 -2.0 和 2.0 之间。正值会根据新标记到目前为止是否出现在文本中来惩罚它们,从而增加模型谈论新主题的可能性。 |
- |
spring.ai.openai.chat.options.response格式 |
指定模型必须输出的格式的对象。设置为 |
- |
spring.ai.openai.chat.options.seed |
此功能处于测试阶段。如果指定,我们的系统将尽最大努力确定性地采样,以便具有相同种子和参数的重复请求应返回相同的结果。 |
- |
spring.ai.openai.chat.options.stop |
最多 4 个序列,API 将停止生成进一步的Tokens。 |
- |
spring.ai.openai.chat.options.topP |
温度采样的替代方法称为核采样,其中模型考虑具有top_p概率质量的标记的结果。因此,0.1 意味着仅考虑包含前 10% 概率质量的Tokens。我们通常建议更改此温度或温度,但不要同时更改两者。 |
- |
spring.ai.openai.chat.options.tools |
模型可能调用的工具列表。目前,仅支持功能作为工具。使用它来提供模型可能为其生成 JSON 输入的函数列表。 |
- |
spring.ai.openai.chat.options.tool选择 |
控制模型调用的函数(如果有)。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 |
(仅适用于流媒体)设置为添加一个额外的块,其中包含整个请求的Tokens使用情况统计信息。这 |
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#builder() 创建的可移植 ChatOptions 实例。 |
函数调用
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 函数调用的更多信息。
Samples控制器
创建一个新的 Spring Boot 项目,并将spring-ai-starter-model-openai
到你的 pom(或 gradle)依赖项。
添加一个application.properties
文件,在src/main/resources
目录,以启用和配置 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-token 显式设置参数,否则将抛出服务器错误。 |
这是一个简单的示例@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);
}
}