Hugging Face 聊天
Hugging Face 文本生成推理(Text Generation Inference,TGI)是一种专门用于在云端部署大型语言模型(LLMs)的解决方案,使其能够通过 API 进行访问。TGI 通过持续批处理(continuous batching)、令牌流式处理(token streaming)和高效内存管理等特性,为文本生成任务提供了优化的性能。
Text Generation Inference 要求模型与其架构特定的优化兼容。虽然许多流行的 LLM 得到了支持,但并非 Hugging Face Hub 上的所有模型都可以使用 TGI 进行部署。如果您需要部署其他类型的模型,请考虑使用标准的 Hugging Face Inference Endpoints。
有关支持的模型和架构的完整和最新列表,请参阅 Text Generation Inference 支持的模型文档。
先决条件
你需要在 Hugging Face 上创建一个 Inference Endpoint 并生成一个 API token 以访问该端点。更多详细信息可以参考这里。Spring AI 项目定义了一个名为 spring.ai.huggingface.chat.api-key
的配置属性,你应该将其设置为从 Hugging Face 获取的 API token 的值。此外,还有一个名为 spring.ai.huggingface.chat.url
的配置属性,你应该将其设置为在 Hugging Face 中配置模型时获得的推理端点 URL。你可以在 Inference Endpoint 的 UI 上找到这个 URL,具体位置在这里。导出环境变量是设置这些配置属性的一种方式:
export SPRING_AI_HUGGINGFACE_CHAT_API_KEY=<INSERT KEY HERE>
export SPRING_AI_HUGGINGFACE_CHAT_URL=<INSERT INFERENCE ENDPOINT URL HERE>
添加 Repositories 和 BOM
Spring AI 的构件发布在 Maven Central 和 Spring Snapshot 仓库中。请参考 Repositories 部分,将这些仓库添加到你的构建系统中。
为了帮助进行依赖管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用的 Spring AI 版本一致。请参考 依赖管理 部分,将 Spring AI BOM 添加到你的构建系统中。
自动配置
Spring AI 为 Hugging Face Chat Client 提供了 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml
文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-huggingface-spring-boot-starter</artifactId>
</dependency>
或到你的 Gradle build.gradle
构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-huggingface-spring-boot-starter'
}
请参考 依赖管理 部分,将 Spring AI BOM 添加到您的构建文件中。
聊天属性
前缀 spring.ai.huggingface
是一个属性前缀,允许你配置 Hugging Face 的聊天模型实现。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.huggingface.chat.api-key | 用于与 Inference Endpoint 进行身份验证的 API Key。 | - |
spring.ai.huggingface.chat.url | 要连接的 Inference Endpoint 的 URL | - |
spring.ai.huggingface.chat.enabled | 启用 Hugging Face 聊天模型。 | true |
示例控制器(自动配置)
创建一个新的 Spring Boot 项目,并将 spring-ai-huggingface-spring-boot-starter
添加到你的 pom(或 gradle)依赖中。
在 src/main/resources
目录下添加一个 application.properties
文件,以启用并配置 Hugging Face 聊天模型:
spring.ai.huggingface.chat.api-key=YOUR_API_KEY
spring.ai.huggingface.chat.url=YOUR_INFERENCE_ENDPOINT_URL
:::提示
将 api-key
和 url
替换为你的 Hugging Face 值。
:::
这将创建一个 HuggingfaceChatModel
实现,你可以将其注入到你的类中。以下是一个简单的 @Controller
类的示例,该类使用聊天模型进行文本生成。
@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));
}
}
手动配置
HuggingfaceChatModel 实现了 ChatModel
接口,并使用 [low-level-api] 连接到 Hugging Face 推理端点。
将 spring-ai-huggingface
依赖添加到您的项目的 Maven pom.xml
文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-huggingface</artifactId>
</dependency>
或到你的 Gradle build.gradle
构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-huggingface'
}
请参考依赖管理部分,将 Spring AI BOM 添加到您的构建文件中。
接下来,创建一个 HuggingfaceChatModel
并使用它进行文本生成:
HuggingfaceChatModel chatModel = new HuggingfaceChatModel(apiKey, url);
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
System.out.println(response.getGeneration().getResult().getOutput().getContent());