Package: textbase-framework
textbase-frameworkWhat is textbase?Get StartedInstallationCreate your first botUsage of decorator with example and response structureTest locallyDeploymentDeploy from CLIDeploy from DashboardExamplesMimicking botOpenAIHuggingFaceWhat is textbase?:sparkles: Textbase is a framework for building chatbots using NLP and ML. :sparkles:Since it is just Python you can use whatever models, libraries, vector databases and APIs you want!Coming soon:PyPI packageEasy web deployment viatextbase deploySMS integrationNative integration of other models (Claude, Llama, ...)Get startedInstallationVideo guide (for Windows and Ubuntu > 19.04):https://youtu.be/pcw7G3S7FGwMake sure to upgrade your Python version to >= 3.8.1 and add it to yourPATH.Now, you need toinstallPoetry, which is a python dependency manager which makes your life easier. It really does.Ubuntu(≤19.04):Follow the guide athttps://gist.github.com/basaks/652eea861a143a9b3d11805c96273488to install Python version 3.9.Install pip using:sudoaptinstallpython-pipInstall poetry using:pipinstallpoetryAdd it to your path using:exportPATH="$HOME/.local/bin:$PATH"poetry config virtualenvs.in-project truein the VS Code terminal inside the folder where you have cloned textbase repo so that you can select the default Python interpreter in VS Code to the one Poetry installed.cdtextbase-frameworkpoetryshellThis will make a new virtual Python environment inside the current directory and then you can select the default python interpreter to be the one in the.venvfolder.poetry installto install all the required dependencies.Create your first botLet's get started on creating your first bot.You can make your own model using NLP and ML or you can make use of one of our inbuilt models. You can do so by importing themodelsmodule.Currently we support:OpenAIHuggingFaceBotLibreUsage of decorator with example and response structureThis particular example uses OpenAI's API. You can use your own or you can even integrate some in the project itself. We are open for contributions!fromtextbase_frameworkimportbot,Messagefromtextbase_framework.modelsimportOpenAIOpenAI.api_key=os.getenv("OPENAI_API_KEY")# System prompt; this will set the tone of the bot for the rest of the conversation.SYSTEM_PROMPT="""You are chatting with an AI. There are no specific prefixes for responses, so you can ask or talk about anything you like.The AI will respond in a natural, conversational manner. Feel free to start the conversation with any question or topic, and let's have apleasant chat!"""@bot()#The decorator functiondefon_message(message_history:List[Message],state:dict=None):# Your logic for the bot. A very basic request to OpenAI is provided below. You can choose to handle it however you want.bot_response=OpenAI.generate(model="gpt-3.5-turbo",system_prompt=SYSTEM_PROMPT,message_history=message_history)'''The response structure HAS to be in the format given below so that our backend framework has no issues communicating with the frontend.'''response={"data":{"messages":[{"data_type":"STRING","value":bot_response}],"state":state},"errors":[{"message":""}]}return{"status_code":200,"response":response}Test locallyYou can execute thetextbase_cli testcommand in order to test your bot locally. This will spin up an UI at port 4000, so you should be able to navigate tolocalhost:4000and check if your bot works and interact with it.DeploymentThere are two methods to deploy your bot to the internet so that everyone can use it.Before using any method, you need to ensure that:You have arequirements.pyfile which includes all the additional requirements which you might have installed while creating your bot.The name of the file in which theon_messagefunction is present is namedmain.py.Zip these two files, i.e.,requirements.txtandmain.pyinto a.ziparchive. It's important that it's a.ziparchive and not anything else.Deploy from CLIBefore deploying your bot from the CLI, you need to generate an API key in the dashboard. To do that, you need to:Navigate to the Textbasedashboard.Sign in using your google account.Generate an API key by clicking onGeneratein the bottom left section.After this, you can execute thetextbase_cli deploycommand to deploy your bot from a terminal.After executing it, it will ask for:Path to the zip folderBot nameTextbase API keyIf you want to run this command in one shot, you can make use of flags:textbase_clideploy--path=<path_to_zip_folder>--bot-name=<name_of_your_bot>--api_key=<api_key>If this command executes successfully, it will return a table withStatus,Bot IDandURLand you can click on that URL to view your bot!Deploy from DashboardNavigate to the Textbasedashboard.Sign in using your google account.Click onCreate Deploymentand then click onCreate Boton the top right.You will need to provide a chatbot name and you need to upload the zip file.Click onCreate Botto start the deployment.This will redirect you to theDeploymentssection after a few seconds. In here, you can check the status of your bot and if it's deployed successfully, a link will be generated. You can click on the blue button with the symbol:</>which will redirect to the link where the bot is deployed and you can test it out!ExamplesMimicking botfromtextbase_frameworkimportbot,Messagefromtextbase_framework.modelsimportget_contentsfromtypingimportList@bot()defon_message(message_history:List[Message],state:dict=None):# Mimic user's responsebot_response=[]bot_response=get_contents(message_history[-1],"STRING")response={"data":{"messages":[{"data_type":"STRING","value":bot_response}],"state":state},"errors":[{"message":""}]}return{"status_code":200,"response":response}OpenAI botimportosfromtextbase_frameworkimportbot,Messagefromtextbase_framework.modelsimportOpenAIfromtypingimportList# Load your OpenAI API key# OpenAI.api_key = ""# or from environment variable:OpenAI.api_key=os.getenv("OPENAI_API_KEY")# Prompt for GPT-3.5 TurboSYSTEM_PROMPT="""You are chatting with an AI. There are no specific prefixes for responses, so you can ask or talk about anything you like.The AI will respond in a natural, conversational manner. Feel free to start the conversation with any question or topic, and let's have apleasant chat!"""@bot()defon_message(message_history:List[Message],state:dict=None):# Generate GPT-3.5 Turbo responsebot_response=OpenAI.generate(system_prompt=SYSTEM_PROMPT,message_history=message_history,# Assuming history is the list of user messagesmodel="gpt-3.5-turbo",)response={"data":{"messages":[{"data_type":"STRING","value":bot_response}],"state":state},"errors":[{"message":""}]}return{"status_code":200,"response":response}HuggingFace botimportosfromtextbase_frameworkimportbot,Messagefromtextbase_framework.modelsimportHuggingFacefromtypingimportList# Load your OpenAI API key# HuggingFace.api_key = ""# or from environment variable:HuggingFace.api_key=os.getenv("HUGGINGFACE_API_KEY")# Prompt for GPT-3.5 TurboSYSTEM_PROMPT="""You are chatting with an AI. There are no specific prefixes for responses, so you can ask or talk about anything you like.The AI will respond in a natural, conversational manner. Feel free to start the conversation with any question or topic, and let's have apleasant chat!"""@bot()defon_message(message_history:List[Message],state:dict=None):# Generate HuggingFace response. Uses the DialoGPT-large model from Microsoft by default.bot_response=HuggingFace.generate(system_prompt=SYSTEM_PROMPT,message_history=message_history,# Assuming history is the list of user messages)response={"data":{"messages":[{"data_type":"STRING","value":bot_response}],"state":state},"errors":[{"message":""}]}return{"status_code":200,"response":response}