LangChain4j integration library for Hugging Face inference capabilities including chat, language, and embedding models
Complete API reference for HuggingFaceChatModel (deprecated).
This class is deprecated since version 1.7.0-beta13 and scheduled for removal.
Migration: Use OpenAiChatModel from langchain4j-open-ai module with Hugging Face's OpenAI-compatible endpoint.
Example Migration:
// Old (deprecated)
HuggingFaceChatModel model = HuggingFaceChatModel.builder()
.accessToken(apiKey)
.modelId("tiiuae/falcon-7b-instruct")
.build();
// New (recommended)
import dev.langchain4j.model.openai.OpenAiChatModel;
OpenAiChatModel model = OpenAiChatModel.builder()
.apiKey(apiKey)
.baseUrl("https://router.huggingface.co/v1")
.modelName("tiiuae/falcon-7b-instruct:hf-inference")
.build();See Migration Guide for complete details.
Provides chat-style interactions with Hugging Face language models through the LangChain4j ChatModel interface.
Package: dev.langchain4j.model.huggingface
Status: ⚠️ Deprecated (scheduled for removal)
Interfaces: ChatModel
package dev.langchain4j.model.huggingface;
@Deprecated(forRemoval = true, since = "1.7.0-beta13")
public class HuggingFaceChatModel implements dev.langchain4j.model.chat.ChatModel {
// Construction
public static Builder builder();
public static HuggingFaceChatModel withAccessToken(String accessToken);
// Chat operations
public ChatResponse chat(ChatRequest chatRequest);
public String chat(String userMessage);
public ChatResponse chat(ChatMessage... messages);
public ChatResponse chat(java.util.List<ChatMessage> messages);
}public static final class Builder {
public Builder baseUrl(String baseUrl);
public Builder accessToken(String accessToken);
public Builder modelId(String modelId);
public Builder timeout(java.time.Duration timeout);
public Builder temperature(Double temperature);
public Builder maxNewTokens(Integer maxNewTokens);
public Builder returnFullText(Boolean returnFullText);
public Builder waitForModel(Boolean waitForModel);
public HuggingFaceChatModel build();
}public static Builder builder()Example:
HuggingFaceChatModel model = HuggingFaceChatModel.builder()
.accessToken(System.getenv("HF_API_KEY"))
.modelId("tiiuae/falcon-7b-instruct")
.temperature(0.7)
.maxNewTokens(200)
.build();public static HuggingFaceChatModel withAccessToken(String accessToken)Quick construction with defaults.
public ChatResponse chat(ChatRequest chatRequest)Parameters:
chatRequest - Request containing messages and parametersReturns: ChatResponse with AI message and metadata
Throws: RuntimeException on API errors
public String chat(String userMessage)Convenience method for single-turn chat.
Parameters:
userMessage - User's message textReturns: AI response text
public ChatResponse chat(ChatMessage... messages)Chat with varargs messages.
public ChatResponse chat(java.util.List<ChatMessage> messages)Chat with message list.
See Configuration Guide for all options.
Key Parameters:
accessToken (required) - API keymodelId (recommended) - Model identifiertemperature (optional) - Sampling temperature (0.0-2.0)maxNewTokens (optional) - Max tokens to generatereturnFullText (optional) - Include prompt (default: false)waitForModel (optional) - Wait if loading (default: true)timeout (optional) - Request timeout (default: 15s)baseUrl (optional) - Custom endpointpublic class SystemMessage extends ChatMessage {
public String text();
public static SystemMessage from(String text);
}public class UserMessage extends ChatMessage {
public String singleText();
public static UserMessage from(String text);
}public class AiMessage extends ChatMessage {
public String text();
public static AiMessage from(String text);
}HuggingFaceChatModel model = HuggingFaceChatModel.builder()
.accessToken(System.getenv("HF_API_KEY"))
.modelId("tiiuae/falcon-7b-instruct")
.build();
String response = model.chat("What is Java?");import dev.langchain4j.data.message.*;
ChatResponse response = model.chat(
SystemMessage.from("You are a helpful assistant"),
UserMessage.from("What is Java?")
);
String aiMessage = response.aiMessage().text();List<ChatMessage> history = new ArrayList<>();
history.add(SystemMessage.from("You are helpful"));
history.add(UserMessage.from("What is ML?"));
ChatResponse response1 = model.chat(history);
history.add(AiMessage.from(response1.aiMessage().text()));
history.add(UserMessage.from("Give an example"));
ChatResponse response2 = model.chat(history);try {
ChatResponse response = model.chat(request);
} catch (RuntimeException e) {
// Error format: "status code: <code>; body: <body>"
System.err.println("Chat failed: " + e.getMessage());
}Common Errors:
Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-hugging-face