Skip to content

Commit e724b18

Browse files
committed
initial commit
0 parents  commit e724b18

36 files changed

Lines changed: 30606 additions & 0 deletions

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python
2+
.venv
3+
*.pyc
4+
__pycache__/
5+
*.pyo
6+
*.pyd
7+
*.pyw
8+
*.pyz
9+
*.pywz
10+
*.ipynb_checkpoints/
11+
12+
# TypeScript React
13+
node_modules/
14+
dist/
15+
build/
16+
17+
.DS_Store
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__/
2+
.env
3+
certs/
4+
.venv
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Hatchet FastAPI Example
2+
3+
This is an example project demonstrating how to use Hatchet with FastAPI.
4+
5+
## Prerequisites
6+
7+
Before running this project, make sure you have the following:
8+
9+
1. Python 3.7 or higher installed on your machine.
10+
2. Poetry package manager installed. You can install it by running `pip install poetry`.
11+
3. Clone this repository to your local machine.
12+
13+
## Setup
14+
15+
1. Create a `.env` file in the project root directory and set the required environment variables. Refer to the documentation for the specific environment variables needed for your application.
16+
17+
2. Open a terminal and navigate to the project root directory.
18+
19+
3. Run the following command to install the project dependencies:
20+
21+
```shell
22+
poetry install
23+
```
24+
25+
## Running the API
26+
27+
To start the FastAPI server, run the following command in the terminal:
28+
29+
```shell
30+
poetry run api
31+
```
32+
33+
## Running the Hatchet Worker
34+
35+
To start the Hatchet worker, run the following command in the terminal:
36+
37+
```shell
38+
poetry run hatchet
39+
```

fast-api-react/fast-api-react/backend/README.md

Whitespace-only changes.

fast-api-react/fast-api-react/backend/__init__.py

Whitespace-only changes.

fast-api-react/fast-api-react/backend/poetry.lock

Lines changed: 1131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[tool.poetry]
2+
name = "src"
3+
version = "0.0.0"
4+
description = "Easily run background tasks in FastAPI with Hatchet"
5+
authors = []
6+
readme = "README.md"
7+
8+
[tool.poetry.scripts]
9+
api = "src.api.main:start"
10+
hatchet = "src.workflows.main:start"
11+
12+
[tool.poetry.dependencies]
13+
python = "^3.8"
14+
python-dotenv = "^1.0.0"
15+
hatchet-sdk = "^0.9.4"
16+
uvicorn = {extras = ["standard"], version = "^0.27.0"}
17+
fastapi = "^0.109.0"
18+
openai = "^1.11.0"
19+
beautifulsoup4 = "^4.12.3"
20+
requests = "^2.31.0"
21+
urllib3 = "1.26.15"
22+
23+
[build-system]
24+
requires = ["poetry-core"]
25+
build-backend = "poetry.core.masonry.api"
26+

fast-api-react/fast-api-react/backend/src/__init__.py

Whitespace-only changes.

fast-api-react/fast-api-react/backend/src/api/__init__.py

Whitespace-only changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
3+
from fastapi.responses import StreamingResponse
4+
5+
from .models import MessageRequest
6+
7+
from hatchet_sdk import Hatchet
8+
import uvicorn
9+
from dotenv import load_dotenv
10+
import json
11+
12+
load_dotenv()
13+
14+
app = FastAPI()
15+
hatchet = Hatchet()
16+
17+
18+
origins = [
19+
"http://localhost:3000",
20+
"localhost:3000"
21+
]
22+
23+
24+
app.add_middleware(
25+
CORSMiddleware,
26+
allow_origins=origins,
27+
allow_credentials=True,
28+
allow_methods=["*"],
29+
allow_headers=["*"]
30+
)
31+
32+
33+
def event_stream_generator(workflowRunId):
34+
stream = hatchet.client.listener.stream(workflowRunId)
35+
36+
for event in stream:
37+
data = json.dumps({
38+
"type": event.type,
39+
"payload": event.payload,
40+
"workflowRunId": workflowRunId
41+
})
42+
43+
# stream.abort()
44+
print(data)
45+
yield "data: " + data + "\n\n"
46+
47+
48+
@app.get("/stream/{messageId}")
49+
async def stream(messageId: str):
50+
# message id -> workflowRunId
51+
workflowRunId = messageId
52+
# stream = hatchet.stream(workflowRunId)
53+
return StreamingResponse(event_stream_generator(workflowRunId), media_type='text/event-stream')
54+
55+
56+
@app.post("/message")
57+
def message(data: MessageRequest):
58+
print(data.model_dump())
59+
60+
messageId = hatchet.client.admin.run_workflow("GenerateWorkflow", {
61+
"request": data.model_dump()
62+
})
63+
64+
# save step message id -> workflowRunId
65+
66+
return {"workflowRunId": messageId}
67+
68+
69+
def start():
70+
"""Launched with `poetry run start` at root level"""
71+
uvicorn.run("src.api.main:app", host="0.0.0.0", port=8000, reload=True)

0 commit comments

Comments
 (0)