momegas commited on
Commit
c768949
β€’
1 Parent(s): a091159

πŸŽ‰ Bot memory, many fixes, some refactoring

Browse files
Files changed (7) hide show
  1. README.md +118 -13
  2. example.ipynb +184 -38
  3. megabots/__init__.py +1 -0
  4. megabots/bot.py +37 -22
  5. megabots/memory.py +9 -50
  6. megabots/prompt.py +18 -0
  7. tests/test_memory.py +16 -33
README.md CHANGED
@@ -2,7 +2,6 @@
2
 
3
  [![Tests](https://github.com/momegas/qnabot/actions/workflows/python-package.yml/badge.svg)](https://github.com/momegas/qnabot/actions/workflows/python-package.yml)
4
  ![](https://dcbadge.vercel.app/api/server/zkqDWk5S7P?style=flat&n&compact=true)
5
- <a href="https://www.producthunt.com/posts/megabots-2?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-megabots&#0045;2" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=390033&theme=light" alt="Megabots - πŸ€–&#0032;Production&#0032;ready&#0032;full&#0045;stack&#0032;LLM&#0032;apps&#0032;made&#0032;mega&#0045;easy | Product Hunt" style="width: 150px; height: 34px;" width="250" height="54" /></a>
6
 
7
  πŸ€– Megabots provides State-of-the-art, production ready LLM apps made mega-easy, so you don't have to build them from scratch 🀯 Create a bot, now 🫡
8
 
@@ -58,13 +57,115 @@ qnabot = bot("qna-over-docs", index="./index")
58
 
59
  # Change the model
60
  qnabot = bot("qna-over-docs", model="text-davinci-003")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- # Change the prompt
63
- prompt_template = "Be humourous in your responses. Question: {question}\nContext: {context}, Answer:"
64
- prompt_variables=["question", "context"]
65
- qnabot = bot("qna-over-docs", prompt_template=prompt_template, prompt_variables=prompt_variables)
 
 
 
 
66
  ```
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  You can also create a FastAPI app that will expose the bot as an API using the create_app function.
69
  Assuming you file is called `main.py` run `uvicorn main:app --reload` to run the API locally.
70
  You should then be able to visit `http://localhost:8000/docs` to see the API documentation.
@@ -75,6 +176,8 @@ from megabots import bot, create_api
75
  app = create_app(bot("qna-over-docs"))
76
  ```
77
 
 
 
78
  You can expose a gradio UI for the bot using `create_interface` function.
79
  Assuming your file is called `ui.py` run `gradio qnabot/ui.py` to run the UI locally.
80
  You should then be able to visit `http://127.0.0.1:7860` to see the API documentation.
@@ -89,14 +192,16 @@ demo = create_interface(bot("qna-over-docs"))
89
 
90
  The `bot` function should serve as the starting point for creating and customising your bot. Below is a list of the available arguments in `bot`.
91
 
92
- | Argument | Description |
93
- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
94
- | task | The type of bot to create. Available options: `qna-over-docs`. More comming soon |
95
- | index | 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 `bot` will look for `index.pkl` or `./index` |
96
- | model | The name of the model to use for the bot. You can specify a different model by providing its name, like "text-davinci-003". Supported models: `gpt-3.5-turbo` (default),`text-davinci-003` More comming soon. |
97
- | prompt_template | A string template for the prompt, which defines the format of the question and context passed to the model. The template should include placeholders for the variables specified in `prompt_variables`. |
98
- | prompt_variables | A list of variables to be used in the prompt template. These variables are replaced with actual values when the bot processes a query. |
99
- | sources | When `sources` is `True` the bot will also include sources in the response. A known [issue](https://github.com/hwchase17/langchain/issues/2858) exists, where if you pass a custom prompt with sources the code breaks. |
 
 
100
 
101
  ## How QnA bot works
102
 
 
2
 
3
  [![Tests](https://github.com/momegas/qnabot/actions/workflows/python-package.yml/badge.svg)](https://github.com/momegas/qnabot/actions/workflows/python-package.yml)
4
  ![](https://dcbadge.vercel.app/api/server/zkqDWk5S7P?style=flat&n&compact=true)
 
5
 
6
  πŸ€– Megabots provides State-of-the-art, production ready LLM apps made mega-easy, so you don't have to build them from scratch 🀯 Create a bot, now 🫡
7
 
 
57
 
58
  # Change the model
59
  qnabot = bot("qna-over-docs", model="text-davinci-003")
60
+ ```
61
+
62
+ ## Changing the bot's prompt
63
+
64
+ You can change the bots 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).
65
+
66
+ ```python
67
+ from megabots import bot
68
+
69
+ prompt = """
70
+ Use the following pieces of context to answer the question at the end.
71
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
72
+ Answer in the style of Tony Stark.
73
+
74
+ {context}
75
+
76
+ Question: {question}
77
+ Helpful humorous answer:"""
78
+
79
+ qnabot = bot("qna-over-docs", index="./index.pkl", prompt=prompt)
80
+
81
+ qnabot.ask("what was the first roster of the avengers?")
82
+ ```
83
+
84
+ ## Working with memory
85
+
86
+ 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.
87
+ Should you need more configuration, you can use the `memory` function and pass the type of memory and the configuration you need.
88
+
89
+ ```python
90
+ from megabots import bot
91
+
92
+ qnabot = bot("qna-over-docs", index="./index.pkl", memory="conversation-buffer")
93
+
94
+ print(qnabot.ask("who is iron man?"))
95
+ print(qnabot.ask("was he in the first roster?"))
96
+ # Bot should understand who "he" refers to.
97
+ ```
98
+
99
+ Or using the `memory`factory function
100
+
101
+ ```python
102
+ from megabots import bot, memory
103
+
104
+ mem("conversation-buffer-window", k=5)
105
+
106
+ qnabot = bot("qna-over-docs", index="./index.pkl", memory=mem)
107
+
108
+ print(qnabot.ask("who is iron man?"))
109
+ print(qnabot.ask("was he in the first roster?"))
110
+ ```
111
+
112
+ 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`.
113
+
114
+ ```python
115
+ from megabots import bot
116
+
117
+ prompt = """
118
+ Use the following pieces of context to answer the question at the end.
119
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
120
+
121
+ {context}
122
 
123
+ {history}
124
+ Human: {question}
125
+ AI:"""
126
+
127
+ qnabot = bot("qna-over-docs", prompt=prompt, index="./index.pkl", memory="conversation-buffer")
128
+
129
+ print(qnabot.ask("who is iron man?"))
130
+ print(qnabot.ask("was he in the first roster?"))
131
  ```
132
 
133
+ ## Using Megabots with Milvus (more DBs comming soon)
134
+
135
+ Megabots `bot` can also use Milvus as a backend for its search engine. You can find an example of how to do it below.
136
+
137
+ 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.
138
+ The command is:
139
+
140
+ ```bash
141
+ wget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.py
142
+ ```
143
+
144
+ You can then [install Attu](https://milvus.io/docs/attu_install-docker.md) as a management tool for Milvus
145
+
146
+ ```python
147
+ from megabots import bot
148
+
149
+ # Attach a vectorstore by passing the name of the database. Default port for milvus is 19530 and default host is localhost
150
+ # Point it to your files directory so that it can index the files and add them to the vectorstore
151
+ bot = bot("qna-over-docs", index="./examples/files/", vectorstore="milvus")
152
+
153
+ bot.ask("what was the first roster of the avengers?")
154
+ ```
155
+
156
+ Or use the `vectorstore` factory function for more customisation
157
+
158
+ ```python
159
+
160
+ from megabots import bot, vectorstore
161
+
162
+ milvus = vectorstore("milvus", host="localhost", port=19530)
163
+
164
+ bot = bot("qna-over-docs", index="./examples/files/", vectorstore=milvus)
165
+ ```
166
+
167
+ ## Exposing an API with FastAPI
168
+
169
  You can also create a FastAPI app that will expose the bot as an API using the create_app function.
170
  Assuming you file is called `main.py` run `uvicorn main:app --reload` to run the API locally.
171
  You should then be able to visit `http://localhost:8000/docs` to see the API documentation.
 
176
  app = create_app(bot("qna-over-docs"))
177
  ```
178
 
179
+ ## Exposing a Gradio chat-like interface
180
+
181
  You can expose a gradio UI for the bot using `create_interface` function.
182
  Assuming your file is called `ui.py` run `gradio qnabot/ui.py` to run the UI locally.
183
  You should then be able to visit `http://127.0.0.1:7860` to see the API documentation.
 
192
 
193
  The `bot` function should serve as the starting point for creating and customising your bot. Below is a list of the available arguments in `bot`.
194
 
195
+ | Argument | Description |
196
+ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
197
+ | task | The type of bot to create. Available options: `qna-over-docs`. More comming soon |
198
+ | index | 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 `bot` will look for `index.pkl` or `./index` |
199
+ | model | The name of the model to use for the bot. You can specify a different model by providing its name, like "text-davinci-003". Supported models: `gpt-3.5-turbo` (default),`text-davinci-003` More comming soon. |
200
+ | prompt | A string template for the prompt, which defines the format of the question and context passed to the model. The template should include placeholder variables like so: `context`, `{question}` and in the case of using memory `history`. |
201
+ | memory | The type of memory to be used by the bot. Can be a string with the type of the memory or you can use `memory` factory function. Supported memories: `conversation-buffer`, `conversation-buffer-window` |
202
+ | vectorstore | The vectorstore to be used for the index. Can be a string with the name of the databse or you can use `vectorstore` factory function. Supported DBs: `milvus`. |
203
+
204
+ | sources | When `sources` is `True` the bot will also include sources in the response. A known [issue](https://github.com/hwchase17/langchain/issues/2858) exists, where if you pass a custom prompt with sources the code breaks. |
205
 
206
  ## How QnA bot works
207
 
example.ipynb CHANGED
@@ -7,17 +7,7 @@
7
  "source": [
8
  "# Examples\n",
9
  "\n",
10
- "Below you can find some examples of how to use the πŸ€– `Megabots` library."
11
- ]
12
- },
13
- {
14
- "cell_type": "code",
15
- "execution_count": 13,
16
- "metadata": {},
17
- "outputs": [],
18
- "source": [
19
- "from megabots import bot\n",
20
- "from dotenv import load_dotenv"
21
  ]
22
  },
23
  {
@@ -29,14 +19,22 @@
29
  "\n",
30
  "The `bot` object is the main object of the library. It is used to create a bot and to interact with it.\n",
31
  "\n",
32
- "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 `bot` will look for `index.pkl` or `./index`"
33
  ]
34
  },
35
  {
36
  "cell_type": "code",
37
- "execution_count": 14,
38
  "metadata": {},
39
  "outputs": [
 
 
 
 
 
 
 
 
40
  {
41
  "name": "stdout",
42
  "output_type": "stream",
@@ -51,14 +49,17 @@
51
  "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
52
  ]
53
  },
54
- "execution_count": 14,
55
  "metadata": {},
56
  "output_type": "execute_result"
57
  }
58
  ],
59
  "source": [
 
 
60
  "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\")\n",
61
- "qnabot.ask(\"what was the first roster of the avengers?\")"
 
62
  ]
63
  },
64
  {
@@ -68,12 +69,12 @@
68
  "source": [
69
  "### Changing the bot's prompt\n",
70
  "\n",
71
- "You can change the bots promnpt to customize it to your needs."
72
  ]
73
  },
74
  {
75
  "cell_type": "code",
76
- "execution_count": 15,
77
  "metadata": {},
78
  "outputs": [
79
  {
@@ -87,33 +88,29 @@
87
  {
88
  "data": {
89
  "text/plain": [
90
- "\"Hmmm! Let me think about that... Ah yes, the original Avengers lineup included Iron Man, Thor, Hulk, Ant-Man, and the Wasp. They were like the ultimate superhero squad, except for maybe the Teenage Mutant Ninja Turtles. But let's be real, they were just a bunch of turtles who liked pizza.\""
91
  ]
92
  },
93
- "execution_count": 15,
94
  "metadata": {},
95
  "output_type": "execute_result"
96
  }
97
  ],
98
  "source": [
99
- "prompt_template = \"\"\"\n",
 
 
100
  "Use the following pieces of context to answer the question at the end. \n",
101
  "If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
102
- "Be very playfull and humourous in your responses. always try to make the user laugh.\n",
103
- "Always start your answers with 'Hmmm! Let me think about that...'\n",
104
  "{context}\n",
105
  "\n",
106
  "Question: {question}\n",
107
  "Helpful humorous answer:\"\"\"\n",
108
  "\n",
109
- "load_dotenv()\n",
110
  "\n",
111
- "qnabot = bot(\n",
112
- " \"qna-over-docs\",\n",
113
- " index=\"./index.pkl\",\n",
114
- " prompt_template=prompt_template,\n",
115
- " prompt_variables=[\"context\", \"question\"],\n",
116
- ")\n",
117
  "qnabot.ask(\"what was the first roster of the avengers?\")\n"
118
  ]
119
  },
@@ -128,16 +125,17 @@
128
  "\n",
129
  "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",
130
  "The command is:\n",
131
- " \n",
132
  "```bash\n",
133
  "wget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.py\n",
134
  "```\n",
135
- "You can then [install Attu](https://milvus.io/docs/attu_install-docker.md) as a management tool for Milvus"
 
136
  ]
137
  },
138
  {
139
  "cell_type": "code",
140
- "execution_count": 11,
141
  "metadata": {},
142
  "outputs": [
143
  {
@@ -153,22 +151,170 @@
153
  "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
154
  ]
155
  },
156
- "execution_count": 11,
157
  "metadata": {},
158
  "output_type": "execute_result"
159
  }
160
  ],
161
  "source": [
162
- "from megabots import bot, vectorstore\n",
163
- "\n",
164
- "# Create a vectorstore object. Default port is 19530 and default host is localhost\n",
165
- "milvus = vectorstore(\"milvus\")\n",
166
  "\n",
 
167
  "# Point it to your files directory so that it can index the files and add them to the vectorstore\n",
168
- "bot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=milvus)\n",
169
  "\n",
170
  "bot.ask(\"what was the first roster of the avengers?\")\n"
171
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  }
173
  ],
174
  "metadata": {
 
7
  "source": [
8
  "# Examples\n",
9
  "\n",
10
+ "Below you can find some examples of how to use the πŸ€– `Megabots` library.\n"
 
 
 
 
 
 
 
 
 
 
11
  ]
12
  },
13
  {
 
19
  "\n",
20
  "The `bot` object is the main object of the library. It is used to create a bot and to interact with it.\n",
21
  "\n",
22
+ "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 `bot` will look for `index.pkl` or `./index`\n"
23
  ]
24
  },
25
  {
26
  "cell_type": "code",
27
+ "execution_count": 1,
28
  "metadata": {},
29
  "outputs": [
30
+ {
31
+ "name": "stderr",
32
+ "output_type": "stream",
33
+ "text": [
34
+ "/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",
35
+ " from .autonotebook import tqdm as notebook_tqdm\n"
36
+ ]
37
+ },
38
  {
39
  "name": "stdout",
40
  "output_type": "stream",
 
49
  "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
50
  ]
51
  },
52
+ "execution_count": 1,
53
  "metadata": {},
54
  "output_type": "execute_result"
55
  }
56
  ],
