forked from humanlayer/humanlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (43 loc) · 1.74 KB
/
app.py
File metadata and controls
65 lines (43 loc) · 1.74 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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from typing import Any, Dict
from humanlayer import AsyncHumanLayer, FunctionCall, FunctionCallSpec
app = FastAPI(
title="HumanLayer FastAPI Webhooks Example",
description="Example of using AsyncHumanLayer with FastAPI",
version="1.0.0",
)
# Initialize HumanLayer
hl = AsyncHumanLayer(verbose=True)
# Root endpoint
@app.get("/")
async def root() -> Dict[str, str]:
return {"message": "Welcome to the HumanLayer Webhooks Example"}
purchases: Dict[str, FunctionCall] = {}
def finalize_purchase(material: str, quantity: int) -> None:
print(f"Purchased {quantity} of {material}")
@app.get("/purchase-materials")
async def purchase_materials() -> Dict[str, str]:
pending_approval = await hl.create_function_call(
spec=FunctionCallSpec(
fn="purchase_materials",
kwargs={"material": "wood", "quantity": 10},
)
)
call_id = pending_approval.call_id
purchases[call_id] = pending_approval
return {"status": "purchase queued for approval", "call_id": call_id}
@app.get("/purchases")
async def get_purchases() -> Dict[str, Any]:
return {"purchases": [purchase.model_dump(mode="json") for purchase in purchases.values()]}
@app.post("/webhook/inbound")
async def webhook_inbound(webhook: FunctionCall) -> Dict[str, str]:
purchases[webhook.call_id] = webhook
if webhook.status is not None and webhook.status.approved:
finalize_purchase(webhook.spec.kwargs["material"], webhook.spec.kwargs["quantity"])
else:
print(f"Purchase {webhook.call_id} denied")
return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) # noqa: S104