此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3spring-doc.cadn.net.cn

Hugging Face 聊天

Hugging Face 文本生成推理(TGI)是一种专门用于在云端部署大型语言模型(LLMs)的解决方案,使这些模型能够通过 API 访问。TGI 通过连续批处理、标记流式传输和高效的内存管理等功能,为文本生成任务提供优化的性能。spring-doc.cadn.net.cn

文本生成推理需要模型与其架构特定的优化兼容。虽然许多流行的大型语言模型都受支持,但并非 Hugging Face Hub 上的所有模型都能使用 TGI 部署。如果您需要部署其他类型的模型,请考虑改用标准的 Hugging Face 推理端点。
有关支持的模型和架构的完整且最新的列表,请参阅 文本生成推理支持模型文档

前提条件

您需要在 Hugging Face 上创建一个推理端点,并生成一个 API Tokens以访问该端点。 更多详细信息请参见 此处spring-doc.cadn.net.cn

Spring AI 项目定义了两个配置属性:spring-doc.cadn.net.cn

  1. spring.ai.huggingface.chat.api-key: 将其设置为从 Hugging Face 获取的 API Tokens值。spring-doc.cadn.net.cn

  2. spring.ai.huggingface.chat.url: 将其设置为在 Hugging Face 中预置模型时获取的推理端点 URL。spring-doc.cadn.net.cn

您可以在推理端点的UI上找到您的推理端点URL 此处spring-doc.cadn.net.cn

您可以在application.properties文件中设置这些配置属性:spring-doc.cadn.net.cn

spring.ai.huggingface.chat.api-key=<your-huggingface-api-key>
spring.ai.huggingface.chat.url=<your-inference-endpoint-url>

为了在处理 API 密钥等敏感信息时增强安全性,可以使用 Spring 表达式语言(SpEL)来引用自定义环境变量:spring-doc.cadn.net.cn

# In application.yml
spring:
  ai:
    huggingface:
      chat:
        api-key: ${HUGGINGFACE_API_KEY}
        url: ${HUGGINGFACE_ENDPOINT_URL}
# In your environment or .env file
export HUGGINGFACE_API_KEY=<your-huggingface-api-key>
export HUGGINGFACE_ENDPOINT_URL=<your-inference-endpoint-url>

您也可以在应用程序代码中以编程方式设置这些配置:spring-doc.cadn.net.cn

// Retrieve API key and endpoint URL from secure sources or environment variables
String apiKey = System.getenv("HUGGINGFACE_API_KEY");
String endpointUrl = System.getenv("HUGGINGFACE_ENDPOINT_URL");

添加仓库和BOM

Spring AI 工件发布在 Maven Central 和 Spring Snapshot 仓库中。 请参阅 工件仓库 部分,以将这些仓库添加到您的构建系统。spring-doc.cadn.net.cn

为了帮助管理依赖,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建系统。spring-doc.cadn.net.cn

自动配置

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。spring-doc.cadn.net.cn

Spring AI 为 Hugging Face Chat Client 提供了 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-huggingface</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-huggingface'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

聊天属性

启用和禁用聊天自动配置现在通过顶级属性使用前缀 spring.ai.model.chat 配置完成。spring-doc.cadn.net.cn

要启用,spring.ai.model.chat=huggingface(默认已启用)spring-doc.cadn.net.cn

要禁用,spring.ai.model.chat=none(或任何与 huggingface 不匹配的值)spring-doc.cadn.net.cn

这种修改是为了允许配置多个模型。spring-doc.cadn.net.cn

前缀 spring.ai.huggingface 是属性前缀,用于配置 Hugging Face 的聊天模型实现。spring-doc.cadn.net.cn

属性spring-doc.cadn.net.cn

描述spring-doc.cadn.net.cn

默认spring-doc.cadn.net.cn

spring.ai.huggingface.chat.api-keyspring-doc.cadn.net.cn

用于向推理端点进行身份验证的 API 密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.huggingface.chat.urlspring-doc.cadn.net.cn

连接到的推理端点的 URLspring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.huggingface.chat.enabled(已移除且不再有效)spring-doc.cadn.net.cn

启用 Hugging Face 聊天模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.model.chatspring-doc.cadn.net.cn

启用 Hugging Face 聊天模型。spring-doc.cadn.net.cn

Hugging Facespring-doc.cadn.net.cn

示例控制器(自动配置)

创建一个新的Spring Boot项目,并将spring-boot-starter-web添加到您的pom(或gradle)依赖中。spring-doc.cadn.net.cn

添加一个application.properties文件,位于src/main/resources目录下,以启用并配置Hugging Face聊天模型:spring-doc.cadn.net.cn

spring.ai.huggingface.chat.api-key=YOUR_API_KEY
spring.ai.huggingface.chat.url=YOUR_INFERENCE_ENDPOINT_URL
api-keyurl 替换为您的 Hugging Face 值。

这将创建一个HuggingfaceChatModel实现,您可以将其注入到您的类中。 以下是一个使用聊天模型进行文本生成的简单@Controller类的例子。spring-doc.cadn.net.cn

@RestController
public class ChatController {

    private final HuggingfaceChatModel chatModel;

    @Autowired
    public ChatController(HuggingfaceChatModel 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));
    }
}

手动配置

The HuggingfaceChatModel implements the ChatModel interface and uses the [low-level-api] to connect to the Hugging Face inference endpoints.spring-doc.cadn.net.cn

spring-ai-huggingface 依赖添加到您项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-huggingface</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-huggingface'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

接下来,创建一个HuggingfaceChatModel并用它来生成文本:spring-doc.cadn.net.cn

HuggingfaceChatModel chatModel = new HuggingfaceChatModel(apiKey, url);

ChatResponse response = this.chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

System.out.println(response.getResult().getOutput().getText());