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
代码后期添加