multi-round chat

This commit is contained in:
Gaetan Hurel 2025-06-26 15:04:54 +02:00
parent 331e2e434d
commit c20be407d5
No known key found for this signature in database

View File

@ -9,60 +9,63 @@ from utils import print_step_info, explain_supervisor_pattern
if __name__ == "__main__": if __name__ == "__main__":
# Create the supervisor # Create the supervisor
supervisor = create_sysadmin_supervisor() supervisor = create_sysadmin_supervisor()
# Example run - demonstrating both invoke and streaming with debug output # Interactive conversation loop
query = { messages = []
"messages": [ print("Welcome to the multi-agent sysadmin assistant!")
{ print("Type your sysadmin question below. Type 'exit' to quit.")
"role": "user", while True:
"content": "Nginx returns 502 Bad Gateway on my server. What can I do?", user_input = input("\n📝 User: ")
} if user_input.strip().lower() == 'exit':
] print("Goodbye!")
} break
messages.append({"role": "user", "content": user_input})
print("🚀 Starting multi-agent sysadmin analysis...") query = {"messages": messages}
print(f"📝 User Query: {query['messages'][0]['content']}")
print("=" * 80) print("\n=== Using invoke() method ===")
result = supervisor.invoke(query)
# Show explanation of the supervisor pattern
explain_supervisor_pattern() print("\n📊 FINAL RESULT:")
print("-" * 40)
print("\n=== Using invoke() method ===") print(result["messages"][-1].content)
result = supervisor.invoke(query) print("-" * 40)
print(f"\n📈 Total messages exchanged: {len(result['messages'])}")
print("\n📊 FINAL RESULT:")
print("-" * 40) # Add the assistant's reply to the conversation history
print(result["messages"][-1].content) messages.append({"role": "assistant", "content": result["messages"][-1].content})
print("-" * 40)
# Ask if the user wants to continue
print(f"\n📈 Total messages exchanged: {len(result['messages'])}") cont = input("\nWould you like to continue the conversation? (y/n): ")
if cont.strip().lower() not in ('y', 'yes'):
print("\n=== Using stream() method for detailed step-by-step analysis ===") print("Session ended.")
step_count = 0 break
max_steps = 20 # Prevent infinite loops
print("\n=== Using stream() method for detailed step-by-step analysis ===")
try: step_count = 0
chunks_processed = [] max_steps = 20 # Prevent infinite loops
for chunk in supervisor.stream(query):
step_count += 1 try:
chunks_processed.append(chunk) chunks_processed = []
print_step_info(step_count, chunk) for chunk in supervisor.stream(query):
step_count += 1
# Safety check to prevent infinite loops chunks_processed.append(chunk)
if step_count >= max_steps: print_step_info(step_count, chunk)
print(f"\n⚠️ Reached maximum steps ({max_steps}), stopping stream...")
break # Safety check to prevent infinite loops
if step_count >= max_steps:
print(f"\n✅ Streaming completed successfully with {step_count} steps") print(f"\n⚠️ Reached maximum steps ({max_steps}), stopping stream...")
print(f"📊 Total chunks processed: {len(chunks_processed)}") break
# Check if the last chunk contains a complete final response print(f"\n✅ Streaming completed successfully with {step_count} steps")
if chunks_processed: print(f"📊 Total chunks processed: {len(chunks_processed)}")
last_chunk = chunks_processed[-1]
print(f"🔍 Last chunk keys: {list(last_chunk.keys()) if isinstance(last_chunk, dict) else type(last_chunk)}") # Check if the last chunk contains a complete final response
if chunks_processed:
except Exception as e: last_chunk = chunks_processed[-1]
print(f"\n❌ Streaming error after {step_count} steps: {e}") print(f"🔍 Last chunk keys: {list(last_chunk.keys()) if isinstance(last_chunk, dict) else type(last_chunk)}")
print("💡 The invoke() method worked fine, so the supervisor itself is functional.")
import traceback except Exception as e:
traceback.print_exc() print(f"\n❌ Streaming error after {step_count} steps: {e}")
print("💡 The invoke() method worked fine, so the supervisor itself is functional.")
import traceback
traceback.print_exc()