| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  | import chainlit as cl | 
					
						
							|  |  |  | from autogen_agentchat.agents import AssistantAgent | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  | from autogen_agentchat.base import TaskResult | 
					
						
							|  |  |  | from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination | 
					
						
							| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  | from autogen_agentchat.teams import RoundRobinGroupChat | 
					
						
							|  |  |  | from autogen_ext.models.openai import OpenAIChatCompletionClient | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async def get_weather(city: str) -> str: | 
					
						
							|  |  |  |     return f"The weather in {city} is 73 degrees and Sunny." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  | @cl.on_chat_start  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  | async def start_chat(): | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  |     cl.user_session.set("prompt_history", "")  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async def run_team(query: str): | 
					
						
							|  |  |  |     assistant_agent = AssistantAgent( | 
					
						
							|  |  |  |         name="assistant_agent", tools=[get_weather], model_client=OpenAIChatCompletionClient(model="gpt-4o-2024-08-06") | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10) | 
					
						
							|  |  |  |     team = RoundRobinGroupChat(participants=[assistant_agent], termination_condition=termination) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response_stream = team.run_stream(task=query) | 
					
						
							|  |  |  |     async for msg in response_stream: | 
					
						
							|  |  |  |         if hasattr(msg, "content"): | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  |             cl_msg = cl.Message(content=msg.content, author="Agent Team")  # type: ignore | 
					
						
							|  |  |  |             await cl_msg.send() | 
					
						
							| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  |         if isinstance(msg, TaskResult): | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  |             cl_msg = cl.Message(content="Termination condition met. Team and Agents are reset.", author="Agent Team") | 
					
						
							|  |  |  |             await cl_msg.send() | 
					
						
							| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  | @cl.on_message  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-12-18 14:49:07 -08:00
										 |  |  | async def chat(message: cl.Message): | 
					
						
							| 
									
										
										
										
											2025-01-07 15:31:29 -08:00
										 |  |  |     await run_team(message.content)  # type: ignore |