diff --git a/README.md b/README.md index fe4aa92ac..38cd1dd85 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Spring AI supports many AI models. For an overview see here. Specific models c * OpenAI * Azure OpenAI * Amazon Bedrock (Anthropic, Llama, Cohere, Titan, Jurassic2) -* HuggingFace +* Hugging Face * Google VertexAI (PaLM2, Gemini) * Mistral AI * Stability AI diff --git a/models/spring-ai-huggingface/README.md b/models/spring-ai-huggingface/README.md index 0b1ab9659..3aefbc631 100644 --- a/models/spring-ai-huggingface/README.md +++ b/models/spring-ai-huggingface/README.md @@ -1,2 +1,2 @@ -[Huggingface Chat Documentation](https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/api/chat/huggingface.html) +[Hugging Face Chat Documentation](https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/api/chat/huggingface.html) diff --git a/pom.xml b/pom.xml index 9f539b0e2..a42f747f5 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ spring-ai-spring-boot-starters/spring-ai-starter-anthropic spring-ai-spring-boot-starters/spring-ai-starter-azure-openai spring-ai-spring-boot-starters/spring-ai-starter-bedrock-ai + spring-ai-spring-boot-starters/spring-ai-starter-huggingface spring-ai-spring-boot-starters/spring-ai-starter-minimax spring-ai-spring-boot-starters/spring-ai-starter-mistral-ai spring-ai-spring-boot-starters/spring-ai-starter-ollama diff --git a/spring-ai-bom/pom.xml b/spring-ai-bom/pom.xml index 0b567d404..60f2f11d0 100644 --- a/spring-ai-bom/pom.xml +++ b/spring-ai-bom/pom.xml @@ -260,6 +260,12 @@ ${project.version} + + org.springframework.ai + spring-ai-huggingface-spring-boot-starter + ${project.version} + + org.springframework.ai spring-ai-milvus-store-spring-boot-starter diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc index 120a8e5c8..b2bf14738 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc @@ -16,7 +16,7 @@ **** xref:api/chat/bedrock/bedrock-cohere.adoc[Cohere] **** xref:api/chat/bedrock/bedrock-titan.adoc[Titan] **** xref:api/chat/bedrock/bedrock-jurassic2.adoc[Jurassic2] -*** xref:api/chat/huggingface.adoc[HuggingFace] +*** xref:api/chat/huggingface.adoc[Hugging Face] *** xref:api/chat/google-vertexai.adoc[Google VertexAI] **** xref:api/chat/vertexai-palm2-chat.adoc[VertexAI PaLM2 ] **** xref:api/chat/vertexai-gemini-chat.adoc[VertexAI Gemini] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/huggingface.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/huggingface.adoc index a356d54f2..7b434a6c1 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/huggingface.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/huggingface.adoc @@ -1,72 +1,134 @@ -= HuggingFace Chat += Hugging Face Chat -HuggingFace Inference Endpoints allow you to deploy and serve machine learning models in the cloud, making them accessible via an API. - -== Getting Started - -Further details on HuggingFace Inference Endpoints can be found link:https://huggingface.co/docs/inference-endpoints/index[here]. +Hugging Face Inference Endpoints allow you to deploy and serve machine learning models in the cloud, making them accessible via an API. == Prerequisites -Add the `spring-ai-huggingface` dependency: - -[source,xml] ----- - - org.springframework.ai - spring-ai-huggingface - ----- - -You should get your HuggingFace API key and set it as an environment variable +You will need to create an Inference Endpoint on Hugging Face and create an API token to access the endpoint. +Further details can be found link:https://huggingface.co/docs/inference-endpoints/index[here]. +The Spring AI project defines a configuration property named `spring.ai.huggingface.chat.api-key` that you should set to the value of the API token obtained from Hugging Face. +There is also a configuration property named `spring.ai.huggingface.chat.url` that you should set to the inference endpoint URL obtained when provisioning your model in Hugging Face. +You can find this on the Inference Endpoint's UI link:https://ui.endpoints.huggingface.co/[here]. +Exporting environment variables is one way to set these configuration properties: [source,shell] ---- -export HUGGINGFACE_API_KEY=your_api_key_here +export SPRING_AI_HUGGINGFACE_CHAT_API_KEY= +export SPRING_AI_HUGGINGFACE_CHAT_URL= +---- + +=== Add Repositories and BOM + +Spring AI artifacts are published in Spring Milestone and Snapshot repositories. +Refer to the xref:getting-started.adoc#repositories[Repositories] section to add these repositories to your build system. + +To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system. + +== Auto-configuration + +Spring AI provides Spring Boot auto-configuration for the Hugging Face Chat Client. +To enable it add the following dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-huggingface-spring-boot-starter + +---- + +or to your Gradle `build.gradle` build file. + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-huggingface-spring-boot-starter' +} ---- TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. -Note, there is not yet a Spring Boot Starter for this chat implementation. +=== Chat Properties -Obtain the endpoint URL of the Inference Endpoint. -You can find this on the Inference Endpoint's UI link:https://ui.endpoints.huggingface.co/[here]. +The prefix `spring.ai.huggingface` is the property prefix that lets you configure the chat model implementation for Hugging Face. -== Making a call to the model +[cols="3,5,1"] +|==== +| Property | Description | Default +| spring.ai.huggingface.chat.api-key | API Key to authenticate with the Inference Endpoint. | - +| spring.ai.huggingface.chat.url | URL of the Inference Endpoint to connect to | - +| spring.ai.huggingface.chat.enabled | Enable Hugging Face chat model. | true +|==== + +== Sample Controller (Auto-configuration) + +https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-huggingface-spring-boot-starter` to your pom (or gradle) dependencies. + +Add an `application.properties` file, under the `src/main/resources` directory, to enable and configure the Hugging Face chat model: + +[source,application.properties] +---- +spring.ai.huggingface.chat.api-key=YOUR_API_KEY +spring.ai.huggingface.chat.url=YOUR_INFERENCE_ENDPOINT_URL +---- + +TIP: replace the `api-key` and `url` with your Hugging Face values. + +This will create a `HuggingfaceChatModel` implementation that you can inject into your class. +Here is an example of a simple `@Controller` class that uses the chat model for text generations. [source,java] ---- -HuggingfaceChatModel chatModel = new HuggingfaceChatModel(apiKey, basePath); -Prompt prompt = new Prompt("Your text here..."); -ChatResponse response = chatModel.call(prompt); -System.out.println(response.getGeneration().getText()); ----- +@RestController +public class ChatController { -== Example + private final HuggingfaceChatModel chatModel; -Using the example found link:https://www.promptingguide.ai/models/mistral-7b[here] + @Autowired + public ChatController(HuggingfaceChatModel chatModel) { + this.chatModel = chatModel; + } -[source,java] ----- -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()); ----- -Will produce the output - -[source,json] ----- -{ - "name": "John", - "lastname": "Smith", - "address": "#1 Samuel St." + @GetMapping("/ai/generate") + public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { + return Map.of("generation", chatModel.call(message)); + } } ---- +== Manual Configuration + +The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-huggingface/src/main/java/org/springframework/ai/huggingface/HuggingfaceChatModel.java[HuggingfaceChatModel] implements the `ChatModel` interface and uses the <> to connect to the Hugging Face inference endpoints. + +Add the `spring-ai-huggingface` dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-huggingface + +---- + +or to your Gradle `build.gradle` build file. + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-huggingface' +} +---- + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +Next, create a `HuggingfaceChatModel` and use it for text generations: + +[source,java] +---- +HuggingfaceChatModel chatModel = new HuggingfaceChatModel(apiKey, url); + +ChatResponse response = chatModel.call( + new Prompt("Generate the names of 5 famous pirates.")); + +System.out.println(response.getGeneration().getResult().getOutput().getContent()); +---- diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatmodel.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatmodel.adoc index 9fdce3b42..560c3cbdb 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatmodel.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chatmodel.adoc @@ -193,7 +193,7 @@ image::spring-ai-chat-completions-clients.jpg[align="center", width="800px"] * xref:api/chat/openai-chat.adoc[OpenAI Chat Completion] (streaming & function-calling support) * xref:api/chat/azure-openai-chat.adoc[Microsoft Azure Open AI Chat Completion] (streaming & function-calling support) * xref:api/chat/ollama-chat.adoc[Ollama Chat Completion] -* xref:api/chat/huggingface.adoc[HuggingFace Chat Completion] (no streaming support) +* xref:api/chat/huggingface.adoc[Hugging Face Chat Completion] (no streaming support) * xref:api/chat/vertexai-palm2-chat.adoc[Google Vertex AI PaLM2 Chat Completion] (no streaming support) * xref:api/chat/vertexai-gemini-chat.adoc[Google Vertex AI Gemini Chat Completion] (streaming, multi-modality & function-calling support) * xref:api/bedrock.adoc[Amazon Bedrock] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/postgresml-embeddings.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/postgresml-embeddings.adoc index 8f4458961..7082b5757 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/postgresml-embeddings.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/postgresml-embeddings.adoc @@ -50,7 +50,7 @@ The prefix `spring.ai.postgresml.embedding` is property prefix that configures t |==== | Property | Description | Default | spring.ai.postgresml.embedding.enabled | Enable PostgresML embedding model. | true -| spring.ai.postgresml.embedding.options.transformer | The Huggingface transformer model to use for the embedding. | distilbert-base-uncased +| spring.ai.postgresml.embedding.options.transformer | The Hugging Face transformer model to use for the embedding. | distilbert-base-uncased | spring.ai.postgresml.embedding.options.kwargs | Additional transformer specific options. | empty map | spring.ai.postgresml.embedding.options.vectorType | PostgresML vector type to use for the embedding. Two options are supported: `PG_ARRAY` and `PG_VECTOR`. | PG_ARRAY | spring.ai.postgresml.embedding.options.metadataMode | Document metadata aggregation mode | EMBED diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/index.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/index.adoc index 1163ddc8b..43cf65ad8 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/index.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/index.adoc @@ -14,7 +14,7 @@ Dropping down to access model specific features is also supported. image::model-hierarchy.jpg[Model hierarchy, width=900, align="center"] -With support for AI Models from OpenAI, Microsoft, Amazon, Google, Amazon Bedrock, Huggingface and more. +With support for AI Models from OpenAI, Microsoft, Amazon, Google, Amazon Bedrock, Hugging Face and more. image::spring-ai-chat-completions-clients.jpg[align="center", width="800px"] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/getting-started.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/getting-started.adoc index 224df22be..fe1bd9b06 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/getting-started.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/getting-started.adoc @@ -144,7 +144,7 @@ Each of the following sections in the documentation shows which dependencies you ** xref:api/chat/openai-chat.adoc[OpenAI Chat Completion] (streaming and function-calling support) ** xref:api/chat/azure-openai-chat.adoc[Microsoft Azure Open AI Chat Completion] (streaming and function-calling support) ** xref:api/chat/ollama-chat.adoc[Ollama Chat Completion] -** xref:api/chat/huggingface.adoc[HuggingFace Chat Completion] (no streaming support) +** xref:api/chat/huggingface.adoc[Hugging Face Chat Completion] (no streaming support) ** xref:api/chat/vertexai-palm2-chat.adoc[Google Vertex AI PaLM2 Chat Completion] (no streaming support) ** xref:api/chat/vertexai-gemini-chat.adoc[Google Vertex AI Gemini Chat Completion] (streaming, multi-modality & function-calling support) ** xref:api/bedrock.adoc[Amazon Bedrock] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/index.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/index.adoc index 9b78c9904..75c05e595 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/index.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/index.adoc @@ -11,7 +11,7 @@ These abstractions have multiple implementations, enabling easy component swappi Spring AI provides the following features: -* Support for all major Model providers such as OpenAI, Microsoft, Amazon, Google, and Huggingface. +* Support for all major Model providers such as OpenAI, Microsoft, Amazon, Google, and Hugging Face. * Supported Model types are Chat, Text to Image, Audio Transcription, Text to Speech, and more on the way. * Portable API across AI providers for all models. Both synchronous and stream API options are supported. Dropping down to access model specific features is also supported. * Mapping of AI Model output to POJOs. diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/huggingface/HuggingfaceChatProperties.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/huggingface/HuggingfaceChatProperties.java index d31bef3fb..e64fff584 100644 --- a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/huggingface/HuggingfaceChatProperties.java +++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/huggingface/HuggingfaceChatProperties.java @@ -17,17 +17,29 @@ package org.springframework.ai.autoconfigure.huggingface; import org.springframework.boot.context.properties.ConfigurationProperties; +/** + * @author Christian Tzolov + * @author Josh Long + * @author Mark Pollack + * @author Thomas Vitale + */ @ConfigurationProperties(HuggingfaceChatProperties.CONFIG_PREFIX) public class HuggingfaceChatProperties { public static final String CONFIG_PREFIX = "spring.ai.huggingface.chat"; + /** + * API Key to authenticate with the Inference Endpoint. + */ private String apiKey; + /** + * URL of the Inference Endpoint. + */ private String url; /** - * Enable Huggingface chat model. + * Enable Hugging Face chat model. */ private boolean enabled = true; diff --git a/spring-ai-spring-boot-starters/spring-ai-starter-huggingface/pom.xml b/spring-ai-spring-boot-starters/spring-ai-starter-huggingface/pom.xml new file mode 100644 index 000000000..18d9e2c09 --- /dev/null +++ b/spring-ai-spring-boot-starters/spring-ai-starter-huggingface/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + org.springframework.ai + spring-ai + 1.0.0-SNAPSHOT + ../../pom.xml + + spring-ai-huggingface-spring-boot-starter + jar + Spring AI Starter - Hugging Face + Spring AI Hugging Face Starter + https://github.com/spring-projects/spring-ai + + + https://github.com/spring-projects/spring-ai + git://github.com/spring-projects/spring-ai.git + git@github.com:spring-projects/spring-ai.git + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.ai + spring-ai-spring-boot-autoconfigure + ${project.parent.version} + + + + org.springframework.ai + spring-ai-huggingface + ${project.parent.version} + + + +