@@ -9,6 +9,7 @@ import { createOllama } from "ollama-ai-provider";
9
9
import OpenAI from "openai" ;
10
10
import { default as tiktoken , TiktokenModel } from "tiktoken" ;
11
11
import Together from "together-ai" ;
12
+ import ImageFile from "together-ai" ;
12
13
import { elizaLogger } from "./index.ts" ;
13
14
import models from "./models.ts" ;
14
15
import { createGoogleGenerativeAI } from "@ai-sdk/google" ;
@@ -26,6 +27,7 @@ import {
26
27
ModelProviderName ,
27
28
ServiceType ,
28
29
} from "./types.ts" ;
30
+ import axios from 'axios' ; // Add this import at the top
29
31
30
32
/**
31
33
* 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 (
654
656
width : number ;
655
657
height : number ;
656
658
count ?: number ;
659
+ useTogetherAI ?: boolean ;
657
660
} ,
658
661
runtime : IAgentRuntime
659
662
) : Promise < {
660
663
success : boolean ;
661
664
data ?: string [ ] ;
662
665
error ?: any ;
663
666
} > => {
664
- const { prompt, width, height } = data ;
667
+ const { prompt, width, height, useTogetherAI } = data ;
665
668
let { count } = data ;
666
669
if ( ! count ) {
667
670
count = 1 ;
@@ -670,43 +673,54 @@ export const generateImage = async (
670
673
const model = getModel ( runtime . character . modelProvider , ModelClass . IMAGE ) ;
671
674
const modelSettings = models [ runtime . character . modelProvider ] . imageSettings ;
672
675
// 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");
677
680
678
681
try {
679
- if ( runtime . character . modelProvider === ModelProviderName . LLAMACLOUD ) {
682
+ const apiKey = runtime . getSetting ( "TOGETHER_API_KEY" )
683
+ if ( useTogetherAI || runtime . character . modelProvider === ModelProviderName . LLAMACLOUD ) {
680
684
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
+
681
688
const response = await together . images . create ( {
682
- model : "black-forest-labs/FLUX.1-schnell " ,
689
+ model : "black-forest-labs/FLUX.1-pro " ,
683
690
prompt,
684
691
width,
685
692
height,
686
- steps : modelSettings ?. steps ?? 4 ,
693
+ steps : modelSettings ?. steps ?? 28 ,
687
694
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" ) ;
708
718
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 (
710
724
targetSize !== "1024x1024" &&
711
725
targetSize !== "1792x1024" &&
712
726
targetSize !== "1024x1792"
@@ -717,7 +731,7 @@ export const generateImage = async (
717
731
const response = await openai . images . generate ( {
718
732
model,
719
733
prompt,
720
- size : targetSize as "1024x1024" | "1792x1024" | "1024x1792" ,
734
+ size : targetSize as "1024x1024" | "1792x1024" | "1024x1792" | "512x512" | "256x256" ,
721
735
n : count ,
722
736
response_format : "b64_json" ,
723
737
} ) ;
0 commit comments