Skip to content

Commit 3a63526

Browse files
committed
Add google colab support
1 parent 8a2d671 commit 3a63526

3 files changed

Lines changed: 204 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ resynth-api
8888
# Interactive docs at http://localhost:8000/docs
8989
```
9090

91+
## Google Colab Usage
92+
93+
ReSynth works seamlessly in Google Colab without .env files:
94+
95+
### Quick Start
96+
```python
97+
import resynth
98+
99+
# Auto-configure for Colab
100+
resynth.Config.setup_for_colab()
101+
102+
# Initialize and use
103+
agent = resynth.ReSynthAgent()
104+
papers = agent.search_and_process("machine learning", max_papers=3)
105+
answer = agent.query("What is deep learning?")
106+
```
107+
108+
### With OpenAI API Key
109+
```python
110+
import resynth
111+
112+
# Setup with API key
113+
resynth.Config.setup_for_colab(openai_api_key="your-key-here")
114+
115+
agent = resynth.ReSynthAgent()
116+
# Better answers with OpenAI
117+
```
118+
119+
**See `examples/colab_setup.ipynb` for complete Colab examples.**
120+
91121
## Requirements
92122

93123
- Python 3.8+

‎examples/colab_setup.ipynb‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# ReSynth on Google Colab Setup
2+
3+
This notebook demonstrates how to use ReSynth in Google Colab without .env files.
4+
5+
## Installation
6+
```bash
7+
# Install ReSynth
8+
!pip install resynth
9+
10+
# Install additional dependencies
11+
!pip install sentence-transformers chromadb openai
12+
```
13+
14+
## Basic Setup (No API Key Required)
15+
```python
16+
import resynth
17+
18+
# Setup for Colab automatically
19+
resynth.Config.setup_for_colab()
20+
21+
# Initialize agent
22+
agent = resynth.ReSynthAgent()
23+
24+
# Search and process papers
25+
papers = agent.search_and_process("machine learning interpretability", max_papers=3)
26+
print(f"Processed {len(papers)} papers")
27+
28+
# Query the papers
29+
answer = agent.query("What are the main challenges in ML interpretability?")
30+
print(answer.answer)
31+
```
32+
33+
## Setup with OpenAI API Key (Recommended)
34+
```python
35+
import os
36+
37+
# Set your OpenAI API key
38+
os.environ["OPENAI_API_KEY"] = "your-openai-api-key-here"
39+
40+
# Setup for Colab with API key
41+
resynth.Config.setup_for_colab(openai_api_key="your-openai-api-key-here")
42+
43+
# Initialize agent
44+
agent = resynth.ReSynthAgent()
45+
46+
# Search and process papers
47+
papers = agent.search_and_process("transformer architectures", max_papers=5)
48+
49+
# Query with better answers
50+
answer = agent.query("How do attention mechanisms work in transformers?")
51+
print(f"Answer: {answer.answer}")
52+
print(f"Confidence: {answer.confidence_score:.2f}")
53+
print(f"References:\n{answer.bibliography}")
54+
```
55+
56+
## Manual Configuration
57+
```python
58+
import resynth
59+
import os
60+
61+
# Manually set configuration for Colab
62+
os.environ["CHROMA_PERSIST_DIRECTORY"] = "/content/chroma_db"
63+
os.environ["PAPER_DOWNLOAD_DIR"] = "/content/papers"
64+
os.environ["CHUNK_SIZE"] = "1000"
65+
os.environ["CHUNK_OVERLAP"] = "200"
66+
67+
# Optional: Set OpenAI API key
68+
# os.environ["OPENAI_API_KEY"] = "your-key-here"
69+
70+
# Initialize and use
71+
agent = resynth.ReSynthAgent()
72+
```
73+
74+
## Check Configuration
75+
```python
76+
# Validate current configuration
77+
issues = resynth.Config.validate()
78+
if issues:
79+
print("Configuration issues:")
80+
for issue in issues:
81+
print(f"- {issue}")
82+
else:
83+
print("Configuration is valid!")
84+
85+
# Check where data will be stored
86+
print(f"Database: {resynth.Config.CHROMA_PERSIST_DIRECTORY}")
87+
print(f"Papers: {resynth.Config.PAPER_DOWNLOAD_DIR}")
88+
```
89+
90+
## Web Interface in Colab
91+
```python
92+
# Start web interface (if needed)
93+
# Note: This may not work in all Colab environments
94+
import subprocess
95+
import threading
96+
97+
def start_web():
98+
subprocess.run(["resynth", "web"])
99+
100+
# Start in background
101+
web_thread = threading.Thread(target=start_web)
102+
web_thread.start()
103+
104+
print("Web interface starting...")
105+
print("Note: Web interface may not be accessible in all Colab environments")
106+
```
107+
108+
## Tips for Colab Usage
109+
110+
1. **Storage**: All data is saved to `/content/` which persists during your session
111+
2. **API Keys**: Use environment variables, never hardcode keys in notebooks
112+
3. **Session Persistence**: Data is lost when Colab session ends
113+
4. **Performance**: Start with small numbers of papers (3-5) to test
114+
5. **Memory**: Large datasets may exceed Colab memory limits
115+
116+
## Download Results
117+
```python
118+
# To download your processed data
119+
from google.colab import files
120+
import zipfile
121+
import os
122+
123+
# Create zip of database
124+
with zipfile.ZipFile('resynth_data.zip', 'w') as zipf:
125+
for root, dirs, files in os.walk('/content/chroma_db'):
126+
for file in files:
127+
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), '/content'))
128+
129+
# Download the zip
130+
files.download('resynth_data.zip')
131+
```

‎src/resynth/config.py‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import os
66
from dotenv import load_dotenv
77

8-
load_dotenv()
8+
# Try to load .env, but don't fail if it doesn't exist (for Colab/cloud environments)
9+
load_dotenv(override=False)
910

1011
class Config:
1112
# API Keys
@@ -14,13 +15,20 @@ class Config:
1415
# Model Settings
1516
HUGGINGFACE_MODEL_NAME = os.getenv("HUGGINGFACE_MODEL_NAME", "sentence-transformers/all-MiniLM-L6-v2")
1617

17-
# Vector Database
18-
CHROMA_PERSIST_DIRECTORY = os.getenv("CHROMA_PERSIST_DIRECTORY", "./chroma_db")
18+
# Vector Database - Use /content for Colab, ./ for local
19+
if os.path.exists("/content"):
20+
# Google Colab environment
21+
CHROMA_PERSIST_DIRECTORY = os.getenv("CHROMA_PERSIST_DIRECTORY", "/content/chroma_db")
22+
PAPER_DOWNLOAD_DIR = os.getenv("PAPER_DOWNLOAD_DIR", "/content/papers")
23+
else:
24+
# Local/other environments
25+
CHROMA_PERSIST_DIRECTORY = os.getenv("CHROMA_PERSIST_DIRECTORY", "./chroma_db")
26+
PAPER_DOWNLOAD_DIR = os.getenv("PAPER_DOWNLOAD_DIR", "./papers")
27+
1928
CHROMA_COLLECTION_NAME = os.getenv("CHROMA_COLLECTION_NAME", "research_papers")
2029

2130
# Paper Fetching
2231
MAX_PAPERS_PER_QUERY = int(os.getenv("MAX_PAPERS_PER_QUERY", "10"))
23-
PAPER_DOWNLOAD_DIR = os.getenv("PAPER_DOWNLOAD_DIR", "./papers")
2432

2533
# API Settings
2634
API_HOST = os.getenv("API_HOST", "0.0.0.0")
@@ -34,12 +42,38 @@ class Config:
3442
TOP_K_RETRIEVAL = int(os.getenv("TOP_K_RETRIEVAL", "5"))
3543
SIMILARITY_THRESHOLD = float(os.getenv("SIMILARITY_THRESHOLD", "0.7"))
3644

45+
@classmethod
46+
def setup_for_colab(cls, openai_api_key=None):
47+
"""Helper function to setup ReSynth for Google Colab"""
48+
if os.path.exists("/content"):
49+
print("Setting up ReSynth for Google Colab...")
50+
51+
# Set environment variables for Colab
52+
os.environ["CHROMA_PERSIST_DIRECTORY"] = "/content/chroma_db"
53+
os.environ["PAPER_DOWNLOAD_DIR"] = "/content/papers"
54+
55+
if openai_api_key:
56+
os.environ["OPENAI_API_KEY"] = openai_api_key
57+
print("OpenAI API key set")
58+
59+
print("Colab configuration complete!")
60+
print(f"Database will be stored at: /content/chroma_db")
61+
print(f"Papers will be downloaded to: /content/papers")
62+
else:
63+
print("Not running in Google Colab")
64+
3765
@classmethod
3866
def validate(cls):
39-
"""Validate configuration settings"""
67+
"""Validate configuration"""
68+
issues = []
69+
4070
if not cls.OPENAI_API_KEY:
41-
print("Warning: OPENAI_API_KEY not set. Some features may not work.")
71+
issues.append("OpenAI API key not set (optional but recommended)")
72+
73+
try:
74+
os.makedirs(cls.CHROMA_PERSIST_DIRECTORY, exist_ok=True)
75+
os.makedirs(cls.PAPER_DOWNLOAD_DIR, exist_ok=True)
76+
except Exception as e:
77+
issues.append(f"Cannot create directories: {e}")
4278

43-
# Create necessary directories
44-
os.makedirs(cls.PAPER_DOWNLOAD_DIR, exist_ok=True)
45-
os.makedirs(cls.CHROMA_PERSIST_DIRECTORY, exist_ok=True)
79+
return issues

0 commit comments

Comments
 (0)