()
| 29 | |
| 30 | |
| 31 | async def main(): |
| 32 | agent = Agent( |
| 33 | name="Image generator", |
| 34 | instructions="Always use the image generation tool when the user asks for a new image.", |
| 35 | tools=[ |
| 36 | ImageGenerationTool( |
| 37 | tool_config={"type": "image_generation", "quality": "low"}, |
| 38 | ) |
| 39 | ], |
| 40 | ) |
| 41 | |
| 42 | with trace("Image generation example"): |
| 43 | print("Generating image, this may take a while...") |
| 44 | result = await Runner.run( |
| 45 | agent, "Create an image of a frog eating a pizza, comic book style." |
| 46 | ) |
| 47 | print(result.final_output) |
| 48 | generated_image = False |
| 49 | for item in result.new_items: |
| 50 | if item.type != "tool_call_item": |
| 51 | continue |
| 52 | |
| 53 | raw_call = item.raw_item |
| 54 | call_type = _get_field(raw_call, "type") |
| 55 | if call_type != "image_generation_call": |
| 56 | continue |
| 57 | |
| 58 | img_result = _get_field(raw_call, "result") |
| 59 | if not isinstance(img_result, str): |
| 60 | continue |
| 61 | |
| 62 | generated_image = True |
| 63 | with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp: |
| 64 | tmp.write(base64.b64decode(img_result)) |
| 65 | temp_path = tmp.name |
| 66 | |
| 67 | print(f"Saved generated image to: {temp_path}") |
| 68 | if is_auto_mode(): |
| 69 | print("Auto mode leaves the image on disk instead of opening it.") |
| 70 | else: |
| 71 | open_file(temp_path) |
| 72 | |
| 73 | if not generated_image: |
| 74 | print("No image_generation_call item was returned.") |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
no test coverage detected