The goal of this project is to simplify the integration of AI/LLM capabilities into your Java application.
This can be achieved thanks to:
A simple and coherent layer of abstractions, designed to ensure that your code does not depend on concrete implementations such as LLM providers, embedding store providers, etc. This allows for easy swapping of components.
Numerous implementations of the above-mentioned abstractions, providing you with a variety of LLMs and embedding stores to choose from.
Range of in-demand features on top of LLMs, such as:
The capability to ingest your own data (documentation, codebase, etc.), allowing the LLM to act and respond based on your data.
Autonomous agents for delegating tasks (defined on the fly) to the LLM, which will strive to complete them.
Prompt templates to help you achieve the highest possible quality of LLM responses.
Memory to provide context to the LLM for your current and past conversations.
Structured outputs for receiving responses from the LLM with a desired structure as Java POJOs.
"AI Services" for declaratively defining complex AI behavior behind a simple API.
Chains to reduce the need for extensive boilerplate code in common use-cases.
Auto-moderation to ensure that all inputs and outputs to/from the LLM are not harmful.
You can now try out OpenAI's gpt-3.5-turbo and text-embedding-ada-002 models with LangChain4j for free, without needing an OpenAI account and keys! Simply use the API key "demo".
15 July:
Added EmbeddingStoreIngestor
Redesigned document loaders (see FileSystemDocumentLoader)
Added "Dynamic Tools":
Now, the LLM can generate code for tasks that require precise calculations, such as math and string manipulation. This will be dynamically executed in a style akin to GPT-4's code interpreter!
We use Judge0, hosted by Rapid API, for code execution. You can subscribe and receive 50 free executions per day.
5 July:
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.
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");
System.out.println(answer);
// 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;
}
interface PersonExtractor {
@UserMessage("Extract information about a person from {{text}}")
Person extractPersonFrom(@V("text") 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 provide tools that LLMs can use! Can be anything: retrieve information from DB, call APIs, etc.
See example here.
You can use the API key "demo" to test OpenAI, which we provide for free.
How to gen an API key?
Create an instance of a model and start interacting:
OpenAiChatModel model = OpenAiChatModel.withApiKey(apiKey);
String answer = model.generate("Hello world!");
System.out.println(answer); // Hello! How can I assist you today?
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.
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.
You will need an API key from OpenAI (paid) or HuggingFace (free) to use LLMs hosted by them.
We recommend using OpenAI LLMs (gpt-3.5-turbo and gpt-4) as they are by far the most capable and are reasonably priced.
It will cost approximately $0.01 to generate 10 pages (A4 format) of text with gpt-3.5-turbo. With gpt-4, the cost will be $0.30 to generate the same amount of text. However, for some use cases, this higher cost may be justified.
For embeddings, we recommend using one of the models from the HuggingFace MTEB leaderboard.
You'll have to find the best one for your specific use case.