langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.24k stars 2.07k forks source link

DallEAPIWrapper returns only one image url even though n > 1 #6151

Closed mya03 closed 1 month ago

mya03 commented 1 month ago

Checked other resources

Example Code

The code that where I use DallEAPIWrapper looks something similar to this:

import { ChatOpenAI, DallEAPIWrapper } from "@langchain/openai"; 

async generateImage(params: ImageDto){
        const {prompt, numImages, model} = params;

        const tool = new DallEAPIWrapper({
            apiKey: API_KEY,
            model,
           n: numImages //numImages = 2 for example
        });

       const result = tool.invoke({prompt});
       return result;

Error Message and Stack Trace (if applicable)

No response

Description

I am trying to generate multiple images using the DallEAPIWrapper tool in my project but I think it currently only returns the first image url even though I set n to be 2.

System Info

"langchain": "^0.2.10" "@langchain/openai": "^0.2.2"

dosubot[bot] commented 1 month ago

Hello, @mya03! I'm here to assist you with any bugs, questions, or contributions. Let's take a look at the issue you've encountered.

To generate multiple images using the DallEAPIWrapper in LangChain.js, you need to ensure that you are using the DALL-E 2 model, as DALL-E 3 only supports generating a single image (n: 1). Here is how you can modify your code to achieve this:

import { DallEAPIWrapper } from "@langchain/openai";

async function generateImage(params: ImageDto) {
    const { prompt, numImages, model } = params;

    const tool = new DallEAPIWrapper({
        apiKey: API_KEY,
        model: model || "dall-e-2", // Ensure you are using DALL-E 2
        n: numImages // numImages = 2 for example
    });

    const result = await tool.invoke(prompt);
    return result;
}

Make sure that the model parameter is set to "dall-e-2" if you want to generate more than one image. If you are using DALL-E 3, it will only return one image regardless of the value of n [1].

Additionally, ensure that your invoke method is awaited properly to handle the asynchronous call [1].

To continue talking to Dosu, mention @dosu.

bracesproul commented 1 month ago

hey @mya03 thanks for flagging this. I just put up a PR which allows for passing n > 1, and returning a list of MessageContentImageUrl objects containing either the image URLs or the base64 strings.