| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  | import argparse | 
					
						
							|  |  |  | import asyncio | 
					
						
							|  |  |  | import logging | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import openai | 
					
						
							|  |  |  | from agnext.application import SingleThreadedAgentRuntime | 
					
						
							| 
									
										
										
										
											2024-06-28 23:15:46 -07:00
										 |  |  | from agnext.components.models import SystemMessage | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  | from agnext.core import AgentRuntime | 
					
						
							| 
									
										
										
										
											2024-06-21 04:06:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | sys.path.append(os.path.abspath(os.path.dirname(__file__))) | 
					
						
							| 
									
										
										
										
											2024-06-25 13:23:29 -07:00
										 |  |  | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | 
					
						
							| 
									
										
										
										
											2024-06-21 04:06:01 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 13:23:29 -07:00
										 |  |  | from common.agents import ChatCompletionAgent, ImageGenerationAgent | 
					
						
							|  |  |  | from common.memory import BufferedChatMemory | 
					
						
							|  |  |  | from common.patterns._group_chat_manager import GroupChatManager | 
					
						
							| 
									
										
										
										
											2024-06-28 23:15:46 -07:00
										 |  |  | from common.utils import get_chat_completion_client_from_envs | 
					
						
							| 
									
										
										
										
											2024-07-01 11:53:45 -04:00
										 |  |  | from utils import TextualChatApp, TextualUserAgent | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  | async def illustrator_critics(runtime: AgentRuntime, app: TextualChatApp) -> None: | 
					
						
							|  |  |  |     await runtime.register( | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         "User", | 
					
						
							|  |  |  |         lambda: TextualUserAgent( | 
					
						
							|  |  |  |             description="A user looking for illustration.", | 
					
						
							|  |  |  |             app=app, | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  |     descriptor = await runtime.register_and_get_proxy( | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         "Descriptor", | 
					
						
							|  |  |  |         lambda: ChatCompletionAgent( | 
					
						
							|  |  |  |             description="An AI agent that provides a description of the image.", | 
					
						
							|  |  |  |             system_messages=[ | 
					
						
							|  |  |  |                 SystemMessage( | 
					
						
							|  |  |  |                     "You create short description for image. \n" | 
					
						
							|  |  |  |                     "In this conversation, you will be given either: \n" | 
					
						
							|  |  |  |                     "1. Request for new image. \n" | 
					
						
							|  |  |  |                     "2. Feedback on some image created. \n" | 
					
						
							|  |  |  |                     "In both cases, you will provide a description of a new image to be created. \n" | 
					
						
							|  |  |  |                     "Only provide the description of the new image and nothing else. \n" | 
					
						
							|  |  |  |                     "Be succinct and precise." | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             memory=BufferedChatMemory(buffer_size=10), | 
					
						
							| 
									
										
										
										
											2024-06-28 23:15:46 -07:00
										 |  |  |             model_client=get_chat_completion_client_from_envs(model="gpt-4-turbo", max_tokens=500), | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  |     illustrator = await runtime.register_and_get_proxy( | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         "Illustrator", | 
					
						
							|  |  |  |         lambda: ImageGenerationAgent( | 
					
						
							|  |  |  |             description="An AI agent that generates images.", | 
					
						
							|  |  |  |             client=openai.AsyncOpenAI(), | 
					
						
							|  |  |  |             model="dall-e-3", | 
					
						
							|  |  |  |             memory=BufferedChatMemory(buffer_size=1), | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  |     critic = await runtime.register_and_get_proxy( | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         "Critic", | 
					
						
							|  |  |  |         lambda: ChatCompletionAgent( | 
					
						
							|  |  |  |             description="An AI agent that provides feedback on images given user's requirements.", | 
					
						
							|  |  |  |             system_messages=[ | 
					
						
							|  |  |  |                 SystemMessage( | 
					
						
							|  |  |  |                     "You are an expert in image understanding. \n" | 
					
						
							|  |  |  |                     "In this conversation, you will judge an image given the description and provide feedback. \n" | 
					
						
							|  |  |  |                     "Pay attention to the details like the spelling of words and number of objects. \n" | 
					
						
							|  |  |  |                     "Use the following format in your response: \n" | 
					
						
							|  |  |  |                     "Number of each object type in the image: <Type 1 (e.g., Husky Dog)>: 1, <Type 2>: 2, ...\n" | 
					
						
							|  |  |  |                     "Feedback: <Your feedback here> \n" | 
					
						
							|  |  |  |                     "Approval: <APPROVE or REVISE> \n" | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             memory=BufferedChatMemory(buffer_size=2), | 
					
						
							| 
									
										
										
										
											2024-06-28 23:15:46 -07:00
										 |  |  |             model_client=get_chat_completion_client_from_envs(model="gpt-4-turbo"), | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  |     await runtime.register( | 
					
						
							| 
									
										
										
										
											2024-06-18 14:53:18 -04:00
										 |  |  |         "GroupChatManager", | 
					
						
							|  |  |  |         lambda: GroupChatManager( | 
					
						
							|  |  |  |             description="A chat manager that handles group chat.", | 
					
						
							|  |  |  |             memory=BufferedChatMemory(buffer_size=5), | 
					
						
							|  |  |  |             participants=[illustrator.id, critic.id, descriptor.id], | 
					
						
							|  |  |  |             termination_word="APPROVE", | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.welcoming_notice = f"""You are now in a group chat with the following agents:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  | 1. 🤖 {(await descriptor.metadata)['name']}: {(await descriptor.metadata).get('description')} | 
					
						
							|  |  |  | 2. 🤖 {(await illustrator.metadata)['name']}: {(await illustrator.metadata).get('description')} | 
					
						
							|  |  |  | 3. 🤖 {(await critic.metadata)['name']}: {(await critic.metadata).get('description')} | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | Provide a prompt for the illustrator to generate an image. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async def main() -> None: | 
					
						
							|  |  |  |     runtime = SingleThreadedAgentRuntime() | 
					
						
							|  |  |  |     app = TextualChatApp(runtime, user_name="You") | 
					
						
							| 
									
										
										
										
											2024-07-23 11:49:38 -07:00
										 |  |  |     await illustrator_critics(runtime, app) | 
					
						
							| 
									
										
										
										
											2024-07-01 11:53:45 -04:00
										 |  |  |     _run_context = runtime.start() | 
					
						
							| 
									
										
										
										
											2024-06-17 17:20:46 -07:00
										 |  |  |     await app.run_async() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser(description="Illustrator-critics pattern for image generation demo.") | 
					
						
							|  |  |  |     parser.add_argument("--verbose", action="store_true", help="Enable verbose logging.") | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  |     if args.verbose: | 
					
						
							|  |  |  |         logging.basicConfig(level=logging.WARNING) | 
					
						
							|  |  |  |         logging.getLogger("agnext").setLevel(logging.DEBUG) | 
					
						
							|  |  |  |         handler = logging.FileHandler("illustrator_critics.log") | 
					
						
							|  |  |  |         logging.getLogger("agnext").addHandler(handler) | 
					
						
							|  |  |  |     asyncio.run(main()) |