generated from nhcarrigan/template
feat: use gemini flash for image generation
This commit is contained in:
+27
-26
@@ -3,18 +3,19 @@
|
|||||||
* @license Naomi's Public License
|
* @license Naomi's Public License
|
||||||
* @author Naomi Carrigan
|
* @author Naomi Carrigan
|
||||||
*/
|
*/
|
||||||
import Anthropic from "@anthropic-ai/sdk";
|
import { Anthropic } from "@anthropic-ai/sdk";
|
||||||
import { GoogleGenAI, PersonGeneration } from "@google/genai";
|
import { GoogleGenAI, } from "@google/genai";
|
||||||
import { AttachmentBuilder } from "discord.js";
|
import { AttachmentBuilder } from "discord.js";
|
||||||
/**
|
/**
|
||||||
*
|
* Utility class for generating project information and images.
|
||||||
*/
|
*/
|
||||||
export class Ai {
|
export class Ai {
|
||||||
anthropic;
|
anthropic;
|
||||||
gemini;
|
gemini;
|
||||||
/**
|
/**
|
||||||
* @param anthropicKey
|
* Creates a new instance of the Ai class.
|
||||||
* @param geminiKey
|
* @param anthropicKey - The API key for the Anthropic API.
|
||||||
|
* @param geminiKey - The API key for the Gemini API.
|
||||||
*/
|
*/
|
||||||
constructor(anthropicKey, geminiKey) {
|
constructor(anthropicKey, geminiKey) {
|
||||||
this.anthropic = new Anthropic({
|
this.anthropic = new Anthropic({
|
||||||
@@ -25,7 +26,9 @@ export class Ai {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param prompt
|
* Generates a list of potential project names and a full body anime girl mascot for the project.
|
||||||
|
* @param prompt - The user's prompt for the project.
|
||||||
|
* @returns A message create options object containing the project name, description, and image.
|
||||||
*/
|
*/
|
||||||
async generateProjectInfo(prompt) {
|
async generateProjectInfo(prompt) {
|
||||||
const projectRequest = await fetch("https://data.nhcarrigan.com/projects.json");
|
const projectRequest = await fetch("https://data.nhcarrigan.com/projects.json");
|
||||||
@@ -37,19 +40,20 @@ export class Ai {
|
|||||||
return p.name;
|
return p.name;
|
||||||
}).
|
}).
|
||||||
join(", ")}`, prompt);
|
join(", ")}`, prompt);
|
||||||
const image = await this.generateImage(`Your task is to generate a full body anime girl mascot for this project. The image should have a transparent background. Potential names: ${names}. The project description is: ${prompt}`);
|
const image = await this.generateImage(prompt);
|
||||||
if (image === null) {
|
if (image === null) {
|
||||||
return { content: `Project Name: ${names}\nProject Description: ${prompt}\nSorry, I was unable to generate an image for you.` };
|
return {
|
||||||
|
content: `Project Name: ${names}\nProject Description: ${prompt}\nSorry, I was unable to generate an image for you.`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return { content: `Project Name: ${names}\nProject Description: ${prompt}`,
|
return {
|
||||||
files: [new AttachmentBuilder(image, { name: "avatar.png" })] };
|
content: `Project Name: ${names}\nProject Description: ${prompt}`,
|
||||||
|
files: [new AttachmentBuilder(image, { name: "avatar.png" })],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @param system
|
|
||||||
* @param prompt
|
|
||||||
*/
|
|
||||||
async generateText(system, prompt) {
|
async generateText(system, prompt) {
|
||||||
const response = await this.anthropic.messages.create({
|
const response = await this.anthropic.messages.create({
|
||||||
|
// eslint-disable-next-line @typescript-eslint/naming-convention -- SDK requirement.
|
||||||
max_tokens: 1000,
|
max_tokens: 1000,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@@ -70,22 +74,19 @@ export class Ai {
|
|||||||
join("");
|
join("");
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @param prompt
|
|
||||||
*/
|
|
||||||
async generateImage(prompt) {
|
async generateImage(prompt) {
|
||||||
const response = await this.gemini.models.generateImages({
|
const response = await this.gemini.models.generateContent({
|
||||||
config: {
|
config: {
|
||||||
aspectRatio: "3:4",
|
imageConfig: { aspectRatio: "3:4" },
|
||||||
imageSize: "2K",
|
systemInstruction: "Your task is to generate a full body anime girl mascot for this project. This means the full character should be visible. The image should have a white background. NEVER include text in the image, no text anywhere at all. The project description is provided by the user.",
|
||||||
numberOfImages: 1,
|
|
||||||
outputMimeType: "image/png",
|
|
||||||
personGeneration: PersonGeneration.ALLOW_ADULT,
|
|
||||||
},
|
},
|
||||||
model: "models/imagen-4.0-generate-001",
|
contents: prompt,
|
||||||
prompt: prompt,
|
model: "gemini-2.5-flash-image",
|
||||||
});
|
});
|
||||||
const base64 = response.generatedImages?.[0]?.image?.imageBytes;
|
const image = response.candidates?.[0]?.content?.parts?.find((p) => {
|
||||||
|
return Boolean(p.inlineData);
|
||||||
|
});
|
||||||
|
const base64 = image?.inlineData?.data;
|
||||||
if (base64 === undefined) {
|
if (base64 === undefined) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-18
@@ -5,7 +5,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Anthropic } from "@anthropic-ai/sdk";
|
import { Anthropic } from "@anthropic-ai/sdk";
|
||||||
import { GoogleGenAI, PersonGeneration } from "@google/genai";
|
import {
|
||||||
|
GoogleGenAI,
|
||||||
|
} from "@google/genai";
|
||||||
import { AttachmentBuilder, type MessageCreateOptions } from "discord.js";
|
import { AttachmentBuilder, type MessageCreateOptions } from "discord.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,14 +36,15 @@ export class Ai {
|
|||||||
* @param prompt - The user's prompt for the project.
|
* @param prompt - The user's prompt for the project.
|
||||||
* @returns A message create options object containing the project name, description, and image.
|
* @returns A message create options object containing the project name, description, and image.
|
||||||
*/
|
*/
|
||||||
public async generateProjectInfo(prompt: string):
|
public async generateProjectInfo(
|
||||||
Promise<MessageCreateOptions> {
|
prompt: string,
|
||||||
|
): Promise<MessageCreateOptions> {
|
||||||
const projectRequest = await fetch(
|
const projectRequest = await fetch(
|
||||||
"https://data.nhcarrigan.com/projects.json",
|
"https://data.nhcarrigan.com/projects.json",
|
||||||
);
|
);
|
||||||
const projectResponse
|
const projectResponse
|
||||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fetch does not accept a generic.
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fetch does not accept a generic.
|
||||||
= (await projectRequest.json()) as Array<{ name: string }>;
|
= (await projectRequest.json()) as Array<{ name: string }>;
|
||||||
const names = await this.generateText(
|
const names = await this.generateText(
|
||||||
`Your task is to generate a project name based on the user's description. Provide ONLY a list of 1-5 fitting names, and an explanation for why you chose them. Note that project names should be unique. Here's a list of all existing project names: ${projectResponse.
|
`Your task is to generate a project name based on the user's description. Provide ONLY a list of 1-5 fitting names, and an explanation for why you chose them. Note that project names should be unique. Here's a list of all existing project names: ${projectResponse.
|
||||||
map((p) => {
|
map((p) => {
|
||||||
@@ -50,12 +53,16 @@ export class Ai {
|
|||||||
join(", ")}`,
|
join(", ")}`,
|
||||||
prompt,
|
prompt,
|
||||||
);
|
);
|
||||||
const image = await this.generateImage(`Your task is to generate a full body anime girl mascot for this project. The image should have a transparent background. NEVER include text in the image. The project description is: ${prompt}`);
|
const image = await this.generateImage(prompt);
|
||||||
if (image === null) {
|
if (image === null) {
|
||||||
return { content: `Project Name: ${names}\nProject Description: ${prompt}\nSorry, I was unable to generate an image for you.` };
|
return {
|
||||||
|
content: `Project Name: ${names}\nProject Description: ${prompt}\nSorry, I was unable to generate an image for you.`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return { content: `Project Name: ${names}\nProject Description: ${prompt}`,
|
return {
|
||||||
files: [ new AttachmentBuilder(image, { name: "avatar.png" }) ] };
|
content: `Project Name: ${names}\nProject Description: ${prompt}`,
|
||||||
|
files: [ new AttachmentBuilder(image, { name: "avatar.png" }) ],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private async generateText(system: string, prompt: string): Promise<string> {
|
private async generateText(system: string, prompt: string): Promise<string> {
|
||||||
@@ -83,18 +90,19 @@ export class Ai {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async generateImage(prompt: string): Promise<Buffer | null> {
|
private async generateImage(prompt: string): Promise<Buffer | null> {
|
||||||
const response = await this.gemini.models.generateImages({
|
const response = await this.gemini.models.generateContent({
|
||||||
config: {
|
config: {
|
||||||
aspectRatio: "3:4",
|
imageConfig: { aspectRatio: "3:4" },
|
||||||
imageSize: "2K",
|
systemInstruction:
|
||||||
numberOfImages: 1,
|
`Your task is to generate a full body anime girl mascot for this project. This means the full character should be visible. The image should have a white background. NEVER include text in the image, no text anywhere at all. The project description is provided by the user.`,
|
||||||
outputMimeType: "image/png",
|
|
||||||
personGeneration: PersonGeneration.ALLOW_ADULT,
|
|
||||||
},
|
},
|
||||||
model: "models/imagen-4.0-generate-001",
|
contents: prompt,
|
||||||
prompt: prompt,
|
model: "gemini-2.5-flash-image",
|
||||||
});
|
});
|
||||||
const base64 = response.generatedImages?.[0]?.image?.imageBytes;
|
const image = response.candidates?.[0]?.content?.parts?.find((p) => {
|
||||||
|
return Boolean(p.inlineData);
|
||||||
|
});
|
||||||
|
const base64 = image?.inlineData?.data;
|
||||||
if (base64 === undefined) {
|
if (base64 === undefined) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user