Skip to content

Commit

Permalink
Restore human in the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin committed Jun 3, 2024
1 parent 27604fd commit 70d09e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/controlflow/llm/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,26 @@ def on_tool_result_done(self, message: ToolMessage):


class PrintHandler(CompletionHandler):
def __init__(self):
self.messages: dict[str, ControlFlowMessage] = {}
self.live = Live(auto_refresh=False)

def on_start(self):
self.live = Live(refresh_per_second=12)
self.live.start()
self.messages: dict[str, ControlFlowMessage] = {}

def on_end(self):
self.live.stop()

def on_exception(self, exc: Exception):
self.live.stop()

def update_live(self):
messages = sorted(self.messages.values(), key=lambda m: m.timestamp)
content = []
for message in messages:
content.append(format_message(message))

self.live.update(Group(*content))
self.live.update(Group(*content), refresh=True)

def on_message_delta(self, delta: AssistantMessage, snapshot: AssistantMessage):
self.messages[snapshot.id] = snapshot
Expand Down
5 changes: 3 additions & 2 deletions src/controlflow/tools/talk_to_human.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from prefect.context import FlowRunContext
from prefect.input.run_input import receive_input
from rich.prompt import Prompt

import controlflow
from controlflow.llm.tools import tool
Expand All @@ -16,8 +17,8 @@ async def get_terminal_input():
# as a convenience, we wait for human input on the local terminal
# this is not necessary for the flow to run, but can be useful for testing
loop = asyncio.get_event_loop()
user_input = await loop.run_in_executor(None, input, "Type your response: ")
# user_input = await loop.run_in_executor(None, Prompt.ask, "Type your response")
# user_input = await loop.run_in_executor(None, input, "Type your response: ")
user_input = await loop.run_in_executor(None, Prompt.ask, "Type your response")
return user_input


Expand Down

0 comments on commit 70d09e8

Please sign in to comment.