Skip to main content

ChatGPTBlock

A Python package for interacting with OpenAI's chat models through the OpenAI API.

Table Of Contents

Introduction

The main purpose of the package is to have a simple interface for interacting with OpenAI api. Both in streamable and non-streamable fashion. OpenAI API itself does not store the history and can throw and error if history is too long. The package provides the solution to this by simply counting the length of the history and trimming it when needed.

Features

  1. streamable mode
  2. supporting the history of the conversation
  3. resetting the history of the conversation
  4. adding your custom pre-processing

Installation

Install Locally

  1. git clone https://github.com/SkuratovichA/chatgpt_block
  2. cd chatgpt_block
  3. pip install -e .

Install With PIP

pip install chatgpt_block

Usage

Creating The Conversation

Here's an example of how to use the ChatGPTBlock class: There are two options of using the class. Streamable and non-streamable.

Non-Streamable Mode

from chatgpt_block import ChatGPTBlock

# Initialize the ChatGPTBlock instance
chat_gpt_block = ChatGPTBlock(
    system_prompt="You are a helpful assistant.",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    preprocessor=lambda x: x,
)

# Get a response from the model
response = chat_gpt_block("Tell me a joke.")
print(response)

Streamable Mode

from chatgpt_block import ChatGPTBlock

chat_gpt_block = ChatGPTBlock(
    system_prompt="You are a helpful assistant.",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    stream=True,
    preprocessor=lambda x: x,
)

generator = chat_gpt_block("Tell me a joke.")
for token in generator:
    print(token, flush=True, end='')

Resetting The Conversation

from chatgpt_block import ChatGPTBlock

chat_gpt_block = ChatGPTBlock(
    system_prompt="You are a helpful assistant.",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    stream=True,
    preprocessor=lambda x: x,
)

chat_gpt_block("Tell me a joke.")
print(chat_gpt_block.answer)

chat_gpt_block.reset()

response = chat_gpt_block("Tell me a story.")
print(response)

Custom Preprocessor Function

from chatgpt_block import ChatGPTBlock

def custom_preprocessor(input_text: str, some_dictionary: dict, prologue: str) -> str:
    return f"{prologue}\n{input_text}. {some_dictionary}"

chat_gpt_block = ChatGPTBlock(
    system_prompt="You are a helpful assistant.",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    preprocessor=custom_preprocessor,
)

response = chat_gpt_block(
    input_text="please check whether this python dictionary is valid", 
    some_dictionary={"hello": "world"}, prologue=''
)
print(response)

Custom Error Handling Function

from chatgpt_block import ChatGPTBlock

def custom_error_handler():
    print("There was an error when communicating with OpenAI API. Please try again later")

chat_gpt_block = ChatGPTBlock(
    system_prompt="You are a helpful assistant.",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    on_error=custom_error_handler,
    preprocessor=lambda x: x,
)

response = chat_gpt_block("tell me a joke")
print(response)

Feeding The Model With The Examples

from chatgpt_block import ChatGPTBlock

examples = [
    ("Please add a vegetable to an animal", 
     "Alpaca + avocado = alpacavocado"),
    ("Now I want something with cats", 
     "Cat + Cappuccino = cappucicatto"),
]

chat_gpt_block = ChatGPTBlock(
    system_prompt=
    """You are a helpful assistant with a sense of humor. 
       You know everything about animals, vegetables and how to combine them. 
       You must add animals to vegetables no matter what to create funny words.
    """,
    examples=examples,
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    preprocessor=lambda x: x,
)

response = chat_gpt_block("Make up some new word with \"tortoise\"")
print(response)

Silent Errors

from chatgpt_block import ChatGPTBlock

# Initialize the ChatGPTBlock instance
chat_gpt_block = ChatGPTBlock(
    system_prompt="wrong key",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    preprocessor=lambda x: x,
    raise_on_error=False,
)

# Get a response from the model
response = chat_gpt_block("Tell me a joke.")
print(response)  # OpenAI internal error. No API key provided. You can ...

Errors as Exceptions

from chatgpt_block import ChatGPTBlock

# Initialize the ChatGPTBlock instance
chat_gpt_block = ChatGPTBlock(
    system_prompt="wrong key",
    openai_api_key="your_openai_api_key",
    model="gpt-4",
    preprocessor=lambda x: x,
    raise_on_error=True,
)

# Get a response from the model
response = chat_gpt_block("Tell me a joke.")
# AuthenticationError: No API key provided ...

Contributing

This package is free to any ideas. Just create an issue or a pull request on GitHub.

License

chatgpt_block is released under MIT License.

Metadata

Release files for chatgpt-block 0.1.6

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

Source distribution (sdist)

Source distribution for chatgpt-block 0.1.6
File Size Uploaded
chatgpt_block-0.1.6.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chatgpt-block 0.1.6
File Interpreter ABI Platform
chatgpt_block-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / chatgpt_block-0.1.6.tar.gz

Download URL chatgpt_block-0.1.6.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
20a4456e5a888bab2d795b4e3e09d24669faa0e8f06c0799bfed3a91c251ff4f
BLAKE2b-256 checksum
How to use checksums
38e5ce70b2f1175264090f0c170c3b4cda3a5b1359f94ffd21ada7accec4c91a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / chatgpt_block-0.1.6-py3-none-any.whl

Download URL chatgpt_block-0.1.6-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e83394c5e49494395eb56512e3fe55c2dd058624d827accc8f7b3476579f63cf
BLAKE2b-256 checksum
How to use checksums
f92fa1d5f2d592b155c8544425d158bfecd449ad7ce989cbb5bebd80da3be513
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

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

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