Add Feature: Image Generation with OpenAI's Dall-E API
I'll like to add a new feature to the app. I want my users to be able to generate images using OpenAI's Dall-E api, users will access this feature via their workspace and it will store info in the image entity like prompt, revised_prompt, image_b64. Print out image.entity.ts.
typescriptimport { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne } from 'typeorm';
import { Workspace } from './workspace.entity';
@Entity('image')
export class Image {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
prompt: string;
@Column({ nullable: true })
revised_prompt: string;
@Column('text')
image_b64: string;
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;
@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;
@ManyToOne(() => Workspace, workspace => workspace.images)
@Column({ name: 'workspace_id' })
workspaceId: string;
}
suggest code for image.service.ts
typescriptimport { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Image } from './image.entity';
import { Workspace } from './workspace.entity';
import { CreateImageDto } from './dto/create-image.dto';
import { OpenAIService } from '../openai/openai.service';
@Injectable()
export class ImageService {
constructor(
@InjectRepository(Image)
private readonly imageRepository: Repository<Image>,
private readonly openAIService: OpenAIService
) {}
async generateImage(createImageDto: CreateImageDto, workspace: Workspace): Promise<Image> {
// Retrieve the DALL-E API Key from the Workspace
const apiKey = workspace.openAiApiKey;
// Call OpenAI Service to generate the image
const imageResponse = await this.openAIService.generateImage(createImageDto.prompt, apiKey);
// Store the new image data
const image = new Image();
image.prompt = createImageDto.prompt;
// Optional: process and store a revised_prompt, if needed
// image.revised_prompt = someRevisedPromptFunction(createImageDto.prompt);
image.image_b64 = imageResponse; // Assuming generateImage method from OpenAIService returns base64 string
image.workspaceId = workspace.id;
return this.imageRepository.save(image);
}
async findImagesByWorkspace(workspaceId: string): Promise<Image[]> {
return this.imageRepository.find({
where: { workspaceId },
});
}
}
Please note that the CreateImageDto
would be a Data Transfer Object that you would need to create to handle the incoming data payload when a user requests image generation. The OpenAIService
is also not defined here; it would be a service you'd write to handle the communication with OpenAI's APIs, including DALL-E for image generation.
The image.image_b64
line assumes that your OpenAIService's generateImage
method is returning a base64-encoded string of the generated image. If it's different, you'll need to handle the response appropriately.
print create-image.dto.ts
typescriptimport { IsNotEmpty, IsString } from 'class-validator';
export class CreateImageDto {
@IsNotEmpty()
@IsString()
prompt: string;
}
This CreateImageDto
ensures that a prompt
is provided and that it is a string when a request is made to generate an image in a workspace. If you require additional fields like adjusting the style or parameters specific to the DALL-E API, you'll have to add those as well, with suitable validation decorators.