|
1 | 1 | from ..hatchet import hatchet |
2 | 2 | from hatchet_sdk import Context |
| 3 | +import openai |
3 | 4 |
|
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. |
5 | 6 | # The workflow is declared decorated with `@hatchet_workflow` and the steps are declared with `@hatchet_step`. |
6 | 7 |
|
7 | 8 |
|
8 | | -@hatchet.workflow(on_events=["simpleai:create"]) |
| 9 | +@hatchet.workflow() |
9 | 10 | class SimpleGenAiWorkflow: |
10 | | - |
11 | 11 | @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}") |
16 | 17 |
|
17 | | - return {"status": "thinking"} |
| 18 | + prompt = prompt.format(message=message['content']) |
18 | 19 |
|
19 | | - @hatchet.step(parents=["step1"]) |
20 | | - def step2(self, context: Context): |
21 | | - print("starting step2") |
| 20 | + model = ctx.playground("model", "gpt-3.5-turbo") |
22 | 21 |
|
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 | + ) |
24 | 29 |
|
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