Code Reference¶
The ChatBot Class¶
- class chatbot.chatbot.ChatBot(api_key: str, prompt: str = "You're a nice helpful chatbot.", message_memory: ~chatbot.chatbot.BaseMessageMemory = <chatbot.chatbot.MessageMemory object>, knowledge_base: ~chatbot.chatbot.BaseKnowledgeBase | None = None, gpt_model: str = 'gpt-3.5-turbo')¶
Bases:
objectWrapper to call OpenAI’s GPT-3.5 API and return a response. Optionally uses child class implementation of
BaseMessageMemoryandBaseKnowledgeBaseclasses to configure how memory is stored and how to get context for the user’s query.- Parameters:
api_key (str) – The API key for OpenAI’s API
prompt (str, optional) – The prompt to use when calling OpenAI’s API, defaults to “You’re a nice helpful chatbot.” You will probably want to change this to make the chatbot more interesting.
message_memory (
BaseMessageMemory, optional) – The message memory to use, defaults toMessageMemory- which is a simple in-memory implementation.knowledge_base (
BaseKnowledgeBase, optional) – The knowledge base to use, defaults to None. If you want to use a knowledge base, you must implement theBaseKnowledgeBase, or use theKnowledgeBaseRedis.gpt_model (str, optional) – The GPT model to use, defaults to “gpt-3.5-turbo-0613”.
- get_reply(user_query: str) str¶
Get a reply from the chatbot.
- Parameters:
user_query (str) – The user’s query
- Returns:
The chatbot’s response
- Return type:
str
The BaseMessageMemory Class¶
- class chatbot.chatbot.BaseMessageMemory(memory_length: int = 5)¶
Bases:
objectBase class for storing the conversation history. This interface is assumed to be implemented by the
ChatBot, so any new child classes must implement these methods.- Parameters:
memory_length (int, optional) – The number of messages to store in the chat history, defaults to 5
- add_latest_bot_response()¶
- add_latest_user_query()¶
- get_message_list()¶
- trim_message_list()¶
The MessageMemory Class¶
- class chatbot.chatbot.MessageMemory(memory_length: int = 5)¶
Bases:
BaseMessageMemoryStores the conversation history in memory and uses the memory_length parameter to determine how many messages to send to the API to provide context.
- Parameters:
memory_length (int, optional) – The number of messages to store in the chat history, defaults to 5
- add_latest_bot_response(bot_response: str)¶
Add the latest bot response to the message queue.
- Parameters:
bot_response (str) – The latest bot response
- add_latest_user_query(latest_message: str)¶
Add the latest user query to the message queue.
- Parameters:
latest_message (str) – The latest user query
- get_message_list() list[dict]¶
Returns the message queue. :return: The message queue :rtype: list[dict]
- trim_message_list()¶
Trims the message list to
memory_length.
The BaseKnowledgeBase Class¶
The KnowledgeBaseRedis Class¶
- class chatbot.chatbot.KnowledgeBaseRedis(redis_url: str, api_key: str)¶
Bases:
BaseKnowledgeBaseA knowledge base that uses Redis to store information and the embeddings for each piece of information. This class uses Redis’ vector search to find the most similar information to the user’s query.
- Parameters:
redis_url (str) – The URL for the Redis instance
api_key (str) – The API key for OpenAI’s API
- get_context(user_query: str) str | None¶
Get the context for the user’s query.
- Parameters:
user_query (str) – The user’s query
- Returns:
The context for the user’s query
- Return type:
str | None