|
| 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 | +``` |
0 commit comments