-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retriever and lvm update for multimodal rag on videos (#606)
* updates Signed-off-by: Tiep Le <tiep.le@intel.com> * cosmetic Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update redis schema Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update multimodal config and docker compose retriever Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update requirements Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update retriever redis Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * multimodal retriever implementation Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * test for multimodal retriever Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * include prompt preparation for multimodal rag on videos application Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> * fix template Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> * add test for llava for mm_rag_on_videos Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> * update test Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix index not found Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> * add LVMSearchedMultimodalDoc Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove INDEX_SCHEMA Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * revise folder structure to comps/retrievers/langchain/redis_multimodal Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update test Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * change port of redis to resolve CI test Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update test Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> * update lvms test Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> --------- Signed-off-by: Tiep Le <tiep.le@intel.com> Signed-off-by: siddhivelankar23 <siddhi.velankar@intel.com> Signed-off-by: sjagtap1803 <siddhant.jagtap@intel.com> Co-authored-by: siddhivelankar23 <siddhi.velankar@intel.com> Co-authored-by: sjagtap1803 <siddhant.jagtap@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
90cc44f
commit 1513998
Showing
15 changed files
with
525 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
class ChatTemplate: | ||
|
||
@staticmethod | ||
def generate_multimodal_rag_on_videos_prompt(question: str, context: str): | ||
template = """The transcript associated with the image is '{context}'. {question}""" | ||
return template.format(context=context, question=question) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# Retriever Microservice | ||
|
||
This retriever microservice is a highly efficient search service designed for handling and retrieving embedding vectors from multimodal data. It operates by receiving an embedding vector as input and conducting a similarity search against vectors stored in a VectorDB database. Users must specify the VectorDB's URL and the index name, and the service searches within that index to find documents with the highest similarity to the input vector. | ||
|
||
The service primarily utilizes similarity measures in vector space to rapidly retrieve contentually similar documents. The vector-based retrieval approach is particularly suited for handling large datasets, offering fast and accurate search results that significantly enhance the efficiency and quality of information retrieval. | ||
|
||
Overall, this microservice provides robust backend support for applications requiring efficient similarity searches, playing a vital role in scenarios such as recommendation systems, information retrieval, or any other context where precise measurement of document similarity is crucial. | ||
|
||
## 🚀1. Start Microservice with Python (Option 1) | ||
|
||
To start the retriever microservice, you must first install the required python packages. | ||
|
||
### 1.1 Install Requirements | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
### 1.2 Setup VectorDB Service | ||
|
||
You need to setup your own VectorDB service (Redis in this example), and ingest your knowledge documents into the vector database. | ||
|
||
As for Redis, you could start a docker container using the following commands. | ||
Remember to ingest data into it manually. | ||
|
||
```bash | ||
docker run -d --name="redis-vector-db" -p 6379:6379 -p 8001:8001 redis/redis-stack:7.2.0-v9 | ||
``` | ||
|
||
### 1.3 Ingest images or video | ||
|
||
Upload a video or images using the dataprep microservice, instructions can be found [here](https://github.com/opea-project/GenAIComps/tree/main/comps/dataprep/redis/multimodal_langchain/README.md). | ||
|
||
### 1.4 Start Retriever Service | ||
|
||
```bash | ||
python retriever_redis.py | ||
``` | ||
|
||
## 🚀2. Start Microservice with Docker (Option 2) | ||
|
||
### 2.1 Setup Environment Variables | ||
|
||
```bash | ||
export your_ip=$(hostname -I | awk '{print $1}') | ||
export REDIS_URL="redis://${your_ip}:6379" | ||
export INDEX_NAME=${your_index_name} | ||
``` | ||
|
||
### 2.2 Build Docker Image | ||
|
||
```bash | ||
cd ../../../../ | ||
docker build -t opea/multimodal-retriever-redis:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/retrievers/langchain/redis_multimodal/docker/Dockerfile . | ||
``` | ||
|
||
To start a docker container, you have two options: | ||
|
||
- A. Run Docker with CLI | ||
- B. Run Docker with Docker Compose | ||
|
||
You can choose one as needed. | ||
|
||
### 2.3 Run Docker with CLI (Option A) | ||
|
||
```bash | ||
docker run -d --name="multimodal-retriever-redis-server" -p 7000:7000 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e REDIS_URL=$REDIS_URL -e INDEX_NAME=$INDEX_NAME opea/multimodal-retriever-redis:latest | ||
``` | ||
|
||
### 2.4 Run Docker with Docker Compose (Option B) | ||
|
||
```bash | ||
cd docker | ||
docker compose -f docker_compose_retriever.yaml up -d | ||
``` | ||
|
||
## 🚀3. Consume Retriever Service | ||
|
||
### 3.1 Consume Embedding Service | ||
|
||
To consume the Retriever Microservice, you can generate a mock embedding vector of length 512 with Python. | ||
|
||
```bash | ||
your_embedding=$(python -c "import random; embedding = [random.uniform(-1, 1) for _ in range(512)]; print(embedding)") | ||
curl http://${your_ip}:7000/v1/multimodal_retrieval \ | ||
-X POST \ | ||
-d "{\"text\":\"What is the revenue of Nike in 2023?\",\"embedding\":${your_embedding}}" \ | ||
-H 'Content-Type: application/json' | ||
``` | ||
|
||
You can set the parameters for the retriever. | ||
|
||
```bash | ||
your_embedding=$(python -c "import random; embedding = [random.uniform(-1, 1) for _ in range(512)]; print(embedding)") | ||
curl http://localhost:7000/v1/multimodal_retrieval \ | ||
-X POST \ | ||
-d "{\"text\":\"What is the revenue of Nike in 2023?\",\"embedding\":${your_embedding},\"search_type\":\"similarity\", \"k\":4}" \ | ||
-H 'Content-Type: application/json' | ||
``` | ||
|
||
```bash | ||
your_embedding=$(python -c "import random; embedding = [random.uniform(-1, 1) for _ in range(512)]; print(embedding)") | ||
curl http://localhost:7000/v1/multimodal_retrieval \ | ||
-X POST \ | ||
-d "{\"text\":\"What is the revenue of Nike in 2023?\",\"embedding\":${your_embedding},\"search_type\":\"similarity_distance_threshold\", \"k\":4, \"distance_threshold\":1.0}" \ | ||
-H 'Content-Type: application/json' | ||
``` | ||
|
||
```bash | ||
your_embedding=$(python -c "import random; embedding = [random.uniform(-1, 1) for _ in range(512)]; print(embedding)") | ||
curl http://localhost:7000/v1/multimodal_retrieval \ | ||
-X POST \ | ||
-d "{\"text\":\"What is the revenue of Nike in 2023?\",\"embedding\":${your_embedding},\"search_type\":\"similarity_score_threshold\", \"k\":4, \"score_threshold\":0.2}" \ | ||
-H 'Content-Type: application/json' | ||
``` | ||
|
||
```bash | ||
your_embedding=$(python -c "import random; embedding = [random.uniform(-1, 1) for _ in range(512)]; print(embedding)") | ||
curl http://localhost:7000/v1/multimodal_retrieval \ | ||
-X POST \ | ||
-d "{\"text\":\"What is the revenue of Nike in 2023?\",\"embedding\":${your_embedding},\"search_type\":\"mmr\", \"k\":4, \"fetch_k\":20, \"lambda_mult\":0.5}" \ | ||
-H 'Content-Type: application/json' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 |
29 changes: 29 additions & 0 deletions
29
comps/retrievers/langchain/redis_multimodal/docker/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
FROM langchain/langchain:latest | ||
|
||
ARG ARCH="cpu" | ||
|
||
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \ | ||
libgl1-mesa-glx \ | ||
libjemalloc-dev \ | ||
vim | ||
|
||
RUN useradd -m -s /bin/bash user && \ | ||
mkdir -p /home/user && \ | ||
chown -R user /home/user/ | ||
|
||
COPY comps /home/user/comps | ||
|
||
USER user | ||
|
||
RUN pip install --no-cache-dir --upgrade pip && \ | ||
if [ ${ARCH} = "cpu" ]; then pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu; fi && \ | ||
pip install --no-cache-dir -r /home/user/comps/retrievers/langchain/redis_multimodal/requirements.txt | ||
|
||
ENV PYTHONPATH=$PYTHONPATH:/home/user | ||
|
||
WORKDIR /home/user/comps/retrievers/langchain/redis_multimodal | ||
|
||
ENTRYPOINT ["python", "retriever_redis.py"] |
23 changes: 23 additions & 0 deletions
23
comps/retrievers/langchain/redis_multimodal/docker/docker_compose_retriever.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
version: "1.0" | ||
|
||
services: | ||
retriever: | ||
image: opea/multimodal-retriever-redis:latest | ||
container_name: multimodal-retriever-redis-server | ||
ports: | ||
- "7000:7000" | ||
ipc: host | ||
environment: | ||
no_proxy: ${no_proxy} | ||
http_proxy: ${http_proxy} | ||
https_proxy: ${https_proxy} | ||
REDIS_URL: ${REDIS_URL} | ||
INDEX_NAME: ${INDEX_NAME} | ||
restart: unless-stopped | ||
|
||
networks: | ||
default: | ||
driver: bridge |
77 changes: 77 additions & 0 deletions
77
comps/retrievers/langchain/redis_multimodal/multimodal_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
|
||
current_file_path = os.path.abspath(__file__) | ||
parent_dir = os.path.dirname(current_file_path) | ||
|
||
|
||
def get_boolean_env_var(var_name, default_value=False): | ||
"""Retrieve the boolean value of an environment variable. | ||
Args: | ||
var_name (str): The name of the environment variable to retrieve. | ||
default_value (bool): The default value to return if the variable | ||
is not found. | ||
Returns: | ||
bool: The value of the environment variable, interpreted as a boolean. | ||
""" | ||
true_values = {"true", "1", "t", "y", "yes"} | ||
false_values = {"false", "0", "f", "n", "no"} | ||
|
||
# Retrieve the environment variable's value | ||
value = os.getenv(var_name, "").lower() | ||
|
||
# Decide the boolean value based on the content of the string | ||
if value in true_values: | ||
return True | ||
elif value in false_values: | ||
return False | ||
else: | ||
return default_value | ||
|
||
|
||
# Check for openai API key | ||
# if "OPENAI_API_KEY" not in os.environ: | ||
# raise Exception("Must provide an OPENAI_API_KEY as an env var.") | ||
|
||
|
||
# Whether or not to enable langchain debugging | ||
DEBUG = get_boolean_env_var("DEBUG", False) | ||
# Set DEBUG env var to "true" if you wish to enable LC debugging module | ||
if DEBUG: | ||
import langchain | ||
|
||
langchain.debug = True | ||
|
||
|
||
# Embedding model | ||
EMBED_MODEL = os.getenv("EMBED_MODEL", "BridgeTower/bridgetower-large-itm-mlm-itc") | ||
|
||
# Redis Connection Information | ||
REDIS_HOST = os.getenv("REDIS_HOST", "localhost") | ||
REDIS_PORT = int(os.getenv("REDIS_PORT", 6379)) | ||
|
||
|
||
def format_redis_conn_from_env(): | ||
redis_url = os.getenv("REDIS_URL", None) | ||
if redis_url: | ||
return redis_url | ||
else: | ||
using_ssl = get_boolean_env_var("REDIS_SSL", False) | ||
start = "rediss://" if using_ssl else "redis://" | ||
|
||
# if using RBAC | ||
password = os.getenv("REDIS_PASSWORD", None) | ||
username = os.getenv("REDIS_USERNAME", "default") | ||
if password is not None: | ||
start += f"{username}:{password}@" | ||
|
||
return start + f"{REDIS_HOST}:{REDIS_PORT}" | ||
|
||
|
||
REDIS_URL = format_redis_conn_from_env() | ||
|
||
# Vector Index Configuration | ||
INDEX_NAME = os.getenv("INDEX_NAME", "test-index") |
11 changes: 11 additions & 0 deletions
11
comps/retrievers/langchain/redis_multimodal/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
docarray[full] | ||
fastapi | ||
langchain_community | ||
opentelemetry-api | ||
opentelemetry-exporter-otlp | ||
opentelemetry-sdk | ||
prometheus-fastapi-instrumentator | ||
redis | ||
shortuuid | ||
transformers | ||
uvicorn |
Oops, something went wrong.