Next.js + Inngest: Unlocking Long-Running AI Workflow Automation
Science & Technology
Introduction
Backend services, particularly those related to AI, can be challenging to handle within an application. AI jobs often require the ability to retry after encountering errors, implement throttling, and effectively manage a sequence of tasks that require coordination. To tackle this complexity, it is beneficial to use a workflow engine like Inngest, making your development process smoother and more efficient.
Building an AI Application
In this article, we will walk through the process of creating an AI application involved in background rating. The application captures an image using a webcam, reviews the background, identifies its theme, and generates a new background based on that theme.
The Workflow of the Application
When the user captures an image, the app initiates two parallel AI jobs: one to review the background and another to determine the theme. The background review includes insights on the good and bad aspects, while the theme detection provides a title that informs the generation of a new background image.
The application works as follows:
- The captured image is sent to two separate AI services for processing.
- The review and theme are generated concurrently.
- Once the theme is identified, it is used to generate a new background image.
- The results are stored appropriately—images in Vercel's blob storage and text outputs in a PostgreSQL database.
To manage retries and throttling, we utilize Inngest, which simplifies the coordination of these tasks while seamlessly handling potential errors.
Code Deep Dive
By reviewing our code, we can see that:
- Upon capturing an image, it is uploaded and stored.
- An event is sent to Inngest, triggering the AI jobs to process the image and generate outcomes.
- The communication between Inngest and the Next.js application is established using API endpoints for function invocation.
- We create separate functions to handle the review, theme detection, and image generation.
In addition, Inngest allows for:
- Task management via retries and throttling.
- Cleaner separation of logic, with the primary application handling all business logic while Inngest coordinates workflow.
Handling Complex Workflows
In order to execute these tasks efficiently, we define functions within Inngest that encapsulate all the necessary logic—ensuring that both the review process and the theme extraction occur in parallel without blocking one another.
Furthermore, for generating the new image with DALL-E, we ensure that request rates stay within API limits using Inngest’s throttling capabilities.
In summary, by employing Inngest within our Next.js application, we can efficiently manage complex workflows, streamline the handling of retries and throttling, and visualize the entire process, making it a powerful tool for developing robust AI applications.
Keywords
Next.js, Inngest, AI workflow, background rating, image generation, API, workflow engine, retries, throttling, DALL-E, PostgreSQL, Vercel.
FAQ
What is Inngest? Inngest is a workflow engine designed to help developers manage complex task sequences, provide automatic retries, support event-driven architectures, and handle workloads such as API calls.
How do I handle image uploads in Next.js? You can use server actions in your Next.js application to handle image uploads, process and store the data in blob storage, and redirect to other application states as needed.
What are the benefits of using Inngest for AI services? By using Inngest, you can automate long-running tasks, manage complex workflows more efficiently, handle retries and throttling, and visualize your task sequences with ease.
Can I run multiple AI jobs in parallel with Inngest? Yes, Inngest allows you to execute multiple jobs in parallel, enabling you to optimize the performance of your application and improve user experience.
How does Inngest help with error handling? Inngest offers built-in handling of retries for failed tasks, allowing developers to focus on business logic without needing to build extensive error recovery mechanisms.