{ "type": "object", "properties": { "prompt": { "description": "Detailed description of the desired content of the generated image. Please keep the specific requirements such as text from the original request fully intact. Omission is prohibited.", "type": "string" } }, "required": ["prompt"]}
from qwen_agent.tools import ImageGenimport json# Initialize with LLM configimage_gen = ImageGen(cfg={ 'llm_cfg': { 'model': 'qwen-vl-plus', 'api_key': 'your-api-key', 'model_server': 'dashscope' }, 'size': '1024*1024'})# Generate an imageresult = image_gen.call( params=json.dumps({ 'prompt': 'A serene landscape with mountains and a lake at sunset, photorealistic style' }))print(result)# Returns: List of ContentItem objects with image data
from qwen_agent.agents import Assistantbot = Assistant( llm={'model': 'qwen-max'}, function_list=['image_gen'], # Image generation tool config passed separately)# Note: Configure the image_gen tool before adding to agentfrom qwen_agent.tools import ImageGenimage_tool = ImageGen(cfg={ 'llm_cfg': { 'model': 'qwen-vl-plus', 'api_key': 'your-api-key' }})messages = [ { 'role': 'user', 'content': 'Create an image of a cute dog playing in a park' }]for response in bot.run(messages=messages): print(response)
from qwen_agent.llm.schema import ContentItem# Example return value[ ContentItem( image='https://example.com/generated-image.png', # or base64 data # Additional metadata may be included )]
Each ContentItem contains:
image: URL or base64-encoded image data
May include additional metadata depending on the LLM
from qwen_agent.agents import Assistantfrom qwen_agent.tools import ImageGenfrom qwen_agent.gui import WebUIdef create_image_generator(): """Create an agent specialized in image generation.""" # Configure image generation tool llm_cfg = { 'model': 'qwen-vl-plus', 'api_key': 'your-api-key', 'model_server': 'dashscope' } bot = Assistant( llm={'model': 'qwen-max'}, name='AI Artist', description='AI image generation service', system_message=( 'You are an AI artist that creates images based on user descriptions. ' 'When users request images, use the image_gen tool to create them. ' 'Ask clarifying questions if the description is too vague.' ), function_list=['image_gen'] ) return bot# Create and use the agentbot = create_image_generator()# Text-based interactionmessages = []while True: user_input = input('Describe the image you want (or "quit"): ') if user_input.lower() in ['quit', 'exit']: break messages.append({'role': 'user', 'content': user_input}) response = [] for response in bot.run(messages=messages): print('Generating...', end='\r') messages.extend(response) print(f"Image generated: {response[-1]['content']}")# Or launch with GUI# WebUI(bot).run()
prompt = ( "A golden retriever puppy sitting in a flower garden, " "surrounded by pink roses and white daisies, " "soft morning sunlight, shallow depth of field, " "photorealistic, 8k quality")
image_gen = ImageGen(cfg={ 'llm_cfg': {'model': 'qwen-vl-plus', 'api_key': 'key'}, 'size': '1024*1024'})prompts = [ 'A red apple on a wooden table', 'A blue ocean wave crashing', 'A green forest in spring']generated_images = []for prompt in prompts: result = image_gen.call(params=json.dumps({'prompt': prompt})) generated_images.append(result) print(f"Generated: {prompt}")
from qwen_agent.agents import Assistantbot = Assistant( llm={'model': 'qwen-max'}, function_list=['image_gen', 'code_interpreter'], system_message=( 'You can generate images and then process them with Python code. ' 'First generate the image, then use code_interpreter to analyze or modify it.' ))messages = [{ 'role': 'user', 'content': 'Generate an image of a data chart and then analyze its colors'}]for response in bot.run(messages=messages): print(response)