File size: 9,982 Bytes
2c8cfc0
 
847491b
 
 
 
 
 
 
c768949
847491b
 
 
 
 
 
 
 
 
 
b1f1dd7
86c7913
b1f1dd7
 
 
 
c768949
2c8cfc0
 
c768949
 
 
 
 
 
 
 
2c8cfc0
 
 
 
 
847491b
2c8cfc0
b1f1dd7
 
 
 
 
 
 
c768949
b1f1dd7
 
2c8cfc0
 
 
c768949
 
b1f1dd7
c768949
 
2c8cfc0
 
847491b
 
 
 
 
 
 
55bb1db
847491b
 
2c8cfc0
 
c768949
2c8cfc0
 
b1f1dd7
 
 
 
 
847491b
b1f1dd7
 
2c8cfc0
cfc0db8
 
c768949
cfc0db8
 
c768949
cfc0db8
 
2c8cfc0
 
 
c768949
 
 
b1f1dd7
 
c768949
 
b1f1dd7
 
 
 
 
c768949
b1f1dd7
 
2c8cfc0
847491b
 
 
 
 
 
 
 
 
 
 
 
c768949
847491b
 
 
c768949
 
847491b
 
 
 
c768949
847491b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c768949
847491b
 
 
 
 
c768949
847491b
c768949
847491b
c768949
847491b
 
 
c768949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c8cfc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Examples\n",
    "\n",
    "Below you can find some examples of how to use the 🤖 `Megabots` library.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a bot\n",
    "\n",
    "The `bot` object is the main object of the library. It is used to create a bot and to interact with it.\n",
    "\n",
    "The `index` argument specifies the index to use for the bot. It can either be a saved index file (e.g., `index.pkl`) or a directory of documents (e.g., `./index`). In the case of the directory the index will be automatically created. If no index is specified the `bot` will look for `index.pkl` or `./index`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/momegas/Desktop/qnabot/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n",
      "Loading path from pickle file:  ./index.pkl ...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from megabots import bot\n",
    "\n",
    "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\")\n",
    "\n",
    "qnabot.ask(\"what was the first roster of the avengers?\")\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Changing the bot's prompt\n",
    "\n",
    "You can change the bot's promnpt to customize it to your needs. In the `qna-over-docs` type of bot you will need to pass 2 variables for the `context` (knwoledge searched from the index) and the `question` (the human question).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n",
      "Loading path from pickle file:  ./index.pkl ...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from megabots import bot\n",
    "\n",
    "prompt = \"\"\"\n",
    "Use the following pieces of context to answer the question at the end. \n",
    "If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
    "Answer in the style of Tony Stark.\n",
    "\n",
    "{context}\n",
    "\n",
    "Question: {question}\n",
    "Helpful humorous answer:\"\"\"\n",
    "\n",
    "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", prompt=prompt)\n",
    "\n",
    "qnabot.ask(\"what was the first roster of the avengers?\")\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using Megabots with Milvus\n",
    "\n",
    "Megabots `bot` can also use Milvus as a backend for its search engine. You can find an example of how to do it below.\n",
    "\n",
    "In order to run Milvus you need to follow [this guide](https://milvus.io/docs/example_code.md) to download a docker compose file and run it.\n",
    "The command is:\n",
    "\n",
    "```bash\n",
    "wget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.py\n",
    "```\n",
    "\n",
    "You can then [install Attu](https://milvus.io/docs/attu_install-docker.md) as a management tool for Milvus\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from megabots import bot\n",
    "\n",
    "# Attach a vectorstore by passing the name of the database. Default port for milvus is 19530 and default host is localhost\n",
    "# Point it to your files directory so that it can index the files and add them to the vectorstore\n",
    "bot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=\"milvus\")\n",
    "\n",
    "bot.ask(\"what was the first roster of the avengers?\")\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or use the `vectorstore` factory function for more customisation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n"
     ]
    }
   ],
   "source": [
    "from megabots import bot, vectorstore\n",
    "\n",
    "milvus = vectorstore(\"milvus\", host=\"localhost\", port=19530)\n",
    "\n",
    "bot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=milvus)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Working with memory\n",
    "\n",
    "You can easily add memory to your `bot` using the `memory` parameter. It accepts a string with the type of the memory to be used. This defaults to some sane dafaults.\n",
    "Should you need more configuration, you can use the `memory` function and pass the type of memory and the configuration you need.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n",
      "Loading path from pickle file:  ./index.pkl ...\n",
      "Iron Man is a superhero character who is a member of the Avengers. He is known for his high-tech suit of armor and his alter ego, Tony Stark.\n",
      "Yes, Iron Man was part of the original Avengers lineup.\n"
     ]
    }
   ],
   "source": [
    "from megabots import bot\n",
    "\n",
    "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", memory=\"conversation-buffer\")\n",
    "\n",
    "print(qnabot.ask(\"who is iron man?\"))\n",
    "print(qnabot.ask(\"was he in the first roster?\"))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or using the `memory`factory function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n",
      "Loading path from pickle file:  ./index.pkl ...\n",
      "Iron Man is a superhero character who is a member of the Avengers. He is known for his high-tech suit of armor and his alter ego, Tony Stark.\n",
      "Yes, Iron Man was part of the original Avengers lineup.\n"
     ]
    }
   ],
   "source": [
    "from megabots import bot, memory\n",
    "\n",
    "qnabot = bot(\n",
    "    \"qna-over-docs\",\n",
    "    index=\"./index.pkl\",\n",
    "    memory=memory(\"conversation-buffer-window\", k=5),\n",
    ")\n",
    "\n",
    "print(qnabot.ask(\"who is iron man?\"))\n",
    "print(qnabot.ask(\"was he in the first roster?\"))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NOTE: For the `qna-over-docs` bot, when using memory and passing your custom prompt, it is important to remember to pass one more variable to your custom prompt to facilitate for chat history. The variable name is `history`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using model: gpt-3.5-turbo\n",
      "Loading path from pickle file:  ./index.pkl ...\n",
      "Iron Man is a superhero character who is a member of the Avengers. He is a wealthy businessman named Tony Stark who uses his advanced technology to create a suit of armor that gives him superhuman abilities.\n",
      "Yes, Iron Man was part of the original Avengers lineup.\n"
     ]
    }
   ],
   "source": [
    "from megabots import bot\n",
    "\n",
    "prompt = \"\"\"\n",
    "Use the following pieces of context to answer the question at the end. \n",
    "If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
    "\n",
    "{context}\n",
    "\n",
    "{history}\n",
    "Human: {question}\n",
    "AI:\"\"\"\n",
    "\n",
    "qnabot = bot(\n",
    "    \"qna-over-docs\",\n",
    "    prompt=prompt,\n",
    "    index=\"./index.pkl\",\n",
    "    memory=\"conversation-buffer\",\n",
    ")\n",
    "\n",
    "print(qnabot.ask(\"who is iron man?\"))\n",
    "print(qnabot.ask(\"was he in the first roster?\"))"
   ]
  }
 ],
 "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.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}