hf_text_embeddings is an Hugging Face Text Embeddings API client for Java 11 or later. It is generated from the OpenAPI spec using the excellent OpenAPI Generator.
It can be used in Android or any Java and Kotlin Project.
To use library in your gradle project follow the steps below:
- Add this in your root
build.gradleat the end of repositories:allprojects { repositories { ... maven { url 'https://jitpack.io' } } } - Add the dependency
dependencies { def HF_TEXT_EMBEDDINGS_VERSION = "..." implementation "llmjava:hf_text_embeddings:$HF_TEXT_EMBEDDINGS_VERSION" }
To use the library in your Maven project, follow the steps below:
- Add the JitPack repository to your build file:
<repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories>
- Add the dependency
<dependency> <groupId>llmjava</groupId> <artifactId>hf_text_embeddings</artifactId> <version>${HF_TEXT_EMBEDDINGS_VERSION}</version> </dependency>
This section provide some examples for interacting with HuggingFace Text Embeddings API in java, see also huggingface-examples.
First, run a Text Embeddings Inference endpoint, e.g. locally it will be available at 127.0.0.1:8080.
See documentation to run an endpoint https://huggingface.co/docs/text-embeddings-inference
Set HF_API_URL with the endpoint url of your choice:
String HF_API_URL = "https://api-inference.huggingface.co";Next, get an API token. If using HuggingFace public endpoint then you can get one at https://huggingface.co/settings/tokens
Now, we create a client to interact with the endpoint and be able to send requests
// create API client
String HF_TOKEN = System.getenv("HF_API_KEY");
ApiClient client = new ApiClient()
.setRequestInterceptor(new Consumer<HttpRequest.Builder>() {
@Override public void accept(HttpRequest.Builder builder) {
builder.header("Authorization", "Bearer " + HF_TOKEN);
}
});
client.updateBaseUri(HF_API_URL);
TextEmbeddingsInferenceApi api = new TextEmbeddingsInferenceApi(client);The rest of this section provide examples for submit different types of requests
Submit Text Embeddings request
// Embed request
EmbedRequest embedRequest = new EmbedRequest().addInputsItem("Hi");
List<List<Float>> response = api.embed(embedRequest);
System.out.println("Generated embeddings" + response);Submit model info request
Info modelInfoResponse = api.getModelInfo();
System.out.println("Model info:\n" + modelInfoResponse);Submit health request
api.health();Submit metrics request
String metricsResponse = api.metrics();
System.out.println("Metrics response:\n" + metricsResponse);Clone the repository and import as Maven project in IntelliJ IDEA or Eclipse
Before building the project, make sure you have the following things installed.
- Maven
- Java 11
To install the library to your local Maven repository, simply execute:
mvn installTo build the library using Gradle, execute the following command
./gradlew buildRefer to the official documentation for more information.