mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-13 22:29:48 +08:00
Merge pull request #57 from zhao-newname/main
feat(baoyu-image-gen): support OpenAI chat completions endpoint for i…
This commit is contained in:
@@ -36,6 +36,7 @@ Environment variables:
|
|||||||
DASHSCOPE_IMAGE_MODEL Default DashScope model (z-image-turbo)
|
DASHSCOPE_IMAGE_MODEL Default DashScope model (z-image-turbo)
|
||||||
REPLICATE_IMAGE_MODEL Default Replicate model (google/nano-banana-pro)
|
REPLICATE_IMAGE_MODEL Default Replicate model (google/nano-banana-pro)
|
||||||
OPENAI_BASE_URL Custom OpenAI endpoint
|
OPENAI_BASE_URL Custom OpenAI endpoint
|
||||||
|
OPENAI_IMAGE_USE_CHAT Use /chat/completions instead of /images/generations (true|false)
|
||||||
GOOGLE_BASE_URL Custom Google endpoint
|
GOOGLE_BASE_URL Custom Google endpoint
|
||||||
DASHSCOPE_BASE_URL Custom DashScope endpoint
|
DASHSCOPE_BASE_URL Custom DashScope endpoint
|
||||||
REPLICATE_BASE_URL Custom Replicate endpoint
|
REPLICATE_BASE_URL Custom Replicate endpoint
|
||||||
|
|||||||
@@ -70,6 +70,10 @@ export async function generateImage(
|
|||||||
|
|
||||||
if (!apiKey) throw new Error("OPENAI_API_KEY is required");
|
if (!apiKey) throw new Error("OPENAI_API_KEY is required");
|
||||||
|
|
||||||
|
if (process.env.OPENAI_IMAGE_USE_CHAT === "true") {
|
||||||
|
return generateWithChatCompletions(baseURL, apiKey, prompt, model);
|
||||||
|
}
|
||||||
|
|
||||||
const size = args.size || getOpenAISize(model, args.aspectRatio, args.quality);
|
const size = args.size || getOpenAISize(model, args.aspectRatio, args.quality);
|
||||||
|
|
||||||
if (args.referenceImages.length > 0) {
|
if (args.referenceImages.length > 0) {
|
||||||
@@ -84,6 +88,40 @@ export async function generateImage(
|
|||||||
return generateWithOpenAIGenerations(baseURL, apiKey, prompt, model, size, args.quality);
|
return generateWithOpenAIGenerations(baseURL, apiKey, prompt, model, size, args.quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function generateWithChatCompletions(
|
||||||
|
baseURL: string,
|
||||||
|
apiKey: string,
|
||||||
|
prompt: string,
|
||||||
|
model: string
|
||||||
|
): Promise<Uint8Array> {
|
||||||
|
const res = await fetch(`${baseURL}/chat/completions`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model,
|
||||||
|
messages: [{ role: "user", content: prompt }],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = await res.text();
|
||||||
|
throw new Error(`OpenAI API error: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = (await res.json()) as { choices: Array<{ message: { content: string } }> };
|
||||||
|
const content = result.choices[0]?.message?.content ?? "";
|
||||||
|
|
||||||
|
const match = content.match(/data:image\/[^;]+;base64,([A-Za-z0-9+/=]+)/);
|
||||||
|
if (match) {
|
||||||
|
return Uint8Array.from(Buffer.from(match[1]!, "base64"));
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("No image found in chat completions response");
|
||||||
|
}
|
||||||
|
|
||||||
async function generateWithOpenAIGenerations(
|
async function generateWithOpenAIGenerations(
|
||||||
baseURL: string,
|
baseURL: string,
|
||||||
apiKey: string,
|
apiKey: string,
|
||||||
|
|||||||
Reference in New Issue
Block a user