HuggingFace Chat

HuggingFace Inference Endpoints 允许您在云中部署和提供机器学习模型,并通过 API 进行访问。

入门

有关 HuggingFace Inference Endpoints 的更多详细信息,请参见此处

先决条件

添加 spring-ai-huggingface 依赖:

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

您需要获取你的 HuggingFace API 密钥并将其设置为环境变量

export HUGGINGFACE_API_KEY=your_api_key_here

请注意,目前还没有用于此客户端实现的 Spring Boot Starter。

获取 Inference Endpoints URL。您可以在 Inference Endpoint’s 界面上找到它。

调用模型

HuggingfaceChatClient client = new HuggingfaceChatClient(apiKey, basePath);
Prompt prompt = new Prompt("Your text here...");
ChatResponse response = client.call(prompt);
System.out.println(response.getGeneration().getText());

示例

使用此处找到的示例 :

String mistral7bInstruct = """
        [INST] You are a helpful code assistant. Your task is to generate a valid JSON object based on the given information:
        name: John
        lastname: Smith
        address: #1 Samuel St.
        Just generate the JSON object without explanations:
        [/INST]""";
Prompt prompt = new Prompt(mistral7bInstruct);
ChatResponse aiResponse = huggingfaceChatClient.call(prompt);
System.out.println(response.getGeneration().getText());

将产生输出

{
    "name": "John",
    "lastname": "Smith",
    "address": "#1 Samuel St."
}
作者:Jeebiz  创建时间:2024-04-05 23:41
最后编辑:Jeebiz  更新时间:2024-07-06 19:00