Skip to content

Commit ae4699e

Browse files
committed
Merge branch 'main' of github.com:hatchet-dev/hatchet-python-quickstart into main
2 parents f0ab06c + 972d279 commit ae4699e

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .hatchet import hatchet
22
from hatchet_sdk import Context
3+
import openai
34

45

56
@hatchet.workflow()
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
from ..hatchet import hatchet
22
from hatchet_sdk import Context
3+
import openai
34

4-
# This is a simple example of a workflow that has 3 steps.
5+
# This is a simple example of a workflow that uses the OpenAI GPT-3 model to generate an answer to a question.
56
# The workflow is declared decorated with `@hatchet_workflow` and the steps are declared with `@hatchet_step`.
67

78

8-
@hatchet.workflow(on_events=["simpleai:create"])
9+
@hatchet.workflow()
910
class SimpleGenAiWorkflow:
10-
1111
@hatchet.step()
12-
def step1(self, context: Context):
13-
# The context object is passed to each step and contains the input data for the workflow.
14-
messages = context.workflow_input()['request']['messages']
15-
print("> starting step1", messages)
12+
def start(self, ctx: Context):
13+
message = ctx.workflow_input()["messages"][-1]
14+
15+
prompt = ctx.playground(
16+
"prompt", "The user is asking the following question: {message}")
1617

17-
return {"status": "thinking"}
18+
prompt = prompt.format(message=message['content'])
1819

19-
@hatchet.step(parents=["step1"])
20-
def step2(self, context: Context):
21-
print("starting step2")
20+
model = ctx.playground("model", "gpt-3.5-turbo")
2221

23-
return {"status": "writing a response"}
22+
completion = openai.chat.completions.create(
23+
model=model,
24+
messages=[
25+
{"role": "system", "content": prompt},
26+
message
27+
]
28+
)
2429

25-
@hatchet.step(parents=["step2"], timeout='5m')
26-
def step3(self, context: Context):
27-
messages = context.workflow_input()['request']['messages']
28-
return {"complete": "true", "status": "idle", "message": "response from step3"}
30+
return {
31+
"answer": completion.choices[0].message.content,
32+
}

0 commit comments

Comments
 (0)