57
  "source": [
58
+ "from megabots import bot\n",
59
+ "\n",
60
  "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\")\n",
61
+ "\n",
62
+ "qnabot.ask(\"what was the first roster of the avengers?\")\n"
63
  ]
64
  },
65
  {
 
69
  "source": [
70
  "### Changing the bot's prompt\n",
71
  "\n",
72
+ "You can change the bots 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"
73
  ]
74
  },
75
  {
76
  "cell_type": "code",
77
+ "execution_count": 2,
78
  "metadata": {},
79
  "outputs": [
80
  {
 
88
  {
89
  "data": {
90
  "text/plain": [
91
+ "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
92
  ]
93
  },
94
+ "execution_count": 2,
95
  "metadata": {},
96
  "output_type": "execute_result"
97
  }
98
  ],
99
  "source": [
100
+ "from megabots import bot\n",
101
+ "\n",
102
+ "prompt = \"\"\"\n",
103
  "Use the following pieces of context to answer the question at the end. \n",
104
  "If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
105
+ "Answer in the style of Tony Stark.\n",
106
+ "\n",
107
  "{context}\n",
108
  "\n",
109
  "Question: {question}\n",
110
  "Helpful humorous answer:\"\"\"\n",
111
  "\n",
112
+ "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", prompt=prompt)\n",
113
  "\n",
 
 
 
 
 
 
114
  "qnabot.ask(\"what was the first roster of the avengers?\")\n"
115
  ]
116
  },
 
