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.
from chatgpt_block import ChatGPTBlock
# Initialize the ChatGPTBlock instance
chatgpt_block = ChatGPTBlock(
system_prompt="You are a helpful assistant.",
openai_api_token="your_openai_api_key",
model="gpt-4",
)
# Get a response from the model
response = chatgpt_block("Tell me a joke.")
print(response)
from chatgpt_block import ChatGPTBlock
chatgpt_block = ChatGPTBlock(
system_prompt="You are a helpful assistant.",
openai_api_token="your_openai_api_key",
model="gpt-4",
stream=True
)
generator = chatgpt_block("Tell me a joke.")
for token in generator:
print(token, flush=True, end='')
Resetting The Conversation
chatgpt_block("Tell me a joke.")
print(chatgpt_block.answer)
chatgpt_block.reset()
response = chatgpt_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}"
chatgpt_block = ChatGPTBlock(
system_prompt="You are a helpful assistant.",
openai_api_token="your_openai_api_key",
model="gpt-4",
preprocessor=custom_preprocessor,
)
response = chatgpt_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")
chatgpt_block = ChatGPTBlock(
system_prompt="You are a helpful assistant.",
openai_api_token="your_openai_api_key",
model="gpt-4",
on_error=custom_error_handler,
)
response = chatgpt_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"),
]
chatgpt_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_token="your_openai_api_key",
model="gpt-4",
)
response = chatgpt_block("Make up some new word with \"tortoise\"")
print(response)
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
Hashes for chatgpt_block-0.1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77fab3912a3bdffe826b8c799eeadc0424f87dd59f08f6906b95b0d2939f6b1e |
|
MD5 | f97e726a793d46cae34db4f1d483537a |
|
BLAKE2b-256 | 9d5bed1244dda2459d69634129fad1daeb56563105eda7d043c1668dc6fbf51f |