|
此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3! |
Hugging Face Chat
Hugging Face Text Generation Inference (TGI) 是一种专门的部署解决方案,用于在云中提供大型语言模型(LLMs),并通过 API 使它们变得可用。TGI 通过连续批处理、标记流式传输和高效内存管理等功能提供了文本生成任务的优化性能。
| 文本生成推理要求模型与其实置优化架构兼容。虽然许多流行的LLM(大型语言模型)被支持,但并非所有在Hugging Face Hub上的模型都可以使用TGI部署。如果需要部署其他类型的模型,请考虑使用标准的Hugging Face 推理端点。 |
| 对于受支持的模型和架构的完整且最新的列表,请参见文本生成推理支持的模型文档。 |
前置条件
您需要在Hugging Face上创建一个推理端点,并生成一个APITokens以访问该端点。 更多详细信息请参阅这里。
Spring AI项目定义了两个配置属性:
-
spring.ai.huggingface.chat.api-key: 设置此值为从Hugging Face 获取的APITokens。 -
spring.ai.huggingface.chat.url: 将此设置为在 Hugging Face 预置模型时获得的推理端点 URL。
您可以在此处找到推理端点的URL:在推理端点的UI上。
您可以在您的application.properties文件中设置这些配置属性:
spring.ai.huggingface.chat.api-key=<your-huggingface-api-key>
spring.ai.huggingface.chat.url=<your-inference-endpoint-url>
当处理如API密钥等敏感信息时,为了增强安全性,您可以使用Spring表达式语言(SpEL)引用自定义环境变量:
# 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>
您也可以在应用程序代码中程序化设置这些配置:
// 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");
Auto-configuration
|
Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。 |
Spring AI 提供了对 Hugging Face Chat 客户端的 Spring Boot 自动配置。
要启用它,请在项目中添加以下依赖项到 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-huggingface</artifactId>
</dependency>
请将以下内容添加到您的Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-huggingface'
}
| 请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。 |
聊天属性
|
现在通过带有前缀 要启用,请设置:spring.ai.model.chat=huggingface(默认已启用) 要禁用,请设置 spring.ai.model.chat=none (或任何不匹配 huggingface 的值) 此更改是为了允许配置多个模型。 |
spring.ai.huggingface 前缀是属性前缀,可以让您配置与 Hugging Face 的聊天模型实现相关的设置。
<property> </property> |
<description> </description> |
默认 |
spring.ai.huggingface.chat.api-key |
使用 API Key 对 inference endpoint 进行身份验证。 |
- |
spring.ai.huggingface.chat.url |
连接到的推理端点的URL |
- |
spring.ai.huggingface.chat.enabled (已移除且不再有效) |
启用 Hugging Face 聊天模型。 |
true |
spring.ai.model.chat |
启用 Hugging Face 聊天模型。 |
HuggingFace |
样例控制器(自动配置)
创建一个新的Spring Boot项目,并在pom(或gradle)依赖中添加spring-ai-starter-model-huggingface。
在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));
}
}
手动配置
The HuggingfaceChatModel 实现了 ChatModel 接口,并使用[低级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.getResult().getOutput().getText());