|
| 1 | +import { extractFileAttachments } from "@llamaindex/server"; |
| 2 | +import { ChatMemoryBuffer, MessageContent, Settings } from "llamaindex"; |
| 3 | + |
| 4 | +import { |
| 5 | + agentStreamEvent, |
| 6 | + createStatefulMiddleware, |
| 7 | + createWorkflow, |
| 8 | + startAgentEvent, |
| 9 | + stopAgentEvent, |
| 10 | + workflowEvent, |
| 11 | +} from "@llamaindex/workflow"; |
| 12 | +import { Message } from "ai"; |
| 13 | +import { promises as fsPromises } from "node:fs"; |
| 14 | + |
| 15 | +const fileHelperEvent = workflowEvent<{ |
| 16 | + userInput: MessageContent; |
| 17 | + fileContent: string; |
| 18 | +}>(); |
| 19 | + |
| 20 | +/** |
| 21 | + * This is an simple workflow to demonstrate how to use uploaded files in the workflow. |
| 22 | + */ |
| 23 | +export function workflowFactory(reqBody: { messages: Message[] }) { |
| 24 | + const llm = Settings.llm; |
| 25 | + |
| 26 | + // First, extract the uploaded file from the messages |
| 27 | + const attachments = extractFileAttachments(reqBody.messages); |
| 28 | + |
| 29 | + if (attachments.length === 0) { |
| 30 | + throw new Error("Please upload a file to start"); |
| 31 | + } |
| 32 | + |
| 33 | + // Then, add the uploaded file info to the workflow state |
| 34 | + const { withState, getContext } = createStatefulMiddleware(() => { |
| 35 | + return { |
| 36 | + memory: new ChatMemoryBuffer({ llm }), |
| 37 | + uploadedFile: attachments[attachments.length - 1], |
| 38 | + }; |
| 39 | + }); |
| 40 | + const workflow = withState(createWorkflow()); |
| 41 | + |
| 42 | + // Handle the start of the workflow: read the file content |
| 43 | + workflow.handle([startAgentEvent], async ({ data }) => { |
| 44 | + const { userInput } = data; |
| 45 | + // Prepare chat history |
| 46 | + const { state } = getContext(); |
| 47 | + if (!userInput) { |
| 48 | + throw new Error("Missing user input to start the workflow"); |
| 49 | + } |
| 50 | + state.memory.put({ role: "user", content: userInput }); |
| 51 | + |
| 52 | + // Read file content |
| 53 | + const fileContent = await fsPromises.readFile( |
| 54 | + state.uploadedFile.path, |
| 55 | + "utf8", |
| 56 | + ); |
| 57 | + |
| 58 | + return fileHelperEvent.with({ |
| 59 | + userInput, |
| 60 | + fileContent, |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + // Use LLM to help the user with the file content |
| 65 | + workflow.handle([fileHelperEvent], async ({ data }) => { |
| 66 | + const { sendEvent } = getContext(); |
| 67 | + |
| 68 | + const prompt = ` |
| 69 | +You are a helpful assistant that can help the user with their file. |
| 70 | +
|
| 71 | +Here is the provided file content: |
| 72 | +${data.fileContent} |
| 73 | +
|
| 74 | +Now, let help the user with this request: |
| 75 | +${data.userInput} |
| 76 | +`; |
| 77 | + |
| 78 | + const response = await llm.complete({ |
| 79 | + prompt, |
| 80 | + stream: true, |
| 81 | + }); |
| 82 | + |
| 83 | + // Stream the response |
| 84 | + for await (const chunk of response) { |
| 85 | + sendEvent( |
| 86 | + agentStreamEvent.with({ |
| 87 | + delta: chunk.text, |
| 88 | + response: chunk.text, |
| 89 | + currentAgentName: "agent", |
| 90 | + raw: chunk.raw, |
| 91 | + }), |
| 92 | + ); |
| 93 | + } |
| 94 | + sendEvent(stopAgentEvent.with({ result: "" })); |
| 95 | + }); |
| 96 | + |
| 97 | + return workflow; |
| 98 | +} |
0 commit comments