Framework for the textbase package.
Project description
textbase-framework
What 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 package
- Easy web deployment via textbase deploy
- SMS integration
- Native integration of other models (Claude, Llama, ...)
Get started
Installation
Video guide (for Windows and Ubuntu > 19.04): https://youtu.be/pcw7G3S7FGw
- Make sure to upgrade your Python version to >= 3.8.1 and add it to your
PATH. - Now, you need to install
Poetry, which is a python dependency manager which makes your life easier. It really does.
Ubuntu (≤19.04):
- Follow the guide at https://gist.github.com/basaks/652eea861a143a9b3d11805c96273488 to install Python version 3.9.
- Install pip using:
sudo apt install python-pip
- Install poetry using:
pip install poetry
- Add it to your path using:
export PATH="$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.-
cd textbase-framework poetry shell
This 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 bot
Let'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 the models module.
Currently we support:
- OpenAI
- HuggingFace
- BotLibre
Usage of decorator with example and response structure
This 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!
from textbase_framework import bot, Message
from textbase_framework.models import OpenAI
OpenAI.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 a
pleasant chat!
"""
@bot() #The decorator function
def on_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 locally
You can execute the textbase_cli test command in order to test your bot locally. This will spin up an UI at port 4000, so you should be able to navigate to localhost:4000 and check if your bot works and interact with it.
Deployment
There 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 a
requirements.pyfile which includes all the additional requirements which you might have installed while creating your bot. - The name of the file in which the
on_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 CLI
Before 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 Textbase dashboard.
- Sign in using your google account.
- Generate an API key by clicking on
Generatein the bottom left section.
After this, you can execute the textbase_cli deploy command to deploy your bot from a terminal.
After executing it, it will ask for:
- Path to the zip folder
- Bot name
- Textbase API key
If you want to run this command in one shot, you can make use of flags:
textbase_cli deploy --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 with Status, Bot ID and URL and you can click on that URL to view your bot!
Deploy from Dashboard
- Navigate to the Textbase dashboard.
- Sign in using your google account.
- Click on
Create 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 on
Create Botto start the deployment. - This will redirect you to the
Deploymentssection 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!
Examples
Mimicking bot
from textbase_framework import bot, Message
from textbase_framework.models import get_contents
from typing import List
@bot()
def on_message(message_history: List[Message], state: dict = None):
# Mimic user's response
bot_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 bot
import os
from textbase_framework import bot, Message
from textbase_framework.models import OpenAI
from typing import List
# 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 Turbo
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 a
pleasant chat!
"""
@bot()
def on_message(message_history: List[Message], state: dict = None):
# Generate GPT-3.5 Turbo response
bot_response = OpenAI.generate(
system_prompt=SYSTEM_PROMPT,
message_history=message_history, # Assuming history is the list of user messages
model="gpt-3.5-turbo",
)
response = {
"data": {
"messages": [
{
"data_type": "STRING",
"value": bot_response
}
],
"state": state
},
"errors": [
{
"message": ""
}
]
}
return {
"status_code": 200,
"response": response
}
HuggingFace bot
import os
from textbase_framework import bot, Message
from textbase_framework.models import HuggingFace
from typing import List
# 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 Turbo
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 a
pleasant chat!
"""
@bot()
def on_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
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file textbase_framework-0.1.3.tar.gz.
File metadata
- Download URL: textbase_framework-0.1.3.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-76-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8cbbacd7a2a287b45ce47401e26a00f1081c4e24bf23d853210a23a76a97f70
|
|
| MD5 |
0100ee4776f520464d997e3c65e98127
|
|
| BLAKE2b-256 |
c54d025720642b1eba494d6db3d9114831df3afaa47e8dfa306807b2773d9acf
|
File details
Details for the file textbase_framework-0.1.3-py3-none-any.whl.
File metadata
- Download URL: textbase_framework-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.6 Linux/5.15.0-76-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e859f17d0d96db841ee0082b8dcb71936c7583b035288ac5e16163fbcfe9b138
|
|
| MD5 |
78a3e841d01960d4d5c03d631fc2b904
|
|
| BLAKE2b-256 |
90d16e76707233af333f30ff6e5386a2cb5ef3950ae4bd9090f61504a47dc512
|