Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: workflow API #1264

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b21a22c
refactor: reimplement Workflow API
himself65 Sep 26, 2024
498c734
revert: get / set api
himself65 Sep 26, 2024
ad81885
fix: type
himself65 Sep 27, 2024
06f0e5e
fix: step call
himself65 Sep 27, 2024
e4bab4e
fix: type
himself65 Sep 27, 2024
044054c
fix: stream event
himself65 Sep 27, 2024
72a26c4
fix: bring things back
himself65 Sep 27, 2024
18b93b3
fix: lint
himself65 Sep 27, 2024
5109e7b
fix: update example
himself65 Sep 27, 2024
1118029
feat: better handling of multi input
himself65 Sep 27, 2024
2d2047c
feat: in out checker
himself65 Sep 27, 2024
1ce4a55
fix: more test
himself65 Sep 27, 2024
3c623e1
feat: chat engine example
himself65 Sep 28, 2024
6e008fc
feat: workflow example
himself65 Sep 28, 2024
0ab163f
feat: loading
himself65 Sep 28, 2024
5eaafc7
fix: remove validation.ts
himself65 Sep 28, 2024
a2a242c
feat: recover api
himself65 Sep 28, 2024
def035b
Merge branch 'main' into himself65/20240925/signal
himself65 Sep 28, 2024
53c50b0
fix: pnpm
himself65 Sep 28, 2024
ee2a61f
chore: remove waku example
himself65 Sep 28, 2024
3310467
Merge remote-tracking branch 'upstream/main' into himself65/20240925/…
himself65 Sep 28, 2024
4bef7e4
feat: update examples
himself65 Sep 28, 2024
eae934a
fix: upload file
himself65 Sep 28, 2024
c64fa6c
Update giant-jars-grab.md
himself65 Sep 28, 2024
e75db73
feat: update example
himself65 Sep 29, 2024
568c5a7
feat: update example
himself65 Sep 29, 2024
cac0ab7
fix: lock
himself65 Sep 29, 2024
3a0913b
fix: utils
himself65 Sep 29, 2024
02788cc
fix: tsconfig
himself65 Sep 29, 2024
72c23a4
Merge remote-tracking branch 'upstream/main' into himself65/20240925/…
himself65 Sep 30, 2024
ee03495
fix: concurrency streaming
himself65 Sep 30, 2024
e1a3398
fix: concurrency regression
himself65 Sep 30, 2024
5d5a170
fix: type check
himself65 Sep 30, 2024
b7a7c7b
text: add cycle test
marcusschiesser Sep 30, 2024
51058ad
Merge remote-tracking branch 'upstream/ms/add-concurrent-workflowtest…
himself65 Sep 30, 2024
0145eb5
feat: update test sample
himself65 Sep 30, 2024
be46c7f
feat: implement `or` API
himself65 Sep 30, 2024
999832f
feat: `sendEvent`
himself65 Sep 30, 2024
c021076
feat: support requireEvent and sendEvent API
himself65 Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/giant-jars-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@llamaindex/core": minor
---

feat: update workflow API v2

- combine await behavior with async iterator, now there's only one queue to handle the `startEvent`.
- each Context will have its own step map, so the step can be executed in parallel.
- remove `context.globals` from API, you can provide your own context by `.with` API
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ jobs:
- nextjs-agent
- nextjs-edge-runtime
- nextjs-node-runtime
- waku-query-engine
- llama-parse-browser
runs-on: ubuntu-latest
name: Build LlamaIndex Example (${{ matrix.packages }})
Expand Down
Binary file added apps/docs/docs/_static/showcase/app-creator.mp4
Binary file not shown.
63 changes: 63 additions & 0 deletions apps/docs/docs/guides/workflow/1_introduction.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
sidebar_position: 1
---

import CodeBlock from "@theme/CodeBlock";
import Basic from "!raw-loader!../../../../../examples/workflow/01_basic.ts";
import Context from "!raw-loader!../../../../../examples/workflow/02_context.ts";
import DynamicContext from "!raw-loader!../../../../../examples/workflow/03_dynamic_context.ts";
import ComplexWorkflow from "!raw-loader!../../../../../examples/workflow/04_complex_workflow.ts";
import ServerAction from "!raw-loader!../../../../../packages/llamaindex/e2e/examples/nextjs/src/actions/app-creator.tsx";
import Page from "!raw-loader!../../../../../packages/llamaindex/e2e/examples/nextjs/src/app/app-creator/page.tsx";

# Introduction

Workflow is a primitive API that allows you to define a series of steps and chain by the event-driven mechanism.
It is a powerful tool to orchestrate the execution of multiple tasks in a specific order.

In this guide, we will walk you through the basic concepts of the Workflow API and how to use it in your application.

