forked from humanlayer/humanlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-statehooks.py
More file actions
234 lines (173 loc) · 7.61 KB
/
app-statehooks.py
File metadata and controls
234 lines (173 loc) · 7.61 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
import logging
from enum import Enum
from fastapi import BackgroundTasks, FastAPI
from typing import Any, Dict, Literal, Union
import marvin
from pydantic import BaseModel
from humanlayer import AsyncHumanLayer, FunctionCall, HumanContact
from humanlayer.core.models import ContactChannel, EmailContactChannel, HumanContactSpec
from humanlayer.core.models_agent_webhook import EmailMessage, EmailPayload
app = FastAPI(title="HumanLayer FastAPI Email Example", version="1.0.0")
logger = logging.getLogger(__name__)
# Root endpoint
@app.get("/")
async def root() -> Dict[str, str]:
return {
"message": "Welcome to a HumanLayer Email Example",
"instructions": "https://github.com/humanlayer/humanlayer/blob/main/examples/fastapi-email/README.md",
}
##############################
######## Biz Logic ########
##############################
class ClarificationRequest(BaseModel):
intent: Literal["request_more_information"]
message: str
class DraftCampaign(BaseModel):
intent: Literal["ready_to_draft_campaign"]
campaign: "Campaign"
class PublishCampaign(BaseModel):
intent: Literal["human_approved__campaign_ready_to_publish"]
campaign_id: int
async def determine_next_step(
thread: "Thread",
) -> ClarificationRequest | DraftCampaign | PublishCampaign:
"""determine the next step in the email thread"""
response: Union[ClarificationRequest, DraftCampaign, PublishCampaign] = await marvin.cast_async(
json.dumps([event.model_dump(mode="json") for event in thread.events]),
Union[ClarificationRequest, DraftCampaign, PublishCampaign],
instructions="""
determine if you have enough information to create a campaign, or if you need more input.
The campaign should be a list of gift boxes to include in a promotional campaign.
Once a campaign is drafted, a human will automatically review it. If the human approves,
and has not requested any changes, you should publish it.
""",
)
return response
class CampaignItem(BaseModel):
id: int
name: str
description: str
class Campaign(BaseModel):
id: int
url: str
items: list[CampaignItem]
def format_approval_message(self) -> str:
items_str = "\n".join(f"• {item.name} - {item.description}" for item in self.items)
return f"""
The preview campaign is live at {self.url}
The items include:
{items_str}
Do you think this is good to publish?
"""
async def publish_campaign(campaign_id: int) -> None:
"""tool to publish a campaign"""
print(f"Published campaign {campaign_id}")
async def is_approval(message: str) -> bool:
"""check if the human approved the campaign"""
answer: str = await marvin.classify_async(
message,
[
"approved",
"rejected",
"unknown",
],
)
return answer == "approved"
##########################
######## CONTEXT #########
##########################
class EventType(Enum):
EMAIL_RECEIVED = "email_received"
REQUEST_MORE_INFORMATION = "request_more_information"
HUMAN_RESPONSE = "human_response"
CAMPAIGN_DRAFTED = "campaign_drafted"
CAMPAIGN_PUBLISHED = "campaign_published"
class Event(BaseModel):
type: EventType
data: Any # don't really care about this, it will just be context to the LLM
# todo you probably want to version this but for now lets assume we're not going to change the schema
class Thread(BaseModel):
initial_email: EmailPayload
# initial_slack_message: SlackMessage
events: list[Event]
def to_state(self) -> dict:
"""Convert thread to a state dict for preservation"""
return self.model_dump(mode="json")
@classmethod
def from_state(cls, state: dict) -> "Thread":
"""Restore thread from preserved state"""
return cls.model_validate(state)
##########################
######## Handlers ########
##########################
async def handle_continued_thread(thread: Thread) -> None:
humanlayer = AsyncHumanLayer(contact_channel=ContactChannel(email=thread.initial_email.as_channel()))
# maybe: if thread gets too long, summarize parts of it - your call!
# new_thread = maybe_summarize_parts_of_thread(thread)
logger.info(f"thread received, determining next step. Last event: {thread.events[-1].type}")
next_step = await determine_next_step(thread)
logger.info(f"next step: {next_step.intent}")
if next_step.intent == "request_more_information":
logger.info(f"requesting more information: {next_step.message}")
thread.events.append(Event(type=EventType.REQUEST_MORE_INFORMATION, data=next_step.message))
await humanlayer.create_human_contact(spec=HumanContactSpec(msg=next_step.message, state=thread.to_state()))
elif next_step.intent == "ready_to_draft_campaign":
campaign_info = next_step.campaign
logger.info(f"drafted campaign_info: {campaign_info.model_dump_json()}")
thread.events.append(Event(type=EventType.CAMPAIGN_DRAFTED, data=campaign_info))
await humanlayer.create_human_contact(
spec=HumanContactSpec(msg=campaign_info.format_approval_message(), state=thread.to_state())
)
elif next_step.intent == "human_approved__campaign_ready_to_publish":
campaign_id = next_step.campaign_id
logger.info(f"drafted campaign_info: {campaign_id}")
await publish_campaign(campaign_id)
thread.events.append(Event(type=EventType.CAMPAIGN_PUBLISHED, data=campaign_id))
await humanlayer.create_human_contact(
spec=HumanContactSpec(
msg="Approved and published campaign. Let me know if you wanna make any other changes!",
state=thread.to_state(),
)
)
logger.info(f"thread sent to humanlayer. Last event: {thread.events[-1].type}")
@app.post("/webhook/new-email-thread")
async def email_inbound(email_payload: EmailPayload, background_tasks: BackgroundTasks) -> Dict[str, Any]:
"""
route to kick off new processing thread from an email
"""
# test payload
if email_payload.is_test or email_payload.from_address == "overworked-admin@coolcompany.com":
logger.info("test payload received, skipping")
return {"status": "ok"}
logger.info(f"inbound email received: {email_payload.model_dump_json()}")
thread = Thread(initial_email=email_payload, events=[])
thread.events.append(Event(type=EventType.EMAIL_RECEIVED, data=email_payload))
background_tasks.add_task(handle_continued_thread, thread)
return {"status": "ok"}
@app.post("/webhook/human-response-on-existing-thread")
async def human_response(
human_response: FunctionCall | HumanContact, background_tasks: BackgroundTasks
) -> Dict[str, Any]:
"""
route to handle human responses
"""
if human_response.spec.state is not None:
thread = Thread.model_validate(human_response.spec.state)
else:
# decide what's the right way to handle this? probably logger.warn and proceed
raise ValueError("state is required")
if isinstance(human_response, HumanContact):
thread.events.append(
Event(type=EventType.HUMAN_RESPONSE, data={"human_response": human_response.status.response})
)
background_tasks.add_task(handle_continued_thread, thread)
return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
uvicorn.run(app, host="0.0.0.0", port=8000) # noqa: S104