-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: streaming for ReAct Agent (#798)
- Loading branch information
Showing
9 changed files
with
11,964 additions
and
8,334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"llamaindex": patch | ||
--- | ||
|
||
fix: streaming for ReAct Agent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ChatResponseChunk, ReActAgent } from "llamaindex"; | ||
import { ReadableStream } from "node:stream/web"; | ||
import { | ||
getCurrentIDTool, | ||
getUserInfoTool, | ||
getWeatherTool, | ||
} from "./utils/tools"; | ||
|
||
async function main() { | ||
// Create an OpenAIAgent with the function tools | ||
const agent = new ReActAgent({ | ||
tools: [getCurrentIDTool, getUserInfoTool, getWeatherTool], | ||
}); | ||
|
||
const task = await agent.createTask( | ||
"What is my current address weather based on my profile?", | ||
true, | ||
); | ||
|
||
for await (const stepOutput of task) { | ||
const stream = stepOutput.output as ReadableStream<ChatResponseChunk>; | ||
if (stepOutput.isLast) { | ||
for await (const chunk of stream) { | ||
process.stdout.write(chunk.delta); | ||
} | ||
process.stdout.write("\n"); | ||
} else { | ||
// handing function call | ||
console.log("handling function call..."); | ||
for await (const chunk of stream) { | ||
console.log("debug:", JSON.stringify(chunk.raw)); | ||
} | ||
} | ||
console.log("---"); | ||
} | ||
} | ||
|
||
void main().then(() => { | ||
console.log("Done"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { FunctionTool } from "llamaindex"; | ||
|
||
export const getCurrentIDTool = FunctionTool.from( | ||
() => { | ||
console.log("Getting user id..."); | ||
return crypto.randomUUID(); | ||
}, | ||
{ | ||
name: "get_user_id", | ||
description: "Get a random user id", | ||
}, | ||
); | ||
|
||
export const getUserInfoTool = FunctionTool.from( | ||
({ userId }: { userId: string }) => { | ||
console.log("Getting user info...", userId); | ||
return `Name: Alex; Address: 1234 Main St, CA; User ID: ${userId}`; | ||
}, | ||
{ | ||
name: "get_user_info", | ||
description: "Get user info", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
userId: { | ||
type: "string", | ||
description: "The user id", | ||
}, | ||
}, | ||
required: ["userId"], | ||
}, | ||
}, | ||
); | ||
|
||
export const getWeatherTool = FunctionTool.from( | ||
({ address }: { address: string }) => { | ||
console.log("Getting weather...", address); | ||
return `${address} is in a sunny location!`; | ||
}, | ||
{ | ||
name: "get_weather", | ||
description: "Get the current weather for a location", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
address: { | ||
type: "string", | ||
description: "The address", | ||
}, | ||
}, | ||
required: ["address"], | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.