## Getting Started

<CodeBlock language="ts">{Basic}</CodeBlock>

## Context

When you want to share state between steps, you can use the `context` object.

For example, you can abstract `OpenAI` to the context level and set it after you create the workflow.

<CodeBlock language="ts">{Context}</CodeBlock>

## Dynamic Context

You can also set the context dynamically by using the `with` after you run a workflow.

<CodeBlock language="ts">{DynamicContext}</CodeBlock>

```txt
Input word or phrase: Hello, world!
OpenAI Result: Bonjour, le monde !
Ollama3.1 Result: Bonjour, monde !
```

## Complex Workflow

We can define a more complex workflow that involves multiple steps and reviews.

<CodeBlock language="ts">{ComplexWorkflow}</CodeBlock>

## Integrate with Next.js

Adding more loading indicators and error handling to the workflow, integrate with server action in Next.js.

<CodeBlock language="tsx">{ServerAction}</CodeBlock>

<CodeBlock language="tsx">{Page}</CodeBlock>

import Preview from "../../_static/showcase/app-creator.mp4";

<video controls width="100%">
<source src={Preview} type="video/mp4" />
Your browser does not support the video tag.
</video>
2 changes: 2 additions & 0 deletions apps/docs/docs/guides/workflow/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
label: Workflow
position: 2
168 changes: 0 additions & 168 deletions apps/docs/docs/modules/workflows.mdx

This file was deleted.

2 changes: 2 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@azure/identity": "^4.4.1",
"@datastax/astra-db-ts": "^1.4.1",
"@llamaindex/core": "^0.2.0",
"@llamaindex/ollama": "workspace:^0.0.3",
"@llamaindex/openai": "workspace:^0.1.10",
"@notionhq/client": "^2.2.15",
"@pinecone-database/pinecone": "^3.0.2",
"@vercel/postgres": "^0.10.0",
Expand Down
22 changes: 22 additions & 0 deletions examples/workflow/01_basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { StartEvent, StopEvent, Workflow } from "@llamaindex/core/workflow";
import { OpenAI } from "@llamaindex/openai";

const myWorkflow = new Workflow({
verbose: true,
});

const openai = new OpenAI();

myWorkflow.addStep(StartEvent, async (_, event) => {
const { input } = event.data;
const response = await openai.complete({
prompt: `Translate English to French: ${input}`,
});
return new StopEvent({
result: response.text,
});
});

const { data } = await myWorkflow.run("Hello, world!");

console.log("Result:", data.result);
35 changes: 35 additions & 0 deletions examples/workflow/02_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { StartEvent, StopEvent, Workflow } from "@llamaindex/core/workflow";
import { OpenAI } from "@llamaindex/openai";
import { createInterface } from "readline/promises";

const myWorkflow = new Workflow({
verbose: true,
}).with({
llm: new OpenAI({
apiKey: process.env.OPENAI_API_KEY!,
}),
});

myWorkflow.addStep(StartEvent, async (context, event) => {
const { llm } = context;
const { input } = event.data;
const response = await llm.complete({
prompt: `Translate English to French: ${input}`,
});
return new StopEvent({
result: response.text,
});
});

const rl = createInterface({
input: process.stdin,
output: process.stdout,
});

const input = await rl.question("Input word or phrase: ");

const { data } = await myWorkflow.run(input);

console.log("Result:", data.result);

rl.close();
55 changes: 55 additions & 0 deletions examples/workflow/03_dynamic_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { LLM } from "@llamaindex/core/llms";
import { StartEvent, StopEvent, Workflow } from "@llamaindex/core/workflow";
import { Ollama } from "@llamaindex/ollama";
import { OpenAI } from "@llamaindex/openai";
import { createInterface } from "readline/promises";

const myWorkflow = new Workflow({
verbose: false,
}).with({
llm: null! as LLM,
});

myWorkflow.addStep(StartEvent, async (context, event) => {
const { llm } = context;
const { input } = event.data;
const response = await llm.complete({
prompt: `Translate English to French: ${input}`,
});
return new StopEvent({
result: response.text,
});
});

const rl = createInterface({
input: process.stdin,
output: process.stdout,
});

const input = await rl.question("Input word or phrase: ");

const emptyRunner = myWorkflow.run(input);

const openaiResult = emptyRunner.with({
llm: new OpenAI({
apiKey: process.env.OPENAI_API_KEY!,
}),
});

const ollamaResult = emptyRunner.with({
llm: new Ollama({
model: "llama3.1",
}),
});

{
const { data } = await openaiResult;
console.log("OpenAI Result:", data.result);
}

{
const { data } = await ollamaResult;
console.log("Ollama3.1 Result:", data.result);
}

rl.close();
Loading