|
| 1 | +# LangChain + Llama Stack Document Processing |
| 2 | + |
| 3 | +This repository contains two different implementations of document processing using LangChain and Llama Stack: |
| 4 | + |
| 5 | +1. **`langchain_llamastack.py`** - Interactive CLI version |
| 6 | +2. **`langchain_llamastack_ray.py`** - Ray Serve API version |
| 7 | + |
| 8 | +Both versions provide AI-powered document processing capabilities including summarization, fact extraction, and question-answering. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 📋 Prerequisites |
| 13 | + |
| 14 | +### System Requirements |
| 15 | +- Python 3.12+ |
| 16 | +- Ray Serve (for API version) |
| 17 | +- Llama Stack server running on `http://localhost:8321/` |
| 18 | +- Ollama or compatible model server |
| 19 | + |
| 20 | +### Required Python Packages |
| 21 | +```bash |
| 22 | +pip install llama-stack-client langchain langchain-core langchain-community |
| 23 | +pip install beautifulsoup4 markdownify readability-lxml requests |
| 24 | +pip install ray[serve] starlette # For Ray Serve version only |
| 25 | +``` |
| 26 | + |
| 27 | +### Environment Setup |
| 28 | +```bash |
| 29 | +# Create and activate virtual environment |
| 30 | +python3.12 -m venv llama-env-py312 |
| 31 | +source llama-env-py312/bin/activate |
| 32 | + |
| 33 | +# Install dependencies |
| 34 | +pip install llama-stack-client langchain langchain-core langchain-community beautifulsoup4 markdownify readability-lxml requests ray[serve] starlette |
| 35 | +``` |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## 🚀 Quick Start |
| 40 | + |
| 41 | +### Start Llama Stack Server |
| 42 | +Before running either version, ensure your Llama Stack server is running: |
| 43 | +```bash |
| 44 | +# Start Llama Stack server (example) |
| 45 | +llama stack run your-config --port 8321 |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## 📖 Option 1: Interactive CLI Version (`langchain_llamastack_updated.py`) |
| 51 | + |
| 52 | +### Features |
| 53 | +- ✅ Interactive command-line interface |
| 54 | +- ✅ Document loading from URLs and PDFs |
| 55 | +- ✅ AI-powered summarization and fact extraction |
| 56 | +- ✅ Question-answering based on document content |
| 57 | +- ✅ Session-based document storage |
| 58 | + |
| 59 | +### How to Run |
| 60 | +```bash |
| 61 | +# Activate environment |
| 62 | +source llama-env-py312/bin/activate |
| 63 | + |
| 64 | +# Run the interactive CLI |
| 65 | +cd /home/omara/langchain_llamastack |
| 66 | +python langchain_llamastack_updated.py |
| 67 | +``` |
| 68 | + |
| 69 | +### Usage Commands |
| 70 | +Once running, you can use these interactive commands: |
| 71 | + |
| 72 | +``` |
| 73 | +🎯 Interactive Document Processing Demo |
| 74 | +Commands: |
| 75 | + load <url_or_path> - Process a document |
| 76 | + ask <question> - Ask about the document |
| 77 | + summary - Show document summary |
| 78 | + facts - Show extracted facts |
| 79 | + help - Show commands |
| 80 | + quit - Exit demo |
| 81 | +``` |
| 82 | + |
| 83 | +### Example Session |
| 84 | +``` |
| 85 | +> load https://en.wikipedia.org/wiki/Artificial_intelligence |
| 86 | +📄 Loading document from: https://en.wikipedia.org/wiki/Artificial_intelligence |
| 87 | +✅ Loaded 45,832 characters |
| 88 | +📝 Generating summary... |
| 89 | +🔍 Extracting key facts... |
| 90 | +✅ Processing complete! |
| 91 | +
|
| 92 | +> summary |
| 93 | +📝 Summary: |
| 94 | +Artificial intelligence (AI) is the simulation of human intelligence... |
| 95 | +
|
| 96 | +> ask What are the main types of AI? |
| 97 | +💬 Q: What are the main types of AI? |
| 98 | +📝 A: Based on the document, the main types of AI include... |
| 99 | +
|
| 100 | +> facts |
| 101 | +🔍 Key Facts: |
| 102 | +- AI was founded as an academic discipline in 1956 |
| 103 | +- Machine learning is a subset of AI... |
| 104 | +
|
| 105 | +> quit |
| 106 | +👋 Thanks for exploring LangChain chains! |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 🌐 Option 2: Ray Serve API Version (`langchain_llamastack_ray.py`) |
| 112 | + |
| 113 | +### Features |
| 114 | +- ✅ RESTful HTTP API |
| 115 | +- ✅ Persistent service (runs indefinitely) |
| 116 | +- ✅ Multiple endpoints for different operations |
| 117 | +- ✅ JSON request/response format |
| 118 | +- ✅ Concurrent request handling |
| 119 | + |
| 120 | +### How to Run |
| 121 | +```bash |
| 122 | +# Activate environment |
| 123 | +source llama-env-py312/bin/activate |
| 124 | + |
| 125 | +# Start the Ray Serve API |
| 126 | +cd /home/omara/langchain_llamastack |
| 127 | +python langchain_llamastack_ray.py |
| 128 | +``` |
| 129 | + |
| 130 | +### Service Endpoints |
| 131 | + |
| 132 | +| Method | Endpoint | Description | Parameters | |
| 133 | +|--------|----------|-------------|------------| |
| 134 | +| GET | `/` | Service status | None | |
| 135 | +| POST | `/process` | Process document | `{"source": "url_or_path"}` | |
| 136 | +| POST | `/ask` | Ask question | `{"question": "text", "source": "optional"}` | |
| 137 | +| GET | `/summary` | Get summary | `?source=url` (optional) | |
| 138 | +| GET | `/facts` | Get facts | `?source=url` (optional) | |
| 139 | +| GET | `/docs` | List documents | None | |
| 140 | + |
| 141 | +### API Usage Examples |
| 142 | + |
| 143 | +#### Using curl: |
| 144 | +```bash |
| 145 | +# Check service status |
| 146 | +curl http://localhost:8000/ |
| 147 | + |
| 148 | +# Process a document |
| 149 | +curl -X POST http://localhost:8000/process \ |
| 150 | + -H 'Content-Type: application/json' \ |
| 151 | + -d '{"source": "https://en.wikipedia.org/wiki/Machine_learning"}' |
| 152 | + |
| 153 | +# Ask a question |
| 154 | +curl -X POST http://localhost:8000/ask \ |
| 155 | + -H 'Content-Type: application/json' \ |
| 156 | + -d '{"question": "What is machine learning?"}' |
| 157 | + |
| 158 | +# Get summary |
| 159 | +curl http://localhost:8000/summary |
| 160 | + |
| 161 | +# Get facts |
| 162 | +curl http://localhost:8000/facts |
| 163 | + |
| 164 | +# List all processed documents |
| 165 | +curl http://localhost:8000/docs |
| 166 | +``` |
| 167 | + |
| 168 | +#### Using Python requests: |
| 169 | +```python |
| 170 | +import requests |
| 171 | + |
| 172 | +# Process a document |
| 173 | +response = requests.post( |
| 174 | + "http://localhost:8000/process", |
| 175 | + json={"source": "https://en.wikipedia.org/wiki/Deep_learning"} |
| 176 | +) |
| 177 | +print(response.json()) |
| 178 | + |
| 179 | +# Ask a question |
| 180 | +response = requests.post( |
| 181 | + "http://localhost:8000/ask", |
| 182 | + json={"question": "What are neural networks?"} |
| 183 | +) |
| 184 | +print(response.json()) |
| 185 | + |
| 186 | +# Get facts |
| 187 | +response = requests.get("http://localhost:8000/facts") |
| 188 | +print(response.json()) |
| 189 | +``` |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## 🔧 Configuration |
| 194 | + |
| 195 | +### Model Configuration |
| 196 | +Both versions use these models by default: |
| 197 | +- **Model ID**: `llama3.2:3b` |
| 198 | +- **Llama Stack URL**: `http://localhost:8321/` |
| 199 | + |
| 200 | +To change the model, edit the `model_id` parameter in the respective files. |
| 201 | + |
| 202 | +### Supported Document Types |
| 203 | +- ✅ **URLs**: Any web page (extracted using readability) |
| 204 | +- ✅ **PDF files**: Local or remote PDF documents |
| 205 | +- ❌ Plain text files (can be added if needed) |
| 206 | + |
| 207 | +--- |
| 208 | + |
| 209 | +## 🛠️ Troubleshooting |
| 210 | + |
| 211 | +### Common Issues |
| 212 | + |
| 213 | +#### 1. Connection Refused to Llama Stack |
| 214 | +**Error**: `Connection refused to http://localhost:8321/` |
| 215 | +**Solution**: |
| 216 | +- Ensure Llama Stack server is running |
| 217 | +- Check if port 8321 is correct |
| 218 | +- Verify network connectivity |
| 219 | + |
| 220 | +#### 2. Model Not Found |
| 221 | +**Error**: `Model not found: llama3.2:3b` |
| 222 | +**Solution**: |
| 223 | +- Check available models: `curl http://localhost:8321/models/list` |
| 224 | +- Update `model_id` in the code to match available models |
| 225 | + |
| 226 | +#### 3. Ray Serve Port Already in Use |
| 227 | +**Error**: `Port 8000 already in use` |
| 228 | +**Solution**: |
| 229 | +```bash |
| 230 | +# Kill process using port 8000 |
| 231 | +lsof -ti :8000 | xargs kill -9 |
| 232 | + |
| 233 | +# Or use a different port by modifying the code |
| 234 | +``` |
| 235 | + |
| 236 | +#### 4. Missing Dependencies |
| 237 | +**Error**: `ModuleNotFoundError: No module named 'ray'` |
| 238 | +**Solution**: |
| 239 | +```bash |
| 240 | +pip install ray[serve] starlette |
| 241 | +``` |
| 242 | + |
| 243 | +### Debug Mode |
| 244 | +To enable verbose logging, add this to the beginning of either file: |
| 245 | +```python |
| 246 | +import logging |
| 247 | +logging.basicConfig(level=logging.DEBUG) |
| 248 | +``` |
| 249 | + |
| 250 | +--- |
| 251 | + |
| 252 | +## 📊 Performance Notes |
| 253 | + |
| 254 | +### CLI Version |
| 255 | +- **Pros**: Simple to use, interactive, good for testing |
| 256 | +- **Cons**: Single-threaded, session-based only |
| 257 | +- **Best for**: Development, testing, manual document analysis |
| 258 | + |
| 259 | +### Ray Serve Version |
| 260 | +- **Pros**: Concurrent requests, persistent service, API integration |
| 261 | +- **Cons**: More complex setup, requires Ray |
| 262 | +- **Best for**: Production, integration with other services, high throughput |
| 263 | + |
| 264 | +--- |
| 265 | + |
| 266 | +## 🛑 Stopping Services |
| 267 | + |
| 268 | +### CLI Version |
| 269 | +- Press `Ctrl+C` or type `quit` in the interactive prompt |
| 270 | + |
| 271 | +### Ray Serve Version |
| 272 | +- Press `Ctrl+C` in the terminal running the service |
| 273 | +- The service will gracefully shutdown and clean up resources |
| 274 | + |
| 275 | +--- |
| 276 | + |
| 277 | +## 📝 Examples |
| 278 | + |
| 279 | +### CLI Workflow |
| 280 | +1. Start: `python langchain_llamastack_updated.py` |
| 281 | +2. Load document: `load https://arxiv.org/pdf/2103.00020.pdf` |
| 282 | +3. Get summary: `summary` |
| 283 | +4. Ask questions: `ask What are the main contributions?` |
| 284 | +5. Exit: `quit` |
| 285 | + |
| 286 | +### API Workflow |
| 287 | +1. Start: `python langchain_llamastack_ray.py` |
| 288 | +2. Process: `curl -X POST http://localhost:8000/process -d '{"source": "https://example.com"}'` |
| 289 | +3. Query: `curl -X POST http://localhost:8000/ask -d '{"question": "What is this about?"}'` |
| 290 | +4. Stop: `Ctrl+C` |
| 291 | + |
| 292 | +--- |
| 293 | + |
| 294 | +## 🤝 Contributing |
| 295 | + |
| 296 | +To extend functionality: |
| 297 | +1. Add new prompt templates for different analysis types |
| 298 | +2. Support additional document formats |
| 299 | +3. Add caching for processed documents |
| 300 | +4. Implement user authentication for API version |
| 301 | + |
| 302 | +--- |
| 303 | + |
| 304 | +## 📜 License |
| 305 | + |
| 306 | +This project is for educational and research purposes. |
0 commit comments