This project is an expiremental application that integrates Spring AI and Codegate (transparent AI gateway providing personally identifiable information (PII) protection). Spring Boot-based chat application leverages OpenAI's API for generating responses to user prompts while CodeGate ensures senstive information (e.g. email address, credit cards) aren't sent to OpenAI.
Additional Learning Resources:
-
CodeGate Resources: GitHub Repository | Documentation | YouTube | Discord
-
Spring AI Resources: GitHub Repository | Documentation
- Interactive Chat: Users can input questions or commands via the console and receive formatted responses from an AI assistant.
- OpenAI Integration: Utilizes OpenAI's GPT-4 models for generating high-quality answers through a Spring Boot service layer.
- Privacy Focus: The application is designed to operate with a strong emphasis on user privacy and data security leveraging CodeGate.
sequenceDiagram
participant U as User
participant A as Application
participant O as OpenAIService
participant C as ChatClient
participant G as CodeGate
participant AI as OpenAI
U->>A: Start CLI interaction
A->>U: Prompt for input
U->>A: User enters question
A->>O: getAnswer(question)
O->>C: chatClient.prompt().user(question)
C->>G: Call OpenAI API via CodeGate.privacyCheck()
G->>AI: Call OpenAI API
AI-->>G: Response from OpenAI
G-->>C: Response from OpenAI
C-->>O: Return response to ChatClient
O-->>A: Return response to Application
A-->>U: Display response to User
Follow these steps to clone and run this project locally:
- Java 17 or higher
- Maven
- An OpenAI API key
- CodeGate is distributed as a Docker container. You need a container runtime like Docker Desktop or Docker Engine. Podman and Podman Desktop are also supported. CodeGate works on Windows, macOS, and Linux operating systems with x86_64 and arm64 (ARM and Apple Silicon) CPU architectures.
git clone https://github.com/StacklokLabs/spring-ai-codegate-sample.git
cd sample
- Create a file named
application.properties
in thesrc/main/resources
directory, and add your OpenAI API key:
spring.ai.openai.api-key=YOUR_OPENAI_API_KEY
Run the following command to build the project:
mvn clean install
Follow the instructions in CodeGate's Quickstart documentation
To start CodeGate, run this simple command:
docker run --name codegate -d -p 8989:8989 -p 9090:9090 -p 8990:8990 \
--mount type=volume,src=codegate_volume,dst=/app/codegate_volume \
--restart unless-stopped ghcr.io/stacklok/codegate:latest
That’s it! CodeGate is now running locally.
You can run the application using the command:
mvn spring-boot:run
When prompted by the CLI, type
codegate version
If CodeGate is configured properly, you should receive the following response:
Assistant: CodeGate version: v0.1.18
Note: You will need to be running CodeGate version v0.1.18 or greater to leverage the PII feature.
To see the privacy feature in action. attempt to send an email address and watch CodeGate redact the email address before sending the request to OpenAI:
Who is test.sender@example.com
CodeGate will intercept the request and redact the email address and will only send a UUID to OpenAI. You should receive the following response:
Assistant: I'm sorry, but I can't provide information about specific UUIDs..
Feel free to fork the repository, make improvements or report issues.