-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneo_server.py
More file actions
104 lines (85 loc) · 3.15 KB
/
neo_server.py
File metadata and controls
104 lines (85 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import uvicorn
from typing import Dict, Any
from fastapi import FastAPI
from google.adk.a2a.utils.agent_to_a2a import to_a2a
from google.adk.agents import LlmAgent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.adk.memory import InMemoryMemoryService
from observability.logger import setup_logging
from config.config import initialize_config, FullConfig
from observability.observability import SmartObserverPlugin
from agents import create_neo_from_config
from config.state_keys import (
STATE_RELEVANT_KNOWLEDGE,
STATE_PERSUASION_COMPLETE,
STATE_EXIT_REASON,
STATE_NEO_FINAL_STATE
)
def create_app() -> FastAPI:
"""
Create and configure the FastAPI application for Neo's agent server.
This function:
1. Sets up logging
2. Loads configuration from config files
3. Creates Neo's agent team with all sub-agents
4. Initializes session and memory services
5. Creates a Runner with observability plugins
6. Converts the agent to an A2A (Agent-to-Agent) FastAPI application
Returns:
FastAPI: Configured FastAPI application ready to serve Neo's agent
via the Agent-to-Agent protocol.
Raises:
ValueError: If configuration is invalid or missing required fields
RuntimeError: If agent creation fails
"""
setup_logging()
config: FullConfig = initialize_config()
os.environ["GOOGLE_API_KEY"] = config.google_api_key
# Create Neo's team from config
neo_agent: LlmAgent = create_neo_from_config(config)
neo_session_service: InMemorySessionService = InMemorySessionService()
# Define initial state for the session
initial_state: Dict[str, Any] = {
STATE_RELEVANT_KNOWLEDGE: [],
STATE_PERSUASION_COMPLETE: False,
STATE_EXIT_REASON: None,
STATE_NEO_FINAL_STATE: None,
}
# Create session with initial state
neo_session_service.create_session_sync(
app_name=config.simulation.app_name,
user_id=config.neo_team.lead_name,
session_id=config.simulation.session_id,
state=initial_state
)
# Create runner with observability
neo_runner: Runner = Runner(
agent=neo_agent,
app_name=config.simulation.app_name,
session_service=neo_session_service,
memory_service=InMemoryMemoryService(),
plugins=[SmartObserverPlugin(team_name="neo_team", color="cyan")]
)
# Create FastAPI app using Agent-to-Agent conversion
app: FastAPI = to_a2a(
neo_agent,
port=config.network.doer_port,
runner=neo_runner
)
return app
# Expose 'app' so command line uvicorn can find it
app: FastAPI = create_app()
if __name__ == "__main__":
# Get config to know which port to use
config: FullConfig = initialize_config()
print(f"🌐 Starting Neo Server on port {config.network.doer_port}")
print(f"📡 Host: {config.network.host}")
print(f"🤖 Agent: {config.neo_team.lead_name}")
print(f"=" * 60)
uvicorn.run(
app,
host=config.network.host,
port=config.network.doer_port
)