Skip to content

Instantly share code, notes, and snippets.

@ruvnet
Last active August 20, 2024 21:54
Show Gist options
  • Save ruvnet/22b50cd5bb4e6b998e2f029d986ea76d to your computer and use it in GitHub Desktop.
Save ruvnet/22b50cd5bb4e6b998e2f029d986ea76d to your computer and use it in GitHub Desktop.
aider-colab.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyP5UHL9Rgz+Wz/ZrQXhF0yQ",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ruvnet/22b50cd5bb4e6b998e2f029d986ea76d/aider-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Aider Browser Script Integration in Google Colab\n",
"### created by rUv\n",
"\n",
"This notebook demonstrates how to integrate the Aider browser script within Google Colab to streamline your workflow. With Aider, you can easily fetch data, run AI-powered code suggestions, and handle web-based queries directly within Colab, eliminating the need to switch between environments. This seamless integration allows you to focus on development while leveraging the power of AI to automate and assist in your coding tasks.\n",
"\n",
"### Key Features:\n",
"- **Seamless Web Integration:** Run browser-based scripts directly within Colab cells.\n",
"- **Automated Data Retrieval:** Fetch data from web sources, perform searches, and extract insights in real-time.\n",
"- **Enhanced Workflow:** Combine the power of Colab with Aider's automation to optimize your workflow.\n",
"\n",
"### Aider Chat Overview\n",
"\n",
"Aider is an advanced AI-powered coding assistant designed for pair programming in your terminal. It enables developers to collaborate with large language models (LLMs) like GPT-4 and Claude 3.5 to directly edit code within their local Git repositories. The tool supports a variety of programming languages, including Python, JavaScript, TypeScript, PHP, HTML, and CSS, making it versatile for many projects.\n",
"\n",
"#### Key Capabilities:\n",
"1. **Interactive Code Editing:** Aider allows you to edit files by interacting with the LLM in real-time. You can request new features, refactor code, fix bugs, or even generate test cases through chat-based interactions.\n",
"2. **Git Integration:** The tool automatically commits changes to your Git repository with meaningful commit messages, simplifying version control.\n",
"3. **Multi-File Editing:** Aider can handle complex, multi-file edits efficiently by maintaining an internal map of your entire Git repository.\n",
"4. **Voice Commands:** In addition to text-based input, Aider supports voice-to-code interactions, allowing you to code hands-free.\n",
"5. **LLM Flexibility:** Aider can connect to multiple LLMs, making it adaptable to various AI services.\n",
"\n",
"Aider is particularly well-suited for large codebases and supports both new projects and existing ones. It helps boost productivity by integrating AI coding capabilities directly into your workflow, eliminating the need for copy-pasting between your code editor and an AI assistant.\n",
"\n",
"For more detailed information, you can visit [Aider's GitHub repository](https://github.com/paul-gauthier/aider) or explore their official website at [aider.chat](https://aider.chat).\n",
"\n",
"### Setup Instructions:\n",
"\n",
"1. **Install Required Libraries:**\n",
" - Install the necessary Python packages for Aider, ngrok, and OpenAI\n",
"\n",
"2. **Get Your ngrok API Key:**\n",
" - Sign up at [ngrok.com](https://ngrok.com) and retrieve your API key from the dashboard under \"Your Authtoken.\"\n",
" - Add the API key to Colab's secrets:\n",
" 1. Click the folder icon in the left sidebar.\n",
" 2. Click on the \"Files\" tab.\n",
" 3. Click on the three dots next to \"Files\" and select \"Add secrets.\"\n",
" 4. Name the secret `NGROK_API_KEY` and paste your API key as the value.\n",
"\n",
"3. **Get Your OpenAI API Key:**\n",
" - Sign up at [platform.openai.com](https://platform.openai.com) and obtain your OpenAI API key from the API section in your dashboard.\n",
" - Add this key to Colab's secrets following the same steps as above:\n",
" 1. Name the secret `OPENAI_API_KEY` and paste your OpenAI API key as the value.\n",
"\n",
"4. **Configure Aider Script:**\n",
" - Set up the Aider browser script with the correct inputs, such as your ngrok and OpenAI API keys, and any other necessary configurations.\n",
" \n",
"5. **Execute and Fetch Results:**\n",
" - Run the script to start Aider, open a tunnel with ngrok, and access the results via the public URL generated by ngrok.\n",
"\n",
"Follow along with the cells below to begin integrating Aider with your Colab project!"
],
"metadata": {
"id": "xY8kayr3qKA4"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YWqX6o5hlhE2"
},
"outputs": [],
"source": [
"!pip install aider-chat pyngrok llama-index pyyaml streamlit"
]
},
{
"cell_type": "code",
"source": [
"# @title Local Git Configuration\n",
"\n",
"import subprocess\n",
"\n",
"# Initialize a local Git repository\n",
"subprocess.run([\"git\", \"init\"])\n",
"\n",
"# Set up Git configuration\n",
"# Define the variables in the Colab UI using input forms\n",
"git_user_name = \"Your Name\" # @param {type:\"string\"}\n",
"git_user_email = \"your.email@example.com\" # @param {type:\"string\"}\n",
"\n",
"# Verify the input\n",
"print(f\"User Name: {git_user_name}\")\n",
"print(f\"User Email: {git_user_email}\")\n",
"\n",
"# Configure Git with the provided user name and email\n",
"subprocess.run([\"git\", \"config\", \"--global\", \"user.name\", git_user_name])\n",
"subprocess.run([\"git\", \"config\", \"--global\", \"user.email\", git_user_email])\n",
"\n",
"# Create a README file as an initial commit\n",
"subprocess.run([\"bash\", \"-c\", 'echo \"# Initial Commit\" >> README.md'])\n",
"\n",
"# Add the README file to the staging area\n",
"subprocess.run([\"git\", \"add\", \"README.md\"])\n",
"\n",
"# Define a commit message as a parameter in the Colab UI\n",
"commit_message = \"Initial commit\" # @param {type:\"string\"}\n",
"\n",
"# Commit the README file using the commit message from the UI\n",
"subprocess.run([\"git\", \"commit\", \"-m\", commit_message])\n",
"\n",
"# Display Git log to confirm initialization\n",
"!git log"
],
"metadata": {
"id": "n7-3Yz7AnS2a",
"cellView": "form"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title API Keys Configuration\n",
"\n",
"import os\n",
"from google.colab import userdata\n",
"\n",
"# Function to retrieve the API key from Colab secrets and set it as an environment variable\n",
"def set_api_key_env_var(secret_name, env_var_name):\n",
" try:\n",
" # Retrieve the secret from Colab secrets\n",
" api_key = userdata.get(secret_name)\n",
" if not api_key:\n",
" raise ValueError(f\"{secret_name} is missing. Please check your setup.\")\n",
"\n",
" # Set the secret as an environment variable\n",
" os.environ[env_var_name] = api_key\n",
" print(f\"{env_var_name} is set as an environment variable.\")\n",
" except Exception as e:\n",
" print(f\"Error: {e}\")\n",
"\n",
"# Retrieve and set the OpenAI API key as an environment variable\n",
"set_api_key_env_var('OPENAI_API_KEY', 'OPENAI_API_KEY')\n",
"\n",
"# Verify that the environment variable is set\n",
"print(\"✅ Key Set\")\n"
],
"metadata": {
"id": "yAlqLbCnl29I",
"cellView": "form"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title Start Aider and expose using NGROK\n",
"\n",
"# Import necessary libraries\n",
"from pyngrok import ngrok\n",
"from google.colab import userdata\n",
"import subprocess\n",
"import re\n",
"from IPython.display import display, HTML\n",
"import time\n",
"\n",
"try:\n",
" # Get the ngrok API key from Google Colab secrets\n",
" ngrok_api_key = userdata.get('NGROK_API_KEY')\n",
"\n",
" if not ngrok_api_key:\n",
" raise ValueError(\"ngrok API key is missing. Please check your setup.\")\n",
"\n",
" # Set the ngrok auth token\n",
" ngrok.set_auth_token(ngrok_api_key)\n",
"\n",
" # Start Aider in browser mode using subprocess\n",
" aider_process = subprocess.Popen(['aider', '--browser'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)\n",
"\n",
" # Capture the external URL output from Aider\n",
" external_url = None\n",
" while True:\n",
" output = aider_process.stdout.readline()\n",
" if output == '' and aider_process.poll() is not None:\n",
" break\n",
" if output:\n",
" print(output.strip())\n",
" # Regex to capture the External URL provided by Aider\n",
" url_match = re.search(r'External URL:\\s(https?://\\S+)', output)\n",
" if url_match:\n",
" external_url = url_match.group(1)\n",
" print(f\"Found External URL: {external_url}\")\n",
" break\n",
"\n",
" if not external_url:\n",
" raise RuntimeError(\"Aider did not provide an External URL.\")\n",
"\n",
" # Use ngrok to forward the Aider External URL\n",
" public_url = ngrok.connect(addr=\"8501\", proto=\"http\")\n",
" ngrok_url = public_url.public_url\n",
"\n",
" print(f\"Public ngrok URL: {ngrok_url}\")\n",
" print(\"You can now access the Aider dashboard through ngrok.\")\n",
"\n",
" # Create iframe HTML using the correct ngrok URL\n",
" iframe_html = f'<iframe src=\"{ngrok_url}\" height=\"800\" width=\"100%\"></iframe>'\n",
" hyperlink_html = f'<a href=\"{ngrok_url}\" target=\"_blank\">Open Aider Dashboard in New Tab</a>'\n",
"\n",
" # Display iframe and hyperlink\n",
" display(HTML(f\"{hyperlink_html}<br><br>{iframe_html}\"))\n",
"\n",
"except Exception as e:\n",
" print(f\"An error occurred: {str(e)}\")\n",
" print(\"If the error is related to the API key, make sure you've set up the 'ngrok_api' secret in Google Colab.\")\n",
" print(\"To set up the secret:\")\n",
" print(\"1. Click on the folder icon in the left sidebar.\")\n",
" print(\"2. Click on the 'Files' tab.\")\n",
" print(\"3. Click on the three dots next to 'Files' and select 'Add secrets'.\")\n",
" print(\"4. Add a new secret with the name 'ngrok_api' and paste your ngrok API key as the value.\")\n",
"\n",
"# Keep the notebook running\n",
"while True:\n",
" time.sleep(60)\n"
],
"metadata": {
"id": "E80-fTZimELX",
"cellView": "form"
},
"execution_count": null,
"outputs": []
}
]
}

Ever wonder if you could program without needing to learn how to code? With Aider, you can make that dream a reality. Imagine having a powerful AI bot at your fingertips, capable of writing, debugging, and editing code with just a simple conversation. And the best part? You can start using Aider for free thanks to my Google Colab notebook.

Aider transforms the way you approach coding. Whether you're a seasoned developer looking for a productivity boost or a complete beginner who just wants to get things done, Aider makes it possible. You can interact with it through natural language, allowing you to describe the features you want, fix bugs, or even refactor your code—all while Aider handles the heavy lifting behind the scenes.

Set up in minutes, and start collaborating with Aider directly in your local Git repository. Just type what you need, and Aider will code it for you. Plus, with built-in Git integration, every change is automatically committed, ensuring that your project stays organized and version-controlled.

This notebook demonstrates how to integrate the Aider browser script within Google Colab to streamline your workflow. With Aider, you can easily fetch data, run AI-powered code suggestions, and handle web-based queries directly within Colab, eliminating the need to switch between environments. This seamless integration allows you to focus on development while leveraging the power of AI to automate and assist in your coding tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment