Victor Dibia 2cc8c73d3b
Fix termination UI in AGS (#5888)
<!-- Thank you for your contribution! Please review
https://microsoft.github.io/autogen/docs/Contribute before opening a
pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Fix termination UI in AGS, ensure it can be edited correctly

<img width="1269" alt="image"
src="https://github.com/user-attachments/assets/eaa7a92f-a1ea-4ab4-a679-2894ac441311"
/>
<img width="1273" alt="image"
src="https://github.com/user-attachments/assets/6db81068-932f-4d4e-9512-279770c02bf2"
/>
<img width="1270" alt="image"
src="https://github.com/user-attachments/assets/5ca9df7d-b968-46c9-9d62-becd78809273"
/>


<!-- Please give a short summary of the change and the problem this
solves. -->

## Related issue number

<!-- For example: "Closes #1234" -->
Closes #5872
## Checks

- [ ] I've included any doc changes needed for
<https://microsoft.github.io/autogen/>. See
<https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to
build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes
introduced in this PR.
- [ ] I've made sure all auto checks have passed.
2025-03-11 11:46:25 -07:00

68 lines
2.1 KiB
Python

import base64
import io
import uuid
from pathlib import Path
from typing import List, Literal, Optional
from autogen_core.code_executor import ImportFromModule
from autogen_core.tools import FunctionTool
from openai import OpenAI
from PIL import Image
async def generate_image(
query: str, output_dir: Optional[Path] = None, image_size: Literal["1024x1024", "512x512", "256x256"] = "1024x1024"
) -> List[str]:
"""
Generate images using OpenAI's DALL-E model based on a text description.
Args:
query: Natural language description of the desired image
output_dir: Directory to save generated images (default: current directory)
image_size: Size of generated image (1024x1024, 512x512, or 256x256)
Returns:
List[str]: Paths to the generated image files
"""
# Initialize the OpenAI client
client = OpenAI()
# Generate images using DALL-E 3
response = client.images.generate(model="dall-e-3", prompt=query, n=1, response_format="b64_json", size=image_size)
saved_files = []
# Process the response
if response.data:
for image_data in response.data:
# Generate a unique filename
file_name: str = f"{uuid.uuid4()}.png"
# Use output_dir if provided, otherwise use current directory
file_path = Path(output_dir) / file_name if output_dir else Path(file_name)
base64_str = image_data.b64_json
if base64_str:
img = Image.open(io.BytesIO(base64.decodebytes(bytes(base64_str, "utf-8"))))
# Save the image to a file
img.save(file_path)
saved_files.append(str(file_path))
return saved_files
# Create the image generation tool
generate_image_tool = FunctionTool(
func=generate_image,
description="Generate images using DALL-E based on text descriptions.",
global_imports=[
"io",
"uuid",
"base64",
ImportFromModule("typing", ("List", "Optional", "Literal")),
ImportFromModule("pathlib", ("Path",)),
ImportFromModule("openai", ("OpenAI",)),
ImportFromModule("PIL", ("Image",)),
],
)