Skip to main content

Build your own chatbot in no time!

Project description

Chatter: Your Friendly Chatbot Assistant

Chatter is a Python-based chatbot assistant designed to help you build and manage a chatbot with ease. It leverages powerful machine learning models for natural language processing and offers a variety of utilities to handle documents and web data.

Features

  • Standalone Query Creation: Automatically reformulates user queries to be standalone, improving the clarity and context for the chatbot.
  • Final Response Generation: Generates comprehensive responses based on user queries, chat history, and top responses.
  • Document Handling: Reads and processes .docx files to extract and preprocess text.
  • Web Scraping: Extracts URLs and scrapes web content to include in documents.
  • Vector Database: Creates and manages a vector database for efficient document retrieval using Annoy index.
  • Memory Management: Keeps track of chat history and recent interactions to maintain context in conversations.

Installation

To use Chatter, you need to have the following dependencies installed:

pip install -r requirements.txt

Usage

Initializing Chatter

Create an instance of the Chatter class with your API key and optional parameters. You also need to specify a folder in which you have information (in docx format) that chatbot can use to asnwer the queries accordingly.

from chatter_rag import Chatter, Memory

data_path = "path/to/your/docx/files.docx"
api_key = "your_groq_api_key"
chatter = Chatter(api_key = api_key, file_path = data_path)

memory = Memory()

Updating vector database

First you always need to create or update your database according to the docx file in you data folder. This part of code can be run only once. If documents are changed you need to run this part again.

_, document_embeddings = chatter.find_documents(data_path)
_ = chatter.create_vector_database(document_embeddings, path = model_path)

Creating Standalone Queries

To have separated chat for each user, you need to specify a unique session_id to each chat. Generate a standalone query from a user query and chat history:

# choose on of the following as your memory
str_mem = memory.get_last_mem(session_id)   # only use the last query and response of the chat
str_mem5 = memory.get_5_mem(session_id)     # use the last 5 sets of queries and responses
standalone_query = chatter.create_standalone_query("user query", str_mem)

Embed the standalone query

To be able to seach the database using the standalone query, you also need to embed it.

query_embedding = chatter.model.encode(chatter.preprocess_text(standalone_query))

Find and load the ann file

The vector database is mustbe loaded first.

ann_file = chatter.find_ann_files(model_path)
dimention, _ = ann_file.split('.')

ann_dir = model_path + ann_file
index = chatter.load_index(ann_dir, int(dimention))

Find indices of similar documents to the standalone query

Some parts of the document are more related to the query. This part will hep you find those. Hete top 10 responses (top_k=10) is the default.

similar_docs_indices, _ = chatter.get_similar_documents(query_embedding, index, top_k=10)

Fetch all the documents

Now that you have the indices, you need to load the document to find that which part of the document is more related to the query:

documents, _ = chatter.find_documents(data_path)
top_responses = [documents[i] for i in similar_docs_indices]

Generate final response

The top responses and the query along with the last 5 sets of queries and responses must be passed to chatter to produce the final response.

final_response = chatter.generate_final_response(top_responses, user_query, str_mem5)

Update chat history

At the end, the memory must be updated with the query and the final response in the correct session_id.

memory.update_chat(session_id, user_query, final_response)

Memory clearance

If needed, you can clear the memory.

memory.remove_mem(session_id)

To sum up

from chatter import Chatter, Memory

data_path = "path/to/your/docx/files.docx"
api_key = "your_groq_api_key"
chatter = Chatter(api_key = api_key, file_path = data_path)

memory = Memory()

# choose on of the following as your memory
str_mem = memory.get_last_mem(session_id)   # only use the last query and response of the chat
str_mem5 = memory.get_5_mem(session_id)     # use the last 5 sets of queries and responses
standalone_query = chatter.create_standalone_query(user_query, str_mem)

query_embedding = chatter.model.encode(chatter.preprocess_text(standalone_query))

ann_file = chatter.find_ann_files(model_path)
dimention, _ = ann_file.split('.')

ann_dir = model_path + ann_file
index = chatter.load_index(ann_dir, int(dimention))

similar_docs_indices, _ = chatter.get_similar_documents(query_embedding, index, top_k=10)

documents, _ = chatter.find_documents(data_path)
top_responses = [documents[i] for i in similar_docs_indices]

final_response = chatter.generate_final_response(top_responses, user_query, str_mem5)

memory.update_chat(session_id, user_query, final_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

urag-0.0.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

URAG-0.0.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file urag-0.0.1.tar.gz.

File metadata

  • Download URL: urag-0.0.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for urag-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4f0db381fc50e0547faa8bc80755c51c6b2fbee23cfdb5e88b2aaa4ed7bd3dc2
MD5 c856d3dd824770a43a01fc728e45b2c0
BLAKE2b-256 4e52d216f8d3c82d183134c68854fe0e23e7d0612f81e52d0da415bf6f5cd0f8

See more details on using hashes here.

File details

Details for the file URAG-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: URAG-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for URAG-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9255d03b628e8e7cdad378a2d7baa2f67f9b0c6304d314d905a240257312ce37
MD5 569cd34c81c3a879cd090bc57b5e1142
BLAKE2b-256 9bb37d979988bf6d705d3f18db813e5475ad58241b2181c716df4e3ac4840ae4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page