Piggy Bank
is a simple AI driven banking application using OpenAI, LangChain4j and Spring Boot with React 🚀.
Piggy Bank
is written in Java using Hilla. The Hilla web framework was chosen because it brings together the power of Spring Boot and React. It allows you to build a full stack web application with a single codebase.
To keep things simple, the application covers the following use cases.
- A Bank Associate can see a list of customers
- A Bank Associate can click on a customer to see their accounts
- Accounts can either be Opened or Closed
Finally, we'll integrate OpenAI's GPT-3 API to allow a customer to close their account through an AI driven chatbot.
You will need to create an OpenAI API key to run the application. You can create a secret API key here.
- Click on the
API keys
link. - Click on the
Create new secret key
button. - Give the secret key a name.
- Click on the
Create secret key
button. - Copy the API key.
The easiest way to build the sample is to use GitHub CodeSpaces. You can also use dev container to build the sample locally or use your own development environment.
To build the sample using CodeSpaces, follow the steps below.
- Navigate to the GitHub repository nickdala/piggy-bank-langchain4j
- Click on the
Code
button. - Click on the
Codespaces
tab. - Click on the
Create codespace on main
button.
This will create a GitHub Codespace and clone the repository. Once the Codespace is ready, you will see the following.
You can also use your own development environment to run the application. Below are the requirements for your development environment according to Hilla.
- Java 17 or later
- Node 18 or later
- An IDE that supports Java and TypeScript — IntelliJ Ultimate and VS Code are good choices
To run the application locally, clone the repository and change directory
git clone https://github.com/nickdala/piggy-bank-langchain4j.git
cd piggy-bank-langchain4j
Next, we need to set the OpenAI API key in the application.properties
file. To do this, navigate to src/main/resources/application.properties
and set the property langchain4j.chat-model.openai.api-key
.
To build and run the application, run the following command in the CodeSpaces terminal.
./mvnw
This will build and run the application. A new browser window will launch automatically with the Piggy Bank application.
Here is the UI of what the application looks like.
To close an account, click on the Chat
menu. This will launch an AI driven chatbot. The chatbot will ask you a series of questions to close the account.
Here is a sample conversation with the chatbot looks like.
Provide the chatbot with the id of the account you want to close. Once the account is closed, verify this in the UI.
Pretty cool, right? 😎
Lets now take a look at how we incorporate LangChain4j into your application.
The first step is to add the LangChain4j dependency to your pom.xml
file of your Spring Boot application.
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>0.24.0</version>
</dependency>
Next, we need to set the OpenAI API key in the application.properties
file. To do this, navigate to src/main/resources/application.properties
and set the property langchain4j.chat-model.openai.api-key
.
langchain4j.chat-model.openai.api-key=<your-openai-api-key>
By using LangChain4j, we were able to build an AI driven chatbot with just a few lines of code. This project contains the following AI features.
- Large Language Models (LLMs)
- Prompt Engineering
- LangChain4j Tools
We were able to leverage the power of LLMs and Prompt Engineering to build a chatbot that can close an account.
Within the interface CustomerSupportAgent, we have the following @SystemMessage
.
public interface CustomerSupportAgent {
@SystemMessage({
"""
You are a customer support agent of a bank named 'Piggy Bank Assistant'.
Ask the customer how you can help them. The only thing you can assist
the customer with is closing an account.
"""
})
String chat(String message);
}
The @SystemMessage
annotation is used to define the prompt for the chatbot. The prompt is the first instruction to the LLM and is used to generate the response from the chatbot.
As an exercise, try changing the prompt and see how the chatbot responds.
Agents are used to tell the LLM what actions to take. In chains, a sequence of actions is hardcoded. Agents, on the other hand, utilize a language model as a reasoning engine to determine which actions to take and in which order. Tools are used to help the LLM perform the actions. They are methods an agent can use to perform an action.
Within the class AccountTools, we have the following @Tool
annotation.
public class AccountTools {
@Tool("Close account by id")
public boolean closeAccount(long accountId) {
accountService.closeAccount(accountId);
return true;
}
@Tool("Finds the customer by id")
public CustomerRecord findCustomerById(long customerId) {
return customerService.findCustomertById(customerId);
}
}
The @Tool
annotation is used to define the tool for the chatbot. It's important to provide a proper description of the tool. This will help the LLM determine which tool to use.
In this article, we built an AI driven chatbot using LangChain4j. We were able to leverage the power of LLMs and Prompt Engineering to build a chatbot that can close an account.
Blog post can be found here.