ArpitxGit / AIChatBotBackend

aichatbotbackend-414059.spheron.app
0 stars 1 forks source link

Refactor assist endpoint for concurrency #3

Closed adg29 closed 6 months ago

adg29 commented 6 months ago

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

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_KbMKmuz8N5nijYH8ucI0wEYV

Posts

asst_gN17ebClyJwaXaD7WI6YvTnt https://platform.openai.com/assistants/asst_gN17ebClyJwaXaD7WI6YvTnt

Key Functionalities:

  1. 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 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.
  2. 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.
  3. 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.
  4. 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.