-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_runner.py
More file actions
64 lines (48 loc) · 1.98 KB
/
agent_runner.py
File metadata and controls
64 lines (48 loc) · 1.98 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
from health_bot import HealthBotSession, UserInputRequest
class HealthBotRunner:
def __init__(self):
self.session = None
def start_conversation(self, initial_question: str):
"""Start a new conversation with the given question"""
self.session = HealthBotSession(initial_question)
self.run_conversation()
def run_conversation(self):
"""Run the conversation loop using the generator interface"""
if not self.session:
return
# Start the conversation generator
conversation = self.session.run_conversation()
response = None
try:
# Get the first response
response = next(conversation)
while True:
if isinstance(response, str):
# AI message - display it
print(response)
# Get next response
response = next(conversation)
elif isinstance(response, UserInputRequest):
# Bot wants input - get it and send back
user_input = self._get_user_input(response)
# Send user input back to generator and get next response
response = conversation.send(user_input)
except StopIteration:
# Conversation ended
print("\nConversation ended.")
def _get_user_input(self, request: UserInputRequest) -> str:
"""Get user input based on the request"""
print(f"\n{request.prompt}")
if request.options:
options_str = "/".join(request.options)
prompt = f"({options_str}): "
else:
prompt = "> "
return input(prompt).strip()
# Add this to run the bot
if __name__ == "__main__":
runner = HealthBotRunner()
# Get initial question from user
initial_question = input("What health topic would you like to research? ")
# Start the conversation
runner.start_conversation(initial_question)