-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path5_human_in_the_loop.py
64 lines (53 loc) · 1.9 KB
/
5_human_in_the_loop.py
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 dotenv import load_dotenv
load_dotenv()
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.core.workflow import Context
from llama_index.core.workflow import (
InputRequiredEvent,
HumanResponseEvent,
)
llm = OpenAI(model="gpt-4o-mini")
# a tool that performs a dangerous task
async def dangerous_task(ctx: Context) -> str:
"""A dangerous task that requires human confirmation."""
# emit an event to the external stream to be captured
ctx.write_event_to_stream(
InputRequiredEvent(
prefix="Are you sure you want to proceed? ",
user_name="Laurie",
)
)
# wait until we see a HumanResponseEvent
response = await ctx.wait_for_event(
HumanResponseEvent, requirements={"user_name": "Laurie"}
)
# act on the input from the event
if response.response.strip().lower() == "yes":
return "Dangerous task completed successfully."
else:
return "Dangerous task aborted."
workflow = AgentWorkflow.from_tools_or_functions(
[dangerous_task],
llm=llm,
system_prompt="You are a helpful assistant that can perform dangerous tasks.",
)
async def main():
handler = workflow.run(user_msg="I want to proceed with the dangerous task.")
async for event in handler.stream_events():
# capture InputRequiredEvent
if isinstance(event, InputRequiredEvent):
# capture keyboard input
response = input(event.prefix)
# send our response back
handler.ctx.send_event(
HumanResponseEvent(
response=response,
user_name=event.user_name,
)
)
response = await handler
print(str(response))
if __name__ == "__main__":
import asyncio
asyncio.run(main())