第六章 Spring AI 中文版


推荐访问官网:Getting Started :: https://docs.spring.io/spring-ai/reference/index.html

Spring AI

Spring AI项目旨在简化包含人工智能功能的应用程序的开发,同时避免不必要的复杂性。

该项目从著名的Python项目(如LangChain和LlamaIndex)中汲取灵感,但Spring AI并不是这些项目的直接移植。该项目建立的信念是,下一波生成式人工智能应用程序将不仅适用于Python开发人员,而且将在许多编程语言中无处不在。

Spring AI的核心是提供抽象,作为开发AI应用程序的基础
Spring AI提供以下特性:

  • 支持所有主要的模型提供商,如OpenAI、微软、亚马逊、谷歌和Huggingface。
  • 支持的模型类型是聊天和文本到图像,并在更多的方式。
  • 用于聊天和嵌入模型的可移植API。同时支持同步和流API选项。还支持下拉访问特定于模型的特性。
  • 支持所有主要的矢量数据库提供商,如Azure矢量搜索,Chroma, Milvus, Neo4j,PostgreSQL/PGVector, PineCone, Qdrant, Redis和Weaviate
  • 跨Vector Store提供程序的可移植API,包括一种新的类似sql的元数据过滤器API,它也是可移植的。 函数调用 Spring
  • Spring Boot Auto Configuration and Starters for AI Models and Vector Stores.
  • 数据工程ETL框架

该特性集允许您实现常见的用例,例如“对文档的问答”或“与文档聊天”。
概念部分提供了AI概念及其在Spring AI中的表示的高级概述。
入门部分向您展示了如何创建第一个AI应用程序。接下来的部分将使用以代码为中心的方法深入研究每个组件和常见用例

本节提供了如何开始使用Spring AI的起点。
您应该根据您的需要遵循以下每个部分中的步骤。

1、Spring CLI

Spring CLI 简化了直接从终端创建新应用程序的过程。和熟悉 JavaScript 生态系统的人的“create-react-app”命令一样,Spring CLI 提供了一个用于创建基于 Spring 的项目spring boot new的命令。Spring CLI 还提供了将外部代码库集成到当前项目中的功能,以及许多其他生产力功能。

若要开始创建 Spring AI 应用程序,请按照下列步骤操作:

下载最新的 Spring CLI版本并按照安装说明进行操作。
要创建基于 OpenAI 的简单应用程序,请使用以下命令:
spring boot new --from ai --name myai
请参考生成 README.md 的文件,以获取有关获取 OpenAI API 密钥和运行您的第一个 AI 应用程序的指导。
若要将相同的简单 AI 应用程序添加到现有项目,请执行:

spring boot add ai

Spring CLI 允许用户定义自己的项目目录,这些目录定义您可以创建哪些项目或将其添加到现有代码库中。

2、初始化项目

跳转到 start.spring.io,然后选择要在新应用程序中使用的 AI 模型和矢量存储。

3、添加Milestone和Snapshot依赖

如果您希望手动添加依赖项代码段,请按照以下部分中的说明进行操作。

要使用 Milestone 和 Snapshot 版本,您需要在构建文件中添加对 Spring Milestone 和/或 Snapshot 存储库的引用。

对于 Maven项目,请根据需要添加以下存储库定义:

spring-milestones Spring Milestones https://repo.spring.io/milestone false spring-snapshots Spring Snapshots https://repo.spring.io/snapshot false 对于 Gradle构建的项目,请根据需要添加以下存储库定义:

repositories {
mavenCentral()
maven { url ‘https://repo.spring.io/milestone’ }
maven { url ‘https://repo.spring.io/snapshot’ }
}

4、依赖管理

Spring AI 清单 (BOM) 声明给定版本的 Spring AI 使用的所有依赖项的推荐版本。使用应用程序生成脚本中的 BOM 可以避免您自己指定和维护依赖项版本。相反,您使用的 BOM 版本决定了使用的依赖项版本。它还可确保默认情况下使用受支持和测试的依赖项版本,除非你选择覆盖它们。

如果您是 Maven 用户,则可以通过将以下内容添加到 pom.xml 文件中来使用 BOM

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

Gradle 用户还可以利用 Gradle (5.0+) 原生支持使用 Maven BOM 声明依赖项约束,从而使用 Spring AI BOM。这是通过向 Gradle 构建脚本的依赖项部分添加“platform”依赖项处理程序方法来实现的。如下面的代码片段所示,然后可以为您希望使用的一个或多个spring-ai模块(例如spring-ai-openai)的Starter Dependencies进行无版本声明。

dependencies {
implementation platform(“org.springframework.ai:spring-ai-bom:0.8.0”)
// Replace the following with the starter dependencies of specific modules you wish to use
implementation ‘org.springframework.ai:spring-ai-openai’
}

5、为特定组件添加依赖项

文档中的以下每个部分都显示了需要添加到项目生成系统中的依赖项。

5.1 嵌入模型
Embeddings API
Spring AI OpenAI Embeddings
Spring AI Azure OpenAI Embeddings
Spring AI Ollama Embeddings
Spring AI Transformers (ONNX) Embeddings
Spring AI PostgresML Embeddings
Spring AI Bedrock Cohere Embeddings
Spring AI Bedrock Titan Embeddings
Spring AI VertexAI Embeddings

5.2 聊天对话模型
Chat Completion API
OpenAI Chat Completion
Microsoft Azure Open AI Chat Completion
Ollama Chat Completion
HuggingFace Chat Completion (no streaming support)
Google Vertex Chat Completion (no streaming support)
Amazon Bedrock
Cohere Chat Completion
Llama2 Chat Completion
Titan Chat Completion
Anthropic Chat Completion

5.3 图像生成模型
Image Generation API
OpenAI Image Generation
StabilityAI Image Generation

5.4 向量数据库
Vector Database API
Azure Vector Search - The Azure vector store.
ChromaVectorStore - The Chroma vector store.
MilvusVectorStore - The Milvus vector store.
Neo4jVectorStore - The Neo4j vector store.
PgVectorStore - The PostgreSQL/PGVector vector store.
PineconeVectorStore - PineCone vector store.
RedisVectorStore - The Redis vector store.
WeaviateVectorStore - The Weaviate vector store.
SimpleVectorStore - A simple (in-memory) implementation of persistent vector storage, good for educational purposes.

6、样例工程

你可以从GitHub上clone这些工程开始学习。

6.1 OpenAI
github.com/rd-1-2022/ai-openai-helloworld

6.2 Azure OpenAI
github.com/rd-1-2022/ai-azure-openai-helloworld
github.com/rd-1-2022/ai-azure-stuff-prompt
github.com/rd-1-2022/ai-azure-prompt-template
github.com/rd-1-2022/ai-azure-openai-prompt-roles
github.com/rd-1-2022/ai-azure-retrieval-augmented-generation

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

明爷们儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值