125
  "\n",
126
  "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",
127
  "The command is:\n",
128
+ "\n",
129
  "```bash\n",
130
  "wget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.py\n",
131
  "```\n",
132
+ "\n",
133
+ "You can then [install Attu](https://milvus.io/docs/attu_install-docker.md) as a management tool for Milvus\n"
134
  ]
135
  },
136
  {
137
  "cell_type": "code",
138
+ "execution_count": 3,
139
  "metadata": {},
140
  "outputs": [
141
  {
 
151
  "'The first roster of the Avengers included Iron Man, Thor, Hulk, Ant-Man, and the Wasp.'"
152
  ]
153
  },
154
+ "execution_count": 3,
155
  "metadata": {},
156
  "output_type": "execute_result"
157
  }
158
  ],
159
  "source": [
160
+ "from megabots import bot\n",
 
 
 
161
  "\n",
162
+ "# Attach a vectorstore by passing the name of the database. Default port for milvus is 19530 and default host is localhost\n",
163
  "# Point it to your files directory so that it can index the files and add them to the vectorstore\n",
164
+ "bot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=\"milvus\")\n",
165
  "\n",
166
  "bot.ask(\"what was the first roster of the avengers?\")\n"
167
  ]
168
+ },
169
+ {
170
+ "attachments": {},
171
+ "cell_type": "markdown",
172
+ "metadata": {},
173
+ "source": [
174
+ "Or use the `vectorstore` factory function for more customisation\n"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": 4,
180
+ "metadata": {},
181
+ "outputs": [
182
+ {
183
+ "name": "stdout",
184
+ "output_type": "stream",
185
+ "text": [
186
+ "Using model: gpt-3.5-turbo\n"
187
+ ]
188
+ }
189
+ ],
190
+ "source": [
191
+ "from megabots import bot, vectorstore\n",
192
+ "\n",
193
+ "milvus = vectorstore(\"milvus\", host=\"localhost\", port=19530)\n",
194
+ "\n",
195
+ "bot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=milvus)\n"
196
+ ]
197
+ },
198
+ {
199
+ "attachments": {},
200
+ "cell_type": "markdown",
201
+ "metadata": {},
202
+ "source": [
203
+ "### Working with memory\n",
204
+ "\n",
205
+ "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",
206
+ "Should you need more configuration, you can use the `memory` function and pass the type of memory and the configuration you need.\n"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": 5,
212
+ "metadata": {},
213
+ "outputs": [
214
+ {
215
+ "name": "stdout",
216
+ "output_type": "stream",
217
+ "text": [
218
+ "Using model: gpt-3.5-turbo\n",
219
+ "Loading path from pickle file: ./index.pkl ...\n",
220
+ "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",
221
+ "Yes, Iron Man was part of the original Avengers lineup.\n"
222
+ ]
223
+ }
224
+ ],
225
+ "source": [
226
+ "from megabots import bot\n",
227
+ "\n",
228
+ "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", memory=\"conversation-buffer\")\n",
229
+ "\n",
230
+ "print(qnabot.ask(\"who is iron man?\"))\n",
231
+ "print(qnabot.ask(\"was he in the first roster?\"))\n"
232
+ ]
233
+ },
234
+ {
235
+ "attachments": {},
236
+ "cell_type": "markdown",
237
+ "metadata": {},
238
+ "source": [
239
+ "Or using the `memory`factory function"
240
+ ]
241
+ },
242
+ {
243
+ "cell_type": "code",
244
+ "execution_count": 6,
245
+ "metadata": {},
246
+ "outputs": [
247
+ {
248
+ "name": "stdout",
249
+ "output_type": "stream",
250
+ "text": [
251
+ "Using model: gpt-3.5-turbo\n",
252
+ "Loading path from pickle file: ./index.pkl ...\n",
253
+ "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",
254
+ "Yes, Iron Man was part of the original Avengers lineup.\n"
255
+ ]
256
+ }
257
+ ],
258
+ "source": [
259
+ "from megabots import bot, memory\n",
260
+ "\n",
261
+ "qnabot = bot(\n",
262
+ " \"qna-over-docs\",\n",
263
+ " index=\"./index.pkl\",\n",
264
+ " memory=memory(\"conversation-buffer-window\", k=5),\n",
265
+ ")\n",
266
+ "\n",
267
+ "print(qnabot.ask(\"who is iron man?\"))\n",
268
+ "print(qnabot.ask(\"was he in the first roster?\"))\n"
269
+ ]
270
+ },
271
+ {
272
+ "attachments": {},
273
+ "cell_type": "markdown",
274
+ "metadata": {},
275
+ "source": [
276
+ "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"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "code",
281
+ "execution_count": 7,
282
+ "metadata": {},
283
+ "outputs": [
284
+ {
285
+ "name": "stdout",
286
+ "output_type": "stream",
287
+ "text": [
288
+ "Using model: gpt-3.5-turbo\n",
289
+ "Loading path from pickle file: ./index.pkl ...\n",
290
+ "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",
291
+ "Yes, Iron Man was part of the original Avengers lineup.\n"
292
+ ]
293
+ }
294
+ ],
295
+ "source": [
296
+ "from megabots import bot\n",
297
+ "\n",
298
+ "prompt = \"\"\"\n",
299
+ "Use the following pieces of context to answer the question at the end. \n",
300
+ "If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
301
+ "\n",
302
+ "{context}\n",
303
+ "\n",
304
+ "{history}\n",
305
+ "Human: {question}\n",
306
+ "AI:\"\"\"\n",
307
+ "\n",
308
+ "qnabot = bot(\n",
309
+ " \"qna-over-docs\",\n",
310
+ " prompt=prompt,\n",
311
+ " index=\"./index.pkl\",\n",
312
+ " memory=\"conversation-buffer\",\n",
313
+ ")\n",
314
+ "\n",
315
+ "print(qnabot.ask(\"who is iron man?\"))\n",
316
+ "print(qnabot.ask(\"was he in the first roster?\"))"
317
+ ]
318
  }
319
  ],
320
  "metadata": {
megabots/__init__.py CHANGED
@@ -1,6 +1,7 @@
1
  from megabots.vectorstore import VectorStore, vectorstore
2
  from megabots.memory import Memory, memory
3
  from megabots.bot import Bot, bot
 
4
  from megabots.utils import create_api, create_interface
5
 
6
 
 
1
  from megabots.vectorstore import VectorStore, vectorstore
2
  from megabots.memory import Memory, memory
3
  from megabots.bot import Bot, bot
4
+ from megabots.prompt import prompt
5
  from megabots.utils import create_api, create_interface
6
 
7
 
megabots/bot.py CHANGED
@@ -10,6 +10,7 @@ from langchain.prompts import PromptTemplate
10
  from langchain.chains.question_answering import load_qa_chain
11
  from langchain.chains.conversational_retrieval.prompts import QA_PROMPT
12
  from langchain.document_loaders import DirectoryLoader
 
13
  from megabots.vectorstore import VectorStore
14
  from megabots.memory import Memory
15
  import megabots
@@ -19,8 +20,7 @@ class Bot:
19
  def __init__(
20
  self,
21
  model: str | None = None,
22
- prompt_template: str | None = None,
23
- prompt_variables: list[str] | None = None,
24
  index: str | None = None,
25
  sources: bool | None = False,
26
  vectorstore: VectorStore | None = None,
@@ -28,36 +28,36 @@ class Bot:
28
  verbose: bool = False,
29
  temperature: int = 0,
30
  ):
 
 
 
31
  self.select_model(model, temperature)
32
  self.create_loader(index)
33
  self.load_or_create_index(index, vectorstore)
34
- self.vectorstore = vectorstore
35
- self.memory = memory
36
  # Load the question-answering chain for the selected model
37
- self.chain = self.create_chain(
38
- prompt_template, prompt_variables, sources=sources, verbose=verbose
39
- )
40
 
41
  def create_chain(
42
  self,
43
- prompt_template: str | None = None,
44
- prompt_variables: list[str] | None = None,
45
  sources: bool | None = False,
46
  verbose: bool = False,
47
  ):
48
- prompt = (
49
- PromptTemplate(template=prompt_template, input_variables=prompt_variables)
50
- if prompt_template is not None and prompt_variables is not None
51
- else QA_PROMPT
52
- )
53
  # TODO: Changing the prompt here is not working. Leave it as is for now.
54
  # Reference: https://github.com/hwchase17/langchain/issues/2858
55
  if sources:
56
  return load_qa_with_sources_chain(
57
- self.llm, chain_type="stuff", verbose=verbose
 
 
 
58
  )
59
  return load_qa_chain(
60
- self.llm, chain_type="stuff", verbose=verbose, prompt=prompt
 
 
 
 
61
  )
62
 
63
  def select_model(self, model: str | None, temperature: float):
@@ -132,6 +132,7 @@ SUPPORTED_TASKS = {
132
  "model": "gpt-3.5-turbo",
133
  "temperature": 0,
134
  "index": "./index",
 
135
  },
136
  }
137
  }
@@ -141,10 +142,10 @@ SUPPORTED_MODELS = {}
141
 
142
  def bot(
143
  task: str | None = None,
 
144
  model: str | None = None,
145
  index: str | None = None,
146
- prompt_template: str | None = None,
147
- prompt_variables: list[str] | None = None,
148
  memory: str | Memory | None = None,
149
  vectorstore: str | VectorStore | None = None,
150
  verbose: bool = False,
@@ -154,13 +155,21 @@ def bot(
154
 
155
  Args:
156
  task (str | None, optional): The given task. Can be one of the SUPPORTED_TASKS.
 
157
  model (str | None, optional): Model to be used. Can be one of the SUPPORTED_MODELS.
 
158
  index (str | None, optional): Data that the model will load and store index info.
159
  Can be either a local file path, a pickle file, or a url of a vector database.
160
  By default it will look for a local directory called "files" in the current working directory.
161
- prompt_template (str | None, optional): Prompt template to be used. Specify variables with {var_name}.
162
- prompt_variables (list[str] | None, optional): Prompt variables to be used in the prompt template.
 
 
 
 
 
163
  verbose (bool, optional): Verbocity. Defaults to False.
 
164
  temperature (int, optional): Temperature. Defaults to 0.
165
 
166
  Raises:
@@ -178,11 +187,17 @@ def bot(
178
 
179
  task_defaults = SUPPORTED_TASKS[task]["default"]
180
 
 
 
 
181
  return SUPPORTED_TASKS[task]["impl"](
182
  model=model or task_defaults["model"],
183
  index=index or task_defaults["index"],
184
- prompt_template=prompt_template,
185
- prompt_variables=prompt_variables,
 
 
 
186
  temperature=temperature,
187
  verbose=verbose,
188
  vectorstore=megabots.vectorstore(vectorstore)
 
10
  from langchain.chains.question_answering import load_qa_chain
11
  from langchain.chains.conversational_retrieval.prompts import QA_PROMPT
12
  from langchain.document_loaders import DirectoryLoader
13
+ from megabots.prompt import QA_MEMORY_PROMPT
14
  from megabots.vectorstore import VectorStore
15
  from megabots.memory import Memory
16
  import megabots
 
20
  def __init__(
21
  self,
22
  model: str | None = None,
23
+ prompt: PromptTemplate | None = None,
 
24
  index: str | None = None,
25
  sources: bool | None = False,
26
  vectorstore: VectorStore | None = None,
 
28
  verbose: bool = False,
29
  temperature: int = 0,
30
  ):
31
+ self.vectorstore = vectorstore
32
+ self.memory = memory
33
+ self.prompt = prompt or QA_MEMORY_PROMPT if self.memory else QA_PROMPT
34
  self.select_model(model, temperature)
35
  self.create_loader(index)
36
  self.load_or_create_index(index, vectorstore)
37
+
 
38
  # Load the question-answering chain for the selected model
39
+ self.chain = self.create_chain(sources=sources, verbose=verbose)
 
 
40
 
41
  def create_chain(
42
  self,
 
 
43
  sources: bool | None = False,
44
  verbose: bool = False,
45
  ):
 
 
 
 
 
46
  # TODO: Changing the prompt here is not working. Leave it as is for now.
47
  # Reference: https://github.com/hwchase17/langchain/issues/2858
48
  if sources:
49
  return load_qa_with_sources_chain(
50
+ self.llm,
51
+ chain_type="stuff",
52
+ memory=self.memory.memory if self.memory else None,
53
+ verbose=verbose,
54
  )
55
  return load_qa_chain(
56
+ self.llm,
57
+ chain_type="stuff",
58
+ verbose=verbose,
59
+ prompt=self.prompt,
60
+ memory=self.memory.memory if self.memory else None,
61
  )
62
 
63
  def select_model(self, model: str | None, temperature: float):
 
132
  "model": "gpt-3.5-turbo",
133
  "temperature": 0,
134
  "index": "./index",
135
+ "input_variables": ["context", "question"],
136
  },
137
  }
138
  }
 
142
 
143
  def bot(
144
  task: str | None = None,
145
+ *,
146
  model: str | None = None,
147
  index: str | None = None,
148
+ prompt: str | None = None,
 
149
  memory: str | Memory | None = None,
150
  vectorstore: str | VectorStore | None = None,
151
  verbose: bool = False,
 
155
 
156
  Args:
157
  task (str | None, optional): The given task. Can be one of the SUPPORTED_TASKS.
158
+
159
  model (str | None, optional): Model to be used. Can be one of the SUPPORTED_MODELS.
160
+
161
  index (str | None, optional): Data that the model will load and store index info.
162
  Can be either a local file path, a pickle file, or a url of a vector database.
163
  By default it will look for a local directory called "files" in the current working directory.
164
+
165
+ prompt (str | None, optional): The prompt that the bot will take in. Mark variables like this: {variable}.
166
+ Variables are context, question, and history if the bot has memory.
167
+
168
+ vectorstore: (str | VectorStore | None, optional): The vectorstore that the bot will save the index to.
169
+ If only a string is passed, the defaults values willl be used.
170
+
171
  verbose (bool, optional): Verbocity. Defaults to False.
172
+
173
  temperature (int, optional): Temperature. Defaults to 0.
174
 
175
  Raises:
 
187
 
188
  task_defaults = SUPPORTED_TASKS[task]["default"]
189
 
190
+ if memory is not None:
191
+ task_defaults["input_variables"].append("history")
192
+
193
  return SUPPORTED_TASKS[task]["impl"](
194
  model=model or task_defaults["model"],
195
  index=index or task_defaults["index"],
196
+ prompt=None
197
+ if prompt is None
198
+ else PromptTemplate(
199
+ template=prompt, input_variables=task_defaults["input_variables"]
200
+ ),
201
  temperature=temperature,
202
  verbose=verbose,
203
  vectorstore=megabots.vectorstore(vectorstore)
megabots/memory.py CHANGED
@@ -1,31 +1,15 @@
1
- from langchain.memory import (
2
- ConversationBufferMemory,
3
- ConversationBufferWindowMemory,
4
- ConversationSummaryMemory,
5
- ConversationSummaryBufferMemory,
6
- )
7
 
8
 
9
  class ConversationBuffer:
10
  def __init__(self):
11
- self.memory = ConversationBufferMemory
12
 
13
 
14
  class ConversationBufferWindow:
15
- def __init__(self, memory_window: int):
16
- self.memory_window: int = memory_window
17
- self.memory = ConversationBufferWindowMemory
18
-
19
-
20
- class ConversationSummary:
21
- def __init__(self):
22
- self.memory = ConversationSummaryMemory
23
-
24
-
25
- class ConversationSummaryBuffer:
26
- def __init__(self, max_token_limit: int):
27
- self.max_token_limit: int = max_token_limit
28
- self.memory = ConversationSummaryBufferMemory
29
 
30
 
31
  SUPPORTED_MEMORY = {
@@ -35,35 +19,17 @@ SUPPORTED_MEMORY = {
35
  },
36
  "conversation-buffer-window": {
37
  "impl": ConversationBufferWindow,
38
- "default": {"memory_window": 3},
39
- },
40
- "conversation-summary": {
41
- "impl": ConversationSummary,
42
- "default": {},
43
- },
44
- "conversation-summary-buffer": {
45
- "impl": ConversationSummaryBuffer,
46
- "default": {"max_token_limit": 40},
47
  },
48
  }
49
 
50
 
51
- Memory = type(
52
- "Memory",
53
- (
54
- ConversationBuffer,
55
- ConversationBufferWindow,
56
- ConversationSummary,
57
- ConversationSummaryBuffer,
58
- ),
59
- {},
60
- )
61
 
62
 
63
  def memory(
64
  name: str = "conversation-buffer-window",
65
- memory_window: int | None = None,
66
- max_token_limit: int | None = None,
67
  ) -> Memory:
68
  if name is None:
69
  raise RuntimeError("Impossible to instantiate memory without a name.")
@@ -74,13 +40,6 @@ def memory(
74
  cl = SUPPORTED_MEMORY[name]["impl"]
75
 
76
  if name == "conversation-buffer-window":
77
- if max_token_limit != None:
78
- raise ValueError(f"max_token_limit cannot be set for {name} memory")
79
- return cl(memory_window=memory_window)
80
-
81
- if name == "conversation-summary-buffer":
82
- if max_token_limit != None:
83
- raise ValueError(f"memory_window cannot be set for {name} memory")
84
- return cl(max_token_limit=max_token_limit)
85
 
86
  return SUPPORTED_MEMORY[name]["impl"]()
 
1
+ from langchain.memory import ConversationBufferMemory, ConversationBufferWindowMemory
 
 
 
 
 
2
 
3
 
4
  class ConversationBuffer:
5
  def __init__(self):
6
+ self.memory = ConversationBufferMemory(input_key="question")
7
 
8
 
9
  class ConversationBufferWindow:
10
+ def __init__(self, k: int):
11
+ self.k: int = k
12
+ self.memory = ConversationBufferWindowMemory(k=self.k, input_key="question")
 
 
 
 
 
 
 
 
 
 
 
13
 
14
 
15
  SUPPORTED_MEMORY = {
 
19
  },
20
  "conversation-buffer-window": {
21
  "impl": ConversationBufferWindow,
22
+ "default": {"k": 3},
 
 
 
 
 
 
 
 
23
  },
24
  }
25
 
26
 
27
+ Memory = type("Memory", (ConversationBuffer, ConversationBufferWindow), {})
 
 
 
 
 
 
 
 
 
28
 
29
 
30
  def memory(
31
  name: str = "conversation-buffer-window",
32
+ k: int | None = None,
 
33
  ) -> Memory:
34
  if name is None:
35
  raise RuntimeError("Impossible to instantiate memory without a name.")
 
40
  cl = SUPPORTED_MEMORY[name]["impl"]
41
 
42
  if name == "conversation-buffer-window":
43
+ return cl(k=k)
 
 
 
 
 
 
 
44
 
45
  return SUPPORTED_MEMORY[name]["impl"]()
megabots/prompt.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from langchain import PromptTemplate
3
+
4
+ QNA_TEMPLATE = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
5
+
6
+ {context}
7
+
8
+ {history}
9
+ Human: {question}
10
+ AI:"""
11
+
12
+ QA_MEMORY_PROMPT = PromptTemplate(
13
+ template=QNA_TEMPLATE, input_variables=["context", "history", "question"]
14
+ )
15
+
16
+
17
+ def prompt(template: str, variables: List[str]):
18
+ return PromptTemplate(template=template, input_variables=variables)
tests/test_memory.py CHANGED
@@ -1,42 +1,25 @@
1
- import pytest
2
- from megabots.memory import (
3
- ConversationBufferWindow,
4
- ConversationSummaryBuffer,
5
- memory,
6
- Memory,
7
- SUPPORTED_MEMORY,
8
- )
9
-
10
-
11
- def test_memory_name_none():
12
- with pytest.raises(RuntimeError):
13
- memory(name=None)
14
 
15
 
16
- def test_memory_not_supported():
17
- with pytest.raises(ValueError):
18
- memory(name="unsupported_memory_type")
19
 
20
 
21
  def test_memory_conversation_buffer_window():
22
- mem_obj = memory(name="conversation-buffer-window", memory_window=5)
23
- assert isinstance(mem_obj, ConversationBufferWindow)
24
- assert mem_obj.memory_window == 5
25
- assert mem_obj.__class__ == SUPPORTED_MEMORY["conversation-buffer-window"]["impl"]
26
-
27
 
28
- def test_memory_conversation_buffer_window_invalid_max_token_limit():
29
- with pytest.raises(ValueError):
30
- memory(name="conversation-buffer-window", memory_window=5, max_token_limit=10)
31
 
 
 
 
32
 
33
- def test_memory_conversation_summary_buffer():
34
- mem_obj = memory(name="conversation-summary-buffer", max_token_limit=10)
35
- assert isinstance(mem_obj, ConversationSummaryBuffer)
36
- assert mem_obj.max_token_limit == 10
37
- assert mem_obj.__class__ == SUPPORTED_MEMORY["conversation-summary-buffer"]["impl"]
38
 
39
-
40
- def test_memory_conversation_summary_buffer_invalid_memory_window():
41
- with pytest.raises(ValueError):
42
- memory(name="conversation-summary-buffer", memory_window=5, max_token_limit=10)
 
 
1
+ from pytest import raises
2
+ from megabots import memory
3
+ from megabots.memory import ConversationBuffer, ConversationBufferWindow
 
 
 
 
 
 
 
 
 
 
4
 
5
 
6
+ def test_memory_conversation_buffer():
7
+ mem = memory(name="conversation-buffer")
8
+ assert isinstance(mem, ConversationBuffer)
9
 
10
 
11
  def test_memory_conversation_buffer_window():
12
+ mem = memory(name="conversation-buffer-window", k=10)
13
+ assert isinstance(mem, ConversationBufferWindow)
 
 
 
14
 
 
 
 
15
 
16
+ def test_memory_unsupported_name():
17
+ with raises(ValueError, match=r"Memory invalid-name is not supported."):
18
+ memory(name="invalid-name")
19
 
 
 
 
 
 
20
 
21
+ def test_memory_no_name():
22
+ with raises(
23
+ RuntimeError, match=r"Impossible to instantiate memory without a name."
24
+ ):
25
+ memory(name=None)