{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "## QA Science Agent"
      ],
      "metadata": {
        "id": "ta8QtaXzrByo"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Installation - We will use OpenAI (LLM) and Tavily (Action-Tool)"
      ],
      "metadata": {
        "id": "QgWKKV05rGuz"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4K_qWjjlcPXz"
      },
      "outputs": [],
      "source": [
        "!pip install openagi"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install tavily-python"
      ],
      "metadata": {
        "id": "STBE74AYia5Y"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from openagi.actions.tools.tavilyqasearch import TavilyWebSearchQA\n",
        "from openagi.actions.files import WriteFileAction,ReadFileAction\n",
        "from openagi.agent import Admin\n",
        "from openagi.planner.task_decomposer import TaskPlanner\n",
        "from openagi.llms.openai import OpenAIModel\n",
        "from openagi.worker import Worker"
      ],
      "metadata": {
        "id": "T-jHjJ59jdmm"
      },
      "execution_count": 26,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "from getpass import getpass\n",
        "\n",
        "os.environ['OPENAI_API_KEY'] = getpass(\"Enter your OpenAI API key:\") #Get key from: https://platform.openai.com/api-keys\n",
        "os.environ['TAVILY_API_KEY'] = getpass(\"Enter your Tavily search key:\") # Get key from: https://app.tavily.com/home"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Umccd17gkEsx",
        "outputId": "959eda92-9161-4990-cae5-1e262f28ff14"
      },
      "execution_count": 35,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Enter your OpenAI API key:··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Define LLM"
      ],
      "metadata": {
        "id": "NLAQ819XsnKe"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "config = OpenAIModel.load_from_env_config()\n",
        "llm = OpenAIModel(config=config)"
      ],
      "metadata": {
        "id": "7_IRcP6LsooF"
      },
      "execution_count": 36,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Define Task Planner and Action"
      ],
      "metadata": {
        "id": "PTFd3ZQPsRIp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "planner = TaskPlanner()\n",
        "action = TavilyWebSearchQA"
      ],
      "metadata": {
        "id": "ZU1LrGuop664"
      },
      "execution_count": 37,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Worker"
      ],
      "metadata": {
        "id": "yPV-dI-KsUFH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "explorer =  Worker(\n",
        "    role=\"Concept Explorer\",\n",
        "    instructions= \"\"\"\n",
        "    As a Science Assignment Reviewer, your role is to help students improve their written assignments and projects. Your tasks include:\n",
        "      - Reviewing the student's work for clarity, accuracy, and depth of understanding.\n",
        "      - Providing feedback on the scientific content and structure.\n",
        "      - Suggesting improvements in argumentation and evidence presentation.\n",
        "    \"\"\",\n",
        "    actions=[\n",
        "        action,WriteFileAction\n",
        "    ],\n",
        "    max_iteration = 10\n",
        ")"
      ],
      "metadata": {
        "id": "pVChH6YAqAdi"
      },
      "execution_count": 29,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "lab_guide =  Worker(\n",
        "    role=\"Lab Guide\",\n",
        "    instructions= \"\"\"\n",
        "    As a Virtual Lab Guide, your role is to assist students in planning and executing science experiments. Your tasks include:\n",
        "      - Searching for experimental protocols and step-by-step guides.\n",
        "      - Recommending safe practices and necessary precautions.\n",
        "      - Suggesting ways to document observations and results.\n",
        "      - Providing troubleshooting tips for common issues during experiments.\n",
        "    \"\"\",\n",
        "    actions=[\n",
        "        ReadFileAction,action,WriteFileAction\n",
        "    ],\n",
        "    max_iteration = 10\n",
        ")"
      ],
      "metadata": {
        "id": "aAs1xRfSr3d6"
      },
      "execution_count": 30,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Define Admin"
      ],
      "metadata": {
        "id": "vQ9nq9K8sX19"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "admin = Admin(\n",
        "    actions = [ReadFileAction,action,WriteFileAction],\n",
        "    planner = planner,\n",
        "    llm=llm,\n",
        ")\n",
        "admin.assign_workers([explorer, lab_guide])"
      ],
      "metadata": {
        "id": "tetoZPgYsP4g"
      },
      "execution_count": 31,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "report = admin.run(\n",
        "    query = \"I need a report on Screw Guage\",\n",
        "    description = \"Explain the Screw Guage concept to a 10 year old kid who enjoys Cricket\"\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AjdzZXs-s4Y2",
        "outputId": "f308afae-8aaf-4091-c21f-7070cdfe5716"
      },
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "Tasks: [{'task_name': 'Research Screw Gauge Concept', 'description': \"Use the Tavily Web Search QA tool to research and gather detailed information on the concept of a screw gauge, ensuring the information is simple enough to explain to a 10-year-old kid who enjoys cricket. Query should include 'What is a screw gauge and how does it work, explained simply?'\", 'worker_id': '3b31cb39b9d24f0a9707141d571a0ef9'}, {'task_name': 'Write Explanation', 'description': 'Using the information gathered from the research, write a simplified explanation of the screw gauge. The content should include analogies or examples related to cricket to make the concept relatable and easier to understand for the kid. Ensure the explanation covers basic functionalities and applications of a screw gauge.', 'worker_id': '3b31cb39b9d24f0a9707141d571a0ef9'}]\n",
            "\n",
            "\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "WARNING:root:No Tasks to summarize.\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n",
            "WARNING:chromadb.segment.impl.vector.local_persistent_hnsw:Number of requested results 10 is greater than number of elements in index 8, updating n_results = 8\n",
            "WARNING:root:No Tasks to summarize.\n",
            "ERROR:root:Error running action: '_Environ' object is not callable\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(report)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fuzePDW9tQ1H",
        "outputId": "f07ca591-8866-4c60-ec5a-205c468c7f88"
      },
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "### Understanding the Use of a Screw Gauge\n",
            "\n",
            "A **screw gauge**, also known as a **micrometer**, serves a purpose similar to the ruler used in cricket for measuring bat thickness, but with far greater precision. Imagine being a batsman who needs to ensure that your bat isn't uncomfortably thick or ineffectively thin for striking the ball. A screw gauge is the tool that allows for measuring these subtle differences with high accuracy.\n",
            "\n",
            "Just as a batsman adjusts his grip to optimize control based on the thickness of the bat, engineers and scientists utilize a screw gauge for precisely adjusting or measuring small objects. This ensures a perfect fit in various applications, from mechanical devices to scientific experiments, mirroring how a cricket bat’s thickness must comply with official regulations for optimal performance.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### WriteFileAction generates response in Markdown"
      ],
      "metadata": {
        "id": "u6ZGwXf5vR9E"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from IPython import display"
      ],
      "metadata": {
        "id": "uM1HvRiCu91f"
      },
      "execution_count": 42,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "display.Markdown(report)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 176
        },
        "id": "sAlIcVI1vWIb",
        "outputId": "357be06d-cd1b-4f47-8984-0de4ecdc2b95"
      },
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ],
            "text/markdown": "### Understanding the Use of a Screw Gauge\n\nA **screw gauge**, also known as a **micrometer**, serves a purpose similar to the ruler used in cricket for measuring bat thickness, but with far greater precision. Imagine being a batsman who needs to ensure that your bat isn't uncomfortably thick or ineffectively thin for striking the ball. A screw gauge is the tool that allows for measuring these subtle differences with high accuracy.\n\nJust as a batsman adjusts his grip to optimize control based on the thickness of the bat, engineers and scientists utilize a screw gauge for precisely adjusting or measuring small objects. This ensures a perfect fit in various applications, from mechanical devices to scientific experiments, mirroring how a cricket bat’s thickness must comply with official regulations for optimal performance."
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    }
  ]
}