|
| 1 | +<p align="center" class="flex items-center gap-1 justify-center flex-wrap"> |
| 2 | + <img src="../../assets/gcp-logo.svg?raw=true" alt="GCP Logo" height="20" width="20"> |
| 3 | + <a href="https://pathway.com/developers/user-guide/deployment/gcp-deploy">Deploy with GCP</a> | |
| 4 | + <img src="../../assets/aws-fargate-logo.svg?raw=true" alt="AWS Logo" height="20" width="20"> |
| 5 | + <a href="https://pathway.com/developers/user-guide/deployment/aws-fargate-deploy">Deploy with AWS</a> | |
| 6 | + <img src="../../assets/azure-logo.svg?raw=true" alt="Azure Logo" height="20" width="20"> |
| 7 | + <a href="https://pathway.com/developers/user-guide/deployment/azure-aci-deploy">Deploy with Azure</a> | |
| 8 | + <img src="../../assets/render.png?raw=true" alt="Render Logo" height="20" width="20"> |
| 9 | + <a href="https://pathway.com/developers/user-guide/deployment/render-deploy"> Deploy with Render </a> |
| 10 | +</p> |
| 11 | + |
| 12 | +# Video RAG with Pathway Live Data Framework and TwelveLabs |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +This app template shows how to build a RAG application **over video** using the |
| 17 | +Pathway Live Data Framework together with [TwelveLabs](https://twelvelabs.io). |
| 18 | + |
| 19 | +Videos are notoriously hard to put into a RAG pipeline: most stacks first |
| 20 | +transcribe the audio and throw away everything that happens on screen. This |
| 21 | +template indexes the *whole* video instead, using two TwelveLabs models: |
| 22 | + |
| 23 | +- **[Pegasus](https://docs.twelvelabs.io/docs/concepts/models/pegasus)** — a |
| 24 | + video-understanding model that turns each video into a rich text description |
| 25 | + (what happens, who and what appears, the setting, on-screen and spoken text, |
| 26 | + the overall topic). Pathway indexes that text exactly like it would index a PDF. |
| 27 | +- **[Marengo](https://docs.twelvelabs.io/docs/concepts/models/marengo)** — a |
| 28 | + multimodal embedding model that produces 512-dimensional vectors in a shared |
| 29 | + space for text, image, audio and video. It is used here as the retriever |
| 30 | + embedder. |
| 31 | + |
| 32 | +Because the template uses the standard Pathway `DocumentStore` and |
| 33 | +`BaseRAGQuestionAnswerer`, everything else works out of the box: live sync with |
| 34 | +your data source, the in-memory vector index, caching, and the HTTP API. Drop a |
| 35 | +new video into the connected folder and it becomes queryable automatically. |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +``` |
| 40 | +video files ─▶ pw.io.fs.read (bytes) |
| 41 | + ─▶ TwelveLabsVideoParser (Pegasus: video → text) |
| 42 | + ─▶ TokenCountSplitter (chunking) |
| 43 | + ─▶ DocumentStore + UsearchKnnFactory |
| 44 | + with MarengoEmbedder (text → 512-dim vectors) |
| 45 | + ─▶ BaseRAGQuestionAnswerer (OpenAI LLM over retrieved context) |
| 46 | + ─▶ REST API on :8000 |
| 47 | +``` |
| 48 | + |
| 49 | +The TwelveLabs components live in the local |
| 50 | +[`pathway_twelvelabs`](pathway_twelvelabs/__init__.py) package: |
| 51 | + |
| 52 | +- `TwelveLabsVideoParser` — a Pathway parser (`pw.UDF`) that uploads the video |
| 53 | + bytes as a TwelveLabs asset and asks Pegasus to describe it. |
| 54 | +- `MarengoEmbedder` — a Pathway embedder (`BaseEmbedder`) that calls the Marengo |
| 55 | + embedding endpoint. |
| 56 | + |
| 57 | +Both are wired in entirely through [`app.yaml`](app.yaml), so you can swap models, |
| 58 | +prompts, the data source, or the LLM without touching any Python. |
| 59 | + |
| 60 | +## Customizing the pipeline |
| 61 | + |
| 62 | +- **Change what is extracted from the video.** Set the `prompt` field of |
| 63 | + `$parser` in `app.yaml`, e.g. `"Describe this video, focusing on the products |
| 64 | + that appear and any prices shown."` |
| 65 | +- **Change the data source.** Replace the `!pw.io.fs.read` source with the Google |
| 66 | + Drive, SharePoint, or S3 connector (a commented Google Drive example is |
| 67 | + included in `app.yaml`). |
| 68 | +- **Change the answering LLM.** Edit the `$llm` block — any |
| 69 | + [Pathway LLM wrapper](https://pathway.com/developers/api-docs/pathway-xpacks-llm/llms) |
| 70 | + works. |
| 71 | + |
| 72 | +## Running the app |
| 73 | + |
| 74 | +### Prerequisites |
| 75 | + |
| 76 | +- A TwelveLabs API key. Get a free one at [twelvelabs.io](https://twelvelabs.io) — |
| 77 | + there is a generous free tier. |
| 78 | +- An OpenAI API key for the question-answering LLM. |
| 79 | + |
| 80 | +Copy `.env.example` to `.env` and fill in your keys: |
| 81 | + |
| 82 | +```bash |
| 83 | +cp .env.example .env |
| 84 | +# edit .env and set TWELVELABS_API_KEY and OPENAI_API_KEY |
| 85 | +``` |
| 86 | + |
| 87 | +Put one or more videos (e.g. `.mp4`, `.mov`) into the `data/` directory. |
| 88 | + |
| 89 | +### With Docker |
| 90 | + |
| 91 | +```bash |
| 92 | +docker build -t video-rag-twelvelabs . |
| 93 | +docker run -v $(pwd)/data:/app/data --env-file .env -p 8000:8000 video-rag-twelvelabs |
| 94 | +``` |
| 95 | + |
| 96 | +### Locally |
| 97 | + |
| 98 | +```bash |
| 99 | +pip install -r requirements.txt |
| 100 | +python app.py |
| 101 | +``` |
| 102 | + |
| 103 | +### Querying |
| 104 | + |
| 105 | +Once the server is up, ask questions about your videos: |
| 106 | + |
| 107 | +```bash |
| 108 | +curl -X POST http://localhost:8000/v1/pw_ai_answer \ |
| 109 | + -H "Content-Type: application/json" \ |
| 110 | + -d '{"prompt": "What products are shown in the videos?"}' |
| 111 | +``` |
| 112 | + |
| 113 | +## Tests |
| 114 | + |
| 115 | +A small test suite lives in [`test_twelvelabs.py`](test_twelvelabs.py). The |
| 116 | +no-network tests run without any credentials; the live Marengo embedding test is |
| 117 | +skipped unless `TWELVELABS_API_KEY` is set: |
| 118 | + |
| 119 | +```bash |
| 120 | +pip install -r requirements.txt pytest |
| 121 | +TWELVELABS_API_KEY=... pytest templates/video_rag_twelvelabs/test_twelvelabs.py |
| 122 | +``` |
0 commit comments