No project description provided
Project description
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
- streamable mode
- supporting the history of the conversation
- resetting the history of the conversation
- adding your custom pre-processing
Installation
Install Locally
git clone https://github.com/SkuratovichA/chatgpt_block
cd chatgpt_block
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file chatgpt_block-0.1.6.tar.gz
.
File metadata
- Download URL: chatgpt_block-0.1.6.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20a4456e5a888bab2d795b4e3e09d24669faa0e8f06c0799bfed3a91c251ff4f |
|
MD5 | 2a04467a3f122cdcbabb7d4639f4a029 |
|
BLAKE2b-256 | 38e5ce70b2f1175264090f0c170c3b4cda3a5b1359f94ffd21ada7accec4c91a |
File details
Details for the file chatgpt_block-0.1.6-py3-none-any.whl
.
File metadata
- Download URL: chatgpt_block-0.1.6-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e83394c5e49494395eb56512e3fe55c2dd058624d827accc8f7b3476579f63cf |
|
MD5 | cfdcd4cc328ad313da465dbb9098c28e |
|
BLAKE2b-256 | f92fa1d5f2d592b155c8544425d158bfecd449ad7ce989cbb5bebd80da3be513 |