-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (55 loc) · 2.22 KB
/
Copy pathmain.py
File metadata and controls
69 lines (55 loc) · 2.22 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
import os
import sys
import uvicorn
import importlib.util
from fastapi import FastAPI
# Suppress tracing warnings during development
os.environ.setdefault("OTEL_SDK_DISABLED", "true")
from google.adk.cli.fast_api import get_fast_api_app
# Get the directory where main.py is located
APP_DIR = os.path.dirname(os.path.abspath(__file__))
# Add parent directory to sys.path if it's not there already
# This fixes the module import issue
parent_dir = os.path.abspath(os.path.join(APP_DIR, os.pardir))
if parent_dir not in sys.path:
sys.path.insert(0, parent_dir)
# Fix for 'ai-agent' module import
# Needed because the framework expects the code to be importable
# under the app's name.
# TODO(adk): Make this automatic.
import types
# Only register the module if it hasn't been registered already
if 'ai-agent' not in sys.modules:
# Create a proper module for 'ai-agent'
ai_agent_module = types.ModuleType('ai-agent')
sys.modules['ai-agent'] = ai_agent_module
# Import the actual agent code
import agent
# Make it available as 'ai-agent.agent'
ai_agent_module.agent = agent
# Expose the root agent directly if it exists
if hasattr(agent, 'root_agent'):
ai_agent_module.root_agent = agent.root_agent
print(f"Registered 'ai-agent' and 'ai-agent.agent' in sys.modules")
else:
# Module already registered, just import agent
import agent
# Example session DB URL (e.g., SQLite)
SESSION_DB_URL = "sqlite:///./sessions.db"
# Example allowed origins for CORS
ALLOWED_ORIGINS = ["http://localhost", "http://localhost:8080", "*"]
# Call the function to get the FastAPI app instance
# The function requires 'agents_dir' and 'web' as mandatory arguments
app: FastAPI = get_fast_api_app(
agents_dir=APP_DIR, # Directory containing the agent code
web=True, # Enable web interface
session_db_url=SESSION_DB_URL,
allow_origins=ALLOWED_ORIGINS,
)
if __name__ == "__main__":
# Use port 8080 by default, or override with PORT env var
port = int(os.environ.get("PORT", 8080))
print(f"Starting agent server on port {port}...")
# Run the server using uvicorn
# Use import string format to enable reload functionality
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)