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QbBv7iSjrA/U71HaV",
|
8 |
+
"include_colab_link": true
|
9 |
+
},
|
10 |
+
"kernelspec": {
|
11 |
+
"name": "python3",
|
12 |
+
"display_name": "Python 3"
|
13 |
+
},
|
14 |
+
"language_info": {
|
15 |
+
"name": "python"
|
16 |
+
}
|
17 |
+
},
|
18 |
+
"cells": [
|
19 |
+
{
|
20 |
+
"cell_type": "markdown",
|
21 |
+
"metadata": {
|
22 |
+
"id": "view-in-github",
|
23 |
+
"colab_type": "text"
|
24 |
+
},
|
25 |
+
"source": [
|
26 |
+
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/Web_Search_API.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "code",
|
31 |
+
"execution_count": 2,
|
32 |
+
"metadata": {
|
33 |
+
"colab": {
|
34 |
+
"base_uri": "https://localhost:8080/"
|
35 |
+
},
|
36 |
+
"id": "JboB5VaCJUrb",
|
37 |
+
"outputId": "2433bc46-9d7f-476e-bfe9-0e4be5f4e51a"
|
38 |
+
},
|
39 |
+
"outputs": [
|
40 |
+
{
|
41 |
+
"output_type": "stream",
|
42 |
+
"name": "stdout",
|
43 |
+
"text": [
|
44 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m12.5/12.5 MB\u001b[0m \u001b[31m24.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
45 |
+
"\u001b[?25h"
|
46 |
+
]
|
47 |
+
}
|
48 |
+
],
|
49 |
+
"source": [
|
50 |
+
"!pip install -q llama-index==0.10.5 openai==1.12.0 tiktoken==0.6.0 llama-index-tools-google==0.1.3"
|
51 |
+
]
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"source": [
|
56 |
+
"import os\n",
|
57 |
+
"\n",
|
58 |
+
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
|
59 |
+
"os.environ[\"OPENAI_API_KEY\"] = \"<YOUR_OPENAI_KEY>\""
|
60 |
+
],
|
61 |
+
"metadata": {
|
62 |
+
"id": "1NKAn5scN_g9"
|
63 |
+
},
|
64 |
+
"execution_count": 5,
|
65 |
+
"outputs": []
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"cell_type": "markdown",
|
69 |
+
"source": [
|
70 |
+
"# Define Google Search Tool"
|
71 |
+
],
|
72 |
+
"metadata": {
|
73 |
+
"id": "0LMypoqUyuXq"
|
74 |
+
}
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "code",
|
78 |
+
"source": [
|
79 |
+
"from llama_index.tools.google import GoogleSearchToolSpec\n",
|
80 |
+
"\n",
|
81 |
+
"tool_spec = GoogleSearchToolSpec(key=\"[GOOGLE_API_KEY]\", engine=\"[GOOGLE_ENGINE_ID]\")"
|
82 |
+
],
|
83 |
+
"metadata": {
|
84 |
+
"id": "4Q7sc69nJvWI"
|
85 |
+
},
|
86 |
+
"execution_count": 54,
|
87 |
+
"outputs": []
|
88 |
+
},
|
89 |
+
{
|
90 |
+
"cell_type": "code",
|
91 |
+
"source": [
|
92 |
+
"# Import and initialize our tool spec\n",
|
93 |
+
"from llama_index.core.tools.tool_spec.load_and_search import LoadAndSearchToolSpec\n",
|
94 |
+
"\n",
|
95 |
+
"# Wrap the google search tool to create an index on top of the returned Google search\n",
|
96 |
+
"wrapped_tool = LoadAndSearchToolSpec.from_defaults(\n",
|
97 |
+
" tool_spec.to_tool_list()[0],\n",
|
98 |
+
").to_tool_list()"
|
99 |
+
],
|
100 |
+
"metadata": {
|
101 |
+
"id": "VrbuIOaMeOIf"
|
102 |
+
},
|
103 |
+
"execution_count": 69,
|
104 |
+
"outputs": []
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "markdown",
|
108 |
+
"source": [
|
109 |
+
"# Create the Agent"
|
110 |
+
],
|
111 |
+
"metadata": {
|
112 |
+
"id": "T3ENpLyBy7UL"
|
113 |
+
}
|
114 |
+
},
|
115 |
+
{
|
116 |
+
"cell_type": "code",
|
117 |
+
"source": [
|
118 |
+
"from llama_index.agent.openai import OpenAIAgent\n",
|
119 |
+
"\n",
|
120 |
+
"agent = OpenAIAgent.from_tools(wrapped_tool, verbose=False)"
|
121 |
+
],
|
122 |
+
"metadata": {
|
123 |
+
"id": "-_Ab47ppK8b2"
|
124 |
+
},
|
125 |
+
"execution_count": 70,
|
126 |
+
"outputs": []
|
127 |
+
},
|
128 |
+
{
|
129 |
+
"cell_type": "code",
|
130 |
+
"source": [
|
131 |
+
"res = agent.chat(\"How many parameters LLaMA2 model has?\")"
|
132 |
+
],
|
133 |
+
"metadata": {
|
134 |
+
"id": "YcUyz1-FlCQ8"
|
135 |
+
},
|
136 |
+
"execution_count": 71,
|
137 |
+
"outputs": []
|
138 |
+
},
|
139 |
+
{
|
140 |
+
"cell_type": "code",
|
141 |
+
"source": [
|
142 |
+
"res.response"
|
143 |
+
],
|
144 |
+
"metadata": {
|
145 |
+
"colab": {
|
146 |
+
"base_uri": "https://localhost:8080/",
|
147 |
+
"height": 35
|
148 |
+
},
|
149 |
+
"id": "w4wK5sY-lOOv",
|
150 |
+
"outputId": "8090a106-6fac-4514-fdbd-c72a01b28169"
|
151 |
+
},
|
152 |
+
"execution_count": 72,
|
153 |
+
"outputs": [
|
154 |
+
{
|
155 |
+
"output_type": "execute_result",
|
156 |
+
"data": {
|
157 |
+
"text/plain": [
|
158 |
+
"'The LLaMA2 model has parameters available in three different sizes: 7 billion, 13 billion, and 70 billion.'"
|
159 |
+
],
|
160 |
+
"application/vnd.google.colaboratory.intrinsic+json": {
|
161 |
+
"type": "string"
|
162 |
+
}
|
163 |
+
},
|
164 |
+
"metadata": {},
|
165 |
+
"execution_count": 72
|
166 |
+
}
|
167 |
+
]
|
168 |
+
},
|
169 |
+
{
|
170 |
+
"cell_type": "code",
|
171 |
+
"source": [
|
172 |
+
"res.sources"
|
173 |
+
],
|
174 |
+
"metadata": {
|
175 |
+
"colab": {
|
176 |
+
"base_uri": "https://localhost:8080/"
|
177 |
+
},
|
178 |
+
"id": "TM_cvBA1nTJM",
|
179 |
+
"outputId": "0bf3533a-c62d-4d0d-bd76-76c043477042"
|
180 |
+
},
|
181 |
+
"execution_count": 73,
|
182 |
+
"outputs": [
|
183 |
+
{
|
184 |
+
"output_type": "execute_result",
|
185 |
+
"data": {
|
186 |
+
"text/plain": [
|
187 |
+
"[ToolOutput(content='Content loaded! You can now search the information using read_google_search', tool_name='google_search', raw_input={'args': (), 'kwargs': {'query': 'parameters of LLaMA2 model'}}, raw_output='Content loaded! You can now search the information using read_google_search', is_error=False),\n",
|
188 |
+
" ToolOutput(content='Answer: The parameters of the LLaMA2 model are available in three different sizes: 7 billion, 13 billion, and 70 billion.', tool_name='read_google_search', raw_input={'args': (), 'kwargs': {'query': 'parameters of LLaMA2 model'}}, raw_output='Answer: The parameters of the LLaMA2 model are available in three different sizes: 7 billion, 13 billion, and 70 billion.', is_error=False)]"
|
189 |
+
]
|
190 |
+
},
|
191 |
+
"metadata": {},
|
192 |
+
"execution_count": 73
|
193 |
+
}
|
194 |
+
]
|
195 |
+
},
|
196 |
+
{
|
197 |
+
"cell_type": "code",
|
198 |
+
"source": [],
|
199 |
+
"metadata": {
|
200 |
+
"id": "SPUgKiKpygLn"
|
201 |
+
},
|
202 |
+
"execution_count": null,
|
203 |
+
"outputs": []
|
204 |
+
}
|
205 |
+
]
|
206 |
+
}
|