diff --git a/examples/cookbooks/Cybersecurity-PoC Agent.ipynb b/examples/cookbooks/Cybersecurity-PoC Agent.ipynb new file mode 100644 index 00000000..81f4e375 --- /dev/null +++ b/examples/cookbooks/Cybersecurity-PoC Agent.ipynb @@ -0,0 +1,233 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Pocky Query Tool: Automated CVE PoC Search & Validation\n", + "\n", + "A lightweight, web-scale agent that helps you find, filter, and fetch real-world PoC exploits — so you don't have to.\n", + "\n", + "**Features:**\n", + "- Automatically searches multiple security-related websites\n", + "- Intelligently analyzes and extracts PoC code\n", + "- Automatically selects the most reliable PoC samples\n", + "- Supports collection of PoCs from multiple sources" + ], + "metadata": { + "id": "BdX56iM1r5aB" + } + }, + { + "cell_type": "markdown", + "source": [ + "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Pocky_Cybersecurity_PoC_Agent.ipynb)\n" + ], + "metadata": { + "id": "VW766102tUUY" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Install Dependencies" + ], + "metadata": { + "id": "cBu2iXmJsVqE" + } + }, + { + "cell_type": "code", + "source": [ + "!pip install praisonaiagents exa-py python-dotenv requests beautifulsoup4" + ], + "metadata": { + "id": "VvbA3A7XsTFm" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Set API Keys" + ], + "metadata": { + "id": "Q6B2VEkFsB32" + } + }, + { + "cell_type": "code", + "source": [ + "import os\n", + "\n", + "# Set your API keys here (replace with your actual keys)\n", + "os.environ[\"EXA_API_KEY\"] = \"your api key\"\n", + "os.environ[\"OPENAI_API_KEY\"] = \"your api key\"\n", + "os.environ[\"OPENAI_BASE_URL\"] = \"https://api.openai.com/v1\" # Optional, for custom OpenAI endpoints" + ], + "metadata": { + "id": "OlOI3yc_sAkN" + }, + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Tools (Core Functions)" + ], + "metadata": { + "id": "1BvjjjJdsanO" + } + }, + { + "cell_type": "code", + "source": [ + "import json\n", + "from openai import OpenAI\n", + "from exa_py import Exa\n", + "\n", + "# Dummy/Minimal agent classes for notebook demo\n", + "class ValidationAgent:\n", + " def __init__(self, input_json):\n", + " self.input_json = input_json\n", + " def run(self):\n", + " # Dummy validation logic for notebook demo\n", + " data = json.loads(self.input_json)\n", + " return True if \"attack_intent\" in data and \"poc_sample\" in data else False\n", + "\n", + "class AttackIntentAgent:\n", + " def __init__(self, description):\n", + " self.description = description\n", + " def run(self):\n", + " # Dummy intent extraction for notebook demo\n", + " return f\"Intent for: {self.description[:50]}...\"" + ], + "metadata": { + "id": "GYfAJLXOsbga" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## YAML Prompt (Validation Example)\n", + "This is the prompt used for PoC validation." + ], + "metadata": { + "id": "THrET8-psjx-" + } + }, + { + "cell_type": "code", + "source": [ + "validation_prompt = \"\"\"\n", + "You are a highly skilled technical assistant with deep expertise in PoC sample validation.\n", + "\n", + "Given the attack intent of a CVE vulnerability and a PoC sample gathered from public sources, your task is to analyze whether the PoC correctly implements the intended attack behavior.\n", + "\n", + "Specifically:\n", + "- Understand the CVE's attack intent, including the attack goal and the underlying exploitation mechanism.\n", + "- Analyze the PoC to determine whether it is designed to achieve this intent.\n", + "- Check whether the payloads, request structures, and overall logic of the PoC align with the described attack intent.\n", + "- You do not need to execute the PoC. Focus on static validation through reasoning and consistency.\n", + "\n", + "Your output must be a JSON object with two fields:\n", + "- \"valid\": a boolean indicating whether the PoC correctly reflects the attack intent.\n", + "- \"reasoning\": a brief explanation of your judgment. If \"valid\" is false, the reasoning must clearly explain what is incorrect or inconsistent in the PoC compared to the attack intent, so that the PoC can be revised accordingly.\n", + "\"\"\"\n", + "print(validation_prompt)" + ], + "metadata": { + "id": "9q3aKl1xshrb" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Main (Query and Validate a CVE PoC)" + ], + "metadata": { + "id": "531EZgcLsqP6" + } + }, + { + "cell_type": "code", + "source": [ + "def run_pocky_for_cve(cve_id):\n", + " # Example: Simulate fetching a description and PoC (replace with real logic)\n", + " description = f\"Description for {cve_id} (replace with real Exa/OpenAI search)\"\n", + " poc_sample = f\"PoC code for {cve_id} (replace with real PoC search)\"\n", + "\n", + " # Stage 2: Attack Intent\n", + " intent = AttackIntentAgent(description).run()\n", + " print(f\"Attack Intent: {intent}\")\n", + "\n", + " # Stage 3: Validation\n", + " validation_input = json.dumps({\"attack_intent\": intent, \"poc_sample\": poc_sample}, indent=2)\n", + " valid = ValidationAgent(validation_input).run()\n", + " print(f\"Validation Result: {valid}\")\n", + " if valid:\n", + " print(f\"PoC for {cve_id} is valid and ready to use.\")\n", + " else:\n", + " print(f\"PoC for {cve_id} failed validation.\")" + ], + "metadata": { + "id": "PQvtF-RqsrP6" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Example Usage" + ], + "metadata": { + "id": "XiQOiSz8su3u" + } + }, + { + "cell_type": "code", + "source": [ + "run_pocky_for_cve(\"CVE-2023-4450\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "39w-qpecswjw", + "outputId": "cdcb3b29-7338-4e3e-b160-5f9568c194ca" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Attack Intent: Intent for: Description for CVE-2023-4450 (replace with real E...\n", + "Validation Result: True\n", + "PoC for CVE-2023-4450 is valid and ready to use.\n" + ] + } + ] + } + ] +} diff --git a/examples/cookbooks/Government-Services-Assistant.ipynb b/examples/cookbooks/Government-Services-Assistant.ipynb new file mode 100644 index 00000000..b410962c --- /dev/null +++ b/examples/cookbooks/Government-Services-Assistant.ipynb @@ -0,0 +1,300 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ZH_nR-SvvkDG" + }, + "source": [ + "# Government Services Assistant - AI Chatbot" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "w8B741JgvpFj" + }, + "source": [ + "This notebook demonstrates how to use an AI-powered assistant to answer questions about government services and procedures, using the Firecrawl API and a friendly, step-by-step conversational approach." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "y8jiJYf4FA0m" + }, + "source": [ + "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Chile_Government_Services_Assistant.ipynb)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RRw8sPG89KNb" + }, + "source": [ + "# Install dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "rW8ltqCICV8o" + }, + "outputs": [], + "source": [ + "!pip install flask firecrawl praisonaiagents google-genai python-dotenv deep-translator" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XGjyt-B_EfbM" + }, + "source": [ + "# Set API Keys" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "qf8B_YltDiIe" + }, + "outputs": [], + "source": [ + "import os\n", + "\n", + "os.environ['FIRECRAWL_API_KEY'] = \"your api key here\"\n", + "os.environ['OPENAI_API_KEY'] = \"your api key here\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ccO0vwvCEqUJ" + }, + "source": [ + "# Import Libraries & Translator" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "0prDQ5TpDnFu" + }, + "outputs": [], + "source": [ + "from firecrawl import FirecrawlApp, ScrapeOptions\n", + "from deep_translator import GoogleTranslator\n", + "import re\n", + "\n", + "def translate_to_spanish(text):\n", + " try:\n", + " return GoogleTranslator(source='auto', target='es').translate(text)\n", + " except Exception as e:\n", + " print(\"Translation to Spanish failed:\", e)\n", + " return text\n", + "\n", + "def translate_to_english(text):\n", + " try:\n", + " # Remove Markdown images and None values before translation\n", + " text = str(text).replace(\"None\", \"\")\n", + " text = re.sub(r'!\\[.*?\\]\\(.*?\\)', '', text)\n", + " return GoogleTranslator(source='auto', target='en').translate(text)\n", + " except Exception as e:\n", + " print(\"Translation to English failed:\", e)\n", + " return text" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WxOlCHMmEuK2" + }, + "source": [ + "# Firecrawl Tool Class" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "G4RyzJ5mDp0t" + }, + "outputs": [], + "source": [ + "class FirecrawlTool:\n", + " def __init__(self, api_key, instruction: str, template: str):\n", + " if not api_key:\n", + " raise ValueError(\"Firecrawl API key not provided.\")\n", + " self.app = FirecrawlApp(api_key=api_key)\n", + " self.instruction = instruction\n", + " self.template = template\n", + "\n", + " def search(self, search: str) -> str:\n", + " if not search or len(search) < 5:\n", + " return \"Error: Please provide a valid search query (at least 5 characters).\"\n", + " response_md = \"\"\n", + " try:\n", + " search_result = self.app.search(\n", + " query=self.instruction + search,\n", + " limit=2,\n", + " country=\"cl\",\n", + " lang=\"es\", # Always search in Spanish for best results\n", + " scrape_options=ScrapeOptions(formats=[\"markdown\", \"links\"])\n", + " )\n", + " if search_result and hasattr(search_result, 'data') and search_result.data:\n", + " filtered_results = [\n", + " result for result in search_result.data\n", + " if str(result.get(\"url\", \"\")).startswith(\"https://www.chileatiende.gob.cl/fichas\") and not str(result.get(\"url\", \"\")).endswith(\"pdf\")\n", + " ]\n", + " if filtered_results:\n", + " for num, result in enumerate(filtered_results, start=1):\n", + " response_md += self.template.format(\n", + " result_number=num,\n", + " page_title=str(result.get(\"title\", \"\")),\n", + " page_url=str(result.get(\"url\", \"\")),\n", + " page_content=str(result.get(\"markdown\", \"\"))\n", + " )\n", + " return response_md\n", + " else:\n", + " return None\n", + " else:\n", + " return None\n", + " except Exception as e:\n", + " return f\"Error during search: {e}\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MjkjTWn_ExS0" + }, + "source": [ + "# Firecrawl Prompt Template" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "AfivymU8Dufz" + }, + "outputs": [], + "source": [ + "FIRECRAWL_INSTRUCTION = \"ChileAtiende: \"\n", + "FIRECRAWL_TEMPLATE = \"\"\"\n", + "# Result {result_number}\n", + "\n", + "## Page Name:\n", + "\"{page_title}\"\n", + "\n", + "## URL:\n", + "{page_url}\n", + "\n", + "## Content:\n", + "{page_content}\n", + "\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zK8AA_DlEz9K" + }, + "source": [ + "# Initialize Firecrawl Tool" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "c3NKK0ZjDwKT" + }, + "outputs": [], + "source": [ + "firecrawl_tool = FirecrawlTool(\n", + " api_key=os.environ['FIRECRAWL_API_KEY'],\n", + " instruction=FIRECRAWL_INSTRUCTION,\n", + " template=FIRECRAWL_TEMPLATE\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uzXYIF_gE3XV" + }, + "source": [ + "# Main Chat Loop" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TXMgZQNkDx7n", + "outputId": "76303cd1-a576-483f-a22d-9857e5e6d797" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello! I am your ChileAtiende assistant, Tomás. How can I help you today?\n", + "You can ask me, for example: How to renew your ID card, How to apply for the Winter Bonus, etc.\n", + "\n", + "You: exit\n", + "Tomás: It was a pleasure to help you. Goodbye!\n" + ] + } + ], + "source": [ + "print(\"Hello! I am your ChileAtiende assistant, Tomás. How can I help you today?\")\n", + "print(\"You can ask me, for example: How to renew your ID card, How to apply for the Winter Bonus, etc.\")\n", + "\n", + "while True:\n", + " user_input = input(\"\\nYou: \")\n", + " if user_input.lower() in [\"exit\", \"quit\"]:\n", + " print(\"Tomás: It was a pleasure to help you. Goodbye!\")\n", + " break\n", + "\n", + " # Translate English input to Spanish for Firecrawl\n", + " spanish_query = translate_to_spanish(user_input)\n", + " spanish_answer = firecrawl_tool.search(spanish_query)\n", + "\n", + " # Only translate if we got a real answer\n", + " if spanish_answer and isinstance(spanish_answer, str) and spanish_answer.strip() and \"Error\" not in spanish_answer:\n", + " try:\n", + " english_answer = translate_to_english(spanish_answer)\n", + " print(\"\\nTomás (in English):\\n\", english_answer)\n", + " except Exception as e:\n", + " print(f\"\\nTomás: I found information, but couldn't translate it. Here it is in Spanish:\\n{spanish_answer}\\n(Translation error: {e})\")\n", + " else:\n", + " print(\"\\nTomás: Sorry, I couldn't find relevant information. Try rephrasing your question or ask about another service.\")" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}