Skip to content

Support agent workflow for TS #515

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 15 additions & 46 deletions helpers/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs/promises";
import os from "os";
import path from "path";
import { bold, cyan, red, yellow } from "picocolors";
import { bold, cyan, yellow } from "picocolors";
import { assetRelocator, copy } from "../helpers/copy";
import { callPackageManager } from "../helpers/install";
import { templatesDir } from "./dir";
Expand Down Expand Up @@ -123,56 +123,25 @@ export const installTSTemplate = async ({
cwd: path.join(compPath, "vectordbs", "typescript", vectorDb ?? "none"),
});

if (template === "multiagent") {
const multiagentPath = path.join(compPath, "multiagent", "typescript");
if (template === "multiagent" && useCase) {
// Copy use case code for multiagent template
console.log("\nCopying use case:", useCase, "\n");
const useCasePath = path.join(compPath, "agents", "typescript", useCase);
const useCaseCodePath = path.join(useCasePath, "workflow");

// copy workflow code for multiagent template
// Copy use case codes
await copy("**", path.join(root, relativeEngineDestPath, "workflow"), {
parents: true,
cwd: path.join(multiagentPath, "workflow"),
cwd: useCaseCodePath,
rename: assetRelocator,
});

// Copy use case code for multiagent template
if (useCase) {
console.log("\nCopying use case:", useCase, "\n");
const useCasePath = path.join(compPath, "agents", "typescript", useCase);
const useCaseCodePath = path.join(useCasePath, "workflow");

// Copy use case codes
await copy("**", path.join(root, relativeEngineDestPath, "workflow"), {
parents: true,
cwd: useCaseCodePath,
rename: assetRelocator,
});

// Copy use case files to project root
await copy("*.*", path.join(root), {
parents: true,
cwd: useCasePath,
rename: assetRelocator,
});
} else {
console.log(
red(
`There is no use case selected for ${template} template. Please pick a use case to use via --use-case flag.`,
),
);
process.exit(1);
}

if (framework === "nextjs") {
// patch route.ts file
await copy("**", path.join(root, relativeEngineDestPath), {
parents: true,
cwd: path.join(multiagentPath, "nextjs"),
});
} else if (framework === "express") {
// patch chat.controller.ts file
await copy("**", path.join(root, relativeEngineDestPath), {
parents: true,
cwd: path.join(multiagentPath, "express"),
});
}
// Copy use case files to project root
await copy("*.*", path.join(root), {
parents: true,
cwd: useCasePath,
rename: assetRelocator,
});
}

// copy loader component (TS only supports llama_parse and file for now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ export class FinancialReportWorkflow extends Workflow<
ctx: HandlerContext<null>,
ev: StartEvent<AgentInput>,
): Promise<InputEvent> => {
const { message } = ev.data;
const { userInput, chatHistory } = ev.data;

if (this.systemPrompt) {
this.memory.put({ role: "system", content: this.systemPrompt });
}
this.memory.put({ role: "user", content: message });
this.memory.put({ role: "user", content: userInput });

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

handleLLMInput = async (
Expand All @@ -162,7 +162,7 @@ export class FinancialReportWorkflow extends Workflow<
const toolCallResponse = await chatWithTools(this.llm, tools, chatHistory);

if (!toolCallResponse.hasToolCall()) {
return new StopEvent(toolCallResponse.responseGenerator);
return new StopEvent(toolCallResponse.responseGenerator as any);
}

if (toolCallResponse.hasMultipleTools()) {
Expand All @@ -171,7 +171,7 @@ export class FinancialReportWorkflow extends Workflow<
content:
"Calling different tools is not allowed. Please only use multiple calls of the same tool.",
});
return new InputEvent({ input: this.memory.getMessages() });
return new InputEvent({ input: await this.memory.getMessages() });
}

// Put the LLM tool call message into the memory
Expand Down Expand Up @@ -263,7 +263,7 @@ export class FinancialReportWorkflow extends Workflow<
// Clone the current chat history
// Add the analysis system prompt and the message from the researcher
const newChatHistory = [
...this.memory.getMessages(),
...(await this.memory.getMessages()),
{ role: "system", content: analysisPrompt },
ev.data.input,
];
Expand All @@ -276,10 +276,10 @@ export class FinancialReportWorkflow extends Workflow<
if (!toolCallResponse.hasToolCall()) {
this.memory.put(await toolCallResponse.asFullResponse());
return new InputEvent({
input: this.memory.getMessages(),
input: await this.memory.getMessages(),
});
} else {
this.memory.put(toolCallResponse.toolCallMessage);
this.memory.put(toolCallResponse.toolCallMessage as ChatMessage);
toolCalls = toolCallResponse.toolCalls;
}
}
Expand All @@ -296,7 +296,7 @@ export class FinancialReportWorkflow extends Workflow<
}

