File size: 7,874 Bytes
d1f1e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')).\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m I need to find out more information about maxwells equations\n",
      "Action: Search\n",
      "Action Input: maxwells equations\u001b[0m"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "1 extra bytes in post.stringData array\n",
      "'created' timestamp seems very low; regarding as unix timestamp\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Observation: \u001b[36;1m\u001b[1;3m \n",
      "From Wikipedia, the free encyclopedia\n",
      "Maxwell's  equations ,  or  Maxwell–Heaviside  equations ,  are  a  set  of  coupled\n",
      "partial  differential  equations  that,  together  with  the  Lorentz  force  law,  form  the\n",
      "foundation  of  classical  electromagnetism ,  classical  optics ,  and  electric  circuits .  The\n",
      "equations provide a mathematical model for electric, optical, and radio technologies,\n",
      "such as power generation, electric motors, wireless  communication, lenses, radar etc.\n",
      "They describe how electric  and magnetic fields  are generated by charges , currents , and\n",
      "changes  of  the  fields.[note  1] The  equations  are  named  after  the  physicist  and\n",
      "mathematician James Clerk Maxwell , who, in 1861 and 1862, published an early form of\n",
      "the equations that included the Lorentz force law. Maxwell first used the equations to\n",
      "propose  that  light  is  an  electromagnetic  phenomenon.  The  modern  form  of  the\n",
      "equations in their most common formulation is credited to Oliver Heaviside .[1] \n",
      "Maxwell's  equations  may  be  combined  to  demonstrate  how  fluctuations  in\n",
      "electromagnetic  fields  (waves)  propagate  at  a  constant  speed  in  vacuum,  c\n",
      "(299 792 458 m/s ).[2] Known as electromagnetic radiation , these waves occur at various\n",
      "wavelengths to produce a spectrum  of radiation from radio waves  to gamma rays . \n",
      "The  equations  have  two  major  variants.  The  microscopic  equations  have  universal\n",
      "applicability but are unwieldy for common calculations. They relate the electric and\n",
      "magnetic fields to total charge and total current, including the complicated charges and\n",
      "currents in materials at the atomic scale . The macroscopic  equations define two new\n",
      "auxiliary  fields  that  describe  the  large-scale  behaviour  of  matter  without  having  to\n",
      "consider atomic-scale charges and quantum phenomena like spins. However, their use\n",
      "requires experimentally determined parameters for a phenomenological description of\n",
      "the electromagnetic response of materi\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
      "Final Answer: Maxwell's equations are a set of coupled partial differential equations that, together with the Lorentz force law, form the foundation of classical electromagnetism, classical optics, and electric circuits. They describe how electric and magnetic fields are generated by charges, currents, and changes of the fields, and how fluctuations in electromagnetic fields (waves) propagate at a constant speed in vacuum.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"Maxwell's equations are a set of coupled partial differential equations that, together with the Lorentz force law, form the foundation of classical electromagnetism, classical optics, and electric circuits. They describe how electric and magnetic fields are generated by charges, currents, and changes of the fields, and how fluctuations in electromagnetic fields (waves) propagate at a constant speed in vacuum.\""
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def openaiKey():\n",
    "    with open(\"../../key.txt\") as f:\n",
    "        return f.read().strip(\"\\n\")\n",
    "    \n",
    "def serpKey():\n",
    "    with open(\"../../serpkey.txt\") as f:\n",
    "        return f.read().strip(\"\\n\")  \n",
    "\n",
    "# -----------------------------------------------------\n",
    "    \n",
    "from langchain.utilities import GoogleSerperAPIWrapper\n",
    "from langchain.llms.openai import OpenAI\n",
    "from langchain.agents import AgentType, initialize_agent, Tool\n",
    "\n",
    "from weasyprint import HTML\n",
    "from PyPDF2 import PdfReader\n",
    "\n",
    "import openai\n",
    "import re\n",
    "openai.api_key = openaiKey()  \n",
    "\n",
    "search = GoogleSerperAPIWrapper(serper_api_key=serpKey())\n",
    "\n",
    "def wrappedSearch(query: str) -> str:\n",
    "    # get top google results for the query\n",
    "    queryResults = search.results(query)\n",
    "\n",
    "    # build a text summary of the results\n",
    "    summaries = \"\"\n",
    "    for i, result in enumerate(queryResults[\"organic\"]):\n",
    "        summaries += \"{}.\\n\\t{}\\n\\t{}\\n\\t{}\\n\".format(i, result[\"title\"], result[\"snippet\"], result[\"link\"])\n",
    "\n",
    "    # choose one of the provided search results\n",
    "    complete = openai.Completion.create(\n",
    "        model=\"text-davinci-003\",\n",
    "        prompt=\"Which of the following websites is most relevant to the prompt '{}'? (provide the number only):\\n{}\".format(query, summaries)\n",
    "    )\n",
    "    idx = int(re.search(r\"(\\d)\", complete[\"choices\"][0][\"text\"]).group(1))\n",
    "    link = queryResults[\"organic\"][idx][\"link\"]\n",
    "\n",
    "    # parse website text\n",
    "    # TODO: do something that isn't absurdly inefficient here:\n",
    "    HTML(link).write_pdf('./out.pdf')\n",
    "    reader = PdfReader('./out.pdf')\n",
    "    content = \"\"\n",
    "    for page in reader.pages:\n",
    "        content += page.extract_text()\n",
    "\n",
    "    # return website contents\n",
    "    # TODO: handle limited query size\n",
    "    return content[:2000]\n",
    "\n",
    "\n",
    "llm = OpenAI(temperature=0, openai_api_key=openaiKey())\n",
    "\n",
    "tools = [\n",
    "    Tool.from_function(\n",
    "        func=wrappedSearch,\n",
    "        name = \"Search\",\n",
    "        description=\"useful for when you need to answer questions about current events or facts. Get the full contents of the top search result from the web\"\n",
    "    ),\n",
    "]\n",
    "\n",
    "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n",
    "agent.run(\"tell me about maxwells equations\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.11.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}