Dynamic Answer class and OpenAI streaming API #19
Reference in New Issue
Block a user
Delete Branch "dynamic_answer"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This pull request introduces several changes with the main objective of enabling OpenAI's streaming API in the
chatmastermindapplication. This allows for the retrieval of AI responses gradually as a stream, which can significantly improve the user experience in interactions that involve large result sets.In particular, the main changes are as follows:
Added
tiktokenimport inopenai.pyand modifications to theOpenAIclass to support streaming. This includes the addition of a new classOpenAIAnswerto handle streaming API responses.Modified
requestfunction in theOpenAIclass: thestream=Trueflag is added to theopenai.ChatCompletion.createmethod to enable streaming API.Modified
question.pyto print the answer parts as they are streamed.Replaced the
Answerclass's string data type with a generator which supportsstrandGenerator[str, None, None]data types. Modifications are made to theAnswerclass methods to handle both data types accordingly.Updated the tests in
test_ais_openai.pyandtest_message.pyto reflect and validate these changes.These changes will provide a more robust interaction with the OpenAI API, especially for larger responses.