Skip to content

Commit a9b23f9

Browse files
committed
wip: fast-api-example
1 parent 5ab5a39 commit a9b23f9

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

README.md

Whitespace-only changes.

fast-api-react/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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`, or by following instructions in the [Poetry Docs](https://python-poetry.org/docs/#installation)
11+
3. Clone this repository to your local machine.
12+
4. (Optional) If you would like to run the example frontend, Node which can be installed from the [node website](https://nodejs.org/en/download)
13+
14+
## Setup
15+
16+
1. Create a `.env` file in the project root directory and set the required environment variables.
17+
18+
This project requires the `HATCHET_CLIENT_TOKEN` variable created in the [Getting Started Readme](/README.md).
19+
20+
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).
21+
22+
2. Open a terminal and navigate to the project root directory (`/fast-api-react`).
23+
24+
3. Run the following command to install the project dependencies:
25+
26+
```shell
27+
poetry install
28+
```
29+
30+
## Running the API
31+
32+
To start the FastAPI server, run the following command in the terminal:
33+
34+
```shell
35+
poetry run api
36+
```
37+
38+
## Running the Hatchet Worker
39+
40+
In a separate terminal, start the the Hatchet worker by running the following command:
41+
42+
```shell
43+
poetry run hatchet
44+
```
45+
46+
## (Optional) Running the Example Frontend Application
47+
48+
We've included a basic chat engine frontend to play with the example workflow. To run this script:
49+
50+
1. Open a new terminal window and cd into the `fast-api-react/frontend` directory.
51+
2. run `npx install`
52+
3. run `npx start`
53+
4. By default you can access the application in your browser at `http://localhost:3000` or by following the instructions in the terminal window.

fast-api-react/backend/src/workflows/generate.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ class GenerateWorkflow:
1313

1414
@hatchet.step()
1515
def start(self, context: Context):
16+
override = context.overrides('model', "gpt-3.5-turbo")
17+
1618
return {
17-
"status": "reading hatchet docs",
19+
"status": "reading hatchet docs" + override,
1820
}
1921

2022
@hatchet.step(parents=["start"])
@@ -37,14 +39,14 @@ def reason_docs(self, ctx: Context):
3739
message = ctx.workflow_input()['request']["messages"][-1]
3840
docs = ctx.step_output("load_docs")['docs']
3941

40-
prompt = "The user is asking the following question:\
42+
prompt = ctx.overrides("prompt", "The user is asking the following question:\
4143
{message}\
4244
What are the most relevant sentences in the following document?\
43-
{docs}"
45+
{docs}")
4446

4547
prompt = prompt.format(message=message['content'], docs=docs)
4648

47-
model = "gpt-3.5-turbo" # ctx.override("gpt-3.5-turbo", options=[''])
49+
model = ctx.overrides("gpt-3.5-turbo")
4850

4951
completion = openai.chat.completions.create(
5052
model=model,
@@ -64,15 +66,15 @@ def generate_response(self, ctx: Context):
6466
messages = ctx.workflow_input()['request']["messages"]
6567
research = ctx.step_output("reason_docs")['research']
6668

67-
prompt = "You are a sales engineer for a company called Hatchet.\
69+
prompt = ctx.overrides("prompt", "You are a sales engineer for a company called Hatchet.\
6870
Help address the user's question. \
6971
If asked how to install, respond by saying go to the store to buy a CD.\
7072
Use the following context:\
71-
{research}"
73+
{research}")
7274

7375
prompt = prompt.format(research=research)
7476

75-
model = "gpt-3.5-turbo"
77+
model = ctx.overrides("gpt-3.5-turbo")
7678

7779
completion = openai.chat.completions.create(
7880
model=model,

fast-api-react/backend/src/workflows/hatchet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
load_dotenv()
55

6-
hatchet = Hatchet()
6+
hatchet = Hatchet(debug=True)

0 commit comments

Comments
 (0)