您的位置:首页 > 汽车 > 时评 > 大模型LLM之SpringAI:Web+AI(二)

大模型LLM之SpringAI:Web+AI(二)

2024/9/25 4:32:19 来源:https://blog.csdn.net/wslzoooo/article/details/142187825  浏览:    关键词:大模型LLM之SpringAI:Web+AI(二)
2.2.2、ChatModel API(聊天模型API)

聊天模型太多了,这里只写OpenAI和Ollama

ChatModel和ChatClient区别:ChatClient针对的是所有模型,共用一个客户端。而ChatModel是针对各个模型实现的。

(1)OpenAI
自动配置
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
重试属性
属性名称描述默认值
spring.ai.retry.max-attempts重试尝试的最大次数。10
spring.ai.retry.backoff.initial-interval指数退避策略的初始休眠持续时间。2 sec.
spring.ai.retry.backoff.multiplier回退间隔乘数。5
spring.ai.retry.backoff.max-interval最大回退持续时间。3 min.
spring.ai.retry.on-client-errors如果为 false,则引发 NonTransientAiException,并且不要尝试重 4xx 试客户端错误代码false
spring.ai.retry.exclude-on-http-codes不应触发重试的 HTTP 状态代码列表(例如,抛出 NonTransientAiException)。empty
spring.ai.retry.on-http-codes应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)。empty
连接属性
属性名称描述默认值
spring.ai.openai.base-url要连接到的 URLapi.openai.com
spring.ai.openai.api-keyAPI 密钥
配置属性
属性名称描述默认值
spring.ai.openai.chat.enabled启用 OpenAI 聊天模型。true
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这是要使用的 OpenAI 聊天模型。 gpt-4ogpt-4-turbo 、、 gpt-4-turbo-2024-04-09 gpt-4-0125-previewgpt-4-turbo-preview gpt-4-vision-preview gpt-4-32k gpt-3.5-turbo gpt-3.5-turbo-0125 gpt-3.5-turbo-1106 。有关详细信息,请参阅模型页面。gpt-3.5-turbo
spring.ai.openai.chat.options.temperature用于控制生成的完成的明显创造力的采样温度。较高的值将使输出更加随机,而较低的值将使结果更加集中和确定。不建议为相同的完成请求修改温度和top_p,因为这两个设置的交互很难预测。0.8
spring.ai.openai.chat.options.frequencyPenalty介于 -2.0 和 2.0 之间的数字。到目前为止,正值会根据新标记在文本中的现有频率来惩罚新标记,从而降低模型逐字重复同一行的可能性。0.0f
spring.ai.openai.chat.options.logitBias修改指定标记在完成中出现的可能性。-
spring.ai.openai.chat.options.maxTokens聊天完成时要生成的最大令牌数。输入令牌和生成的令牌的总长度受模型上下文长度的限制。-
spring.ai.openai.chat.options.n要为每个输入消息生成多少个聊天完成选项。请注意,您将根据所有选项中生成的令牌数量付费。将 n 保持为 1 以最大程度地降低成本。1
spring.ai.openai.chat.options.presencePenalty介于 -2.0 和 2.0 之间的数字。正值会根据新标记到目前为止是否出现在文本中来惩罚它们,从而增加了模型谈论新主题的可能性。-
spring.ai.openai.chat.options.responseFormat一个对象,指定模型必须输出的格式。设置为 { "type": "json_object" } 启用 JSON 模式,该模式可保证模型生成的消息是有效的 JSON。-
spring.ai.openai.chat.options.seed此功能处于测试阶段。如果指定,我们的系统将尽最大努力确定性地进行采样,以便具有相同种子和参数的重复请求应返回相同的结果。-
spring.ai.openai.chat.options.stop最多 4 个序列,API 将停止生成更多令牌。-
spring.ai.openai.chat.options.topP温度采样的一种替代方法,称为原子核抽样,其中模型考虑具有top_p概率质量的标记的结果。因此,0.1 表示仅考虑包含前 10% 概率质量的代币。我们通常建议改变这个温度或温度,但不要两者兼而有之。-
spring.ai.openai.chat.options.tools模型可以调用的工具列表。目前,仅支持将函数作为工具。使用它来提供模型可能为其生成 JSON 输入的函数列表。-
spring.ai.openai.chat.options.toolChoice控制模型调用哪个(如果有)函数。None 表示模型不会调用函数,而是生成一条消息。自动意味着模型可以在生成消息或调用函数之间进行选择。通过 {“type: ”function“, ”function“: {”name“: ”my_function“}} 指定特定函数会强制模型调用该函数。当不存在任何函数时,None 是默认值。如果存在函数,则 AUTO 是默认值。-
spring.ai.openai.chat.options.user代表您的最终用户的唯一标识符,可以帮助 OpenAI 监控和检测滥用行为。-
spring.ai.openai.chat.options.functions函数列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。-
运行时选项

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().withModel("gpt-4-32k").withTemperature(0.4).build()));
多模态

多模态是指模型同时理解和处理来自各种来源的信息的能力,包括文本、图像、音频和其他数据格式。目前,OpenAI gpt-4-visual-previewgpt-4o 模型提供多模式支持。有关更多信息,请参阅愿景指南。OpenAI 用户消息 API 可以将 base64 编码的图像列表或图像 url 与消息合并。Spring AI 的 Message 接口通过引入 Media 类型来促进多模态 AI 模型。此类型包含有关消息中媒体附件的数据和详细信息,使用 Spring org.springframework.util.MimeTypejava.lang.Object 作为原始媒体数据。下面是一个代码示例,摘自 OpenAiChatModelIT.java,演示了用户文本与使用模型 GPT_4_VISION_PREVIEW 的图像的融合。

byte[] imageData = new ClassPathResource("/multimodal.test.png").getContentAsByteArray();var userMessage = new UserMessage("Explain what do you see on this picture?",List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData)));ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue()).build()));

或使用模型 GPT_4_O 的图像 URL 等效项:

var userMessage = new UserMessage("Explain what do you see on this picture?",List.of(new Media(MimeTypeUtils.IMAGE_PNG,"https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/_images/multimodal.test.png")));ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_O.getValue()).build()));
Controller样例
@RestController
public class ChatController {private final OpenAiChatModel chatModel;@Autowiredpublic 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", 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 chatModel.stream(prompt);}
}

配置参数

spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.7
底层openAIAPi客户端

OpenAiApi 提供的是用于 OpenAI 聊天 API 的轻量级 Java 客户端 OpenAI 聊天 API。
在这里插入图片描述
下面是一个如何以编程方式使用 api 的简单片段:

OpenAiApi openAiApi =new OpenAiApi(System.getenv("OPENAI_API_KEY"));ChatCompletionMessage chatCompletionMessage =new ChatCompletionMessage("Hello world", Role.USER);// Sync request
ResponseEntity<ChatCompletion> response = openAiApi.chatCompletionEntity(new ChatCompletionRequest(List.of(chatCompletionMessage), "gpt-3.5-turbo", 0.8f, false));// Streaming request
Flux<ChatCompletionChunk> streamResponse = openAiApi.chatCompletionStream(new ChatCompletionRequest

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com