-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (113 loc) · 4.21 KB
/
Copy pathmain.py
File metadata and controls
144 lines (113 loc) · 4.21 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import asyncio
import os
import sys
import uuid
import google.generativeai as genai
from google.genai import types
from dotenv import load_dotenv
# Try imports for different ADK versions
try:
from google.adk.runners import InMemoryRunner
except ImportError:
from google.adk.core.runner import Runner as InMemoryRunner
try:
from uninav_agent.agent import uni_navigator_agent
except ImportError as e:
print(f"❌ Agent import failed: {e}")
sys.exit(1)
async def handle_model_event(event, runner, session_id, user_id):
"""
Handles ALL types of streaming responses:
- Text chunks
- Candidate results
- Tool calls
- Final replies after tools
"""
# 1️⃣ Standard text streaming (post-tool)
if hasattr(event, "text") and event.text:
print(event.text, end="", flush=True)
# 2️⃣ Direct content streaming
if hasattr(event, "content") and event.content:
for part in event.content.parts:
if hasattr(part, "stream_text") and part.stream_text:
print(part.stream_text, end="", flush=True)
elif hasattr(part, "text") and part.text:
print(part.text, end="", flush=True)
# 3️⃣ Candidate response + tool call detection
candidates = getattr(event, "candidates", [])
for candidate in candidates:
parts = getattr(candidate.content, "parts", [])
for part in parts:
# Normal text responses
if hasattr(part, "text") and part.text:
print(part.text, end="", flush=True)
# Tool calls when LLM decides to use a function
if hasattr(part, "function_call") and part.function_call:
fn = part.function_call
print(f"\n\n--- 🔧 TOOL CALL: {fn.name} ---")
print(f"Args: {fn.args}")
try:
# Execute tool
tool_result = await runner.tool_service.call(
session_id=session_id,
user_id=user_id,
name=fn.name,
arguments=fn.args,
)
except Exception as e:
print(f"\n❌ Tool failed: {e}")
return
# Send tool result back to model
async for follow_up_event in runner.run_tool_response(
session_id=session_id, user_id=user_id, tool_response=tool_result
):
await handle_model_event(
follow_up_event, runner, session_id, user_id
)
async def main():
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
print("❌ ERROR: GEMINI_API_KEY missing in .env file.")
return
genai.configure(api_key=api_key)
print("\n" + "=" * 60)
print("🎓 UniNavigation Agent is ONLINE")
print("💬 Type 'exit' to quit")
print("=" * 60 + "\n")
runner = InMemoryRunner(agent=uni_navigator_agent, app_name="agents")
user_id = "user_01"
session_id = str(uuid.uuid4())
# Create a session
try:
await runner.session_service.create_session(
app_name="agents", user_id=user_id, session_id=session_id
)
except Exception:
pass
# Chat loop
while True:
try:
user_input = input("\nStudent: ").strip()
if user_input.lower() in ["exit", "quit"]:
print("\n👋 Goodbye!")
break
if not user_input:
continue
print("🤖 Agent: ", end="", flush=True)
message = types.Content(
role="user", parts=[types.Part.from_text(text=user_input)]
)
# Main streaming call
async for event in runner.run_async(
session_id=session_id, user_id=user_id, new_message=message
):
await handle_model_event(event, runner, session_id, user_id)
print("\n" + "-" * 60)
except KeyboardInterrupt:
print("\n⛔ Force Quit")
break
except Exception as e:
print(f"\n❌ Runtime Error: {e}\n")
if __name__ == "__main__":
asyncio.run(main())