Hugging Face 聊天
Hugging Face Text Generation Inference (TGI) 是一種專門用於在雲端服務大型語言模型 (LLM) 的部署解決方案,透過 API 使其可訪問。TGI 透過連續批處理、令牌流和高效記憶體管理等功能,為文字生成任務提供了最佳化的效能。
| Text Generation Inference 要求模型與其架構特定的最佳化相容。雖然許多流行的 LLM 都受支援,但並非 Hugging Face Hub 上的所有模型都可以使用 TGI 進行部署。如果您需要部署其他型別的模型,請考慮使用標準的 Hugging Face Inference Endpoints。 |
| 有關支援模型和架構的完整和最新列表,請參閱 Text Generation Inference 支援模型文件。 |
先決條件
您需要在 Hugging Face 上建立一個 Inference Endpoint,並建立一個 API 令牌以訪問該端點。更多詳細資訊請參閱 此處。
Spring AI 專案定義了兩個配置屬性
-
spring.ai.huggingface.chat.api-key: 將此設定為從 Hugging Face 獲取的 API 令牌的值。 -
spring.ai.huggingface.chat.url: 將此設定為在 Hugging Face 中配置模型時獲得的推理端點 URL。
您可以在推理端點 UI 此處 找到您的推理端點 URL。
您可以在 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");
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
Spring AI 為 Hugging Face 聊天客戶端提供了 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 的聊天模型實現。
財產 |
描述 |
預設值 |
spring.ai.huggingface.chat.api-key |
用於與推理端點進行身份驗證的 API 金鑰。 |
- |
spring.ai.huggingface.chat.url |
要連線的推理端點的 URL |
- |
spring.ai.huggingface.chat.enabled(已移除且不再有效) |
啟用 Hugging Face 聊天模型。 |
true |
spring.ai.model.chat |
啟用 Hugging Face 聊天模型。 |
huggingface |
示例控制器(自動配置)
建立一個新的 Spring Boot 專案,並將 spring-ai-starter-model-huggingface 新增到您的 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 介面,並使用 [低階 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());