{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Examples\n", "\n", "Below you can find some examples of how to use the 🤖 `Megabots` library." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "from megabots import bot\n", "from dotenv import load_dotenv" ] }, { "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 `bot` will look for `index.pkl` or `./index`" ] }, { "cell_type": "code", "execution_count": 14, "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": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qnabot = bot(\"qna-over-docs\", index=\"./index.pkl\")\n", "qnabot.ask(\"what was the first roster of the avengers?\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Changing the bot's prompt\n", "\n", "You can change the bots promnpt to customize it to your needs." ] }, { "cell_type": "code", "execution_count": 15, "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": [ "\"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.\"" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt_template = \"\"\"\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", "Be very playfull and humourous in your responses. always try to make the user laugh.\n", "Always start your answers with 'Hmmm! Let me think about that...'\n", "{context}\n", "\n", "Question: {question}\n", "Helpful humorous answer:\"\"\"\n", "\n", "load_dotenv()\n", "\n", "qnabot = bot(\n", " \"qna-over-docs\",\n", " index=\"./index.pkl\",\n", " prompt_template=prompt_template,\n", " prompt_variables=[\"context\", \"question\"],\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", "You can then [install Attu](https://milvus.io/docs/attu_install-docker.md) as a management tool for Milvus" ] }, { "cell_type": "code", "execution_count": 11, "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": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from megabots import bot, vectorstore\n", "\n", "# Create a vectorstore object. Default port is 19530 and default host is localhost\n", "milvus = vectorstore(\"milvus\")\n", "\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" ] } ], "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 }