Skip to content

Commit 1871e9b

Browse files
authored
Togetherai Image Gen
Togetherai returns url to image Convert to Base64 String *Need to have image saved to file here & same with openai then return the file path back up *Or just send Base64 up & have image plugin handle saving image & returing file path
1 parent ccbd1de commit 1871e9b

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

packages/core/src/generation.ts

+44-30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createOllama } from "ollama-ai-provider";
99
import OpenAI from "openai";
1010
import { default as tiktoken, TiktokenModel } from "tiktoken";
1111
import Together from "together-ai";
12+
import ImageFile from "together-ai";
1213
import { elizaLogger } from "./index.ts";
1314
import models from "./models.ts";
1415
import { createGoogleGenerativeAI } from "@ai-sdk/google";
@@ -26,6 +27,7 @@ import {
2627
ModelProviderName,
2728
ServiceType,
2829
} from "./types.ts";
30+
import axios from 'axios'; // Add this import at the top
2931

3032
/**
3133
* Send a message to the model for a text generateText - receive a string back and parse how you'd like
@@ -654,14 +656,15 @@ export const generateImage = async (
654656
width: number;
655657
height: number;
656658
count?: number;
659+
useTogetherAI?: boolean;
657660
},
658661
runtime: IAgentRuntime
659662
): Promise<{
660663
success: boolean;
661664
data?: string[];
662665
error?: any;
663666
}> => {
664-
const { prompt, width, height } = data;
667+
const { prompt, width, height, useTogetherAI } = data;
665668
let { count } = data;
666669
if (!count) {
667670
count = 1;
@@ -670,43 +673,54 @@ export const generateImage = async (
670673
const model = getModel(runtime.character.modelProvider, ModelClass.IMAGE);
671674
const modelSettings = models[runtime.character.modelProvider].imageSettings;
672675
// some fallbacks for backwards compat, should remove in the future
673-
const apiKey =
674-
runtime.token ??
675-
runtime.getSetting("TOGETHER_API_KEY") ??
676-
runtime.getSetting("OPENAI_API_KEY");
676+
// const apiKey =
677+
//runtime.token ??
678+
//runtime.getSetting("TOGETHER_API_KEY") ??
679+
//runtime.getSetting("OPENAI_API_KEY");
677680

678681
try {
679-
if (runtime.character.modelProvider === ModelProviderName.LLAMACLOUD) {
682+
const apiKey = runtime.getSetting("TOGETHER_API_KEY")
683+
if (useTogetherAI || runtime.character.modelProvider === ModelProviderName.LLAMACLOUD) {
680684
const together = new Together({ apiKey: apiKey as string });
685+
console.log("generating image from togetherai")
686+
console.log("IMAGE GENERATION PROMPT TO TOGETHER: " + prompt);
687+
681688
const response = await together.images.create({
682-
model: "black-forest-labs/FLUX.1-schnell",
689+
model: "black-forest-labs/FLUX.1-pro",
683690
prompt,
684691
width,
685692
height,
686-
steps: modelSettings?.steps ?? 4,
693+
steps: modelSettings?.steps ?? 28,
687694
n: count,
688-
});
689-
const urls: string[] = [];
690-
for (let i = 0; i < response.data.length; i++) {
691-
const json = response.data[i].b64_json;
692-
// decode base64
693-
const base64 = Buffer.from(json, "base64").toString("base64");
694-
urls.push(base64);
695-
}
696-
const base64s = await Promise.all(
697-
urls.map(async (url) => {
698-
const response = await fetch(url);
699-
const blob = await response.blob();
700-
const buffer = await blob.arrayBuffer();
701-
let base64 = Buffer.from(buffer).toString("base64");
702-
base64 = "data:image/jpeg;base64," + base64;
703-
return base64;
704-
})
705-
);
706-
return { success: true, data: base64s };
707-
} else {
695+
});
696+
// Type assertion to access the URL while we know it exists
697+
const imageUrl = (response.data[0] as any).url;
698+
console.log("Image URL from Together:", imageUrl);
699+
700+
if (!imageUrl) {
701+
console.error("No image URL in response:", response);
702+
return { success: false, error: "No image URL in response" };
703+
}
704+
705+
// Fetch image data
706+
const imageResponse = await fetch(imageUrl);
707+
const imageArrayBuffer = await imageResponse.arrayBuffer();
708+
const base64Data = Buffer.from(imageArrayBuffer).toString('base64');
709+
710+
// Format as data URL
711+
const base64Image = `data:image/jpeg;base64,${base64Data}`;
712+
713+
return { success: true, data: [base64Image] };
714+
715+
716+
}else {
717+
const apiKey = runtime.getSetting("OPENAI_API_KEY");
708718
let targetSize = `${width}x${height}`;
709-
if (
719+
if (targetSize == "256x256"){
720+
targetSize = "256x256"
721+
console.log("generating 400 by 400 image")
722+
}
723+
else if (
710724
targetSize !== "1024x1024" &&
711725
targetSize !== "1792x1024" &&
712726
targetSize !== "1024x1792"
@@ -717,7 +731,7 @@ export const generateImage = async (
717731
const response = await openai.images.generate({
718732
model,
719733
prompt,
720-
size: targetSize as "1024x1024" | "1792x1024" | "1024x1792",
734+
size: targetSize as "1024x1024" | "1792x1024" | "1024x1792" | "512x512" | "256x256",
721735
n: count,
722736
response_format: "b64_json",
723737
});

0 commit comments

Comments
 (0)