LangChain4j integration library for Hugging Face inference capabilities including chat, language, and embedding models
—
—
Does it follow best practices?
Impact
—
No eval scenarios have been run
—
The risk profile of this skill
Integration library for using Hugging Face models with LangChain4j framework. Provides Java implementations for chat, language, and embedding models with builder-pattern configuration.
Package: dev.langchain4j:langchain4j-hugging-face:1.11.0
Core Classes:
HuggingFaceChatModel - Chat-style interactions (deprecated)HuggingFaceLanguageModel - Text generation (deprecated)HuggingFaceEmbeddingModel - Vector embeddingsHuggingFaceClient - Low-level API clientHuggingFaceModelName - Model constants (deprecated)Critical Notice: HuggingFaceChatModel and HuggingFaceLanguageModel are deprecated since 1.7.0-beta13. Use OpenAiChatModel from langchain4j-open-ai module instead. HuggingFaceEmbeddingModel remains supported.
Maven:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-hugging-face</artifactId>
<version>1.11.0</version>
</dependency>Gradle:
implementation 'dev.langchain4j:langchain4j-hugging-face:1.11.0'import dev.langchain4j.model.huggingface.HuggingFaceEmbeddingModel;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.output.Response;
import java.util.List;
// Create model
HuggingFaceEmbeddingModel model = HuggingFaceEmbeddingModel.builder()
.accessToken(System.getenv("HF_API_KEY"))
.modelId("sentence-transformers/all-MiniLM-L6-v2")
.build();
// Single embedding
Response<Embedding> response = model.embed("Hello world");
float[] vector = response.content().vector();
// Batch embeddings
List<TextSegment> segments = List.of(
TextSegment.from("Text 1"),
TextSegment.from("Text 2")
);
Response<List<Embedding>> batchResponse = model.embedAll(segments);import dev.langchain4j.model.huggingface.HuggingFaceChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
HuggingFaceChatModel model = HuggingFaceChatModel.builder()
.accessToken(System.getenv("HF_API_KEY"))
.modelId("tiiuae/falcon-7b-instruct")
.temperature(0.7)
.build();
String response = model.chat("What is Java?");import dev.langchain4j.model.huggingface.HuggingFaceLanguageModel;
import dev.langchain4j.model.output.Response;
HuggingFaceLanguageModel model = HuggingFaceLanguageModel.builder()
.accessToken(System.getenv("HF_API_KEY"))
.modelId("microsoft/Phi-3-mini-4k-instruct")
.temperature(0.8)
.build();
Response<String> response = model.generate("Write a haiku");Choose HuggingFaceEmbeddingModel when:
Avoid HuggingFaceChatModel/LanguageModel (deprecated):
All models support builder pattern with these common options:
| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
| accessToken | String | - | Yes | Hugging Face API key |
| modelId | String | - | Recommended | Model identifier |
| baseUrl | String | https://router.huggingface.co/hf-inference/ | No | Custom API endpoint |
| timeout | Duration | 15 seconds | No | Request timeout |
| waitForModel | Boolean | true | No | Wait if model loading |
Get API Token: https://huggingface.co/settings/tokens
See Configuration Guide for all options.
Embedding Models:
sentence-transformers/all-MiniLM-L6-v2 - Fast, 384-dimsentence-transformers/all-mpnet-base-v2 - High quality, 768-dimBAAI/bge-small-en-v1.5 - Optimized for retrievalLanguage/Chat Models (for OpenAI module):
tiiuae/falcon-7b-instruct - General purposemicrosoft/Phi-3-mini-4k-instruct - Efficient, 4K contextmistralai/Mistral-7B-Instruct-v0.2 - High quality┌─────────────────────────────────────────┐
│ Model Classes │
│ (ChatModel, LanguageModel, Embedding) │
└──────────────┬──────────────────────────┘
│
├─ Builder Pattern Configuration
│
┌──────────────▼──────────────────────────┐
│ HuggingFaceClient Interface │
└──────────────┬──────────────────────────┘
│
┌──────────────▼──────────────────────────┐
│ DefaultHuggingFaceClient (Impl) │
│ (Retrofit2 + OkHttp3) │
└──────────────┬──────────────────────────┘
│
┌──────────────▼──────────────────────────┐
│ Hugging Face API │
│ https://router.huggingface.co/ │
└─────────────────────────────────────────┘Extension Points:
// Embedding (recommended)
import dev.langchain4j.model.huggingface.HuggingFaceEmbeddingModel;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
// Chat (deprecated)
import dev.langchain4j.model.huggingface.HuggingFaceChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.data.message.UserMessage;
// Language (deprecated)
import dev.langchain4j.model.huggingface.HuggingFaceLanguageModel;
import dev.langchain4j.model.output.Response;
// Low-level client
import dev.langchain4j.model.huggingface.client.HuggingFaceClient;
import dev.langchain4j.model.huggingface.client.EmbeddingRequest;
import dev.langchain4j.model.huggingface.client.TextGenerationRequest;All model methods throw RuntimeException for API errors:
try {
Response<Embedding> response = model.embed("text");
} catch (RuntimeException e) {
// Error format: "status code: <code>; body: <body>"
System.err.println("API error: " + e.getMessage());
}Common Error Codes:
See Error Handling Guide for details.
If using HuggingFaceChatModel or HuggingFaceLanguageModel, migrate to OpenAiChatModel:
Old (Deprecated):
HuggingFaceChatModel model = HuggingFaceChatModel.builder()
.accessToken(System.getenv("HF_API_KEY"))
.modelId("tiiuae/falcon-7b-instruct")
.build();New (Recommended):
import dev.langchain4j.model.openai.OpenAiChatModel;
OpenAiChatModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("HF_API_KEY"))
.baseUrl("https://router.huggingface.co/v1")
.modelName("tiiuae/falcon-7b-instruct:hf-inference")
.build();See Migration Guide for complete details.
This library requires:
langchain4j-core:1.11.0 - Core interfacesRetrofit2 - HTTP clientOkHttp3 - HTTP transportJackson - JSON serializationembedAll() for multiple texts (more efficient)waitForModel(true) to avoid 503 errorsFor issues or questions: