server.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. FastAPI Server Example for Agency Swarm v1.x
  3. This example demonstrates how to serve agencies via FastAPI with proper
  4. streaming support, showing agent and callerAgent fields in responses.
  5. To run:
  6. 1. Set your OPENAI_API_KEY environment variable
  7. 2. Run: python server.py
  8. 3. Test with the client.py script or via curl/Postman
  9. """
  10. import os
  11. import sys
  12. # Path setup for standalone examples
  13. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "src")))
  14. from agents import function_tool
  15. from agency_swarm import Agency, Agent, run_fastapi
  16. # --- Simple Tools --- #
  17. @function_tool
  18. def ExampleTool(name: str, greeting_type: str = "Hello") -> str:
  19. """A tool that provides a simple greeting message with customization options.
  20. This tool can be used to generate personalized greetings for users."""
  21. return f"{greeting_type}, {name}!"
  22. # --- Agent Setup --- #
  23. def create_agency(load_threads_callback=None):
  24. """Create a demo agency with two agents for testing communication flows."""
  25. # First agent - receives user requests
  26. agent = Agent(
  27. name="ExampleAgent",
  28. description="Primary agent that handles user requests",
  29. instructions="""You are the primary agent. When asked to call the second agent:
  30. 1. Use the send_message tool to communicate with ExampleAgent2
  31. 2. Have ExampleAgent2 use the ExampleTool
  32. 3. Return the result to the user""",
  33. tools=[],
  34. )
  35. # Second agent - performs tasks
  36. agent2 = Agent(
  37. name="ExampleAgent2",
  38. description=(
  39. "A helpful and knowledgeable assistant that provides "
  40. "comprehensive support and guidance across various domains."
  41. ),
  42. instructions="You are a helpful assistant. Use the ExampleTool when asked to greet someone.",
  43. tools=[ExampleTool],
  44. )
  45. # Create agency with communication flow
  46. agency = Agency(
  47. agent,
  48. agent2,
  49. communication_flows=[agent > agent2],
  50. shared_instructions="Be helpful and demonstrate inter-agent communication.",
  51. load_threads_callback=load_threads_callback,
  52. )
  53. return agency
  54. # --- Main --- #
  55. if __name__ == "__main__":
  56. print("🚀 Starting FastAPI server for Agency Swarm")
  57. print("=" * 50)
  58. print("📍 Server will run at: http://localhost:8080")
  59. print("📝 Available endpoints:")
  60. print(" - POST /my-agency/get_response")
  61. print(" - POST /my-agency/get_response_stream (SSE)")
  62. print(" - GET /my-agency/get_metadata")
  63. print("=" * 50)
  64. # Run the FastAPI server
  65. run_fastapi(
  66. agencies={
  67. "my-agency": create_agency,
  68. },
  69. port=8080,
  70. app_token_env="APP_TOKEN", # Optional: Set APP_TOKEN env var for authentication
  71. )