Technical AI Prompt

Image Editing Service API

Build an API to edit multiple images with OpenAI and store token usage with logging. Perfectly crafted free system prompt or custom instructions for ChatGPT, Gemini, and Claude chatbots and models.

Create a Python service, API endpoint, view, and serializer that implement a new image editing function capable of processing multiple image inputs. This function must:

- Accept multiple image files opened in binary mode (rb) as a list and pass them to the OpenAI image editing client.
- Support both "gpt-image-1" model, which accepts multiple images, and "dall-e-2" model, which accepts only one image.
- Use the OpenAI Python client for image editing as shown in the provided sample code.
- Handle exceptions robustly, including but not limited to file handling errors, API errors, and validation errors.
- Implement structured logging using `structlog` to log key events and errors clearly.
- Collect and store OpenAI token usage details (such as total tokens, input and output tokens, and token breakdowns) after each image editing request.
- Return or store the edited image(s) in base64 format after decoding, similar to the sample.

Requirements:

- The API should accept image uploads and a prompt.
- Validate the number of images based on the model selected.
- Ensure clean and maintainable code with appropriate use of serializers for input validation.
- The service layer should encapsulate the interaction with the OpenAI API.


# Steps

1. Design a serializer class to validate the input images and prompt text.
2. Create a service function that:
   - Opens images in 'rb' mode,
   - Calls the appropriate OpenAI image edit method,
   - Handles different model capabilities for multiple images,
   - Logs request and error details with structlog,
   - Extracts and stores token usage.
3. Develop an API view (using Django Rest Framework or similar framework) to:
   - Accept multi-part/form-data including images and prompt,
   - Select the model and enforce image count restrictions,
   - Invoke the service function,
   - Handle and return proper HTTP responses.
4. Implement error handling across all layers with meaningful log entries.

# Output Format

Provide complete, runnable Python code snippets for:
- Serializer class(es).
- Image editing service function/class.
- API view handling the request.

Include inline comments explaining key parts of the code. Use idiomatic and clear Python 3.x syntax.

# Notes

- Assume the OpenAI Python client and structlog are installed and configured.
- You may use Django Rest Framework or a similar web framework for API view and serializer implementations.
- Focus on clarity, error handling, and logging completeness.

# Examples

Use the provided sample code as a reference for calling the OpenAI image editing endpoint with multiple images.


Create your own custom GPT chatbot with your own data and knowledge. Use for customer support, internal knowledge sharing, or anything else you can imagine.

Customer avatarCustomer avatarCustomer avatarCustomer avatarCustomer avatar
Loved by 75k+ users
OSZAR »