Skip to content

Commit f5723e1

Browse files
committed
feat: init simple examples
1 parent ccb47d7 commit f5723e1

22 files changed

Lines changed: 308 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ __pycache__/
77
# C extensions
88
*.so
99

10+
certs
1011
# Distribution / packaging
1112
.Python
1213
build/

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cSpell.words": ["basicrag", "genai"]
3+
}

simple-examples/README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Hatchet FastAPI Example
1+
# Hatchet Simple Examples
22

3-
This is an example project demonstrating how to run a simple Hatchet worker.
3+
This is an example project demonstrating how to use Hatchet with Python.
44

55
## Prerequisites
66

@@ -11,11 +11,12 @@ Before running this project, make sure you have the following:
1111

1212
## Setup
1313

14-
1. Create a `.env` file in this directory and set the required environment variables. This requires the `HATCHET_CLIENT_TOKEN` variable created in the [Getting Started README](/README.md).
14+
1. Create a `.env` file in the `./backend` directory and set the required environment variables. This requires the `HATCHET_CLIENT_TOKEN` variable created in the [Getting Started README](../README.md). If you would like to try the Generative AI examples in [./src/genai](./src/genai) You will also need, a `OPENAI_API_KEY` which can be created on the [OpenAI Website](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key).
1515

1616
```
1717
HATCHET_CLIENT_TLS_STRATEGY=none
1818
HATCHET_CLIENT_TOKEN="<token>"
19+
OPENAI_API_KEY="<openai-key>" # (OPTIONAL) only required to run examples in [./src/genai](./src/genai)
1920
```
2021

2122
2. Open a terminal and navigate to the project root directory (`/simple-examples`).
@@ -26,12 +27,41 @@ HATCHET_CLIENT_TOKEN="<token>"
2627
poetry install
2728
```
2829

29-
## Running the Hatchet Worker
30+
### Running the Hatchet Worker
3031

3132
In a separate terminal, start the the Hatchet worker by running the following command:
3233

3334
```shell
3435
poetry run hatchet
3536
```
3637

37-
You can then navigate to the workflow in the Hatchet dashboard to view the structure of this workflow.
38+
## Triggering a workflow
39+
40+
TODO
41+
42+
## Project Overview
43+
44+
### Example Workflows
45+
46+
The project contains example workflows in the [`./src`](./src) directory. These workflows are registered with hatchet in [`./src/main.py`](./src/main.py) which is started when running `poetry run hatchet`.
47+
48+
#### Super Simple Workflows
49+
50+
The project includes a variety of basic workflows to demonstrate Hatchet's core capabilities, each showcasing different features:
51+
52+
1. **[Simple Workflow](./src/simple/worker.py)**: Demonstrates a straightforward process flow, showcasing the basics of setting up a workflow in Hatchet.
53+
2. **[Concurrency Limit Workflow](./src/concurrency_limit/worker.py)**: Shows how to manage concurrency limits within workflows to ensure that only a certain number of instances run simultaneously.
54+
3. **[Directed Acyclic Graph (DAG) Workflow](./src/dag/worker.py)**: Illustrates setting up workflows with dependencies that form a Directed Acyclic Graph, demonstrating the advanced orchestration capabilities of Hatchet.
55+
4. **[Manual Trigger Workflow](./src/manual_trigger/worker.py)**: Explains how to initiate workflows manually, offering control over workflow execution triggers.
56+
5. **[Timeout Workflow](./src/timeout/worker.py)**: Demonstrates handling timeout scenarios within workflows, ensuring that long-running or stalled processes are appropriately managed.
57+
58+
#### Generative AI Workflows
59+
60+
For more complex use cases, the project includes examples that integrate with OpenAI's API for generative tasks:
61+
62+
1. **[Simple Response Generation](./src/genai/simple.py)**: A single-step workflow that makes a request to OpenAI, showcasing how to incorporate AI services into Hatchet workflows.
63+
2. **[Basic Retrieval Augmented Generation (BasicRag)](./src/genai/basicrag.py)**: A multi-step workflow that involves loading website content with Beautiful Soup, reasoning about the information, and generating a response with OpenAI, demonstrating the potential for complex, AI-driven processes.
64+
65+
### Exposing the workflows via a RestAPI
66+
67+
For a more complete example of how you might use Hatchet as part of a deployed production service, check out the [FastAPI Example](../fast-api-react/README.md)

simple-examples/src/concurrency_limit/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from ..hatchet import hatchet
2+
3+
hatchet.client.event.push(
4+
"concurrency-test",
5+
{
6+
"test": "test"
7+
}
8+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from ..hatchet import hatchet
2+
3+
4+
@hatchet.workflow(on_events=["concurrency-test"])
5+
class ConcurrencyDemoWorkflow:
6+
7+
@hatchet.concurrency(max_runs=5)
8+
def concurrency(self, context) -> str:
9+
return "concurrency-key"
10+
11+
@hatchet.step()
12+
def step1(self, context):
13+
print("executed step1")
14+
pass
15+
16+
@hatchet.step(parents=["step1"], timeout='4s')
17+
def step2(self, context):
18+
print("started step2")
19+
context.sleep(1)
20+
print("finished step2")

simple-examples/src/dag/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from ..hatchet import hatchet
2+
3+
4+
hatchet.client.event.push(
5+
"user:create",
6+
{
7+
"test": "test"
8+
}
9+
)

simple-examples/src/dag/worker.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from hatchet_sdk import Context
2+
from ..hatchet import hatchet
3+
4+
5+
@hatchet.workflow(on_events=["user:create"])
6+
class DagWorkflow:
7+
8+
@hatchet.step()
9+
def step1(self, context: Context):
10+
overrideValue = context.playground(
11+
"prompt", "You are an AI assistant...")
12+
13+
print("executed step1", context.workflow_input())
14+
return {
15+
"step1": overrideValue,
16+
}
17+
18+
@hatchet.step()
19+
def step2(self, context: Context):
20+
print("executed step2", context.workflow_input())
21+
return {
22+
"step2": "step2",
23+
}
24+
25+
@hatchet.step(parents=["step1", "step2"])
26+
def step3(self, context: Context):
27+
print("executed step3", context.workflow_input(),
28+
context.step_output("step1"), context.step_output("step2"))
29+
return {
30+
"step3": "step3",
31+
}
32+
33+
@hatchet.step(parents=["step1", "step3"])
34+
def step4(self, context: Context):
35+
print("executed step4", context.workflow_input(),
36+
context.step_output("step1"), context.step_output("step3"))
37+
return {
38+
"step4": "step4",
39+
}

simple-examples/src/genai/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)