-
-
Notifications
You must be signed in to change notification settings - Fork 744
Add GitHub repo analyzer agent #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GitHub repo analyzer agent #683
Conversation
WalkthroughFour new examples are introduced: three Jupyter notebooks and one Python script. These cover AI agent applications for fuel emission estimation, GitHub repository analysis, and personalized learning assistance, each demonstrating agent configuration, user interaction, and fallback mechanisms. The learning assistant is implemented both as a notebook and a standalone script. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Notebook/Script
participant Agent
participant OpenAI API
participant HuggingFace GPT-2
User->>Notebook/Script: Provide input/query
Notebook/Script->>Agent: Send query
Agent->>OpenAI API: Request completion
OpenAI API-->>Agent: Return response
Agent-->>Notebook/Script: Return answer
Notebook/Script-->>User: Display answer
alt Agent fails
Notebook/Script->>HuggingFace GPT-2: Generate fallback response
HuggingFace GPT-2-->>Notebook/Script: Return fallback answer
Notebook/Script-->>User: Display fallback answer
end
Possibly related PRs
Suggested labels
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @Dhivya-Bharathy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly broadens the scope of AI agent examples by adding three distinct new agents: a GitHub repository analyzer, a fuel emission estimator, and a personalized learning assistant. While the title specifically highlights the GitHub repo analyzer, the changes encompass a wider range of AI applications, showcasing the versatility of PraisonAI and OpenAI for different problem domains.
Highlights
- New Feature: GitHub Repo Analyzer Agent: A new Jupyter notebook has been added that introduces an AI agent capable of analyzing GitHub repositories. It fetches repository structure using the GitHub API and summarizes its purpose via AI, designed for public repositories without authentication.
- New Feature: Fuel Emission Estimator & Forecasting Agent: A new Jupyter notebook is included for an intelligent agent that calculates annual CO₂ emissions based on user driving patterns and vehicle type. It also forecasts how emissions would change with a switch to more fuel-efficient or hybrid vehicles.
- New Feature: AI-Powered Personalized Learning Assistant: This pull request adds a Jupyter notebook and a corresponding Python script for a personalized learning assistant. This agent acts as a smart content chatbot, providing clear and helpful answers to learner questions, with a fallback mechanism to Hugging Face's GPT-2 if OpenAI is unavailable.
- Code Structure Enhancement: The changes introduce multiple new examples of AI agents, demonstrating various applications of PraisonAI and OpenAI within the
examples/cookbooksandexamples/python/toolsdirectories.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #683 +/- ##
=======================================
Coverage 14.50% 14.50%
=======================================
Files 25 25
Lines 2517 2517
Branches 357 357
=======================================
Hits 365 365
Misses 2136 2136
Partials 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request adds a new example notebook and a corresponding Python script for creating a personalized learning assistant. The changes involve setting up dependencies, configuring the OpenAI API key, initializing the AI agent, and creating an interactive chat loop. The feedback focuses on improving code readability by adding comments to explain the purpose of setting the environment variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Nitpick comments (2)
examples/cookbooks/github_repo_analyzer_agent.ipynb (2)
39-39: LGTM! Consider pinning versions for reproducibility.The dependency installation is correct. For production use, consider pinning specific versions to ensure reproducible builds.
-!pip install praisonaiagents openai requests +!pip install praisonaiagents>=1.0.0 openai>=1.0.0 requests>=2.25.0
175-179: Good integration function with basic error handling.The function effectively combines file fetching with agent analysis. The fallback error message is helpful for users.
Consider enhancing the error message to be more informative:
- return "❌ Cannot fetch repo contents." + return "❌ Cannot fetch repo contents. Please check the URL and ensure it's a valid public GitHub repository."
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb(1 hunks)examples/cookbooks/github_repo_analyzer_agent.ipynb(1 hunks)examples/cookbooks/personalized_learning_assistant.ipynb(1 hunks)examples/python/tools/exa-tool/learning_assistant.py(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: test-core (3.11)
- GitHub Check: quick-test
🔇 Additional comments (12)
examples/python/tools/exa-tool/learning_assistant.py (3)
52-58: Well-implemented fallback mechanism.The error handling and fallback to GPT-2 is properly implemented, ensuring robustness when the primary agent fails.
62-69: Interactive loop implementation is solid.The while loop with proper exit conditions and user-friendly messaging provides a good user experience.
45-47: [web_search]What is the recommended way to initialize the OpenAI API key in the latest OpenAI Python library?examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb (2)
78-81: Good security practice using getpass.Using
getpassfor API key input properly protects sensitive credentials from being displayed in plain text.
105-119: Well-structured agent configuration.The YAML-based agent configuration with clear role, goal, and tools specification follows good practices for agent setup.
examples/cookbooks/personalized_learning_assistant.ipynb (2)
306-311: Well-implemented fallback mechanism.The
chat_with_aifunction properly handles exceptions and falls back to GPT-2, ensuring robust operation similar to the Python script version.
755-761: Good interactive chat implementation.The chat loop with proper exit conditions provides a user-friendly interface for the learning assistant.
examples/cookbooks/github_repo_analyzer_agent.ipynb (5)
83-85: Excellent API key handling!The implementation correctly avoids hardcoding sensitive credentials and uses interactive input, which is the recommended approach for notebooks.
105-109: Clean and appropriate imports.All imports are necessary and properly structured. The OpenAI API key assignment follows the environment variable pattern established earlier.
129-132: Well-defined agent with clear instructions.The agent configuration is appropriate with concise, clear instructions that align with the notebook's purpose.
445-446: Clean and user-friendly execution flow.The interactive approach is perfect for a notebook demonstration, providing clear prompts and formatted output.
19-19: Verify the Colab badge URL points to the correct branch.The Colab badge references the
mainbranch ofDhivyaBharathy-web/PraisonAI, but this appears to be a fork. Ensure this matches the intended repository structure.#!/bin/bash # Verify if the repository and branch exist curl -s -o /dev/null -w "%{http_code}" https://raw.githubusercontent.com/DhivyaBharathy-web/PraisonAI/main/examples/cookbooks/github_repo_analyzer_agent.ipynb
| # 4. 🔐 Set OpenAI API Key | ||
| # ============================ | ||
| import os | ||
| os.environ['OPENAI_API_KEY'] = input('Enter your OpenAI API key: ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security issue: API key exposure in plain text.
Using input() to collect the OpenAI API key will display it in plain text, which is a security risk. Use getpass module instead for secure password input.
Apply this diff to fix the security issue:
+from getpass import getpass
import os
-os.environ['OPENAI_API_KEY'] = input('Enter your OpenAI API key: ')
+os.environ['OPENAI_API_KEY'] = getpass('🔐 Enter your OpenAI API key: ')🤖 Prompt for AI Agents
In examples/python/tools/exa-tool/learning_assistant.py at line 38, replace the
use of input() for collecting the OpenAI API key with the getpass module's
getpass() function to prevent the API key from being displayed in plain text.
Import getpass at the top if not already imported, then use getpass.getpass() to
securely prompt the user for the API key and assign it to
os.environ['OPENAI_API_KEY'].
| "id": "06ae55aa" | ||
| }, | ||
| "source": [ | ||
| "[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Fix repository reference in Colab badge.
The Colab badge URL points to a personal fork (DhivyaBharathy-web/PraisonAI) instead of the main repository. This should reference the official repository for consistency.
Update the URL to point to the main repository:
-"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb)"
+"[](https://colab.research.google.com/github/MervinPraison/PraisonAI/blob/main/examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb)"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| "[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb)" | |
| "[](https://colab.research.google.com/github/MervinPraison/PraisonAI/blob/main/examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb)" |
🤖 Prompt for AI Agents
In examples/cookbooks/Fuel_Emission_Agent_Intelligence_Agent.ipynb at line 22,
the Colab badge URL references a personal fork repository. Update the URL to
point to the official main repository by replacing
"DhivyaBharathy-web/PraisonAI" with the correct main repository path to ensure
consistency and proper linking.
| "id": "dSQ-cb--A7Ys" | ||
| }, | ||
| "source": [ | ||
| "[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/personalized_learning_assistant.ipynb)\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Fix repository reference in Colab badge.
Same issue as the fuel emission notebook - the Colab badge points to a personal fork instead of the main repository.
Update the URL to point to the main repository:
-"[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/personalized_learning_assistant.ipynb)\n"
+"[](https://colab.research.google.com/github/MervinPraison/PraisonAI/blob/main/examples/cookbooks/personalized_learning_assistant.ipynb)\n"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| "[](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/personalized_learning_assistant.ipynb)\n" | |
| "[](https://colab.research.google.com/github/MervinPraison/PraisonAI/blob/main/examples/cookbooks/personalized_learning_assistant.ipynb)\n" |
🤖 Prompt for AI Agents
In examples/cookbooks/personalized_learning_assistant.ipynb at line 19, the
Colab badge URL incorrectly points to a personal fork repository. Update the URL
to reference the main repository instead by replacing the username and
repository path with the official main repository path in the badge link.
| "outputs": [], | ||
| "source": [ | ||
| "import os\n", | ||
| "os.environ['OPENAI_API_KEY'] = input('Enter your OpenAI API key: ')" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security issue: Use getpass for API key input.
Unlike the fuel emission notebook which correctly uses getpass, this notebook uses input() which exposes the API key in plain text. This creates inconsistency across examples and a security vulnerability.
Apply this diff to fix the security issue and maintain consistency:
-"os.environ['OPENAI_API_KEY'] = input('Enter your OpenAI API key: ')"
+"from getpass import getpass\n",
+"os.environ['OPENAI_API_KEY'] = getpass('🔐 Enter your OpenAI API key: ')"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| "os.environ['OPENAI_API_KEY'] = input('Enter your OpenAI API key: ')" | |
| "from getpass import getpass\n", | |
| "os.environ['OPENAI_API_KEY'] = getpass('🔐 Enter your OpenAI API key: ')" |
🤖 Prompt for AI Agents
In examples/cookbooks/personalized_learning_assistant.ipynb at line 84, replace
the use of input() for entering the OpenAI API key with getpass.getpass() to
securely capture the key without displaying it on the screen. Import getpass if
not already imported, and assign the API key using os.environ['OPENAI_API_KEY']
= getpass.getpass('Enter your OpenAI API key: '). This change will prevent the
API key from being exposed in plain text and align with the security practice
used in the fuel emission notebook.
| "def fetch_repo_files(repo_url):\n", | ||
| " api_url = repo_url.replace(\"https://github.com\", \"https://api.github.com/repos\") + \"/contents\"\n", | ||
| " r = requests.get(api_url)\n", | ||
| " return [f[\"name\"] for f in r.json()] if r.status_code == 200 else []" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance the repository file fetching with better error handling and broader coverage.
The current implementation has several limitations that affect the analyzer's effectiveness:
- Limited scope: Only fetches root-level files, missing important subdirectories
- No error handling: Network issues or malformed URLs will cause failures
- No authentication: Limited to public repositories only
- Rate limiting: GitHub API limits aren't handled
- URL validation: No validation of repository URL format
+import urllib.parse
+from typing import List, Optional
-def fetch_repo_files(repo_url):
- api_url = repo_url.replace("https://github.com", "https://api.github.com/repos") + "/contents"
- r = requests.get(api_url)
- return [f["name"] for f in r.json()] if r.status_code == 200 else []
+def fetch_repo_files(repo_url: str, path: str = "") -> List[str]:
+ """Fetch repository files recursively with proper error handling."""
+ try:
+ # Validate and parse URL
+ if not repo_url.startswith("https://github.com/"):
+ return []
+
+ api_url = repo_url.replace("https://github.com", "https://api.github.com/repos")
+ if path:
+ api_url += f"/contents/{path}"
+ else:
+ api_url += "/contents"
+
+ headers = {"Accept": "application/vnd.github.v3+json"}
+ r = requests.get(api_url, headers=headers, timeout=10)
+
+ if r.status_code != 200:
+ print(f"⚠️ API request failed: {r.status_code}")
+ return []
+
+ files = []
+ for item in r.json():
+ if item["type"] == "file":
+ files.append(item["path"] if path else item["name"])
+ elif item["type"] == "dir" and len(files) < 50: # Limit recursion
+ files.extend(fetch_repo_files(repo_url, item["path"]))
+
+ return files
+
+ except Exception as e:
+ print(f"❌ Error fetching repository files: {e}")
+ return []📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| "def fetch_repo_files(repo_url):\n", | |
| " api_url = repo_url.replace(\"https://github.com\", \"https://api.github.com/repos\") + \"/contents\"\n", | |
| " r = requests.get(api_url)\n", | |
| " return [f[\"name\"] for f in r.json()] if r.status_code == 200 else []" | |
| import urllib.parse | |
| from typing import List, Optional | |
| def fetch_repo_files(repo_url: str, path: str = "") -> List[str]: | |
| """Fetch repository files recursively with proper error handling.""" | |
| try: | |
| # Validate and parse URL | |
| if not repo_url.startswith("https://github.com/"): | |
| return [] | |
| api_url = repo_url.replace("https://github.com", "https://api.github.com/repos") | |
| if path: | |
| api_url += f"/contents/{path}" | |
| else: | |
| api_url += "/contents" | |
| headers = {"Accept": "application/vnd.github.v3+json"} | |
| r = requests.get(api_url, headers=headers, timeout=10) | |
| if r.status_code != 200: | |
| print(f"⚠️ API request failed: {r.status_code}") | |
| return [] | |
| files: List[str] = [] | |
| for item in r.json(): | |
| if item["type"] == "file": | |
| files.append(item["path"] if path else item["name"]) | |
| elif item["type"] == "dir" and len(files) < 50: # Limit recursion depth | |
| files.extend(fetch_repo_files(repo_url, item["path"])) | |
| return files | |
| except Exception as e: | |
| print(f"❌ Error fetching repository files: {e}") | |
| return [] |
🤖 Prompt for AI Agents
In examples/cookbooks/github_repo_analyzer_agent.ipynb around lines 152 to 155,
improve the fetch_repo_files function by adding validation for the repo_url
format to ensure it is a valid GitHub repository URL. Implement error handling
to catch network errors and invalid responses gracefully. Add support for
authentication using a GitHub token to access private repositories and increase
rate limits. Modify the function to recursively fetch files from all
subdirectories, not just the root level, by traversing the contents API
responses. Also, handle GitHub API rate limiting by checking response headers
and retrying or backing off as needed.
This notebook introduces a GitHub Repo Analyzer Agent powered by PraisonAI and OpenAI.
It fetches a repository’s structure using the GitHub API and summarizes its purpose via AI.
Built for Colab, it works without authentication for public repos and includes fallback handling.
Summary by CodeRabbit