Skip to main content

Chatstack

Minimalist Context Management for message-based GPTs

This Python code provides a chatbot implementation with context management using OpenAI's GPT-3.5-turbo or GPT-4 chat models. The chatbot maintains a conversation history and help manages the context state and size in tokens.

Dependencies

  • loguru
  • pydantic
  • openai
  • tiktoken

OPEN_API_KEY

Chatstack finds your OpenAI API key via the OPENAI_API_KEY environment variable.

Classes

  • ChatRoleMessage: A base data class for messages with role, text, and tokens.
  • SystemMessage: A data class for representing a message with the 'system' role.
  • ContextMessage: A data class representing additional information context for the model.
  • AssistantMessage: A data class for representing a message with the 'assistant' role .
  • UserMessage: A data class for representing a message with the 'user' role.
  • ChatContext: A class that manages the conversation context and generates responses using OpenAI message interface models.
  • ChatReponse: A data class that contains the model response to a user message along with a record of the input context sent to the model, and other significant details such as the model used, the number of tokens used, and the estimated cost of the request.

Usage

  1. Import the ChatContext class.
  2. Create an instance of the ChatContext class with the desired configuration.
  3. Call the user_message or user_message_stream methods with the user's message text to get a response from the chatbot.

Example:

from chatstack import ChatContext

BASE_SYSTEM_PROMPT  = "You are a clever bot.  Do not apologize, or make excuses.  "
BASE_SYSTEM_PROMPT += "Do not mention that you are an AI language model since that is annoying to users."

def main():
    chat_context = ChatContext(base_system_msg_text=BASE_SYSTEM_PROMPT)

    print("Welcome to the Chatbot!")
    
    while True:
        user_input = input("You: ")      
        print("Chatbot:")
        response = chat_context.user_message(user_input, stream=True)
        print(response.text)


if __name__ == "__main__":
    main()

Configuration

The ChatContext class accepts the following parameters:

  • min_response_tokens: Minimum number of tokens to reserve for model completion response.
  • max_response_tokens: Maximum number of tokens to allow for model completion response.
  • chat_context_messages: Number of recent assistant and user messages to keep in context.
  • model: The name of the GPT model to use (default: "gpt-3.5-turbo").
  • temperature: The temperature for the model's response generation.
  • base_system_msg_text: The base system message text to provide context for the model.

The primary method of the ChatContext is the user_message() which is used to assemble the input context to the model and generate a completion.

user_message(msg_text: str) -> ChatResponse

This method takes a user's message text as input and generates a response from the chatbot using the conversation context.

user_message_stream(msg_text: str) -> ChatResponse

This method is a generator that takes a user's message text as input and yields ChatResponse objects containing the incremental and cumulative response text from the chatbot using the conversation context.

add_message(msg : ChatRoleMessage)

Add a message to the context for presentation to the model in subsequent completion requests.

Parameters:

  • msg_text (str): The text of the user's message.

Returns:

  • ChatResponse: An instance of the ChatResponse data class that includes the model response text, the actual input messages sent to the model, and other relevant details such as the token counts and estimated price of the completion.

Metadata

Release files for chatstack 0.1.5

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

Source distribution (sdist)

Source distribution for chatstack 0.1.5
File Size Uploaded
chatstack-0.1.5.tar.gz 10.6 kB Details

Built distribution (wheel)

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

Total release size: 21.8 kB

Release files / chatstack-0.1.5.tar.gz

Download URL chatstack-0.1.5.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f9a23a9a67356b31b9ce509b7308283ea3c23462018a112bfe6978b39b43bf96
BLAKE2b-256 checksum
How to use checksums
ccd184d29e55068632d4342a9ceddf799097d5516a92d5568121d322d9c98f24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release files / chatstack-0.1.5-py3-none-any.whl

Download URL chatstack-0.1.5-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1d269f98bae12b8b8dc79a46f2be40a6241d20b60a2fe273806f108e81d3e1a
BLAKE2b-256 checksum
How to use checksums
89ac3ec71e111c2f5509fd338506867c79a15b6e42f8299bd26d87db305ff4b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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