d427b7ba06
This PR: adds a github action for running unit tests tests that require an OpenAI/HuggingFace token and hit their API are now considered integration tests (and have been renamed to end in IT) integration tests are now run through a separate goal (mvn integration-test) via the maven-failsafe-plugin to fix the PromptTemplate tests a Clock has been added to that class. Its constructor is now private: whether this is the convention we want to follow can be discussed |
||
---|---|---|
.github/workflows | ||
.idea | ||
langchain4j | ||
langchain4j-core | ||
langchain4j-pinecone | ||
langchain4j-spring-boot-starter | ||
.gitignore | ||
LICENSE | ||
README.md | ||
pom.xml |
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:
- Added Spring Boot Starter
- Added support for HuggingFace models
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
- Customer support chatbot that can:
- 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:
- AI Services:
- Integration with OpenAI (ChatGPT) for:
- Chats (sync + streaming + functions)
- Completions (sync + streaming)
- Embeddings
- Integration with HuggingFace Inference API for:
- Memory for Chats
- Chat with Documents
- Integration with Pinecone embedding store
- In-memory embedding store (for prototyping and testing)
- Structured outputs
- Prompt templates
- Structured prompt templates
- Streaming of LLM responses
- Loading text and PDF documents from the file system and via URL
- Splitting documents into segments:
- by paragraph
- by sentence
- by character count
- Token count estimation (so that you can predict how much you will pay)
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 "Let’s 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.