This refactor aids in handling concurrent requests more effectively by isolating thread creation, run monitoring, and message parsing into individual, composable functions. Additionally, error handling has been centralized using middleware, improving the robustness of error responses. The result is an assist endpoint that is more maintainable
Changes
Added image generation and description capabilities using OpenAI's API, implemented comprehensive asynchronous handling for threads and runs, and introduced robust error handling.
Revamped the /api/assist route to streamline processing and enhance efficiency.
Replaced synchronous checks within app.post with an asynchronous createAndMonitorRun function that utilizes polling for run status updates.
Integrated handleRequiredActions for dynamic action handling and retrieveAssistantMessages for cleaner message retrieval.
A middleware is employed to capture and respond to errors throughout the application, ensuring that all unhandled exceptions are caught and appropriately logged.
Assistants
Intended to be used with function calling for generating role and post images. See OpenAI platform for new assistants with defined functions
generateImage(prompt): Generates an image based on a given prompt using the DALL-E model from OpenAI. It logs the image generation process and returns the image URL.
generateImageConsistent(prompt, imagegen_ledger): Similar to generateImage but modifies the prompt with additional descriptions to maintain consistency, useful for follow-up images that need to align with an initial theme or style.
Image Description:
describeImage(imgUrl, title, imagegen_ledger): Uses the OpenAI chat model to generate a detailed description of the main subject in an image. This description aids in maintaining consistency in image characteristics across multiple generations. It updates a ledger with image details, including URL and description.
Thread and Run Management:
createAndMonitorRun(threadId, assistantId): Manages the lifecycle of a thread run, checking its status and handling different outcomes like "completed" or "requires_action". If action is required, it triggers handleRequiredActions to process these needs.
handleRequiredActions(run, threadId): Handles specific actions required by a run, such as generating additional images or descriptions based on the run's needs.
Utility Functions:
retrieveAssistantMessages(threadId): Retrieves messages from a thread, specifically filtering messages by the assistant role.
processThread(req, res, next): Orchestrates the processing of a thread from the API request to response, handling message creation, run monitoring, and response formatting.
formatResponse(userInput, latestAssistantValue, run): Formats the server response to include the original user input, the assistant's response, and the run status.
parseLatestAssistantMessage(messages): Parses and processes the latest message from the assistant, extracting structured data from text.
Refactor
This refactor aids in handling concurrent requests more effectively by isolating thread creation, run monitoring, and message parsing into individual, composable functions. Additionally, error handling has been centralized using middleware, improving the robustness of error responses. The result is an assist endpoint that is more maintainable
Changes
/api/assist
route to streamline processing and enhance efficiency.app.post
with an asynchronouscreateAndMonitorRun
function that utilizes polling for run status updates.handleRequiredActions
for dynamic action handling andretrieveAssistantMessages
for cleaner message retrieval.Assistants
Intended to be used with function calling for generating role and post images. See OpenAI platform for new assistants with defined functions
Role
asst_KbMKmuz8N5nijYH8ucI0wEYV
https://platform.openai.com/assistants/asst_KbMKmuz8N5nijYH8ucI0wEYVPosts
asst_gN17ebClyJwaXaD7WI6YvTnt
https://platform.openai.com/assistants/asst_gN17ebClyJwaXaD7WI6YvTntKey Functionalities:
Image Generation:
generateImage(prompt)
: Generates an image based on a given prompt using the DALL-E model from OpenAI. It logs the image generation process and returns the image URL.generateImageConsistent(prompt, imagegen_ledger)
: Similar togenerateImage
but modifies the prompt with additional descriptions to maintain consistency, useful for follow-up images that need to align with an initial theme or style.Image Description:
describeImage(imgUrl, title, imagegen_ledger)
: Uses the OpenAI chat model to generate a detailed description of the main subject in an image. This description aids in maintaining consistency in image characteristics across multiple generations. It updates a ledger with image details, including URL and description.Thread and Run Management:
createAndMonitorRun(threadId, assistantId)
: Manages the lifecycle of a thread run, checking its status and handling different outcomes like "completed" or "requires_action". If action is required, it triggershandleRequiredActions
to process these needs.handleRequiredActions(run, threadId)
: Handles specific actions required by a run, such as generating additional images or descriptions based on the run's needs.Utility Functions:
retrieveAssistantMessages(threadId)
: Retrieves messages from a thread, specifically filtering messages by the assistant role.processThread(req, res, next)
: Orchestrates the processing of a thread from the API request to response, handling message creation, run monitoring, and response formatting.formatResponse(userInput, latestAssistantValue, run)
: Formats the server response to include the original user input, the assistant's response, and the run status.parseLatestAssistantMessage(messages)
: Parses and processes the latest message from the assistant, extracting structured data from text.