Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ignore-notebooks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
05_nvidia_ai_rag_redis
01_routing_optimization
02_semantic_cache_optimization
spring_ai_redis_rag.ipynb
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,4 @@ pip-selfcheck.json
libs/redis/docs/.Trash*
.python-version
.idea/*
java-recipes/.*
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Need quickstarts to begin your Redis AI journey? **Start here.**
| [/vector-search/02_hybrid_search.ipynb](/python-recipes/vector-search/02_hybrid_search.ipynb) | Hybrid search techniques with Redis (BM25 + Vector) |
| [/vector-search/03_dtype_support.ipynb](/python-recipes/vector-search/03_dtype_support.ipynb) | Shows how to convert a float32 index to float16 or integer dataypes|

### Non-Python Redis AI Recipes

#### ☕️ Java

A set of Java recipes can be found under [/java-recipes](/java-recipes/README.md).

### Retrieval Augmented Generation (RAG)

Expand Down Expand Up @@ -119,7 +124,7 @@ Routing is a simple and effective way of preventing misuses with your AI applica
## Tutorials
Need a *deeper-dive* through different use cases and topics?

| Tutorial | Description |
| Tutorial | Description |
| -------- | ------------ |
| [Agentic RAG](https://github.com/redis-developer/agentic-rag) | A tutorial focused on agentic RAG with LlamaIndex and Cohere |
| [RAG on VertexAI](https://github.com/redis-developer/gcp-redis-llm-stack/tree/main) | A RAG tutorial featuring Redis with Vertex AI |
Expand Down
466 changes: 466 additions & 0 deletions java-recipes/RAG/spring_ai_redis_rag.ipynb

Large diffs are not rendered by default.

148 changes: 148 additions & 0 deletions java-recipes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<div align="center">
<div><img src="../assets/redis-logo.svg" style="width: 130px"> </div>
<h1>Redis AI Java Resources</h1>
<div align="center">

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Java](https://img.shields.io/badge/Java-21-orange)
![Spring AI](https://img.shields.io/badge/Spring%20AI-1.0.0--M6-green)

</div>
<div>
✨ Java-based code examples, notebooks, and resources for using Redis in AI and ML applications. ✨
</div>

<div></div>
<br>

[**Setup**](#setup) | [**Running the Project**](#running-the-project) | [**Notebooks**](#notebooks) | [**Project Structure**](#project-structure) | [**Implementation Details**](#implementation-details)

</div>
<br>

## Setup

This project uses Docker Compose to set up a complete environment for running Java-based AI applications with Redis. The environment includes:

- A Jupyter Notebook server with Java kernel support
- Redis Stack (includes Redis and RedisInsight)
- Pre-installed dependencies for AI/ML workloads

### Prerequisites

- [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/)
- OpenAI API key (for notebooks that use OpenAI services)

### Environment Configuration

1. Create a `.env` file in the project root with your OpenAI API key:

```bash
OPENAI_API_KEY=your_openai_api_key_here
```

## Running the Project

1. Clone the repository (if you haven't already):

```bash
git clone https://github.com/redis-developer/redis-ai-resources.git
cd redis-ai-resources/java-resources
```

2. Start the Docker containers:

```bash
docker-compose up -d
```

3. Access the Jupyter environment:
- Open your browser and navigate to [http://localhost:8888](http://localhost:8888)
- The token is usually shown in the docker-compose logs. You can view them with:

```bash
docker-compose logs jupyter
```

4. Access RedisInsight:
- Open your browser and navigate to [http://localhost:8001](http://localhost:8001)
- Connect to Redis using the following details:
- Host: redis-java
- Port: 6379
- No password (unless configured)

5. When finished, stop the containers:

```bash
docker-compose down
```

## Notebooks

| Notebook | Description |
| --- | --- |
| [RAG/spring_ai_redis_rag.ipynb](./RAG/spring_ai_redis_rag.ipynb) | Demonstrates building a RAG-based beer recommendation chatbot using Spring AI and Redis as the vector store |

## Project Structure

```bash
java-recipes/
├── .env # Environment variables (create this)
├── docker-compose.yml # Docker Compose configuration
├── jupyter/ # Jupyter configuration files
│ ├── Dockerfile # Dockerfile for Jupyter with Java kernel
│ ├── environment.yml # Conda environment specification
│ ├── install.py # JJava kernel installation script
│ ├── kernel.json # Kernel specification
│ └── java/ # Java dependencies and configuration
│ └── pom.xml # Maven project file with dependencies
└── resources/ # Data files for notebooks
└── beers.json.gz # Compressed beer dataset
```

## Implementation Details

### Java Jupyter Kernel

The project uses [JJava](https://github.com/dflib/jjava), a Jupyter kernel for Java based on JShell. This allows for interactive Java development in Jupyter notebooks.

Key components:

- Java 21 for modern Java features
- Maven for dependency management
- JJava kernel for Jupyter integration

### Spring AI Integration

The Spring AI notebooks showcase how to use Spring's AI capabilities with Redis:
- **Spring AI**: Framework for building AI-powered applications
- **Redis Vector Store**: Used for storing and querying vector embeddings
- **Transformer Models**: For generating embeddings locally
- **RAG Pattern**: Demonstrates the Retrieval Augmented Generation pattern
### Docker Configuration
The Docker setup includes:
1. **Jupyter Container**:
- Based on minimal Jupyter notebook image
- Adds Java 21, Maven, and the JJava kernel
- Includes Python environment with PyTorch and other ML libraries
2. **Redis Container**:
- Uses Redis Stack image with Vector Search capabilities
- Persists data using Docker volumes
- Exposes Redis on port 6379 and RedisInsight on port 8001
## Example Applications
### Beer Recommendation Chatbot
The `spring-ai-rag.ipynb` notebook demonstrates:
- Loading and embedding beer data into Redis Vector Store
- Using local transformer models for generating embeddings
- Connecting to OpenAI for LLM capabilities
- Building a RAG pipeline to answer beer-related queries
- Semantic search over beer properties and descriptions
25 changes: 25 additions & 0 deletions java-recipes/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: redis-ai-java
services:
jupyter:
build:
context: .
dockerfile: ./jupyter/Dockerfile
ports:
- "8888:8888"
environment:
- JUPYTER_ENABLE_LAB=yes
env_file:
- .env
volumes:
- ./:/home/jovyan/
- ./resources:/home/jovyan/resources
redis-java:
image: redis/redis-stack:latest
ports:
- "6379:6379" # Redis database port
- "8001:8001" # RedisInsight port
volumes:
- redis-data:/data # Persist Redis data

volumes:
redis-data:
59 changes: 59 additions & 0 deletions java-recipes/jupyter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
FROM quay.io/jupyter/minimal-notebook:latest

RUN mkdir /home/jovyan/resources

USER root
WORKDIR /home/jovyan

# Install dependencies: Java 21 and Maven
RUN apt-get update && apt-get install -y openjdk-21-jdk maven

# Copy the pre-created Maven project and jjava-glue project
COPY ./jupyter/java /home/jovyan/java
COPY ./jupyter/install.py /home/jovyan/install.py

# Use Maven to download dependencies for JJava
WORKDIR /home/jovyan/java

# Download the JJava jar directly
RUN mvn dependency:get -Dartifact=org.dflib.jjava:jjava:1.0-M3 -Ddest=./ -Dtransitive=false
RUN mv jjava-1.0-M3.jar jjava.jar

# Pre-download Spring AI Dependencies
RUN mvn dependency:get -Dartifact=org.springframework.ai:spring-ai-openai:1.0.0-M6
RUN mvn dependency:get -Dartifact=org.springframework.ai:spring-ai-transformers:1.0.0-M6
RUN mvn dependency:get -Dartifact=org.springframework.ai:spring-ai-redis-store:1.0.0-M6
# Pre-download Jedis
RUN mvn dependency:get -Dartifact=redis.clients:jedis:5.2.0
# Download all dependencies
RUN mvn dependency:copy-dependencies -DoutputDirectory=./lib

# Create a list of dependencies for the classpath
RUN find ./lib -name "*.jar" | tr '\n' ':' > classpath.txt
# Add the jjava.jar to the classpath
RUN echo -n "/home/jovyan/java/jjava.jar:" >> classpath.txt

# Install the kernel with classpath configuration
WORKDIR /home/jovyan
RUN python install.py --prefix /opt/conda/ --classpath $(cat /home/jovyan/java/classpath.txt)

# Pre-download Transformer Models
RUN pip install transformers torch
RUN mkdir -p /home/jovyan/.cache/huggingface/hub
# Pre-download the specific model used in Spring AI Transformers
RUN python -c "from transformers import AutoModel; AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')"

# Clean up Maven artifacts but keep the jjava.jar and lib directory
RUN rm -rf /home/jovyan/java/target /home/jovyan/java/.m2 /home/jovyan/java/pom.xml \
/home/jovyan/java/classpath.txt \
&& rm -f /home/jovyan/install.py

# Install conda packages from environment.yml
COPY ./jupyter/environment.yml /tmp/
RUN conda env update -f /tmp/environment.yml && \
conda clean --all -f -y && \
fix-permissions "${CONDA_DIR}" && \
fix-permissions "/home/${NB_USER}"

WORKDIR /home/jovyan
USER $NB_UID
9 changes: 9 additions & 0 deletions java-recipes/jupyter/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: base
channels:
- pytorch
- conda-forge
- defaults
dependencies:
- pytorch
- torchtext
- gensim
Loading