Package: chatgpt-block
ChatGPTBlockA Python package for interacting with OpenAI's chat models through the OpenAI API.Table Of ContentsIntroductionFeaturesInstallationUsageContributingLicenseIntroductionThe 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.Featuresstreamable modesupporting the history of the conversationresetting the history of the conversationadding your custom pre-processingInstallationInstall Locallygit clone https://github.com/SkuratovichA/chatgpt_blockcd chatgpt_blockpip install -e .Install With PIPpip install chatgpt_blockUsageCreating The ConversationHere's an example of how to use the ChatGPTBlock class:
There are two options of using the class. Streamable and non-streamable.Non-Streamable Modefromchatgpt_blockimportChatGPTBlock# Initialize the ChatGPTBlock instancechat_gpt_block=ChatGPTBlock(system_prompt="You are a helpful assistant.",openai_api_key="your_openai_api_key",model="gpt-4",preprocessor=lambdax:x,)# Get a response from the modelresponse=chat_gpt_block("Tell me a joke.")print(response)Streamable Modefromchatgpt_blockimportChatGPTBlockchat_gpt_block=ChatGPTBlock(system_prompt="You are a helpful assistant.",openai_api_key="your_openai_api_key",model="gpt-4",stream=True,preprocessor=lambdax:x,)generator=chat_gpt_block("Tell me a joke.")fortokeningenerator:print(token,flush=True,end='')Resetting The Conversationfromchatgpt_blockimportChatGPTBlockchat_gpt_block=ChatGPTBlock(system_prompt="You are a helpful assistant.",openai_api_key="your_openai_api_key",model="gpt-4",stream=True,preprocessor=lambdax: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 Functionfromchatgpt_blockimportChatGPTBlockdefcustom_preprocessor(input_text:str,some_dictionary:dict,prologue:str)->str:returnf"{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 Functionfromchatgpt_blockimportChatGPTBlockdefcustom_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=lambdax:x,)response=chat_gpt_block("tell me a joke")print(response)Feeding The Model With The Examplesfromchatgpt_blockimportChatGPTBlockexamples=[("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=lambdax:x,)response=chat_gpt_block("Make up some new word with\"tortoise\"")print(response)Silent Errorsfromchatgpt_blockimportChatGPTBlock# Initialize the ChatGPTBlock instancechat_gpt_block=ChatGPTBlock(system_prompt="wrong key",openai_api_key="your_openai_api_key",model="gpt-4",preprocessor=lambdax:x,raise_on_error=False,)# Get a response from the modelresponse=chat_gpt_block("Tell me a joke.")print(response)# OpenAI internal error. No API key provided. You can ...Errors as Exceptionsfromchatgpt_blockimportChatGPTBlock# Initialize the ChatGPTBlock instancechat_gpt_block=ChatGPTBlock(system_prompt="wrong key",openai_api_key="your_openai_api_key",model="gpt-4",preprocessor=lambdax:x,raise_on_error=True,)# Get a response from the modelresponse=chat_gpt_block("Tell me a joke.")# AuthenticationError: No API key provided ...ContributingThis package is free to any ideas. Just create an issue or a pull request on GitHub.Licensechatgpt_blockis released underMIT License.