Spring AI 第二讲 之 Chat Model API 第五节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 AI BOM 添加到构建文件中。

请注意,该聊天实现还没有 Spring Boot Starter。

获取推理的端点 URL

调用模型

HuggingfaceChatModel chatModel = new HuggingfaceChatModel(apiKey, basePath);
Prompt prompt = new Prompt("Your text here...");
ChatResponse response = chatModel.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 = huggingfaceChatModel.call(prompt);
System.out.println(response.getGeneration().getText());

将产生以下输出

{
    "name": "John",
    "lastname": "Smith",
    "address": "#1 Samuel St."
}

下节:Spring AI 第二讲 之 Chat Model API 第五节Google VertexAI API

代码后期添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值