# Multimodal Custom Function:  Integrating Text-to-Image Generation in Your Agent

{% hint style="warning" %}
**Notice:** GAME Cloud is currently **deprecated.** You can only use **GAME SDK** at this moment.
{% endhint %}

In this article, we’ll explore an exciting use case where your Agent can perform **text-to-image generation** within the GAME Cloud.

We'll guide you through the process of integrating an external image generator, passing the necessary arguments, headers, and payload, and handling both success and error feedback.

<figure><img src="/files/Wk37xSL5tBx8BwUf6wTT" alt=""><figcaption><p>getimg.ai </p></figcaption></figure>

## **About** [**getimg.ai**](http://getimg.ai/) **and its image gen models**

Within the [getimg.ai](http://getimg.ai) platform, you’ll find several powerful models used for image generation. Among the most popular are **Stable Diffusion 1.5** and **Stable Diffusion XL**.

To provide some context, SDXL utilizes a **base model** during the high-noise diffusion stage and a **refiner model** for the low-noise diffusion stage.

The base resolution of SDXL is 1024x1024 (though it’s possible to train with other resolutions as well). In contrast, SD15 has a base resolution of 512x512 (with common resolutions around 768x768, although other options can be explored).

As a result, SDXL offers a higher base image resolution (1024x1024) and is more adept at recognizing complex prompts. It also boasts advanced LoRA training and full fine-tuning support. One of SDXL's key advantages is its resistance to overtraining, making it a more stable and reliable model compared to SD15.

For example, here’s an image generated using **Stable Diffusion 1.5.** Hovering over an image in the gallery allows you to distinguish the model used (the SD15 model in these examples has been fine-tuned specifically for photorealism).

<figure><img src="/files/PvgNdwZoXIXIGHpuOnb1" alt=""><figcaption><p><em><strong>Stable Diffusion 1.5 Photorealism</strong></em></p></figcaption></figure>

SDXL is specifically designed to be used with an additional "**refiner**" model to complete the render, and it’s also intended to be used with different artistic styles. For those who wish to experiment with lower output resolutions, you can opt to use just the refiner model:

<figure><img src="/files/8xmK4Wfys2kzqDz9vvbG" alt=""><figcaption><p><em><strong>Stable Diffusion XL with Refiner</strong></em></p></figcaption></figure>

It’s useful to quickly recap these two models before jumping into the demo of using the image-generation tool via GAME-LITE.

Next, the first step is to review the existing API documentation of [getimg.ai](http://getimg.ai) to better grasp the endpoints for **POST** method.

<figure><img src="/files/mpp64gjBUOEUR1oychmx" alt=""><figcaption><p>API Documentation</p></figcaption></figure>

Within the documentation, you'll find the endpoint for text-to-image:

`POST <https://api.getimg.ai/v1/essential-v2/text-to-image`>.

This endpoint utilizes the getimg.ai Essential V2 pipeline to generate high-quality images based on a text prompt and a predefined style.

{% hint style="info" %}
**Note:** Essential V2 Endpoint (`/v1/essential-v2/text-to-image`) differs from the legacy version. Please check that the submitted parameters match the ones described in the documentation.
{% endhint %}

As for the arguments, we can see that they fall under the **'body params'** context, where we can identify which parameters are essential for our custom functions in GAME-LITE.

## **Custom Functions in Practice with** [**getimg.ai**](http://getimg.ai/)

#### **STEP 1: Input Necessary Argument**

In the previous [article](https://whitepaper.virtuals.io/developer-documents/game-framework/content-hub/articles/game-lite-custom-functions-retrieving-articles-using-dev.to) , we’ve tackle how to ensure your custom functions is named properly and the arguments is aligned from the documentation of your API you’re using.

<figure><img src="/files/B2vhRGNRFNEJz22CaOTX" alt=""><figcaption><p>Create Custom Function</p></figcaption></figure>

As for the '**arguments**', we are referring to this section from the [documentation](https://docs.getimg.ai/reference/introduction), where the necessary parameters for generating the image are outlined.

These arguments include the `prompt`, `style`, `width`, `height`, `output_format`, and `response_format`, all of which are required to customize the image generation process before passing the resulting image URL to our `post_tweet` function for posting

<figure><img src="/files/AB93xFGqRlECOrZnVymy" alt=""><figcaption><p>Arguments</p></figcaption></figure>

Now that we’re able to add the first part, let’s populate then the **“Headers”** and **“Payload”**

<figure><img src="/files/8H34LyWLGE8kw6gnyKEw" alt=""><figcaption><p>API URL</p></figcaption></figure>

In this case, we can verify that the necessary arguments are set. As for the API URL, we will refer to the documentation of getimg.ai and use it as the basis for the POST method

Great! I hope you're able to follow these steps before we move on to the exciting part, which is ensuring that your payload and headers are properly aligned

#### **STEP 2: Proper Payload and Header Formatting**

In this step, we will tackle best practices on how to set this properly your payload and header using the [getimg.ai](http://getimg.ai).

{% hint style="info" %}
You need to define the arguments to ensure they align with your payloads.
{% endhint %}

#### **Payload**

```python
{
    "prompt": "a nerd programmer girl playing with a cat",
    "width": 1024,
    "height": 1024,
    "output_format": "jpeg",
    "response_format": "url",
    "style": "photorealism"
}
```

#### **Headers**

```python
{
  "Authorization": "Bearer <insert key>",
  "Content-Type": "application/json"
}
```

Make sure to obtain your API key and include it in the `Authorization` field of the headers.

**Success and Error Feedback**

We will then complete this step by adding the success and error feedback.

<figure><img src="/files/vfuS0vN7VX07Z8YB07vl" alt=""><figcaption><p>Success and Error Feedback</p></figcaption></figure>

Amazing! We’ve successfully completed this step with all the necessary details.

#### **STEP 3:** Simulate Output and Analyse Simulation Output

Time to observe how the Agent responds to the custom function you’ve implemented! Start by checking the functions you would like to simulate output with.

<figure><img src="/files/7J4X2WbqSzeGZXCLID61" alt=""><figcaption><p>Custom Functions</p></figcaption></figure>

From there, click on the simulation and check if it returns the expected output.

<figure><img src="/files/kFhrzOHFcVAeIuxMSyGC" alt=""><figcaption><p>Custom-Function-Request</p></figcaption></figure>

Now that we can see the custom-function-request and custom-function-response, we can verify if an image was successfully generated

<figure><img src="/files/Yvc1syHt3c6iEhcs3oRe" alt=""><figcaption><p>Custom-Function-Response</p></figcaption></figure>

If it’s working, you’ll notice the cost per image call and the URL of the image displayed in the response.

<figure><img src="/files/wipwZvHXlAtnbXAHlm5x" alt=""><figcaption><p>Generated Image</p></figcaption></figure>

#### **STEP 4: Analyze the Simulated Behavior of the Agent**

Now, let’s analyze the behavior of the Agent when utilizing the custom function we created with [getimg.ai](http://getimg.ai).

<figure><img src="/files/gYyUc1Sa2cOaOhPaMilt" alt=""><figcaption><p>Feedback Request</p></figcaption></figure>

To validate if the custom function is working, check **feedback-request** in the simulation output.

<figure><img src="/files/MzPteweQjg1upws2DpFS" alt=""><figcaption><p>Current Action </p></figcaption></figure>

Under current\_action, where you can see the status, whether it’s a success or failure. We can also check the **feedback-response** for further validation and analysis of your Agent’s behavior.

<figure><img src="/files/yvW9XkTLFu5fJcuZASmF" alt=""><figcaption><p>Simulation Logs</p></figcaption></figure>

Here, you'll be able to view the actual logs showing how the custom-function processes the image generation, including the caption for the tweet and the post\_tweet function.

```python
current_task": {
        "task_id": "0f22aa69-4f3f-44ad-98aa-536b82c97280",
        "task": "Use getimg.ai to generate coding-related memes and images",
        "location_id": "twitter_main_location",
        "task_reasoning": "My next immediate task should be to generate content using getimg.ai. This will allow me to create visually engaging images and GIFs that highlight programming concepts, industry insights, and humorous coding moments.",
        "llp": {
          "plan_id": "f472dc2d-39cc-404a-947f-f84ece5286b0",
          "plan_reasoning": "Next step is to post the generated image on Twitter",
          "situation_analysis": "",
          "plan": [
            "Generate image using getimg.ai",
            "Post tweet with generated image"
          ],
          "reflection": "Generated coding meme image with specified dimensions",
          "change_indicator": "next_step"
```

***

## Conclusion

We have successfully created and utilized the custom function for text-to-image generation using [getimg.ai](http://getimg.ai). By setting up the payload and headers, we generated images based on specific text prompts and validated the function's behavior through simulations.

This process ensured that the generated image aligned with expectations and could be accessed via a URL, which was then passed to the `post_tweet` function for sharing on Twitter.

This integration demonstrates the potential of enhancing an Agent’s capabilities by automating content generation and social media posting.

Incorporating text-to-image generation into workflows, we can provide more dynamic, engaging experiences for users and simplify content creation tasks, ultimately improving automation and interactivity in a variety of applications.

***

### Stay Connected and Join the Virtuals Community! 🤖 🎈

<table data-view="cards"><thead><tr><th></th><th data-type="content-ref"></th><th></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><mark style="background-color:blue;"><strong>X: @GAME_Virtuals</strong></mark></td><td><a href="https://x.com/GAME_Virtuals">https://x.com/GAME_Virtuals</a></td><td>For updates and to join our live streaming jam sessions every Wednesday. Stay in the loop and engage with us in real time!</td><td><a href="/files/SKftUrknnGwpVwnfOt2R">/files/SKftUrknnGwpVwnfOt2R</a></td></tr><tr><td><mark style="background-color:blue;"><strong>Discord: @Virtuals Protocol</strong></mark></td><td><a href="http://discord.gg/virtualsio">http://discord.gg/virtualsio</a></td><td>Join Discord for tech support and troubleshooting, and don’t miss our GAME Jam session every Wednesday!</td><td><a href="/files/7cncpQIKabUdDG4afQkp">/files/7cncpQIKabUdDG4afQkp</a></td></tr><tr><td><mark style="background-color:blue;"><strong>Telegram: @Virtuals Protocol</strong></mark></td><td><a href="https://t.me/virtuals">https://t.me/virtuals</a></td><td>Join our Telegram group for non-tech support! Whether you need advice, a chat, or just a friendly space, we’re here to help!</td><td><a href="/files/kuTzXgQ3hnHheSx8QSau">/files/kuTzXgQ3hnHheSx8QSau</a></td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.game.virtuals.io/how-to/articles/multimodal-custom-function-integrating-text-to-image-generation-in-your-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
