{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "245a954a",
   "metadata": {},
   "source": [
    "# Google Search\n",
    "\n",
    "This notebook goes over how to use the google search component.\n",
    "\n",
    "First, you need to set up the proper API keys and environment variables. To set it up, follow the instructions found [here](https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search).\n",
    "\n",
    "Then we will need to set some environment variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "34bb5968",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"GOOGLE_CSE_ID\"] = \"\"\n",
    "os.environ[\"GOOGLE_API_KEY\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ac4910f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.utilities import GoogleSearchAPIWrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "84b8f773",
   "metadata": {},
   "outputs": [],
   "source": [
    "search = GoogleSearchAPIWrapper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "068991a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1 Child\\'s First Name. 2. 6. 7d. Street Address. 71. (Type or print). BARACK. Sex. 3. This Birth. 4. If Twin or Triplet,. Was Child Born. Barack Hussein Obama II is an American retired politician who served as the 44th president of the United States from 2009 to 2017. His full name is Barack Hussein Obama II. Since the “II” is simply because he was named for his father, his last name is Obama. Feb 9, 2015 ... Michael Jordan misspelled Barack Obama\\'s first name on 50th-birthday gift ... Knowing Obama is a Chicagoan and huge basketball fan,\\xa0... Aug 18, 2017 ... It took him several seconds and multiple clues to remember former President Barack Obama\\'s first name. Miller knew that every answer had to end\\xa0... First Lady Michelle LaVaughn Robinson Obama is a lawyer, writer, and the wife of the 44th President, Barack Obama. She is the first African-American First\\xa0... Barack Obama, in full Barack Hussein Obama II, (born August 4, 1961, Honolulu, Hawaii, U.S.), 44th president of the United States (2009–17) and the first\\xa0... When Barack Obama was elected president in 2008, he became the first African American to hold ... The Middle East remained a key foreign policy challenge. Feb 27, 2020 ... President Barack Obama was born Barack Hussein Obama, II, as shown here on his birth certificate here . As reported by Reuters here , his\\xa0... Jan 16, 2007 ... 4, 1961, in Honolulu. His first name means \"one who is blessed\" in Swahili. While Obama\\'s father, Barack Hussein Obama Sr., was from Kenya, his\\xa0...'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.run(\"Obama's first name?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074b7f07",
   "metadata": {},
   "source": [
    "## Number of Results\n",
    "You can use the `k` parameter to set the number of results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5083fbdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "search = GoogleSearchAPIWrapper(k=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "77aaa857",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The official home of the Python Programming Language.'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.run(\"python\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11c8d94f",
   "metadata": {},
   "source": [
    "'The official home of the Python Programming Language.'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73473110",
   "metadata": {},
   "source": [
    "## Metadata Results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "109fe796",
   "metadata": {},
   "source": [
    "Run query through GoogleSearch and return snippet, title, and link metadata.\n",
    "\n",
    "- Snippet: The description of the result.\n",
    "- Title: The title of the result.\n",
    "- Link: The link to the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "028f4cba",
   "metadata": {},
   "outputs": [],
   "source": [
    "search = GoogleSearchAPIWrapper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4d8f734f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'snippet': 'Discover the innovative world of Apple and shop everything iPhone, iPad, Apple Watch, Mac, and Apple TV, plus explore accessories, entertainment,\\xa0...',\n",
       "  'title': 'Apple',\n",
       "  'link': 'https://www.apple.com/'},\n",
       " {'snippet': \"Jul 10, 2022 ... Whether or not you're up on your apple trivia, no doubt you know how delicious this popular fruit is, and how nutritious. Apples are rich in\\xa0...\",\n",
       "  'title': '25 Types of Apples and What to Make With Them - Parade ...',\n",
       "  'link': 'https://parade.com/1330308/bethlipton/types-of-apples/'},\n",
       " {'snippet': 'An apple is an edible fruit produced by an apple tree (Malus domestica). Apple trees are cultivated worldwide and are the most widely grown species in the\\xa0...',\n",
       "  'title': 'Apple - Wikipedia',\n",
       "  'link': 'https://en.wikipedia.org/wiki/Apple'},\n",
       " {'snippet': 'Apples are a popular fruit. They contain antioxidants, vitamins, dietary fiber, and a range of other nutrients. Due to their varied nutrient content,\\xa0...',\n",
       "  'title': 'Apples: Benefits, nutrition, and tips',\n",
       "  'link': 'https://www.medicalnewstoday.com/articles/267290'},\n",
       " {'snippet': \"An apple is a crunchy, bright-colored fruit, one of the most popular in the United States. You've probably heard the age-old saying, “An apple a day keeps\\xa0...\",\n",
       "  'title': 'Apples: Nutrition & Health Benefits',\n",
       "  'link': 'https://www.webmd.com/food-recipes/benefits-apples'}]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.results(\"apples\", 5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
