Skip to content

Commit 40ee761

Browse files
feat: add asQueryTool to index and add factory methods for simplifying agent usage (#1715)
1 parent c14a21b commit 40ee761

File tree

26 files changed

+529
-385
lines changed

26 files changed

+529
-385
lines changed

.changeset/sour-rats-complain.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"llamaindex": patch
3+
"@llamaindex/workflow": patch
4+
"@llamaindex/core": patch
5+
---
6+
7+
Add factory methods agent and multiAgent to simplify agent usage

.changeset/wise-ghosts-play.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"llamaindex": patch
3+
---
4+
5+
feat: add asQueryTool to index

apps/next/src/app/(home)/page.tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,20 @@ const response = await agent.chat({
125125
description="Truly powerful retrieval-augmented generation applications use agentic techniques, and LlamaIndex.TS makes it easy to build them."
126126
>
127127
<CodeBlock
128-
code={`import { FunctionTool } from "llamaindex";
129-
import { OpenAIAgent } from "@llamaindex/openai";
128+
code={`import { agent } from "llamaindex";
129+
import { OpenAI } from "@llamaindex/openai";
130130
131-
const interpreterTool = FunctionTool.from(...);
132-
const systemPrompt = \`...\`;
131+
// using a previously created LlamaIndex index to query information from
132+
const queryTool = index.queryTool();
133133
134-
const agent = new OpenAIAgent({
135-
llm,
136-
tools: [interpreterTool],
137-
systemPrompt,
134+
const agent = agent({
135+
llm: new OpenAI({
136+
model: "gpt-4o",
137+
}),
138+
tools: [queryTool],
138139
});
139140
140-
await agent.chat('...');`}
141+
await agent.run('...');`}
141142
lang="ts"
142143
/>
143144
</Feature>

apps/next/src/content/docs/llamaindex/modules/agent_workflow.mdx

+10-28
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,7 @@ import { DynamicCodeBlock } from 'fumadocs-ui/components/dynamic-codeblock';
66
import CodeSource from "!raw-loader!../../../../../../../examples/agentworkflow/blog_writer.ts";
77
import { Tab, Tabs } from "fumadocs-ui/components/tabs";
88

9-
`AgentWorkflow` is a powerful system that enables you to create and orchestrate one or multiple agents with tools to perform specific tasks. It's built on top of the base `Workflow` system and provides a streamlined interface for agent interactions.
10-
11-
## Installation
12-
13-
You'll need to install the `@llamaindex/workflow` package:
14-
15-
<Tabs groupId="install" items={["npm", "yarn", "pnpm"]} persist>
16-
```shell tab="npm"
17-
npm install @llamaindex/workflow
18-
```
19-
20-
```shell tab="yarn"
21-
yarn add @llamaindex/workflow
22-
```
23-
24-
```shell tab="pnpm"
25-
pnpm add @llamaindex/workflow
26-
```
27-
</Tabs>
9+
Agent Workflows are a powerful system that enables you to create and orchestrate one or multiple agents with tools to perform specific tasks. It's built on top of the base `Workflow` system and provides a streamlined interface for agent interactions.
2810

2911
## Usage
3012

@@ -33,7 +15,7 @@ You'll need to install the `@llamaindex/workflow` package:
3315
The simplest use case is creating a single agent with specific tools. Here's an example of creating an assistant that tells jokes:
3416

3517
```typescript
36-
import { AgentWorkflow, FunctionTool } from "llamaindex";
18+
import { agent, FunctionTool } from "llamaindex";
3719
import { OpenAI } from "@llamaindex/openai";
3820

3921
// Define a joke-telling tool
@@ -45,8 +27,8 @@ const jokeTool = FunctionTool.from(
4527
}
4628
);
4729

48-
// Create an agent workflow with the tool
49-
const workflow = AgentWorkflow.fromTools({
30+
// Create an single agent workflow with the tool
31+
const workflow = agent({
5032
tools: [jokeTool],
5133
llm: new OpenAI({
5234
model: "gpt-4o-mini",
@@ -60,7 +42,7 @@ console.log(result); // Baby Llama is called cria
6042

6143
### Event Streaming
6244

63-
`AgentWorkflow` provides a unified interface for event streaming, making it easy to track and respond to different events during execution:
45+
Agent Workflows provide a unified interface for event streaming, making it easy to track and respond to different events during execution:
6446

6547
```typescript
6648
import { AgentToolCall, AgentStream } from "llamaindex";
@@ -81,7 +63,7 @@ for await (const event of context) {
8163

8264
### Multi-Agent Workflow
8365

84-
`AgentWorkflow` can orchestrate multiple agents, enabling complex interactions and task handoffs. Each agent in a multi-agent workflow requires:
66+
An Agent Workflow can orchestrate multiple agents, enabling complex interactions and task handoffs. Each agent in a multi-agent workflow requires:
8567

8668
- `name`: Unique identifier for the agent
8769
- `description`: Purpose description used for task routing
@@ -91,12 +73,12 @@ for await (const event of context) {
9173
Here's an example of a multi-agent system that combines joke-telling and weather information:
9274

9375
```typescript
94-
import { AgentWorkflow, FunctionAgent, FunctionTool } from "llamaindex";
76+
import { multiAgent, agent, FunctionTool } from "llamaindex";
9577
import { OpenAI } from "@llamaindex/openai";
9678
import { z } from "zod";
9779

9880
// Create a weather agent
99-
const weatherAgent = new FunctionAgent({
81+
const weatherAgent = agent({
10082
name: "WeatherAgent",
10183
description: "Provides weather information for any city",
10284
tools: [
@@ -115,7 +97,7 @@ const weatherAgent = new FunctionAgent({
11597
});
11698

11799
// Create a joke-telling agent
118-
const jokeAgent = new FunctionAgent({
100+
const jokeAgent = agent({
119101
name: "JokeAgent",
120102
description: "Tells jokes and funny stories",
121103
tools: [jokeTool], // Using the joke tool defined earlier
@@ -124,7 +106,7 @@ const jokeAgent = new FunctionAgent({
124106
});
125107

126108
// Create the multi-agent workflow
127-
const workflow = new AgentWorkflow({
109+
const workflow = multiAgent({
128110
agents: [jokeAgent, weatherAgent],
129111
rootAgent: jokeAgent, // Start with the joke agent
130112
});

e2e/examples/nextjs-node-runtime/src/actions/openai.ts

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
"use server";
22
import { HuggingFaceEmbedding } from "@llamaindex/huggingface";
33
import { SimpleDirectoryReader } from "@llamaindex/readers/directory";
4-
import {
5-
OpenAI,
6-
OpenAIAgent,
7-
QueryEngineTool,
8-
Settings,
9-
VectorStoreIndex,
10-
} from "llamaindex";
4+
import { OpenAI, OpenAIAgent, Settings, VectorStoreIndex } from "llamaindex";
115

126
Settings.llm = new OpenAI({
137
apiKey: process.env.NEXT_PUBLIC_OPENAI_KEY ?? "FAKE_KEY_TO_PASS_TESTS",
@@ -31,23 +25,20 @@ export async function getOpenAIModelRequest(query: string) {
3125
const reader = new SimpleDirectoryReader();
3226
const documents = await reader.loadData(currentDir);
3327
const index = await VectorStoreIndex.fromDocuments(documents);
34-
const retriever = index.asRetriever({
35-
similarityTopK: 10,
36-
});
37-
const queryEngine = index.asQueryEngine({
38-
retriever,
39-
});
4028

4129
// define the query engine as a tool
4230
const tools = [
43-
new QueryEngineTool({
44-
queryEngine: queryEngine,
31+
index.queryTool({
32+
options: {
33+
similarityTopK: 10,
34+
},
4535
metadata: {
4636
name: "deployment_details_per_env",
4737
description: `This tool can answer detailed questions about deployments happened in various environments.`,
4838
},
4939
}),
5040
];
41+
5142
// create the agent
5243
const agent = new OpenAIAgent({ tools });
5344

examples/agent/large_toolcall.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OpenAI } from "@llamaindex/openai";
2-
import { AgentWorkflow, FunctionTool } from "llamaindex";
2+
import { FunctionTool, agent } from "llamaindex";
33
import { z } from "zod";
44

55
const csvData =
@@ -33,7 +33,7 @@ const userQuestion = "which are the best comedies after 2010?";
3333
const systemPrompt =
3434
"You are a Python interpreter.\n - You are given tasks to complete and you run python code to solve them.\n - The python code runs in a Jupyter notebook. Every time you call $(interpreter) tool, the python code is executed in a separate cell. It's okay to make multiple calls to $(interpreter).\n - Display visualizations using matplotlib or any other visualization library directly in the notebook. Shouldn't save the visualizations to a file, just return the base64 encoded data.\n - You can install any pip package (if it exists) if you need to but the usual packages for data analysis are already preinstalled.\n - You can run any python code you want in a secure environment.";
3535

36-
const workflow = AgentWorkflow.fromTools({
36+
const workflow = agent({
3737
tools: [interpreterTool],
3838
llm,
3939
verbose: false,

examples/agent/openai.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OpenAI } from "@llamaindex/openai";
2-
import { AgentWorkflow, FunctionTool } from "llamaindex";
2+
import { FunctionTool, agent } from "llamaindex";
33
import { z } from "zod";
44

55
const sumNumbers = FunctionTool.from(
@@ -27,7 +27,7 @@ const divideNumbers = FunctionTool.from(
2727
);
2828

2929
async function main() {
30-
const workflow = AgentWorkflow.fromTools({
30+
const workflow = agent({
3131
tools: [sumNumbers, divideNumbers],
3232
llm: new OpenAI({ model: "gpt-4o-mini" }),
3333
verbose: false,

examples/agent/wiki.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { OpenAI } from "@llamaindex/openai";
2-
import { AgentStream, AgentWorkflow } from "llamaindex";
2+
import { AgentStream, agent } from "llamaindex";
33
import { WikipediaTool } from "../wiki";
44

55
async function main() {
66
const llm = new OpenAI({ model: "gpt-4-turbo" });
77
const wikiTool = new WikipediaTool();
88

9-
const workflow = AgentWorkflow.fromTools({
9+
const workflow = agent({
1010
tools: [wikiTool],
1111
llm,
1212
verbose: false,

examples/agentworkflow/blog_writer.ts renamed to examples/agentworkflow/blog-writer.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { OpenAI } from "@llamaindex/openai";
22
import fs from "fs";
33
import {
4+
agent,
45
AgentToolCall,
56
AgentToolCallResult,
6-
AgentWorkflow,
7-
FunctionAgent,
87
FunctionTool,
8+
multiAgent,
99
} from "llamaindex";
1010
import os from "os";
1111
import { z } from "zod";
@@ -34,7 +34,7 @@ const saveFileTool = FunctionTool.from(
3434
);
3535

3636
async function main() {
37-
const reportAgent = new FunctionAgent({
37+
const reportAgent = agent({
3838
name: "ReportAgent",
3939
description:
4040
"Responsible for crafting well-written blog posts based on research findings",
@@ -43,7 +43,7 @@ async function main() {
4343
llm,
4444
});
4545

46-
const researchAgent = new FunctionAgent({
46+
const researchAgent = agent({
4747
name: "ResearchAgent",
4848
description:
4949
"Responsible for gathering relevant information from the internet",
@@ -53,7 +53,7 @@ async function main() {
5353
llm,
5454
});
5555

56-
const workflow = new AgentWorkflow({
56+
const workflow = multiAgent({
5757
agents: [researchAgent, reportAgent],
5858
rootAgent: researchAgent,
5959
});

examples/agentworkflow/multiple_agents.ts renamed to examples/agentworkflow/multiple-agents.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
import { OpenAI } from "@llamaindex/openai";
77
import {
8+
agent,
89
AgentInput,
910
AgentOutput,
1011
AgentStream,
1112
AgentToolCall,
1213
AgentToolCallResult,
13-
AgentWorkflow,
14-
FunctionAgent,
1514
FunctionTool,
15+
multiAgent,
1616
StopEvent,
1717
} from "llamaindex";
1818
import { z } from "zod";
@@ -55,15 +55,15 @@ const temperatureFetcherTool = FunctionTool.from(
5555

5656
// Create agents
5757
async function multiWeatherAgent() {
58-
const converterAgent = new FunctionAgent({
58+
const converterAgent = agent({
5959
name: "TemperatureConverterAgent",
6060
description:
6161
"An agent that can convert temperatures from Fahrenheit to Celsius.",
6262
tools: [temperatureConverterTool],
6363
llm,
6464
});
6565

66-
const weatherAgent = new FunctionAgent({
66+
const weatherAgent = agent({
6767
name: "FetchWeatherAgent",
6868
description: "An agent that can get the weather in a city. ",
6969
systemPrompt:
@@ -76,7 +76,7 @@ async function multiWeatherAgent() {
7676
});
7777

7878
// Create agent workflow with the agents
79-
const workflow = new AgentWorkflow({
79+
const workflow = multiAgent({
8080
agents: [weatherAgent, converterAgent],
8181
rootAgent: weatherAgent,
8282
verbose: false,

examples/agentworkflow/single_agent.ts renamed to examples/agentworkflow/single-agent.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
* This example shows how to use AgentWorkflow as a single agent with tools
33
*/
44
import { OpenAI } from "@llamaindex/openai";
5-
import { AgentWorkflow, Settings } from "llamaindex";
5+
import { Settings, agent } from "llamaindex";
66
import { getWeatherTool } from "../agent/utils/tools";
77

8-
const llm = new OpenAI({
8+
Settings.llm = new OpenAI({
99
model: "gpt-4o",
1010
});
1111

12-
Settings.llm = llm;
13-
1412
async function singleWeatherAgent() {
15-
const workflow = AgentWorkflow.fromTools({
13+
const workflow = agent({
1614
tools: [getWeatherTool],
1715
verbose: false,
1816
});

examples/agentworkflow/with-anthropic.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from "fs";
22
import {
3+
agent,
34
AgentToolCall,
45
AgentToolCallResult,
5-
AgentWorkflow,
6-
FunctionAgent,
76
FunctionTool,
7+
multiAgent,
88
} from "llamaindex";
99
import { z } from "zod";
1010

@@ -63,7 +63,7 @@ const saveFileTool = FunctionTool.from(
6363
);
6464

6565
async function main() {
66-
const reportAgent = new FunctionAgent({
66+
const reportAgent = agent({
6767
name: "ReportAgent",
6868
description:
6969
"Responsible for creating concise reports about weather and inflation data",
@@ -72,7 +72,7 @@ async function main() {
7272
llm,
7373
});
7474

75-
const researchAgent = new FunctionAgent({
75+
const researchAgent = agent({
7676
name: "ResearchAgent",
7777
description:
7878
"Responsible for gathering relevant information from the internet",
@@ -82,7 +82,7 @@ async function main() {
8282
llm,
8383
});
8484

85-
const workflow = new AgentWorkflow({
85+
const workflow = multiAgent({
8686
agents: [researchAgent, reportAgent],
8787
rootAgent: researchAgent,
8888
});

packages/core/src/utils/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ export {
8080
extractText,
8181
imageToDataUrl,
8282
messagesToHistory,
83-
MockLLM,
8483
toToolDescriptions,
8584
} from "./llms";
8685

86+
export { MockLLM } from "./mock";
87+
8788
export { objectEntries } from "./object-entries";

0 commit comments

Comments
 (0)