Skip to content

Commit ef86d64

Browse files
committed
update with new workflow code
1 parent a05d444 commit ef86d64

File tree

2 files changed

+68
-30
lines changed
  • templates/components/agents/typescript

2 files changed

+68
-30
lines changed

templates/components/agents/typescript/financial_report/app/api/chat/workflow/finReport.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export class FinancialReportWorkflow extends Workflow<
7373
this.writeEvents = options.writeEvents;
7474
this.queryEngineTool = options.queryEngineTool;
7575
this.codeInterpreterTool = options.codeInterpreterTool;
76-
console.log("Chat history:", options.chatHistory);
7776

7877
this.documentGeneratorTool = options.documentGeneratorTool;
7978
this.memory = new ChatMemoryBuffer({
@@ -133,9 +132,12 @@ export class FinancialReportWorkflow extends Workflow<
133132
ctx: HandlerContext<null>,
134133
ev: StartEvent<AgentInput>,
135134
) {
135+
const message = ev.data;
136+
136137
if (this.systemPrompt) {
137138
this.memory.put({ role: "system", content: this.systemPrompt });
138139
}
140+
this.memory.put({ role: "user", content: message });
139141

140142
return new InputEvent({ input: this.memory.getMessages() });
141143
}

templates/components/agents/typescript/form_filling/app/api/chat/workflow/formFilling.ts

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import {
22
Context,
3+
HandlerContext,
34
StartEvent,
45
StopEvent,
56
Workflow,
67
WorkflowEvent,
7-
} from "@llamaindex/core/workflow";
8+
} from "@llamaindex/workflow";
89
import {
910
BaseToolWithCall,
1011
ChatMemoryBuffer,
1112
ChatMessage,
13+
ChatResponseChunk,
14+
MessageContent,
1215
Settings,
1316
ToolCall,
1417
ToolCallLLM,
@@ -38,7 +41,11 @@ If there is no query engine tool or the gathered information has many N/A values
3841
You can make multiple tool calls at once but only call with the same tool.
3942
`;
4043

41-
export class FormFillingWorkflow extends Workflow {
44+
export class FormFillingWorkflow extends Workflow<
45+
null,
46+
string | MessageContent,
47+
ChatResponseChunk
48+
> {
4249
llm: ToolCallLLM;
4350
memory: ChatMemoryBuffer;
4451
extractorTool: BaseToolWithCall;
@@ -75,31 +82,58 @@ export class FormFillingWorkflow extends Workflow {
7582
});
7683

7784
// Add steps
78-
this.addStep(StartEvent<AgentInput>, this.prepareChatHistory, {
79-
outputs: InputEvent,
80-
});
81-
this.addStep(InputEvent, this.handleLLMInput, {
82-
outputs: [
83-
InputEvent,
84-
ExtractMissingCellsEvent,
85-
FindAnswersEvent,
86-
FillMissingCellsEvent,
87-
StopEvent,
88-
],
89-
});
90-
this.addStep(ExtractMissingCellsEvent, this.handleExtractMissingCells, {
91-
outputs: InputEvent,
92-
});
93-
this.addStep(FindAnswersEvent, this.handleFindAnswers, {
94-
outputs: InputEvent,
95-
});
96-
this.addStep(FillMissingCellsEvent, this.handleFillMissingCells, {
97-
outputs: InputEvent,
98-
});
85+
this.addStep(
86+
{
87+
inputs: [StartEvent<AgentInput>],
88+
outputs: [InputEvent],
89+
},
90+
this.prepareChatHistory.bind(this),
91+
);
92+
93+
this.addStep(
94+
{
95+
inputs: [InputEvent],
96+
outputs: [
97+
InputEvent,
98+
ExtractMissingCellsEvent,
99+
FindAnswersEvent,
100+
FillMissingCellsEvent,
101+
StopEvent,
102+
],
103+
},
104+
this.handleLLMInput.bind(this),
105+
);
106+
107+
this.addStep(
108+
{
109+
inputs: [ExtractMissingCellsEvent],
110+
outputs: [InputEvent],
111+
},
112+
this.handleExtractMissingCells.bind(this),
113+
);
114+
115+
this.addStep(
116+
{
117+
inputs: [FindAnswersEvent],
118+
outputs: [InputEvent],
119+
},
120+
this.handleFindAnswers.bind(this),
121+
);
122+
123+
this.addStep(
124+
{
125+
inputs: [FillMissingCellsEvent],
126+
outputs: [InputEvent],
127+
},
128+
this.handleFillMissingCells.bind(this),
129+
);
99130
}
100131

101-
private async prepareChatHistory(ctx: Context, ev: StartEvent<AgentInput>) {
102-
const message = ev.data.input.message;
132+
private async prepareChatHistory(
133+
ctx: HandlerContext<null>,
134+
ev: StartEvent<AgentInput>,
135+
) {
136+
const message = ev.data;
103137

104138
if (this.systemPrompt) {
105139
this.memory.put({ role: "system", content: this.systemPrompt });
@@ -109,7 +143,7 @@ export class FormFillingWorkflow extends Workflow {
109143
return new InputEvent({ input: this.memory.getMessages() });
110144
}
111145

112-
private async handleLLMInput(ctx: Context, ev: InputEvent) {
146+
private async handleLLMInput(ctx: HandlerContext<null>, ev: InputEvent) {
113147
const chatHistory = ev.data.input;
114148

115149
const tools = [this.extractorTool, this.fillMissingCellsTool];
@@ -150,14 +184,16 @@ export class FormFillingWorkflow extends Workflow {
150184
return new FindAnswersEvent({
151185
toolCalls: toolCallResponse.toolCalls,
152186
});
187+
default:
188+
throw new Error(`Unknown tool: ${toolCallResponse.toolName()}`);
153189
}
154190
}
155191

156192
private async handleExtractMissingCells(
157193
ctx: Context,
158194
ev: ExtractMissingCellsEvent,
159195
) {
160-
ctx.writeEventToStream(
196+
ctx.sendEvent(
161197
new AgentRunEvent({
162198
name: "CSVExtractor",
163199
text: "Extracting missing cells",
@@ -182,7 +218,7 @@ export class FormFillingWorkflow extends Workflow {
182218
if (!this.queryEngineTool) {
183219
throw new Error("Query engine tool is not available");
184220
}
185-
ctx.writeEventToStream(
221+
ctx.sendEvent(
186222
new AgentRunEvent({
187223
name: "Researcher",
188224
text: "Finding answers",
@@ -203,7 +239,7 @@ export class FormFillingWorkflow extends Workflow {
203239
}
204240

205241
private async handleFillMissingCells(
206-
ctx: Context,
242+
ctx: HandlerContext<null>,
207243
ev: FillMissingCellsEvent,
208244
) {
209245
const { toolCalls } = ev.data;

0 commit comments

Comments
 (0)