{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# ChatBot" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Get Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "os.chdir('../') # set the working directory to the root of the project\n", "from symai import *\n", "from symai.components import *\n", "from IPython.display import display" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Writing a chatbot is *fairly* easy with our framework. All we need to do is basically derive from the `ChatBot` class and implement the `forward` method. The base class `ChatBot` has already some helper capabilities and context detection dictionaries. All we have to do is use the `self.narrate` method to instruct our chatbot to say what we want. \n", "\n", "Afterwards, we can use the `self.context_choice` method to classify the context of the user input. This is done by using a dictionary of context keywords. The `self.context_choice` method returns the context key that matches the user input. This key can then be used to determine the next action / condition of the chatbot.\n", "\n", "See the following example:" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "By creating an instance of the `SymbiaChat` and calling the `forward` method, we can start a chat with our chatbot. The `forward` method takes a user input and returns a chatbot response." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "from symai.chat import ChatBot\n", "from symai.interfaces import Interface\n", "\n", "\n", "class SymbiaChat(ChatBot):\n", " def forward(self) -> str:\n", " message = self.narrate('Symbia introduces herself, writes a greeting message and asks how to help.')\n", " while True:\n", " # query user\n", " usr = self.input(message)\n", "\n", " # detect context\n", " ctxt = self.context_choice(usr)\n", "\n", " if 'option 3' in ctxt: # exit\n", " self.narrate('Symbia writes goodbye message.', end=True)\n", " break # end chat\n", "\n", " elif 'option 4' in ctxt: # help\n", " message = self.narrate('Symbia writes for each capability one sentence.',\n", " context=self.capabilities)\n", "\n", " elif 'option 1' in ctxt: # chit chat\n", " message = self.narrate('Symbia replies to the user question in a casual way.')\n", "\n", " elif 'option 2' in ctxt:\n", " # detect command\n", " option = self.capabilities_choice(usr)\n", "\n", " if 'option 1' in option:\n", " q = usr.extract('user query request')\n", " rsp = self.search(q)\n", " message = self.narrate('Symbia replies to the user based on the online search results.',\n", " context=rsp)\n", " elif 'option 2' in option:\n", " q = usr.extract('URL')\n", " site = self.crawler(q)\n", " site.save('tmp.html')\n", " message = self.narrate('Symbia explains that the website is downloaded to the `tmp.html` file.')\n", " elif 'option 3' in option:\n", " pass\n", "\n", " # TODO ...\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " Symbia: Goodbye! If you have any more questions in the future, don't hesitate to reach out. Have a great day!\n" ] }, { "data": { "text/html": [ "Symbia: Goodbye! If you have any more questions in the future, don't hesitate to reach out. Have a great day!" ], "text/plain": [ "(value=Symbia: Goodbye! If you have any more questions in the future, don't hesitate to reach out. Have a great day!)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from symai.chat import SymbiaChat\n", "\n", "chat = SymbiaChat()\n", "chat()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The above bot can answer some trivia, use the Google search engine to retrieve information and " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "symai", "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.13" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }