-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PoC]: Declarative Workflows in V2 SDK #292
base: main
Are you sure you want to change the base?
Changes from all commits
47aa0fd
a5d7077
9e1a967
b859eb0
8f5b243
b49bc27
865af16
e66a979
232a352
b784805
88070dc
d08f542
ef8be85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,3 +162,4 @@ cython_debug/ | |
#.idea/ | ||
|
||
openapitools.json | ||
.python-version |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from hatchet_sdk.v2.hatchet import Hatchet | ||
|
||
hatchet = Hatchet(debug=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from collections import Counter | ||
from typing import Literal | ||
|
||
from examples.v2.declarative_workflows.client import hatchet | ||
from examples.v2.declarative_workflows.workflows import ( | ||
Greeting, | ||
Language, | ||
greet_workflow, | ||
language_counter_workflow, | ||
) | ||
from hatchet_sdk import Context | ||
|
||
|
||
def complete_greeting(greeting: Greeting) -> str: | ||
match greeting: | ||
case "Hello": | ||
return "world!" | ||
case "Ciao": | ||
return "mondo!" | ||
case "Hej": | ||
return "världen!" | ||
|
||
|
||
@greet_workflow.declare() | ||
async def greet(ctx: Context) -> dict[Literal["message"], str]: | ||
workflow_input = greet_workflow.workflow_input(ctx) | ||
greeting = workflow_input.greeting | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these type check correctly, so no need to |
||
|
||
await language_counter_workflow.spawn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refer to the workflow as an object instead of passing a string with the name |
||
context=ctx, | ||
input=language_counter_workflow.construct_spawn_workflow_input( | ||
input=workflow_input | ||
), | ||
Comment on lines
+31
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. types are enforced here, so you know the shape of |
||
) | ||
|
||
return {"message": greeting + " " + complete_greeting(greeting)} | ||
|
||
|
||
## Imagine this is a metric in a monitoring system | ||
counter: Counter[Language] = Counter() | ||
|
||
|
||
@language_counter_workflow.declare() | ||
async def language_counter( | ||
ctx: Context, | ||
) -> dict[Language, int]: | ||
greeting = language_counter_workflow.workflow_input(ctx).greeting | ||
|
||
match greeting: | ||
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type checking here for |
||
case "Hello": | ||
counter["English"] += 1 | ||
case "Ciao": | ||
counter["Italian"] += 1 | ||
case "Hej": | ||
counter["Swedish"] += 1 | ||
|
||
return dict(counter) | ||
|
||
|
||
if __name__ == "__main__": | ||
worker = hatchet.worker("my-worker") | ||
|
||
worker.start() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Literal | ||
|
||
from pydantic import BaseModel | ||
|
||
from examples.v2.declarative_workflows.client import hatchet | ||
|
||
Greeting = Literal["Hello", "Ciao", "Hej"] | ||
Language = Literal["English", "Swedish", "Italian"] | ||
|
||
|
||
class ExampleInput(BaseModel): | ||
greeting: Greeting | ||
|
||
|
||
greet_workflow = hatchet.declare_workflow(input_validator=ExampleInput) | ||
language_counter_workflow = hatchet.declare_workflow( | ||
input_validator=ExampleInput, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
single decorator to register a function in Hatchet