Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

ThomasVitale/langchain4j-spring-boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

LangChain4j Spring Boot

Warning This package has been archived in favour of the upstream LangChain4j Spring project.

This project brings LangChain4j support in Spring Boot to build AI and LLM-powered applications. It provides integrations with LLM services and vector stores, as well as tools, chains, and AI services.

Using the starter projects in this repository, you gain the following advantages over using the vanilla LangChain4j libraries in Spring Boot:

  • Autoconfiguration and unified configuration properties for models and vector stores
  • HTTP infrastructure with RestClient, WebClient, and Jackson for all integrations
  • Built-in observability with Micrometer, including LLM-specific metrics and traces
  • Dev services with Docker Compose and Testcontainers for models and vector stores
  • Service bindings for automatic connection configuration when running on Kubernetes.

🦜 Models

OpenAI

Gradle:

implementation 'io.thomasvitale.langchain4j:langchain4j-openai-spring-boot-starter:0.9.0'

Configuration:

langchain4j:
  open-ai:
    client:
      api-key: ${OPENAI_API_KEY}

Example:

@RestController
class ChatController {
    private final ChatLanguageModel chatLanguageModel;

    ChatController(ChatLanguageModel chatLanguageModel) {
        this.chatLanguageModel = chatLanguageModel;
    }

    @GetMapping("/ai/chat")
    String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
        return chatLanguageModel.generate(message);
    }
}

Ollama

Gradle:

implementation 'io.thomasvitale.langchain4j:langchain4j-ollama-spring-boot-starter:0.9.0'

Configuration:

langchain4j:
  ollama:
    chat:
      model: llama3

Example:

@RestController
class ChatController {
    private final ChatLanguageModel chatLanguageModel;

    ChatController(ChatLanguageModel chatLanguageModel) {
        this.chatLanguageModel = chatLanguageModel;
    }

    @GetMapping("/ai/chat")
    String chat(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) {
        return chatLanguageModel.generate(message);
    }
}

🫙 Vector Stores

Chroma

Gradle:

implementation 'io.thomasvitale.langchain4j:langchain4j-chroma-spring-boot-starter:0.9.0'

Example:

class ChromaDataIngestor {
    private final ChromaEmbeddingStore embeddingStore;
    private final EmbeddingModel embeddingModel;

    ChatController(ChromaEmbeddingStore embeddingStore, EmbeddingModel embeddingModel) {
        this.embeddingStore = embeddingStore;
        this.embeddingModel = embeddingModel;
    }

    public void ingest(List<Document> documents) {
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .documentSplitter(recursive(300, 0))
                .build();
        ingestor.ingest(documents);
    }
}

Weaviate

Gradle:

implementation 'io.thomasvitale.langchain4j:langchain4j-weaviate-spring-boot-starter:0.9.0'

Example:

class WeaviateDataIngestor {
    private final WeaviateEmbeddingStore embeddingStore;
    private final EmbeddingModel embeddingModel;

    ChatController(WeaviateEmbeddingStore embeddingStore, EmbeddingModel embeddingModel) {
        this.embeddingStore = embeddingStore;
        this.embeddingModel = embeddingModel;
    }

    public void ingest(List<Document> documents) {
        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .documentSplitter(recursive(300, 0))
                .build();
        ingestor.ingest(documents);
    }
}

🌟 Examples

Check these examples to see LangChain4j and Spring Boot in action.

🛡️  Security

The security process for reporting vulnerabilities is described in SECURITY.md.

🖊️  License

This project is licensed under the Apache License 2.0. See LICENSE for more information.

About

LangChain4j support in Spring Boot to build AI and LLM-powered applications.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages