|
33 | 33 |
|
34 | 34 | @app.post("/message") |
35 | 35 | def message(data: MessageRequest): |
36 | | - |
37 | | - messageId = hatchet.client.admin.run_workflow("GenerateWorkflow", { |
| 36 | + ''' This endpoint is called by the client to start a message generation workflow. ''' |
| 37 | + messageId = hatchet.client.admin.run_workflow("BasicRagWorkflow", { |
38 | 38 | "request": data.model_dump() |
39 | 39 | }) |
40 | 40 |
|
41 | | - # save step message id -> workflowRunId |
| 41 | + # normally, we'd save the workflowRunId to a database and return a reference to the client |
| 42 | + # for this simple example, we just return the workflowRunId |
42 | 43 |
|
43 | | - return {"workflowRunId": messageId} |
| 44 | + return {"messageId": messageId} |
44 | 45 |
|
45 | 46 |
|
46 | 47 | def event_stream_generator(workflowRunId): |
| 48 | + ''' This helper function is a generator that yields events from the Hatchet event stream. ''' |
47 | 49 | stream = hatchet.client.listener.stream(workflowRunId) |
48 | 50 |
|
49 | 51 | for event in stream: |
| 52 | + ''' you can filter and transform event data here that will be sent to the client''' |
50 | 53 | data = json.dumps({ |
51 | 54 | "type": event.type, |
52 | 55 | "payload": event.payload, |
53 | | - "workflowRunId": workflowRunId |
| 56 | + "messageId": workflowRunId |
54 | 57 | }) |
55 | 58 | yield "data: " + data + "\n\n" |
56 | 59 |
|
57 | 60 |
|
58 | | -@app.get("/stream/{messageId}") |
| 61 | +@app.get("/message/{messageId}") |
59 | 62 | async def stream(messageId: str): |
60 | | - # message id -> workflowRunId |
| 63 | + ''' |
| 64 | + in a normal application you might use the message id to look up a workflowRunId |
| 65 | + for this simple case, we have no persistence and just use the message id as the workflowRunId |
| 66 | +
|
| 67 | + you might also consider looking up the workflowRunId in a database and returning the results if the message has already been processed |
| 68 | + ''' |
61 | 69 | workflowRunId = messageId |
62 | 70 | return StreamingResponse(event_stream_generator(workflowRunId), media_type='text/event-stream') |
63 | 71 |
|
|
0 commit comments