Search code examples
node.jstypescriptamazon-web-servicesaws-sdkamazon-bedrock

How to generate an image with @aws-sdk/client-bedrock-runtime


After creating a BedrockRuntimeClient in typescript, I don't know how to invoke the model and send the command.

const client = new BedrockRuntimeClient({
        region: "us-east-1",
        apiVersion: '2023-09-30',
        credentials:{
            accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? '',
            secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? ''
        }
    });

Solution

  • You have to create first an input like this:

    const input = { 
        contentType: 'application/json',
        accept: '*/*',
        modelId: 'stability.stable-diffusion-xl-v0',
        body: `{
            "text_prompts":[
                {
                    "text":"A beautiful picture of a bird"
                }],
            "cfg_scale":10,
            "seed":0,
            "steps":50}`,
    };
    

    Then, create the command and send it:

    const command = new InvokeModelCommand(input);
    const response = await client.send(command);
    

    (you have to do this in an async function)

    Next, convert the response into a json to get the base64 image

    const blobAdapter = response.body;    
    
    const textDecoder = new TextDecoder('utf-8');
    const jsonString = textDecoder.decode(blobAdapter.buffer);
    
    try {
    const parsedData = JSON.parse(jsonString);
        return parsedData.artifacts[0].base64
    } catch (error) {
        console.error('Error parsing JSON:', error);
        return 'TextError';
    }
    

    Finally, you can do whatever you want with the base64, like put it in an <img> tag

    <img src={`data:image/png;base64, ${base64_image}`}>
    

    Here's the final code:

    export async function functionName(prompt:string){   
        const client = new BedrockRuntimeClient({
            region: "us-east-1",
            apiVersion: '2023-09-30',
            credentials:{
                accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? '',
                secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? ''
            }
        });
    
        const input = { 
            contentType: 'application/json',
            accept: '*/*',
            modelId: 'stability.stable-diffusion-xl-v0',
            body: `{
                "text_prompts":[
                    {
                        "text":"${prompt}"
                    }],
                "cfg_scale":10,
                "seed":0,
                "steps":50}`,
        };
    
        const command = new InvokeModelCommand(input);
        const response = await client.send(command);
    
        const blobAdapter = response.body;    
    
        const textDecoder = new TextDecoder('utf-8');
        const jsonString = textDecoder.decode(blobAdapter.buffer);
    
        try {
        const parsedData = JSON.parse(jsonString);
            return parsedData.artifacts[0].base64
        } catch (error) {
            console.error('Error parsing JSON:', error);
            return 'TextError';
        }
    }
    
    

    I hope this helps someone.