Skip to main content

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)

Release files for URAG 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for URAG 0.0.1
File Size Uploaded
urag-0.0.1.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for URAG 0.0.1
File Interpreter ABI Platform
URAG-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / urag-0.0.1.tar.gz

Download URL urag-0.0.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4f0db381fc50e0547faa8bc80755c51c6b2fbee23cfdb5e88b2aaa4ed7bd3dc2
BLAKE2b-256 checksum
How to use checksums
4e52d216f8d3c82d183134c68854fe0e23e7d0612f81e52d0da415bf6f5cd0f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Release files / URAG-0.0.1-py3-none-any.whl

Download URL URAG-0.0.1-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9255d03b628e8e7cdad378a2d7baa2f67f9b0c6304d314d905a240257312ce37
BLAKE2b-256 checksum
How to use checksums
9bb37d979988bf6d705d3f18db813e5475ad58241b2181c716df4e3ac4840ae4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page