/spring-petclinic-langchain4j

It is a langchain4j LLM demo of petclinic

Primary LanguageCSSApache License 2.0Apache-2.0

Spring PetClinic Sample Application With OpenAI and Langchain4j

Understanding the Spring Petclinic application with a few diagrams

This is the spring langchain4j version for spring petclinic, if you want to see a springai version, please refer to spring-petclinic-springai

Run Petclinic locally

Spring Petclinic is a Spring Boot application built using Maven. You can build a jar file and run it from the command line (it should work just as well with Java 17 or newer), Before you build the application, you need to setup some openai properties in src/main/resources/application.properties:

git clone https://github.com/showpune/spring-petclinic-langchain4j.git
cd spring-petclinic-langchain4j
mv src/main/resources/application.properties.example src/main/resources/application.properties

edit the src/main/resources/application.properties file and add the following properties:

langchain4j.azure.open-ai.chat-model.endpoint=https://*****.openai.azure.com/
langchain4j.azure.open-ai.chat-model.deployment-name=gpt-4
langchain4j.azure.open-ai.chat-model.api-key=**

You can build the application by running the following command:

./mvnw package
java -jar target/*.jar

You can then access the Petclinic at http://localhost:8080/ petclinic-screenshot

And the OpenAI chatbot at http://localhost:8080/chat.html. petclinic-screenshot

The Sequence workflow of langchain4j

image

Workthrough of the chat agent

You can talk with the agent, it can help to recommend the vet according to the symptoms of the pet. The agent can also help to book an appointment with the vet. image Go to the owner page, you can see you are registered as an owner with the help of the agent image

LLM Progamming

Normally a LLM application contains 5 parts: Prompt, Model, Memory, Native Functions( tools )and RAG (content retriever)

Prompt

The prompt is defined in agent

Model

The model is is defined in model, the model can be used for chat, or can be used for content augmentor

Memory

Memory Store: The demo still use the local memory defined in memory store, it means it can not share memory between instances, you can enhance it use memory on Redis

Memory ID: It use the username as memory id Memory ID

Interact with natives functions

The Demo provided two local tools to interactive with native functions

  1. Vets and their specialist: The agent will know the system can return list of Vets, include their specialist, it can be used to recommend a vet
  2. Owner and Pets: he agent will know the system register new owner and their pets

Content Retriever

It still use the local file as content retriever, it provided the guideline how the agent should work, which is in Term of Use, the content is ingest when the store in initialized here. If you have lots of content, tried to use the AI Search to create the RAG

Talk with Other language

You can also talk with the agent with your own language, like Chinese image

The problem is that your Term of Use is in English, the traditional way is that provide a localized term of use for each language, but you can use openAI to make it easier

We can define a Retrieval Augmentor, and translate your ask into English before you retrieve the content

License

The Spring PetClinic sample application is released under version 2.0 of the Apache License.