forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: step wise agent + examples (run-llama#594)
- Loading branch information
1 parent
c744a99
commit 698cd9c
Showing
7 changed files
with
260 additions
and
5 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: step wise agent + examples |
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,95 @@ | ||
import { FunctionTool, OpenAIAgent } from "llamaindex"; | ||
|
||
// Define a function to sum two numbers | ||
function sumNumbers({ a, b }: { a: number; b: number }): number { | ||
return a + b; | ||
} | ||
|
||
// Define a function to divide two numbers | ||
function divideNumbers({ a, b }: { a: number; b: number }): number { | ||
return a / b; | ||
} | ||
|
||
// Define the parameters of the sum function as a JSON schema | ||
const sumJSON = { | ||
type: "object", | ||
properties: { | ||
a: { | ||
type: "number", | ||
description: "The first number", | ||
}, | ||
b: { | ||
type: "number", | ||
description: "The second number", | ||
}, | ||
}, | ||
required: ["a", "b"], | ||
}; | ||
|
||
const divideJSON = { | ||
type: "object", | ||
properties: { | ||
a: { | ||
type: "number", | ||
description: "The dividend a to divide", | ||
}, | ||
b: { | ||
type: "number", | ||
description: "The divisor b to divide by", | ||
}, | ||
}, | ||
required: ["a", "b"], | ||
}; | ||
|
||
async function main() { | ||
// Create a function tool from the sum function | ||
const functionTool = new FunctionTool(sumNumbers, { | ||
name: "sumNumbers", | ||
description: "Use this function to sum two numbers", | ||
parameters: sumJSON, | ||
}); | ||
|
||
// Create a function tool from the divide function | ||
const functionTool2 = new FunctionTool(divideNumbers, { | ||
name: "divideNumbers", | ||
description: "Use this function to divide two numbers", | ||
parameters: divideJSON, | ||
}); | ||
|
||
// Create an OpenAIAgent with the function tools | ||
const agent = new OpenAIAgent({ | ||
tools: [functionTool, functionTool2], | ||
verbose: true, | ||
}); | ||
|
||
// Create a task to sum and divide numbers | ||
const task = agent.createTask("How much is 5 + 5? then divide by 2"); | ||
|
||
let count = 0; | ||
|
||
while (true) { | ||
const stepOutput = await agent.runStep(task.taskId); | ||
|
||
console.log(`Runnning step ${count++}`); | ||
console.log(`======== OUTPUT ==========`); | ||
if (stepOutput.output.response) { | ||
console.log(stepOutput.output.response); | ||
} else { | ||
console.log(stepOutput.output.sources); | ||
} | ||
console.log(`==========================`); | ||
|
||
if (stepOutput.isLast) { | ||
const finalResponse = await agent.finalizeResponse( | ||
task.taskId, | ||
stepOutput, | ||
); | ||
console.log({ finalResponse }); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
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,64 @@ | ||
import { | ||
OpenAIAgent, | ||
QueryEngineTool, | ||
SimpleDirectoryReader, | ||
VectorStoreIndex, | ||
} from "llamaindex"; | ||
|
||
async function main() { | ||
// Load the documents | ||
const documents = await new SimpleDirectoryReader().loadData({ | ||
directoryPath: "node_modules/llamaindex/examples", | ||
}); | ||
|
||
// Create a vector index from the documents | ||
const vectorIndex = await VectorStoreIndex.fromDocuments(documents); | ||
|
||
// Create a query engine from the vector index | ||
const abramovQueryEngine = vectorIndex.asQueryEngine(); | ||
|
||
// Create a QueryEngineTool with the query engine | ||
const queryEngineTool = new QueryEngineTool({ | ||
queryEngine: abramovQueryEngine, | ||
metadata: { | ||
name: "abramov_query_engine", | ||
description: "A query engine for the Abramov documents", | ||
}, | ||
}); | ||
|
||
// Create an OpenAIAgent with the function tools | ||
const agent = new OpenAIAgent({ | ||
tools: [queryEngineTool], | ||
verbose: true, | ||
}); | ||
|
||
const task = agent.createTask("What was his salary?"); | ||
|
||
let count = 0; | ||
|
||
while (true) { | ||
const stepOutput = await agent.runStep(task.taskId); | ||
|
||
console.log(`Runnning step ${count++}`); | ||
console.log(`======== OUTPUT ==========`); | ||
if (stepOutput.output.response) { | ||
console.log(stepOutput.output.response); | ||
} else { | ||
console.log(stepOutput.output.sources); | ||
} | ||
console.log(`==========================`); | ||
|
||
if (stepOutput.isLast) { | ||
const finalResponse = await agent.finalizeResponse( | ||
task.taskId, | ||
stepOutput, | ||
); | ||
console.log({ finalResponse }); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
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,90 @@ | ||
import { FunctionTool, ReActAgent } from "llamaindex"; | ||
|
||
// Define a function to sum two numbers | ||
function sumNumbers({ a, b }: { a: number; b: number }): number { | ||
return a + b; | ||
} | ||
|
||
// Define a function to divide two numbers | ||
function divideNumbers({ a, b }: { a: number; b: number }): number { | ||
return a / b; | ||
} | ||
|
||
// Define the parameters of the sum function as a JSON schema | ||
const sumJSON = { | ||
type: "object", | ||
properties: { | ||
a: { | ||
type: "number", | ||
description: "The first number", | ||
}, | ||
b: { | ||
type: "number", | ||
description: "The second number", | ||
}, | ||
}, | ||
required: ["a", "b"], | ||
}; | ||
|
||
const divideJSON = { | ||
type: "object", | ||
properties: { | ||
a: { | ||
type: "number", | ||
description: "The dividend", | ||
}, | ||
b: { | ||
type: "number", | ||
description: "The divisor", | ||
}, | ||
}, | ||
required: ["a", "b"], | ||
}; | ||
|
||
async function main() { | ||
// Create a function tool from the sum function | ||
const functionTool = new FunctionTool(sumNumbers, { | ||
name: "sumNumbers", | ||
description: "Use this function to sum two numbers", | ||
parameters: sumJSON, | ||
}); | ||
|
||
// Create a function tool from the divide function | ||
const functionTool2 = new FunctionTool(divideNumbers, { | ||
name: "divideNumbers", | ||
description: "Use this function to divide two numbers", | ||
parameters: divideJSON, | ||
}); | ||
|
||
// Create an OpenAIAgent with the function tools | ||
const agent = new ReActAgent({ | ||
tools: [functionTool, functionTool2], | ||
verbose: true, | ||
}); | ||
|
||
const task = agent.createTask("Divide 16 by 2 then add 20"); | ||
|
||
let count = 0; | ||
|
||
while (true) { | ||
const stepOutput = await agent.runStep(task.taskId); | ||
|
||
console.log(`Runnning step ${count++}`); | ||
console.log(`======== OUTPUT ==========`); | ||
console.log(stepOutput.output); | ||
console.log(`==========================`); | ||
|
||
if (stepOutput.isLast) { | ||
const finalResponse = await agent.finalizeResponse( | ||
task.taskId, | ||
stepOutput, | ||
); | ||
console.log({ finalResponse }); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
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
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