NVIDIA 聊天
NVIDIA LLM API 是一款代理 AI 推理引擎,提供来自多家提供商的多种模型。
Spring AI 通过重用现有的 OpenAI 客户端与 NVIDIA LLM API 集成。
为此你需要将 base-url 设置为https://integrate.api.nvidia.com选择提供的LLM模型之一,得到API密钥为了它。
NVIDIA LLM API 要求maxTokens参数将被明确设置,否则服务器会出错。 |
检查NvidiaWithOpenAiChatModelIT.java测试 以下是使用 NVIDIA LLM API 配合 Spring AI 的示例。
前提条件
-
创建NVIDIA账户并获得足够的信用点。
-
选择一个LLM模型来使用。例如,
meta/llama-3.1-70b-instruct下面的截图。 -
从所选型号的页面,你可以获得
API密钥用于访问该模型。
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
Spring AI 为 OpenAI 聊天客户端提供 Spring Boot 自动配置。
要启用它,请在项目的Maven中添加以下依赖pom.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的物料清单添加到你的构建文件中。 |
聊天属性
重试属性
前缀spring.ai.retry作为属性前缀,用于配置OpenAI聊天模型的重试机制。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.retry.max尝试 |
重试次数上限。 |
10 |
spring.ai.retry.backoff.initial-interval |
指数式退避政策的初始睡眠时长。 |
两秒钟。 |
spring.ai.retry.backoff.multiplier |
后退间隔倍数。 |
5 |
spring.ai.retry.backoff.max区间 |
最大退回时间。 |
3分钟。 |
spring.ai.retry.on-client-errors |
如果为假,抛出非瞬态AiException,且不要尝试重试 |
false |
spring.ai.retry.exclude-on-http-codes |
不应该触发重试的HTTP状态码列表(例如抛出非瞬态Ai异常)。 |
empty |
spring.ai.retry.on-http-codes |
应触发重试的HTTP状态码列表(例如抛出TransientAiException)。 |
empty |
连接性质
前缀春.ai.openai作为属性前缀,允许你连接到OpenAI。
| 属性 | 描述 | 默认值 |
|---|---|---|
Spring.ai.openai.base-url |
连接的网址。必须设置为 |
- |
spring.ai.openai.api-key(春.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.chat |
启用OpenAI聊天模型。 |
OpenAI |
spring.ai.openai.chat.base-url |
可选覆盖 spring.ai.openai.base-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.0f |
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之间。正值会根据新Tokens是否出现在文本中来惩罚它们,从而提高模型讨论新话题的可能性。 |
- |
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.toolChoice |
控制模型调用的函数(如果有的话)。无(non)表示模型不会调用函数,而是生成消息。自动意味着模型可以选择生成消息或调用函数。通过{“类型:”函数“, ”函数“: {”name“: ”my_function“}}指定特定函数,会强制模型调用该函数。当没有函数存在时,默认为 none。如果有函数,自动是默认的。 |
- |
spring.ai.openai.chat.options.user |
一个代表终端用户的唯一标识符,可以帮助OpenAI监控和检测滥用行为。 |
- |
spring.ai.openai.chat.options.stream-usage |
(仅限流媒体播放)设置为为整个请求添加一个额外的块,包含Tokens使用统计数据。这 |
false |
spring.ai.openai.chat.options.tool-names |
列出了工具,按名称标识,用于在一次提示请求中实现函数调用。带有这些名称的工具必须存在于 ToolCallback 注册表中。 |
- |
spring.ai.openai.chat.options.tool-callbacks |
工具回调以注册到聊天模型。 |
- |
spring.ai.openai.chat.options.internal-tool-execution-enabled |
如果为 false,Spring AI 不会在内部处理工具调用,而是代理到客户端。然后由客户端负责处理工具调用,将其派遣到相应函数,并返回结果。如果为真(默认为真),Spring AI 会在内部处理函数调用。仅适用于支持函数调用的聊天模型 |
true |
运行时选项
OpenAiChatOptions.java提供模型配置,如所用模型、温度、频率惩罚等。
启动时,默认选项可以配置为OpenAiChatModel(api, options)构造者或spring.ai.openai.chat.options.*性能。
运行时,你可以通过添加新的、请求专属的选项来覆盖默认选项,提示叫。
例如,针对特定请求覆盖默认型号和温度:
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连接起来。
工具示例
这里有一个简单的示例,展示了如何用 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);
}
}
}
在这个例子中,当模型需要天气信息时,它会自动调用weatherFunctionBean,然后可以获取实时天气数据。
预期的回应是这样的:“阿姆斯特丹目前天气为20摄氏度,巴黎天气为25摄氏度。”
阅读更多关于OpenAI函数调用的信息。
采样控制器
创建一个新的 Spring Boot 项目并添加Spring-AI-starter-model-openai对你的POM(或Gradle)依赖。
添加一个application.properties文件,在src/主/资源目录,用于启用和配置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密钥用你的NVIDIA凭证。 |
NVIDIA LLM API 要求max-token(最大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);
}
}