return new InputEvent({
input: this.memory.getMessages(),
input: await this.memory.getMessages(),
});
};

Expand All @@ -315,6 +315,6 @@ export class FinancialReportWorkflow extends Workflow<
for (const toolMsg of toolMsgs) {
this.memory.put(toolMsg);
}
return new InputEvent({ input: this.memory.getMessages() });
return new InputEvent({ input: await this.memory.getMessages() });
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ export class FormFillingWorkflow extends Workflow<
ctx: HandlerContext<null>,
ev: StartEvent<AgentInput>,
): Promise<InputEvent> => {
const { message } = ev.data;
const { userInput, chatHistory } = ev.data;

if (this.systemPrompt) {
this.memory.put({ role: "system", content: this.systemPrompt });
}
this.memory.put({ role: "user", content: message });
this.memory.put({ role: "user", content: userInput });

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

handleLLMInput = async (
Expand All @@ -163,7 +163,7 @@ export class FormFillingWorkflow extends Workflow<
const toolCallResponse = await chatWithTools(this.llm, tools, chatHistory);

if (!toolCallResponse.hasToolCall()) {
return new StopEvent(toolCallResponse.responseGenerator);
return new StopEvent(toolCallResponse.responseGenerator as any);
}

if (toolCallResponse.hasMultipleTools()) {
Expand All @@ -172,7 +172,7 @@ export class FormFillingWorkflow extends Workflow<
content:
"Calling different tools is not allowed. Please only use multiple calls of the same tool.",
});
return new InputEvent({ input: this.memory.getMessages() });
return new InputEvent({ input: await this.memory.getMessages() });
}

// Put the LLM tool call message into the memory
Expand Down Expand Up @@ -224,7 +224,7 @@ export class FormFillingWorkflow extends Workflow<
for (const toolMsg of toolMsgs) {
this.memory.put(toolMsg);
}
return new InputEvent({ input: this.memory.getMessages() });
return new InputEvent({ input: await this.memory.getMessages() });
};

handleFindAnswers = async (
Expand Down Expand Up @@ -252,7 +252,7 @@ export class FormFillingWorkflow extends Workflow<
for (const toolMsg of toolMsgs) {
this.memory.put(toolMsg);
}
return new InputEvent({ input: this.memory.getMessages() });
return new InputEvent({ input: await this.memory.getMessages() });
};

handleFillMissingCells = async (
Expand All @@ -270,6 +270,6 @@ export class FormFillingWorkflow extends Workflow<
for (const toolMsg of toolMsgs) {
this.memory.put(toolMsg);
}
return new InputEvent({ input: this.memory.getMessages() });
return new InputEvent({ input: await this.memory.getMessages() });
};
}
69 changes: 0 additions & 69 deletions templates/components/multiagent/typescript/workflow/stream.ts

This file was deleted.

2 changes: 1 addition & 1 deletion templates/types/streaming/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"duck-duck-scrape": "^2.2.5",
"express": "^4.18.2",
"llamaindex": "^0.9.1",
"@llamaindex/workflow": "^0.0.16",
"pdf2json": "^3.0.5",
"ajv": "^8.12.0",
"@e2b/code-interpreter": "^1.0.4",
Expand All @@ -37,7 +38,6 @@
"@types/express": "^4.17.21",
"@types/node": "^20.9.5",
"typescript-eslint": "^8.14.0",
"@llamaindex/workflow": "^0.0.3",
"@types/papaparse": "^5.3.15",
"concurrently": "^8.2.2",
"eslint": "^9.14.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def run(self, event: Any) -> Any:

def _is_retrieval_result_event(self, event: Any) -> bool:
if isinstance(event, ToolCallResult):
if event.tool_name == "query_index":
if event.tool_name == "query_engine":
return True
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_query_engine_tool(
description (optional): The description of the tool.
"""
if name is None:
name = "query_index"
name = "query_engine"
if description is None:
description = (
"Use this tool to retrieve information about the text corpus from an index."
Expand Down
Loading