Spring AI 来啦,快速上手

2 篇文章 0 订阅
2 篇文章 0 订阅

Spring AI

Spring框架在软件开发领域,特别是在Java企业级应用中,一直扮演着举足轻重的角色。它以其强大的功能和灵活的架构,帮助开发者高效构建复杂的应用程序。而Spring Boot的推出,更是简化了新Spring应用的初始搭建和开发过程,让开发者能够更快速地构建和部署应用。
随着人工智能技术的快速发展,越来越多的领域开始融合AI技术,以提高效率、优化流程和增强用户体验。在这个背景下,Spring AI项目的推出无疑为Java开发者打开了一扇新的大门,使他们能够更容易地将AI技术集成到Spring应用程序中。
Spring AI项目不仅为开发者提供了一套丰富的工具和库,还集成了自然语言处理(NLP)、机器学习以及图像识别和计算机视觉等功能。这意味着开发者无需深入了解AI技术的细节,就可以利用Spring AI轻松实现复杂的AI功能,如文本处理、情感分析、语音识别、图像识别等。
此外,Spring AI的推出也标志着Spring生态的进一步扩展。随着AI技术的不断发展和普及,未来Spring框架有望与更多AI技术进行深度集成,为开发者提供更强大的功能和更广阔的应用场景。

Spring AI 特性

模型支持:Spring AI框架集成了多家国外主流的AI模型提供商,如OpenAI等,使得开发者能够轻松地使用这些高质量的AI模型。此外,框架的扩展性设计也使得后续集成国内的大模型变得十分方便,从而满足更广泛的业务需求。
模型类型:Spring AI支持多种模型类型,包括聊天机器人、文本到图像转换等,为开发者提供了丰富的选择。这些模型类型能够覆盖各种应用场景,满足不同的业务需求。
API可移植性:Spring AI框架提供了跨不同AI提供商的可移植API,这使得开发者能够轻松地在不同的服务提供商之间切换,而无需担心功能丢失或重新开发。这种可移植性有助于降低技术锁定风险,提高系统的灵活性和可扩展性。
数据映射:Spring AI将AI模型的输出映射到Java对象(POJO),从而简化了数据处理流程。这种映射机制使得开发者能够更方便地处理和使用AI模型的输出数据,提高了开发效率和代码的可读性。
矢量数据库支持:Spring AI框架覆盖了所有主流的矢量数据库供应商,从Azure Vector Search到Weaviate等,为开发者提供了丰富的数据库选择。这种支持使得开发者能够根据自己的需求选择合适的数据库,从而优化系统的性能和稳定性。
SQL类过滤器API:Spring AI提供了类似SQL的元数据过滤器API,使得开发者能够跨供应商实现一致性的数据过滤操作。这种API设计降低了学习成本,提高了开发效率,使得开发者能够更快速地构建出功能强大的AI应用。
Spring Boot集成:Spring AI专为Spring Boot设计了自动配置和启动器,使得AI集成变得轻而易举。开发者只需在Spring Boot项目中添加相应的依赖和配置,即可快速集成AI功能,无需进行繁琐的手动配置和编码工作。这种集成方式降低了技术门槛,使得更多的开发者能够轻松地利用Spring AI框架构建出高质量的AI应用。

快速入门

MAVEN配置

导入Spring AI BOM: 使用Spring AI BOM定义

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

添加AI功能:添加相关的AI模块依赖项到pom.xml,比如OpenAI 聊天。
(您需要使用 OpenAI 创建 API 来访问 ChatGPT 模型。在OpenAI 注册页面创建帐户并在API 密钥页面生成令牌。 Spring AI 项目定义了一个配置属性,您应该将其设置为从 openai.com 获取的spring.ai.openai.api-key值。API Key导出环境变量是设置该配置属性的一种方法:export SPRING_AI_OPENAI_API_KEY=)

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

Chat Properties配置

重试属性

该前缀spring.ai.retry用作属性前缀,可让您配置 OpenAI Chat 客户端的重试机制。

PropertyDescriptionDefault
spring.ai.retry.max-attemptsMaximum number of retry attempts.10
spring.ai.retry.backoff.initial-intervalInitial sleep duration for the exponential backoff policy.2 sec.
spring.ai.retry.backoff.multiplierBackoff interval multiplier.5
spring.ai.retry.backoff.max-intervalMaximum backoff duration.3 min.
spring.ai.retry.on-client-errorsIf false, throw a NonTransientAiException, and do not attempt retry for 4xx client error codesfalse
spring.ai.retry.exclude-on-http-codesList of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException).empty
spring.ai.retry.on-http-codesList of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException).empty
连接属性

该前缀spring.ai.openai用作允许您连接到 OpenAI 的属性前缀。

PropertyDescriptionDefault
spring.ai.openai.base-urlThe URL to connect toapi.openai.com
spring.ai.openai.api-keyThe API Key-
配置属性

前缀spring.ai.openai.chat是属性前缀,可让您配置 OpenAI 的聊天客户端实现。

PropertyDescriptionDefault
spring.ai.openai.chat.enabledEnable OpenAI chat client.true
spring.ai.openai.chat.base-urlOptional overrides the spring.ai.openai.base-url to provide chat specific url-
spring.ai.openai.chat.api-keyOptional overrides the spring.ai.openai.api-key to provide chat specific api-key-
spring.ai.openai.chat.options.modelThis is the OpenAI Chat model to usegpt-3.5-turbo (the gpt-3.5-turbo, gpt-4, and gpt-4-32k point to the latest model versions)
spring.ai.openai.chat.options.temperatureThe sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict.0.8
spring.ai.openai.chat.options.frequencyPenaltyNumber between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.0.0f
spring.ai.openai.chat.options.logitBiasModify the likelihood of specified tokens appearing in the completion.-
spring.ai.openai.chat.options.maxTokensThe maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model’s context length.-
spring.ai.openai.chat.options.nHow many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.1
spring.ai.openai.chat.options.presencePenaltyNumber between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.-
spring.ai.openai.chat.options.responseFormatAn object specifying the format that the model must output. Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.-
spring.ai.openai.chat.options.seedThis feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.-
spring.ai.openai.chat.options.stopUp to 4 sequences where the API will stop generating further tokens.-
spring.ai.openai.chat.options.topPAn alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.-
spring.ai.openai.chat.options.toolsA list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.-
spring.ai.openai.chat.options.toolChoiceControls which (if any) function is called by the model. none means the model will not call a function and instead generates a message. auto means the model can pick between generating a message or calling a function. Specifying a particular function via {"type: “function”, “function”: {“name”: “my_function”}} forces the model to call that function. none is the default when no functions are present. auto is the default if functions are present.-
spring.ai.openai.chat.options.userA unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.-
spring.ai.openai.chat.options.functionsList of functions, identified by their names, to enable for function calling in a single prompt requests. Functions with those names must exist in the functionCallbacks registry.-

您可以覆盖通用的spring.ai.openai.base-urland spring.ai.openai.api-keyforChatClient和EmbeddingClient实现。如果设置了和属性spring.ai.openai.chat.base-url,spring.ai.openai.chat.api-key则优先于公共属性。如果您想对不同的模型和不同的模型端点使用不同的 OpenAI 帐户,这非常有用。

代码demo

配置

spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.openai.embedding.options.model=text-embedding-ada-002

Controller代码

@RestController
public class EmbeddingController {

    private final EmbeddingClient embeddingClient;

    @Autowired
    public EmbeddingController(EmbeddingClient embeddingClient) {
        this.embeddingClient = embeddingClient;
    }

    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
}

其它

ttps://spring.io/projects/spring-ai

  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端马农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值