-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
43 lines (34 loc) · 1.3 KB
/
Copy pathchat.py
File metadata and controls
43 lines (34 loc) · 1.3 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
from agent import Agent
from models import History
from utils import comment_log
class Chat:
"""
Runs a continuous loop, receiving INPUT from a user or process,
passing it to the agent, and returning the agents response as OUTPUT.
"""
def __init__(self, agent: Agent):
self.agent = agent
self.history: History = []
def run(self):
while True:
comment_log("INPUT:")
user_input = input("👤:")
if user_input.lower() in ("q", "quit", "exit"):
self.agent.finish()
break
self.history.append({"role": "user", "content": user_input})
# Stream to the user and collect it for history in the same loop.
response_parts: list[str] = []
stream = self.agent.respond(self.history, user_input)
first_chunk = next(stream)
comment_log("OUTPUT: Streaming...")
print(f"🤖: {first_chunk}", end="", flush=True)
response_parts.append(first_chunk)
for chunk in stream:
print(chunk, end="", flush=True)
response_parts.append(chunk)
print("")
self.history.append({
"role": "assistant",
"content": "".join(response_parts),
})