Skip to content

Instantly share code, notes, and snippets.

@ageek
Last active September 19, 2025 17:09
Show Gist options
  • Save ageek/db4cd1f3132182352e5370e78cd8113b to your computer and use it in GitHub Desktop.
Save ageek/db4cd1f3132182352e5370e78cd8113b to your computer and use it in GitHub Desktop.
Langchain based Wikipeida Search RAG Pipeline docker container
# **Minimal Docker Guide: LangChain RAG to Container**
Follow these exact steps to create a working Docker image from your existing LangChain script.
## **Step 1: Prepare Your Directory**
```bash
# Create new directory for containerization
mkdir langserve-docker
cd langserve-docker
```
## **Step 2: Copy and Update Your Script**
Copy your existing script to this directory and make minimal changes:
```python
# your_script_name.py (keep your original name)
import os
from fastapi import FastAPI
from langserve import add_routes
# Your existing imports (unchanged)
from langchain_sambanova import ChatSambaNovaCloud
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_community.retrievers import WikipediaRetriever
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set User-Agent
os.environ["USER_AGENT"] = "LangchainApp/1.0"
# Validate API key
api_key = os.getenv("SAMBANOVA_API_KEY")
if not api_key:
raise ValueError("SAMBANOVA_API_KEY environment variable is not set.")
# ADD THIS LINE - Create FastAPI app
app = FastAPI()
# Your existing code (unchanged)
my_llm = ChatSambaNovaCloud(
sambanova_api_key=api_key,
model="DeepSeek-V3-0324",
max_tokens=1024,
temperature=0.0,
top_p=0.01,
)
retriever = WikipediaRetriever()
prompt = ChatPromptTemplate.from_template(
"""
Answer the question based only on the context provided.
Context: {context}
Question: {question}
Note: Give the best possible answer based on the context ONLY.
"""
)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| my_llm
| StrOutputParser()
)
# ADD THIS LINE - Expose chain as REST API
add_routes(app, chain, path="/rag")
# ADD THIS BLOCK - Server runner
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
```
**Changes Made**: Only 4 additions to your existing code:
1. `from fastapi import FastAPI`
2. `from langserve import add_routes`
3. `app = FastAPI()`
4. `add_routes(app, chain, path="/rag")`
5. Server runner block
## **Step 3: Create requirements.txt**
```text
langchain==0.2.16
langchain-core==0.2.38
langchain-community==0.2.16
langchain-sambanova==0.1.9
langserve[all]==0.2.6
fastapi==0.104.1
uvicorn==0.24.0
python-dotenv==1.0.1
wikipedia==1.4.0
beautifulsoup4==4.12.3
requests==2.32.3
```
## **Step 4: Create .env File**
```bash
# .env
SAMBANOVA_API_KEY=your_actual_sambanova_api_key_here
```
## **Step 5: Create Dockerfile**
**Replace `your_script_name.py` with your actual script filename:**
```dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY your_script_name.py .
COPY .env .
EXPOSE 8000
CMD ["uvicorn", "your_script_name:app", "--host", "0.0.0.0", "--port", "8000"]
```
**Example**: If your script is `sambanova_rag.py`, the CMD line becomes:
```dockerfile
CMD ["uvicorn", "sambanova_rag:app", "--host", "0.0.0.0", "--port", "8000"]
```
## **Step 6: Build Docker Image**
```bash
# Build the image
docker build -t my-rag-api .
# Check if image was created
docker images
```
## **Step 7: Run Docker Container**
```bash
# Run container
docker run -d -p 8000:8000 --name rag-container my-rag-api
# Check if container is running
docker ps
```
## **Step 8: Test Your API**
```bash
# Test the health of container
curl http://localhost:8000/docs
# Test your RAG endpoint
curl -X POST "http://localhost:8000/rag/invoke" \
-H "Content-Type: application/json" \
-d '"When was the Taj Mahal built?"'
# Check container logs if needed
docker logs rag-container
```
## **Step 9: Access Your API**
**Web Interfaces:**
- API Documentation: `http://localhost:8000/docs`
- Interactive Testing: `http://localhost:8000/rag/playground`
**Endpoints:**
- `POST /rag/invoke` - Single question
- `POST /rag/batch` - Multiple questions
- `POST /rag/stream` - Streaming response
## **Your Final Directory Structure**
```
langserve-docker/
├── your_script_name.py # Your updated script
├── requirements.txt # Dependencies
├── .env # API keys
├── Dockerfile # Container definition
```
## **Quick Commands Reference**
```bash
# Build
docker build -t my-rag-api .
# Run
docker run -d -p 8000:8000 --name rag-container my-rag-api
# Test
curl -X POST "http://localhost:8000/rag/invoke" -H "Content-Type: application/json" -d '"Your question here"'
# Stop
docker stop rag-container
# Remove
docker rm rag-container
# View logs
docker logs rag-container
```
**That's it!** Your LangChain RAG script is now containerized and running as a REST API with just 4 code additions and 3 new files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment