Go to file
Julien Perrochet 0cc9bb81ae
[misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12)
Also cleanup some gitignore entries
2023-07-06 08:33:45 +02:00
.github/workflows add maven test github action (#11) 2023-07-05 21:55:49 +02:00
.idea init - 0.4.0 2023-06-20 17:57:52 +02:00
langchain4j [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
langchain4j-core [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
langchain4j-pinecone [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
langchain4j-spring-boot-starter [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
.gitignore [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
LICENSE Initial commit 2023-06-20 17:30:29 +02:00
README.md Update README.md 2023-07-05 19:22:40 +02:00
mvnw [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
mvnw.cmd [misc] Clean up maven wrapper scripts: have one at the root, delete the others. (#12) 2023-07-06 08:33:45 +02:00
pom.xml released 0.10.0 2023-07-05 18:55:20 +02:00

README.md

LangChain4j: Supercharge your Java application with the power of AI

Do you want to integrate various AI tools and services into your Java application?

Did you hear about LangChain and want to try it in Java?

This library might be what you need.

News

05.07.2023:

  • Now you can add your custom knowledge base to "AI Services". Relevant information will be automatically retrieved and injected into the prompt. This way, the LLM will have a context of your data and will answer based on it!
  • The current date and time can now be automatically injected into the prompt using special {{current_date}}, {{current_time}} and {{current_date_time}} placeholders.

03.07.2023:

  • Added support for Spring Boot 3

02.07.2023:

01.07.2023:

Highlights

You can declaratively define concise "AI Services" that are powered by LLMs:

interface Assistant {

    String chat(String userMessage);
}

Assistant assistant = AiServices.create(Assistant.class, model);

String answer = assistant.chat("Hello");

// Hello! How can I assist you today?

You can use LLM as a classifier:

enum Sentiment {
    POSITIVE, NEUTRAL, NEGATIVE;
}
        
interface SentimentAnalyzer {

    @UserMessage("Analyze sentiment of {{it}}")
    Sentiment analyzeSentimentOf(String text);

    @UserMessage("Does {{it}} have a positive sentiment?")
    boolean isPositive(String text);
}

SentimentAnalyzer sentimentAnalyzer = AiServices.create(SentimentAnalyzer.class, model);

Sentiment sentiment = sentimentAnalyzer.analyzeSentimentOf("It is good!");
// POSITIVE

boolean positive = sentimentAnalyzer.isPositive("It is bad!");
// false

You can easily extract structured information from unstructured data:

class Person {

    private String firstName;
    private String lastName;
    private LocalDate birthDate;

    public String toString() {...}
}

interface PersonExtractor {

    @UserMessage("Extract information about a person from {{it}}")
    Person extractPersonFrom(String text);
}

PersonExtractor extractor = AiServices.create(PersonExtractor.class, model);

String text = "In 1968, amidst the fading echoes of Independence Day, "
    + "a child named John arrived under the calm evening sky. "
    + "This newborn, bearing the surname Doe, marked the start of a new journey.";
    
Person person = extractor.extractPersonFrom(text);
// Person { firstName = "John", lastName = "Doe", birthDate = 1968-07-04 }

You can define more sophisticated prompt templates using mustache syntax:

interface Translator {

    @SystemMessage("You are a professional translator into {{language}}")
    @UserMessage("Translate the following text: {{text}}")
    String translate(@V("text") String text, @V("language") String language);
}

Translator translator = AiServices.create(Translator.class, model);

String translation = translator.translate("Hello, how are you?", "Italian");
// Ciao, come stai?

You can provide tools that LLMs can use! Can be anything: retrieve information from DB, call APIs, etc. See example here.

Code examples

Please see more examples of how LangChain4j can be used:

Compatibility

  • Java: 8 or higher
  • Spring Boot: 2 or 3

Project goals

The goal of this project is to simplify the integration of AI capabilities into your Java application. This can be achieved thanks to:

  • Simple and coherent layer of abstractions, so that your code does not depend on concrete implementations (LLM providers, embedding store providers, etc) and you can swap them easily
  • Numerous implementations of the above-mentioned interfaces, so that you have freedom to choose
  • Range of in-demand features on top of LLMs, such as:
    • Prompt templates, so that you can achieve the best possible quality of LLM responses
    • Memory, so that LLM has a context of your current and previous conversations
    • Ingesting your own data (documentation, codebase, etc.), so that LLM can act and answer based on your data
    • Chains, so that you don't need to write lots of boilerplate code for common use-cases
    • Structured outputs, so that you can receive responses from LLM as Java objects
    • Autonomous agents, so that you can delegate tasks (defined on the fly) to LLM, and it will do its best to complete them
    • "AI Services", so that you can declaratively define complex AI behavior behind a simple API
    • Auto-moderation, so that you can be sure that all inputs and outputs to/from LLM are not harmful

Use cases

You might ask why would I need all of this? Here are a couple of examples:

  • You want to implement a custom AI-powered chatbot that has access to your data and behaves the way you want it:
    • Customer support chatbot that can:
      • politely answer customer questions
      • take /change/cancel orders
    • Educational assistant that can:
      • Teach various subjects
      • Explain unclear parts
      • Assess user's understanding/knowledge
  • You want to process a lot of unstructured data (files, web pages, etc) and extract structured information from them. For example:
    • extract insights from customer reviews and support chat history
    • extract interesting information from the websites of your competitors
    • extract insights from CVs of job applicants
  • You want to generate information, for example:
    • Emails tailored for each of your customers
    • Content for your app/website:
      • Blog posts
      • Stories
  • You want to transform information, for example:
    • Summarize
    • Proofread and rewrite
    • Translate

Disclaimer

Please note that the library is in active development and:

  • Many features are still missing. We are working hard on implementing them ASAP.
  • API might change at any moment. At this point, we prioritize good design in the future over backward compatibility now. We hope for your understanding.
  • We need your input! Please let us know what features you need and your concerns about the current implementation.

Current capabilities:

Coming soon:

  • Extending "AI Service" features
  • Integration with more LLM providers (commercial and open)
  • Integrations with more embedding stores (commercial and open)
  • Support for more document types
  • Long-term memory for chatbots and agents
  • Chain-of-Thought and Tree-of-Thought

Please let us know what features you need!

Start using

Maven:

<dependency>
  <groupId>dev.langchain4j</groupId>
  <artifactId>langchain4j</artifactId>
  <version>0.10.0</version>
</dependency>

Gradle:

implementation 'dev.langchain4j:langchain4j:0.10.0'

Request features

Please let us know what features you need.

Contribute

Please help us make this open-source library better by contributing.

Best practices

We highly recommend watching this amazing 90-minute tutorial on prompt engineering best practices, presented by Andrew Ng (DeepLearning.AI) and Isa Fulford (OpenAI). This course will teach you how to use LLMs efficiently and achieve the best possible results. Good investment of your time!

Here are some best practices for using LLMs:

  • Be responsible. Use AI for Good.
  • Be specific. The more specific your query, the best results you will get.
  • Add magical "Lets think step by step" instruction to your prompt.
  • Specify steps to achieve the desired goal yourself. This will make the LLM do what you want it to do.
  • Provide examples. Sometimes it is best to show LLM a few examples of what you want instead of trying to explain it.
  • Ask LLM to provide structured output (JSON, XML, etc). This way you can parse response more easily and distinguish different parts of it.
  • Use unusual delimiters, such as ```triple backticks``` to help the LLM distinguish data or input from instructions.