Skip to main content

Hugging Face

In order to use Hugging Face Inference Providers, you need to import the langchain4j-open-ai module, as Hugging Face Inference Providers API is OpenAI-compatible.

Maven Dependency

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.8.0</version>
</dependency>

Examples

ChatModel model = OpenAiChatModel.builder()
.apiKey(System.getenv("HF_API_KEY"))
.baseUrl("https://router.huggingface.co/v1")
.modelName("HuggingFaceTB/SmolLM3-3B:hf-inference")
.build();

StreamingChatModel streamingModel = OpenAiStreamingChatModel.builder()
.apiKey(System.getenv("HF_API_KEY"))
.baseUrl("https://router.huggingface.co/v1")
.modelName("HuggingFaceTB/SmolLM3-3B:hf-inference")
.build();