Compare commits

..

1 Commits

Author SHA1 Message Date
Alex Yang 6dde924d0a chore: bump bunchee 2025-03-09 01:19:33 -08:00
197 changed files with 947 additions and 2676 deletions
+13 -46
View File
@@ -14,14 +14,13 @@ There are some important folders in the repository:
all JS runtime environments.
- `env`: The environment package of LlamaIndex.TS, which contains the environment-specific classes and interfaces. It
includes compatibility layers for Node.js, Deno, Vercel Edge Runtime, Cloudflare Workers...
- `providers/*`: The providers package of LlamaIndex.TS, which contains the providers for LLM and other services.
- `apps/*`: The applications based on LlamaIndex.TS.
- `next`: Our documentation website based on Next.js.
- `examples`: The code examples of LlamaIndex.TS using Node.js.
## Getting Started
Make sure you have Node.js LTS (Long-term Support) installed. You can check your Node.js version by running:
Make sure you have Node.js LIS (Long-term Support) installed. You can check your Node.js version by running:
```shell
node -v
@@ -31,7 +30,7 @@ node -v
### Use pnpm
```shell
npm install -g pnpm
corepack enable
```
### Install dependencies
@@ -42,65 +41,33 @@ pnpm install
### Build the packages
You'll need Turbo to build the packages. If you don't have it, you can run it with `pnpx`.
To build all packages, run:
```shell
pnpm build
# Build all packages
pnpx turbo build --filter "./packages/*"
# Or if you have turbo installed, you can run:
turbo build --filter "./packages/*"
```
### Run tests
#### Unit tests
After build, to run all unit tests, call:
```shell
pnpm test
```
Unit tests are located in the `tests` folder of each package. They are using their own package (e.g. `@llamaindex/core-tests` for `@llamaindex/core`). The tests are importing the package under test and the test package is not published.
#### E2E tests
To run all E2E tests, call:
```shell
pnpm e2e
```
All E2E tests are in the `e2e` folder.
### Docs
See the [docs](./apps/next/README.md) for more information.
## Adding a new package
Please follow these steps to add a new package:
1. Only add new packages to the `packages/providers` folder.
2. Use the `package.json` and `tsconfig.json` of an existing packages as template.
3. Reference your new package in the root `tsconfig.json` file
4. Add your package to the `examples/package.json` file if you add a new example.
## Before sending a PR
Before sending a PR, make sure of the following:
1. Tests are all running and you added meaningful tests for your change.
2. If you have a new feature, document it in the `apps/next` docs folder.
3. If you have a new feature, add a new example in the `examples` folder.
4. You have a descriptive changeset for each PR:
### Changesets
## Changeset
We use [changesets](https://github.com/changesets/changesets) for managing versions and changelogs. To create a new
changeset, run in the root folder:
```shell
```
pnpm changeset
```
Please send a descriptive changeset for each PR.
## Publishing (maintainers only)
The [Release Github Action](.github/workflows/release.yml) is automatically generating and updating a
-28
View File
@@ -1,33 +1,5 @@
# @llamaindex/doc
## 0.1.11
### Patch Changes
- a8c0637: feat: simplify to provide base URL to OpenAI
- a654f58: Added docs for using perplexity
- 98eebf7: Add RequestOptions parameter passing to support Gemini proxy calls.
Add a usage example for the RequestOptions parameter.
- Updated dependencies [a8c0637]
- @llamaindex/openai@0.1.61
- llamaindex@0.9.11
## 0.1.10
### Patch Changes
- Updated dependencies [aea550a]
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- @llamaindex/openai@0.1.60
- llamaindex@0.9.10
- @llamaindex/workflow@0.0.16
- @llamaindex/core@0.5.8
- @llamaindex/cloud@3.0.9
- @llamaindex/node-parser@1.0.8
- @llamaindex/readers@2.0.8
## 0.1.9
### Patch Changes
+2 -1
View File
@@ -6,7 +6,8 @@ This is a Next.js application generated with
Run development server:
```bash
pnpm run dev
turbo run dev
# turbo will build all required packages before running the dev server
```
## Learn More
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/doc",
"version": "0.1.11",
"version": "0.1.9",
"private": true,
"scripts": {
"postinstall": "fumadocs-mdx",
+1 -5
View File
@@ -4,8 +4,4 @@ import { updateLlamaCloud } from "./update-llamacloud.mjs";
env.loadEnvConfig(process.cwd());
if (process.env.VERCEL_ENV === "production") {
updateLlamaCloud().catch((error) => {
console.error(error);
});
}
await updateLlamaCloud();
+39 -44
View File
@@ -60,7 +60,7 @@ export default function HomePage() {
icon={Footprints}
subheading="Progressive"
heading="From the simplest to the most complex"
description="LlamaIndex.TS is designed to be simple to get started, but powerful enough to build complex, agentic AI applications using multi-agents."
description="LlamaIndex.TS is designed to be simple to get started, but powerful enough to build complex, agentic AI applications."
>
<Suspense
fallback={
@@ -76,48 +76,44 @@ export default function HomePage() {
>
<MagicMove
code={[
`import { openai } from "@llamaindex/openai";
`import { OpenAI } from "@llamaindex/openai";
const llm = openai();
const llm = new OpenAI();
const response = await llm.complete({ prompt: "How are you?" });`,
`import { openai } from "@llamaindex/openai";
`import { OpenAI } from "@llamaindex/openai";
const llm = openai();
const llm = new OpenAI();
const response = await llm.chat({
messages: [{ content: "Tell me a joke.", role: "user" }],
});`,
`import { agent } from "llamaindex";
import { openai } from "@llamaindex/openai";
`import { ChatMemoryBuffer } from "llamaindex";
import { OpenAI } from "@llamaindex/openai";
const analyseAgent = agent({
llm: openai({ model: "gpt-4o" }),
tools: [analyseTools],
const llm = new OpenAI({ model: 'gpt4o-turbo' });
const buffer = new ChatMemoryBuffer({
tokenLimit: 128_000,
})
buffer.put({ content: "Tell me a joke.", role: "user" })
const response = await llm.chat({
messages: buffer.getMessages(),
stream: true
});`,
`import { ChatMemoryBuffer } from "llamaindex";
import { OpenAIAgent } from "@llamaindex/openai";
const agent = new OpenAIAgent({
llm,
tools: [...myTools]
systemPrompt,
});
const response = await analyseAgent.run(\`Analyse the given data:
\${data}\`);`,
`import { agent, multiAgent } from "llamaindex";
import { openai } from "@llamaindex/openai";
const analyseAgent = agent({
name: "AnalyseAgent",
llm: openai({ model: "gpt-4o" }),
tools: [analyseTools],
});
const reporterAgent = agent({
name: "ReporterAgent",
llm: openai({ model: "gpt-4o" }),
tools: [reporterTools],
canHandoffTo: [analyseAgent],
});
const agents = multiAgent({
agents: [analyseAgent, reporterAgent],
rootAgent: reporterAgent,
});
const response = await agents.run(\`Analyse the given data:
\${data}\`);`,
const buffer = new ChatMemoryBuffer({
tokenLimit: 128_000,
})
buffer.put({ content: "Analysis the data based on the given data.", role: "user" })
buffer.put({ content: \`\${data}\`, role: "user" })
const response = await agent.chat({
message: buffer.getMessages(),
});`,
]}
/>
</Suspense>
@@ -129,20 +125,19 @@ const response = await agents.run(\`Analyse the given data:
description="Truly powerful retrieval-augmented generation applications use agentic techniques, and LlamaIndex.TS makes it easy to build them."
>
<CodeBlock
code={`import { agent, SimpleDirectoryReader, VectorStoreIndex } from "llamaindex";
import { openai } from "@llamaindex/openai";
code={`import { FunctionTool } from "llamaindex";
import { OpenAIAgent } from "@llamaindex/openai";
// load documents from current directoy into an index
const reader = new SimpleDirectoryReader();
const documents = await reader.loadData(currentDir);
const index = await VectorStoreIndex.fromDocuments(documents);
const interpreterTool = FunctionTool.from(...);
const systemPrompt = \`...\`;
const myAgent = agent({
llm: openai({ model: "gpt-4o" }),
tools: [index.queryTool()],
const agent = new OpenAIAgent({
llm,
tools: [interpreterTool],
systemPrompt,
});
await myAgent.run('...');`}
await agent.chat('...');`}
lang="ts"
/>
</Feature>
@@ -7,7 +7,7 @@ import CodeSource from "!raw-loader!../../../../../../../examples/mistral";
By default LlamaIndex.TS uses OpenAI's LLMs and embedding models, but we support [lots of other LLMs](../modules/llms) including models from Mistral (Mistral, Mixtral), Anthropic (Claude) and Google (Gemini).
If you don't want to use an API at all you can [run a local model](./local_llm).
If you don't want to use an API at all you can [run a local model](../../examples/local_llm).
This example runs you through the process of setting up a Mistral model:
@@ -106,38 +106,21 @@ Some modules uses `Web Stream` API like `ReadableStream` and `WritableStream`, y
}
```
```typescript
import { agent, tool } from 'llamaindex'
import { openai } from "@llamaindex/openai";
```ts twoslash
import { OpenAIAgent } from '@llamaindex/openai'
Settings.llm = openai({
model: "gpt-4o-mini",
});
const agent = new OpenAIAgent({
tools: []
})
const addTool = tool({
name: "add",
description: "Adds two numbers",
parameters: z.object({x: z.number(), y: z.number()}),
execute: ({ x, y }) => x + y,
});
const myAgent = agent({
tools: [addTool],
});
// Chat with the agent
const context = myAgent.run("Hello, how are you?");
for await (const event of context) {
if (event instanceof AgentStream) {
for (const chunk of event.data.delta) {
process.stdout.write(chunk); // stream response
}
} else {
console.log(event); // other events
}
const response = await agent.chat({
message: 'Hello, how are you?',
stream: true
})
for await (const _ of response) {
//^?
// ...
}
```
## Run TypeScript Script in Node.js
@@ -25,21 +25,15 @@ npx tsx example.ts
First we'll need to pull in our dependencies. These are:
- The OpenAI class to use the OpenAI LLM
- tool to provide tools to our agent
- agent to create the single agent
- FunctionTool to provide tools to our agent
- OpenAIAgent to create the agent itself
- Settings to define some global settings for the library
- Dotenv to load our API key from the .env file
- Zod to define the schema for our tool
```javascript
import { FunctionTool, Settings } from "llamaindex";
import { OpenAI, OpenAIAgent } from "@llamaindex/openai";
import "dotenv/config";
import {
agent,
AgentStream,
tool,
openai,
Settings,
} from "llamaindex";
import { z } from "zod";
```
@@ -48,12 +42,25 @@ import { z } from "zod";
We need to tell our OpenAI class where its API key is, and which of OpenAI's models to use. We'll be using `gpt-4o`, which is capable while still being pretty cheap. This is a global setting, so anywhere an LLM is needed will use the same model.
```javascript
Settings.llm = openai({
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4o",
});
```
### Turn on logging
We want to see what our agent is up to, so we're going to hook into some events that the library generates and print them out. There are several events possible, but we'll specifically tune in to `llm-tool-call` (when a tool is called) and `llm-tool-result` (when it responds).
```javascript
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail);
});
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail);
});
```
### Create a function
We're going to create a very simple function that adds two numbers together. This will be the tool we ask our agent to use.
@@ -68,7 +75,7 @@ Note that we're passing in an object with two named parameters, `a` and `b`. Thi
### Turn the function into a tool for the agent
This is the most complicated part of creating an agent. We need to define a `tool`. We have to pass in:
This is the most complicated part of creating an agent. We need to define a `FunctionTool`. We have to pass in:
- The function itself (`sumNumbers`)
- A name for the function, which the LLM will use to call it
@@ -77,7 +84,7 @@ This is the most complicated part of creating an agent. We need to define a `too
- You can see [more examples of function schemas](https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models).
```javascript
const addTool = tool({
const tool = FunctionTool.from(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: z.object({
@@ -88,14 +95,13 @@ const addTool = tool({
description: "Second number to sum",
}),
}),
execute: sumNumbers,
});
```
We then wrap up the tools into an array. We could provide lots of tools this way, but for this example we're just using the one.
```javascript
const tools = [addTool];
const tools = [tool];
```
### Create the agent
@@ -103,7 +109,7 @@ const tools = [addTool];
With your LLM already set up and your tools defined, creating an agent is simple:
```javascript
const myAgent = agent({ tools });
const agent = new OpenAIAgent({ tools });
```
### Ask the agent a question
@@ -111,109 +117,61 @@ const myAgent = agent({ tools });
We can use the `chat` interface to ask our agent a question, and it will use the tools we've defined to find an answer.
```javascript
const context = myAgent.run("Sum 101 and 303");
const result = await context;
console.log(result.data);
```
You will see the following output:
let response = await agent.chat({
message: "Add 101 and 303",
});
**_Output_**
```
{ result: 'The sum of 101 and 303 is 404.' }
```
To stream the response, you can use the `AgentStream` event which provides chunks of the response as they become available. This allows you to display the response incrementally rather than waiting for the full response:
```javascript
const context = myAgent.run("Add 101 and 303");
for await (const event of context) {
if (event instanceof AgentStream) {
process.stdout.write(event.data.delta);
}
}
```
**_Streaming Output_**
```
The sum of 101 and 303 is 404.
```
### Logging workflow events
To log the workflow events, you can check the event type and log the event data.
```javascript
const context = myAgent.run("Sum 202 and 404");
for await (const event of context) {
if (event instanceof AgentStream) {
// Stream the response
for (const chunk of event.data.delta) {
process.stdout.write(chunk);
}
} else {
// Log other events
console.log("\nWorkflow event:", JSON.stringify(event, null, 2));
}
}
console.log(response);
```
Let's see what running this looks like using `npx tsx agent.ts`
**_Output_**
```
Workflow event: {
"data": {
"userInput": "Sum 202 and 404"
```javascript
{
toolCall: {
id: 'call_ze6A8C3mOUBG4zmXO8Z4CPB5',
name: 'sumNumbers',
input: { a: 101, b: 303 }
},
"displayName": "StartEvent"
toolResult: {
tool: FunctionTool { _fn: [Function: sumNumbers], _metadata: [Object] },
input: { a: 101, b: 303 },
output: '404',
isError: false
}
}
Workflow event: {
"data": {
"input": [
{
"role": "user",
"content": "Sum 202 and 404"
}
],
"currentAgentName": "Agent"
},
"displayName": "AgentInput"
}
Workflow event: {
"data": {
"input": [
{
"role": "system",
"content": "You are a helpful assistant. Use the provided tools to answer questions."
},
{
"role": "user",
"content": "Sum 202 and 404"
}
],
"currentAgentName": "Agent"
},
"displayName": "AgentSetup"
}
....
```
We're seeing several workflow events being logged:
```javascript
{
response: {
raw: {
id: 'chatcmpl-9KwauZku3QOvH78MNvxJs81mDvQYK',
object: 'chat.completion',
created: 1714778824,
model: 'gpt-4-turbo-2024-04-09',
choices: [Array],
usage: [Object],
system_fingerprint: 'fp_ea6eb70039'
},
message: {
content: 'The sum of 101 and 303 is 404.',
role: 'assistant',
options: {}
}
},
sources: [Getter]
}
```
1. `AgentToolCall` - Shows the agent preparing to call our tool with the numbers 202 and 404
2. `AgentToolCallResult` - Shows the result of calling the tool, which returned "606"
3. `AgentInput` - Shows the original user input
4. `AgentOutput` - Shows the agent's response
We're seeing two pieces of output here. The first is our callback firing when the tool is called. You can see in `toolResult` that the LLM has correctly passed `101` and `303` to our `sumNumbers` function, which adds them up and returns `404`.
Great! We've built an agent that can understand requests and use tools to fulfill them. Next you can:
The second piece of output is the response from the LLM itself, where the `message.content` key is giving us the answer.
- [See the full code](https://github.com/run-llama/LlamaIndexTS/blob/main/examples/agentworkflow/blog-writer.ts)
Great! We've built an agent with tool use! Next you can:
- [See the full code](https://github.com/run-llama/ts-agents/blob/main/1_agent/agent.ts)
- [Switch to a local LLM](3_local_model)
- Move on to [add Retrieval-Augmented Generation to your agent](4_agentic_rag)
@@ -23,27 +23,70 @@ The first time you run it will also automatically download and install the model
There are two changes you need to make to the code we already wrote in `1_agent` to get Mixtral 8x7b to work. First, you need to switch to that model. Replace the call to `Settings.llm` with this:
```javascript
Settings.llm = ollama({
Settings.llm = new Ollama({
model: "mixtral:8x7b",
});
```
### Run local agent
### Swap to a ReActAgent
You can also create local agent by importing `agent` from `llamaindex`.
In our original code we used a specific OpenAIAgent, so we'll need to switch to a more generic agent pattern, the ReAct pattern. This is simple: change the `const agent` line in your code to read
```javascript
import { agent } from "llamaindex";
const workflow = agent({
tools: [getWeatherTool],
});
const workflowContext = workflow.run(
"What's the weather like in San Francisco?",
);
const agent = new ReActAgent({ tools });
```
(You will also need to bring in `Ollama` and `ReActAgent` in your imports)
### Run your totally local agent
Because your embeddings were already local, your agent can now run entirely locally without making any API calls.
```bash
node agent.mjs
```
Note that your model will probably run a lot slower than OpenAI, so be prepared to wait a while!
**_Output_**
```javascript
{
response: {
message: {
role: 'assistant',
content: ' Thought: I need to use a tool to add the numbers 101 and 303.\n' +
'Action: sumNumbers\n' +
'Action Input: {"a": 101, "b": 303}\n' +
'\n' +
'Observation: 404\n' +
'\n' +
'Thought: I can answer without using any more tools.\n' +
'Answer: The sum of 101 and 303 is 404.'
},
raw: {
model: 'mixtral:8x7b',
created_at: '2024-05-09T00:24:30.339473Z',
message: [Object],
done: true,
total_duration: 64678371209,
load_duration: 57394551334,
prompt_eval_count: 475,
prompt_eval_duration: 4163981000,
eval_count: 94,
eval_duration: 3116692000
}
},
sources: [Getter]
}
```
Tada! You can see all of this in the folder `1a_mixtral`.
### Extending to other examples
You can use a ReActAgent instead of an OpenAIAgent in any of the further examples below, but keep in mind that GPT-4 is a lot more capable than Mixtral 8x7b, so you may see more errors or failures in reasoning if you are using an entirely local setup.
### Next steps
Now you've got a local agent, you can [add Retrieval-Augmented Generation to your agent](4_agentic_rag).
@@ -37,7 +37,7 @@ import { Tab, Tabs } from "fumadocs-ui/components/tabs";
We'll be bringing in `SimpleDirectoryReader`, `HuggingFaceEmbedding`, `VectorStoreIndex`, and `QueryEngineTool`, `OpenAIContextAwareAgent` from LlamaIndex.TS, as well as the dependencies we previously used.
```javascript
import { QueryEngineTool, Settings, VectorStoreIndex } from "llamaindex";
import { FunctionTool, QueryEngineTool, Settings, VectorStoreIndex } from "llamaindex";
import { OpenAI, OpenAIAgent } from "@llamaindex/openai";
import { HuggingFaceEmbedding } from "@llamaindex/huggingface";
import { SimpleDirectoryReader } from "@llamaindex/readers/directory";
@@ -115,8 +115,10 @@ The total budget for the City and County of San Francisco for the fiscal year 20
If you prefer more flexibility and don't mind additional complexity, you can create a `QueryEngineTool`. This approach allows you to define the query logic, providing a more tailored way to interact with the data, but note that it introduces a delay due to the extra tool call.
```javascript
const queryEngine = await index.asQueryEngine({ retriever });
const tools = [
index.queryTool({
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "san_francisco_budget_tool",
description: `This tool can answer detailed questions about the individual components of the budget of San Francisco in 2023-2024.`,
@@ -125,9 +127,11 @@ const tools = [
];
// Create an agent using the tools array
const myAgent = agent({ tools });
const agent = new OpenAIAgent({ tools });
let toolResponse = await myAgent.run("What's the budget of San Francisco in 2023-2024?");
let toolResponse = await agent.chat({
message: "What's the budget of San Francisco in 2023-2024?",
});
console.log(toolResponse);
```
@@ -7,13 +7,14 @@ In [our third iteration of the agent](https://github.com/run-llama/ts-agents/blo
```javascript
// define the query engine as a tool
const tools = [
index.queryTool({
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "san_francisco_budget_tool",
description: `This tool can answer detailed questions about the individual components of the budget of San Francisco in 2023-2024.`,
},
}),
tool({
FunctionTool.from(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: z.object({
@@ -24,15 +25,14 @@ const tools = [
description: "Second number to sum",
}),
}),
execute: ({ a, b }) => `${a + b}`,
}),
];
```
You can also use JSON Schema to define the tool parameters as an alternative to Zod.
You can also use JSON Schema to define the tool parameters as an alternative to Zod.
```javascript
tool(sumNumbers, {
FunctionTool.from(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
@@ -56,13 +56,22 @@ tool(sumNumbers, {
These tool descriptions are identical to the ones we previously defined. Now let's ask it 3 questions in a row:
```javascript
let response = await agent.run("What's the budget of San Francisco for community health in 2023-24?");
let response = await agent.chat({
message:
"What's the budget of San Francisco for community health in 2023-24?",
});
console.log(response);
let response2 = await agent.run("What's the budget of San Francisco for public protection in 2023-24?");
let response2 = await agent.chat({
message:
"What's the budget of San Francisco for public protection in 2023-24?",
});
console.log(response2);
let response3 = await agent.run("What's the combined budget of San Francisco for community health and public protection in 2023-24?");
let response3 = await agent.chat({
message:
"What's the combined budget of San Francisco for community health and public protection in 2023-24?",
});
console.log(response3);
```
@@ -2,8 +2,29 @@
title: Agent Workflow
---
import { DynamicCodeBlock } from 'fumadocs-ui/components/dynamic-codeblock';
import CodeSource from "!raw-loader!../../../../../../../examples/agentworkflow/blog_writer.ts";
import { Tab, Tabs } from "fumadocs-ui/components/tabs";
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.
`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.
## Installation
You'll need to install the `@llamaindex/workflow` package:
<Tabs groupId="install" items={["npm", "yarn", "pnpm"]} persist>
```shell tab="npm"
npm install @llamaindex/workflow
```
```shell tab="yarn"
yarn add @llamaindex/workflow
```
```shell tab="pnpm"
pnpm add @llamaindex/workflow
```
</Tabs>
## Usage
@@ -12,11 +33,11 @@ Agent Workflows are a powerful system that enables you to create and orchestrate
The simplest use case is creating a single agent with specific tools. Here's an example of creating an assistant that tells jokes:
```typescript
import { agent, tool } from "llamaindex";
import { openai } from "@llamaindex/openai";
import { AgentWorkflow, FunctionTool } from "llamaindex";
import { OpenAI } from "@llamaindex/openai";
// Define a joke-telling tool
const jokeTool = tool(
const jokeTool = FunctionTool.from(
() => "Baby Llama is called cria",
{
name: "joke",
@@ -24,20 +45,22 @@ const jokeTool = tool(
}
);
// Create an single agent workflow with the tool
const jokeAgent = agent({
// Create an agent workflow with the tool
const workflow = AgentWorkflow.fromTools({
tools: [jokeTool],
llm: openai({ model: "gpt-4o-mini" }),
llm: new OpenAI({
model: "gpt-4o-mini",
}),
});
// Run the workflow
const result = await jokeAgent.run("Tell me something funny");
const result = await workflow.run("Tell me something funny");
console.log(result); // Baby Llama is called cria
```
### Event Streaming
Agent Workflows provide a unified interface for event streaming, making it easy to track and respond to different events during execution:
`AgentWorkflow` provides a unified interface for event streaming, making it easy to track and respond to different events during execution:
```typescript
import { AgentToolCall, AgentStream } from "llamaindex";
@@ -58,7 +81,7 @@ for await (const event of context) {
### Multi-Agent Workflow
An Agent Workflow can orchestrate multiple agents, enabling complex interactions and task handoffs. Each agent in a multi-agent workflow requires:
`AgentWorkflow` can orchestrate multiple agents, enabling complex interactions and task handoffs. Each agent in a multi-agent workflow requires:
- `name`: Unique identifier for the agent
- `description`: Purpose description used for task routing
@@ -68,46 +91,46 @@ An Agent Workflow can orchestrate multiple agents, enabling complex interactions
Here's an example of a multi-agent system that combines joke-telling and weather information:
```typescript
import { multiAgent, agent, tool } from "llamaindex";
import { openai } from "@llamaindex/openai";
import { AgentWorkflow, FunctionAgent, FunctionTool } from "llamaindex";
import { OpenAI } from "@llamaindex/openai";
import { z } from "zod";
// Create a weather agent
const weatherAgent = agent({
const weatherAgent = new FunctionAgent({
name: "WeatherAgent",
description: "Provides weather information for any city",
tools: [
tool(
FunctionTool.from(
({ city }: { city: string }) => `The weather in ${city} is sunny`,
{
name: "fetchWeather",
description: "Get weather information for a city",
parameters: z.object({
city: z.string(),
}),
execute: ({ city }) => `The weather in ${city} is sunny`,
}
),
],
llm: openai({ model: "gpt-4o-mini" }),
llm: new OpenAI({ model: "gpt-4o-mini" }),
});
// Create a joke-telling agent
const jokeAgent = agent({
const jokeAgent = new FunctionAgent({
name: "JokeAgent",
description: "Tells jokes and funny stories",
tools: [jokeTool], // Using the joke tool defined earlier
llm: openai({ model: "gpt-4o-mini" }),
llm: new OpenAI({ model: "gpt-4o-mini" }),
canHandoffTo: [weatherAgent], // Can hand off to the weather agent
});
// Create the multi-agent workflow
const agents = multiAgent({
const workflow = new AgentWorkflow({
agents: [jokeAgent, weatherAgent],
rootAgent: jokeAgent, // Start with the joke agent
});
// Run the workflow
const result = await agents.run(
const result = await workflow.run(
"Give me a morning greeting with a joke and the weather in San Francisco"
);
```
@@ -31,20 +31,6 @@ Settings.llm = new Gemini({
});
```
## Usage with Proxy
```ts
import { Gemini, GEMINI_MODEL } from "@llamaindex/google";
import { Settings } from "llamaindex";
Settings.llm = new Gemini({
model: GEMINI_MODEL.GEMINI_PRO,
requestOptions: {
baseUrl: <YOUR_PROXY_URL> // optional, but useful for custom endpoints
}
});
```
### Usage with Vertex AI
To use Gemini via Vertex AI you can use `GeminiVertexSession`.
@@ -34,18 +34,6 @@ You can setup the apiKey on the environment variables, like:
export OPENAI_API_KEY="<YOUR_API_KEY>"
```
You can optionally set a custom base URL, like:
```bash
export OPENAI_BASE_URL="https://api.scaleway.ai/v1"
```
or
```ts
Settings.llm = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0, apiKey: <YOUR_API_KEY>, baseURL: "https://api.scaleway.ai/v1" });
```
## Load and index documents
For this example, we will use a single document. In a real-world scenario, you would have multiple documents to index.
@@ -1,116 +0,0 @@
---
title: Perplexity LLM
---
## Usage
```ts
import { Settings } from "llamaindex";
import { perplexity } from "@llamaindex/perplexity";
Settings.llm = perplexity({
apiKey: "<YOUR_API_KEY>",
model: "sonar", // or available models
});
```
## Example
```ts
import { perplexity } from "@llamaindex/perplexity";
const perplexityLlm = perplexity({
apiKey: "<YOUR_API_KEY>",
model: "sonar", // or avaiable models
});
async function main() {
const response = await perplexityLlm.chat({
messages: [
{
role: "system",
content: "You are an AI assistant",
},
{
role: "user",
content: "Tell me about San Francisco",
},
],
stream: false,
});
console.log(response);
const stream = await perplexityLlm.chat({
messages: [
{
role: "system",
content: "You are a creative AI assistant that tells engaging stories",
},
{
role: "user",
content: "Tell me a short story",
},
],
stream: true,
});
console.log("\nStreaming response:");
for await (const chunk of stream) {
process.stdout.write(chunk.delta);
}
}
```
## Full Example
```ts
import { perplexity } from "@llamaindex/perplexity";
import { Document, Settings, VectorStoreIndex } from "llamaindex";
// Use the perplexity LLM
Settings.llm = perplexity({ model: "sonar", apiKey: "<YOUR_API_KEY>" });
async function main() {
const document = new Document({ text: essay, id_: "essay" });
// Load and index documents
const index = await VectorStoreIndex.fromDocuments([document]);
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
});
const query = "What is the meaning of life?";
// Query
const response = await queryEngine.query({
query,
});
// Log the response
console.log(response.response);
}
```
## Available Models
The following models are available:
- `sonar`: 128k context window
- `sonar-pro`: 200k context window
- `sonar-deep-research`: 128k context window
- `sonar-reasoning`: 128k context window
- `sonar-reasoning-pro`: 128k context window
- `r1-1776`: 128k context window
# Limitations
Currently does not support function calling.
## API Reference
- [Perplexity](/docs/api/classes/Perplexity)
@@ -29,8 +29,6 @@ Note: calling the `bind` method will return a new `FunctionTool` instance, witho
Example to pass a `userToken` as additional argument:
```ts
import { agent, tool } from "llamaindex";
// first arg is LLM input, second is bound arg
const queryKnowledgeBase = async ({ question }, { userToken }) => {
const response = await fetch(`https://knowledge-base.com?token=${userToken}&query=${question}`);
@@ -38,7 +36,7 @@ const queryKnowledgeBase = async ({ question }, { userToken }) => {
};
// define tool as usual
const kbTool = tool(queryKnowledgeBase, {
const kbTool = FunctionTool.from(queryKnowledgeBase, {
name: 'queryKnowledgeBase',
description: 'Query knowledge base',
parameters: z.object({
@@ -50,7 +48,7 @@ const kbTool = tool(queryKnowledgeBase, {
// create an agent
const additionalArg = { userToken: 'abcd1234' };
const workflow = agent({
const kbAgent = new LLMAgent({
tools: [kbTool.bind(additionalArg)],
// llm, systemPrompt etc
})
@@ -119,7 +119,7 @@ Lastly, we run the workflow. The `.run()` method is async, so we use await here
Optionally, you can choose to use a shared context between steps by specifying a context type when creating the workflow. Here's an example where multiple steps access a shared state:
```typescript
import { HandlerContext } from "llamaindex";
import { HandlerContext } from "@llamaindex/workflow";
type MyContextData = {
query: string;
-6
View File
@@ -1,5 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
@@ -9,11 +8,6 @@
"next-env.d.ts",
"src/content/docs/cloud/api/**",
"src/content/docs/api/**"
],
"env": [
"LLAMA_CLOUD_API_KEY",
"LLAMA_CLOUD_PIPELINE_ID",
"OPENAI_API_KEY"
]
},
"dev": {
@@ -1,20 +1,5 @@
# @llamaindex/cloudflare-worker-agent-test
## 0.0.145
### Patch Changes
- llamaindex@0.9.11
## 0.0.144
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 0.0.143
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloudflare-worker-agent-test",
"version": "0.0.145",
"version": "0.0.143",
"type": "module",
"private": true,
"scripts": {
@@ -1,11 +1,5 @@
# @llamaindex/llama-parse-browser-test
## 0.0.54
### Patch Changes
- @llamaindex/cloud@3.0.9
## 0.0.53
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/llama-parse-browser-test",
"private": true,
"version": "0.0.54",
"version": "0.0.53",
"type": "module",
"scripts": {
"dev": "vite",
-15
View File
@@ -1,20 +1,5 @@
# @llamaindex/next-agent-test
## 0.1.145
### Patch Changes
- llamaindex@0.9.11
## 0.1.144
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 0.1.143
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-agent-test",
"version": "0.1.145",
"version": "0.1.143",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,20 +1,5 @@
# test-edge-runtime
## 0.1.144
### Patch Changes
- llamaindex@0.9.11
## 0.1.143
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 0.1.142
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/nextjs-edge-runtime-test",
"version": "0.1.144",
"version": "0.1.142",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,24 +1,5 @@
# @llamaindex/next-node-runtime
## 0.1.11
### Patch Changes
- llamaindex@0.9.11
- @llamaindex/huggingface@0.0.45
## 0.1.10
### Patch Changes
- Updated dependencies [aea550a]
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- @llamaindex/huggingface@0.0.44
- llamaindex@0.9.10
- @llamaindex/readers@2.0.8
## 0.1.9
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-node-runtime-test",
"version": "0.1.11",
"version": "0.1.9",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,7 +1,13 @@
"use server";
import { HuggingFaceEmbedding } from "@llamaindex/huggingface";
import { SimpleDirectoryReader } from "@llamaindex/readers/directory";
import { OpenAI, OpenAIAgent, Settings, VectorStoreIndex } from "llamaindex";
import {
OpenAI,
OpenAIAgent,
QueryEngineTool,
Settings,
VectorStoreIndex,
} from "llamaindex";
Settings.llm = new OpenAI({
apiKey: process.env.NEXT_PUBLIC_OPENAI_KEY ?? "FAKE_KEY_TO_PASS_TESTS",
@@ -25,20 +31,23 @@ export async function getOpenAIModelRequest(query: string) {
const reader = new SimpleDirectoryReader();
const documents = await reader.loadData(currentDir);
const index = await VectorStoreIndex.fromDocuments(documents);
const retriever = index.asRetriever({
similarityTopK: 10,
});
const queryEngine = index.asQueryEngine({
retriever,
});
// define the query engine as a tool
const tools = [
index.queryTool({
options: {
similarityTopK: 10,
},
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "deployment_details_per_env",
description: `This tool can answer detailed questions about deployments happened in various environments.`,
},
}),
];
// create the agent
const agent = new OpenAIAgent({ tools });
@@ -1,20 +1,5 @@
# vite-import-llamaindex
## 0.0.11
### Patch Changes
- llamaindex@0.9.11
## 0.0.10
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 0.0.9
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "vite-import-llamaindex",
"private": true,
"version": "0.0.11",
"version": "0.0.9",
"type": "module",
"scripts": {
"build": "vite build",
@@ -1,20 +1,5 @@
# @llamaindex/waku-query-engine-test
## 0.0.145
### Patch Changes
- llamaindex@0.9.11
## 0.0.144
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 0.0.143
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/waku-query-engine-test",
"version": "0.0.145",
"version": "0.0.143",
"type": "module",
"private": true,
"scripts": {
-73
View File
@@ -1,78 +1,5 @@
# examples
## 0.2.10
### Patch Changes
- 1587e48: Added support for Perplexity api
- Updated dependencies [387a192]
- Updated dependencies [a8c0637]
- Updated dependencies [1587e48]
- Updated dependencies [98eebf7]
- @llamaindex/mistral@0.0.14
- @llamaindex/openai@0.1.61
- @llamaindex/perplexity@0.0.2
- @llamaindex/google@0.1.2
- llamaindex@0.9.11
- @llamaindex/clip@0.0.45
- @llamaindex/deepinfra@0.0.45
- @llamaindex/deepseek@0.0.5
- @llamaindex/fireworks@0.0.5
- @llamaindex/groq@0.0.60
- @llamaindex/huggingface@0.0.45
- @llamaindex/jinaai@0.0.5
- @llamaindex/together@0.0.5
- @llamaindex/vllm@0.0.31
## 0.2.9
### Patch Changes
- aea550a: Add factory convenience factory for each LLM provider, e.g. you can use openai instead of 'new OpenAI'
- Updated dependencies [c14a21b]
- Updated dependencies [33f9856]
- Updated dependencies [aea550a]
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- @llamaindex/azure@0.1.8
- @llamaindex/google@0.1.1
- @llamaindex/huggingface@0.0.44
- @llamaindex/portkey-ai@0.0.41
- @llamaindex/anthropic@0.2.6
- @llamaindex/deepinfra@0.0.44
- @llamaindex/fireworks@0.0.4
- @llamaindex/replicate@0.0.41
- @llamaindex/deepseek@0.0.4
- @llamaindex/together@0.0.4
- @llamaindex/mistral@0.0.13
- @llamaindex/ollama@0.0.48
- @llamaindex/openai@0.1.60
- @llamaindex/vercel@0.0.19
- @llamaindex/groq@0.0.59
- @llamaindex/vllm@0.0.30
- llamaindex@0.9.10
- @llamaindex/workflow@0.0.16
- @llamaindex/core@0.5.8
- @llamaindex/clip@0.0.44
- @llamaindex/jinaai@0.0.4
- @llamaindex/milvus@0.1.8
- @llamaindex/qdrant@0.1.8
- @llamaindex/cloud@3.0.9
- @llamaindex/node-parser@1.0.8
- @llamaindex/cohere@0.0.13
- @llamaindex/mixedbread@0.0.13
- @llamaindex/astra@0.0.13
- @llamaindex/chroma@0.0.13
- @llamaindex/firestore@1.0.6
- @llamaindex/mongodb@0.0.13
- @llamaindex/pinecone@0.0.13
- @llamaindex/postgres@0.0.41
- @llamaindex/upstash@0.0.13
- @llamaindex/weaviate@0.0.13
- @llamaindex/voyage-ai@1.0.5
- @llamaindex/readers@2.0.8
## 0.2.8
### Patch Changes
+2 -2
View File
@@ -1,5 +1,5 @@
import { OpenAI } from "@llamaindex/openai";
import { FunctionTool, agent } from "llamaindex";
import { AgentWorkflow, FunctionTool } from "llamaindex";
import { z } from "zod";
const csvData =
@@ -33,7 +33,7 @@ const userQuestion = "which are the best comedies after 2010?";
const systemPrompt =
"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.";
const workflow = agent({
const workflow = AgentWorkflow.fromTools({
tools: [interpreterTool],
llm,
verbose: false,
+2 -2
View File
@@ -1,5 +1,5 @@
import { OpenAI } from "@llamaindex/openai";
import { FunctionTool, agent } from "llamaindex";
import { AgentWorkflow, FunctionTool } from "llamaindex";
import { z } from "zod";
const sumNumbers = FunctionTool.from(
@@ -27,7 +27,7 @@ const divideNumbers = FunctionTool.from(
);
async function main() {
const workflow = agent({
const workflow = AgentWorkflow.fromTools({
tools: [sumNumbers, divideNumbers],
llm: new OpenAI({ model: "gpt-4o-mini" }),
verbose: false,
+34 -24
View File
@@ -1,31 +1,41 @@
import { tool } from "llamaindex";
import { FunctionTool } from "llamaindex";
import { z } from "zod";
export const getCurrentIDTool = tool({
name: "get_user_id",
description: "Get a random user id",
parameters: z.object({}),
execute: () => {
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 = tool({
name: "get_user_info",
description: "Get user info",
parameters: z.object({
userId: z.string().describe("The user id"),
}),
execute: ({ userId }) =>
`Name: Alex; Address: 1234 Main St, CA; User ID: ${userId}`,
});
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: z.object({
userId: z.string().describe("The user id"),
}),
},
);
export const getWeatherTool = tool({
name: "get_weather",
description: "Get the current weather for a location",
parameters: z.object({
address: z.string().describe("The address"),
}),
execute: ({ address }) => `${address} is in a sunny location!`,
});
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: z.object({
address: z.string().describe("The address"),
}),
},
);
+2 -2
View File
@@ -1,12 +1,12 @@
import { OpenAI } from "@llamaindex/openai";
import { AgentStream, agent } from "llamaindex";
import { AgentStream, AgentWorkflow } from "llamaindex";
import { WikipediaTool } from "../wiki";
async function main() {
const llm = new OpenAI({ model: "gpt-4-turbo" });
const wikiTool = new WikipediaTool();
const workflow = agent({
const workflow = AgentWorkflow.fromTools({
tools: [wikiTool],
llm,
verbose: false,
@@ -1,38 +1,40 @@
import { openai } from "@llamaindex/openai";
import { OpenAI } from "@llamaindex/openai";
import fs from "fs";
import {
agent,
AgentToolCall,
AgentToolCallResult,
multiAgent,
tool,
AgentWorkflow,
FunctionAgent,
FunctionTool,
} from "llamaindex";
import os from "os";
import { z } from "zod";
import { WikipediaTool } from "../wiki";
const llm = openai({
const llm = new OpenAI({
model: "gpt-4o-mini",
});
const saveFileTool = tool({
name: "saveFile",
description:
"Save the written content into a file that can be downloaded by the user",
parameters: z.object({
content: z.string({
description: "The content to save into a file",
}),
}),
execute: ({ content }: { content: string }) => {
const saveFileTool = FunctionTool.from(
({ content }: { content: string }) => {
const filePath = os.tmpdir() + "/report.md";
fs.writeFileSync(filePath, content);
return `File saved successfully at ${filePath}`;
},
});
{
name: "saveFile",
description:
"Save the written content into a file that can be downloaded by the user",
parameters: z.object({
content: z.string({
description: "The content to save into a file",
}),
}),
},
);
async function main() {
const reportAgent = agent({
const reportAgent = new FunctionAgent({
name: "ReportAgent",
description:
"Responsible for crafting well-written blog posts based on research findings",
@@ -41,7 +43,7 @@ async function main() {
llm,
});
const researchAgent = agent({
const researchAgent = new FunctionAgent({
name: "ResearchAgent",
description:
"Responsible for gathering relevant information from the internet",
@@ -51,7 +53,7 @@ async function main() {
llm,
});
const workflow = multiAgent({
const workflow = new AgentWorkflow({
agents: [researchAgent, reportAgent],
rootAgent: researchAgent,
});
@@ -3,55 +3,59 @@
* 1. FetchWeatherAgent - Fetches the weather in a city
* 2. TemperatureConverterAgent - Converts the temperature from Fahrenheit to Celsius
*/
import { openai } from "@llamaindex/openai";
import { OpenAI } from "@llamaindex/openai";
import { StopEvent } from "@llamaindex/workflow";
import {
agent,
AgentInput,
AgentOutput,
AgentStream,
AgentToolCall,
AgentToolCallResult,
multiAgent,
StopEvent,
tool,
AgentWorkflow,
FunctionAgent,
FunctionTool,
} from "llamaindex";
import { z } from "zod";
const llm = openai({
const llm = new OpenAI({
model: "gpt-4o-mini",
});
// Define tools for the agents
const temperatureConverterTool = tool({
description: "Convert a temperature from Fahrenheit to Celsius",
name: "fahrenheitToCelsius",
parameters: z.object({
temperature: z.number({
description: "The temperature in Fahrenheit",
}),
}),
execute: ({ temperature }) => {
const temperatureConverterTool = FunctionTool.from(
({ temperature }: { temperature: number }) => {
return ((temperature - 32) * 5) / 9;
},
});
const temperatureFetcherTool = tool({
description: "Fetch the temperature (in Fahrenheit) for a city",
name: "fetchTemperature",
parameters: z.object({
city: z.string({
description: "The city to fetch the temperature for",
{
description: "Convert a temperature from Fahrenheit to Celsius",
name: "fahrenheitToCelsius",
parameters: z.object({
temperature: z.number({
description: "The temperature in Fahrenheit",
}),
}),
}),
execute: ({ city }) => {
},
);
const temperatureFetcherTool = FunctionTool.from(
({ city }: { city: string }) => {
const temperature = Math.floor(Math.random() * 58) + 32;
return `The current temperature in ${city} is ${temperature}°F`;
},
});
{
description: "Fetch the temperature (in Fahrenheit) for a city",
name: "fetchTemperature",
parameters: z.object({
city: z.string({
description: "The city to fetch the temperature for",
}),
}),
},
);
// Create agents
async function multiWeatherAgent() {
const converterAgent = agent({
const converterAgent = new FunctionAgent({
name: "TemperatureConverterAgent",
description:
"An agent that can convert temperatures from Fahrenheit to Celsius.",
@@ -59,7 +63,7 @@ async function multiWeatherAgent() {
llm,
});
const weatherAgent = agent({
const weatherAgent = new FunctionAgent({
name: "FetchWeatherAgent",
description: "An agent that can get the weather in a city. ",
systemPrompt:
@@ -72,7 +76,7 @@ async function multiWeatherAgent() {
});
// Create agent workflow with the agents
const workflow = multiAgent({
const workflow = new AgentWorkflow({
agents: [weatherAgent, converterAgent],
rootAgent: weatherAgent,
verbose: false,
@@ -1,17 +1,18 @@
/**
* This example shows how to use AgentWorkflow as a single agent with tools
*/
import { openai } from "@llamaindex/openai";
import { Settings, agent } from "llamaindex";
import { OpenAI } from "@llamaindex/openai";
import { AgentWorkflow } from "llamaindex";
import { getWeatherTool } from "../agent/utils/tools";
Settings.llm = openai({
const llm = new OpenAI({
model: "gpt-4o",
});
async function singleWeatherAgent() {
const workflow = agent({
const workflow = AgentWorkflow.fromTools({
tools: [getWeatherTool],
llm,
verbose: false,
});
+50 -44
View File
@@ -1,63 +1,69 @@
import fs from "fs";
import {
agent,
AgentToolCall,
AgentToolCallResult,
multiAgent,
tool,
AgentWorkflow,
FunctionAgent,
FunctionTool,
} from "llamaindex";
import { z } from "zod";
import { anthropic } from "@llamaindex/anthropic";
import { Anthropic } from "@llamaindex/anthropic";
const weatherTool = tool({
name: "weather",
description: "Get the weather",
parameters: z.object({
location: z.string({
description: "The location to get the weather for",
}),
}),
execute: ({ location }) => {
return `The weather in ${location} is sunny`;
},
const llm = new Anthropic({
model: "claude-3-5-sonnet",
});
const inflationTool = tool({
name: "inflation",
description: "Get the inflation",
parameters: z.object({
location: z.string({
description: "The location to get the inflation for",
}),
}),
execute: ({ location }) => {
return `The inflation in ${location} is 2%`;
const weatherTool = FunctionTool.from(
(query: { location: string }) => {
return `The weather in ${query.location} is sunny`;
},
});
const saveFileTool = tool({
name: "saveFile",
description:
"Save the written content into a file that can be downloaded by the user",
parameters: z.object({
content: z.string({
description: "The content to save into a file",
{
name: "weather",
description: "Get the weather",
parameters: z.object({
location: z.string({
description: "The location to get the weather for",
}),
}),
}),
execute: ({ content }) => {
},
);
const inflationTool = FunctionTool.from(
(query: { location: string }) => {
return `The inflation in ${query.location} is 2%`;
},
{
name: "inflation",
description: "Get the inflation",
parameters: z.object({
location: z.string({
description: "The location to get the inflation for",
}),
}),
},
);
const saveFileTool = FunctionTool.from(
({ content }: { content: string }) => {
const filePath = "./report.md";
fs.writeFileSync(filePath, content);
return `File saved successfully at ${filePath}`;
},
});
{
name: "saveFile",
description:
"Save the written content into a file that can be downloaded by the user",
parameters: z.object({
content: z.string({
description: "The content to save into a file",
}),
}),
},
);
async function main() {
const llm = anthropic({
model: "claude-3-5-sonnet",
});
const reportAgent = agent({
const reportAgent = new FunctionAgent({
name: "ReportAgent",
description:
"Responsible for creating concise reports about weather and inflation data",
@@ -66,7 +72,7 @@ async function main() {
llm,
});
const researchAgent = agent({
const researchAgent = new FunctionAgent({
name: "ResearchAgent",
description:
"Responsible for gathering relevant information from the internet",
@@ -76,7 +82,7 @@ async function main() {
llm,
});
const workflow = multiAgent({
const workflow = new AgentWorkflow({
agents: [researchAgent, reportAgent],
rootAgent: researchAgent,
});
-25
View File
@@ -1,25 +0,0 @@
import { ollama } from "@llamaindex/ollama";
import { agent } from "llamaindex";
import { getWeatherTool } from "../agent/utils/tools";
async function main() {
const myAgent = agent({
tools: [getWeatherTool],
verbose: false,
llm: ollama({ model: "granite3.2:2b" }),
});
const sfResult = await myAgent.run(
"What's the weather like in San Francisco?",
);
// The weather in San Francisco, CA is currently sunny.
console.log(`${JSON.stringify(sfResult, null, 2)}`);
// Reuse the context from the previous run
const caResult = await myAgent.run("Compare it with California?");
// Both San Francisco and California are currently experiencing sunny weather.
console.log(`${JSON.stringify(caResult, null, 2)}`);
}
main().catch(console.error);
+31 -19
View File
@@ -1,31 +1,43 @@
import { anthropic } from "@llamaindex/anthropic";
import { agent, tool } from "llamaindex";
import { Anthropic, AnthropicAgent } from "@llamaindex/anthropic";
import { FunctionTool, Settings } from "llamaindex";
import { z } from "zod";
import { WikipediaTool } from "../wiki";
const workflow = agent({
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log("llm-tool-call", event.detail.toolCall);
});
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: "claude-3-7-sonnet",
});
const agent = new AnthropicAgent({
llm: anthropic,
tools: [
tool({
name: "weather",
description: "Get the weather",
parameters: z.object({
location: z.string().describe("The location to get the weather for"),
}),
execute: ({ location }) => `The weather in ${location} is sunny`,
}),
FunctionTool.from(
(query) => {
return `The weather in ${query.location} is sunny`;
},
{
name: "weather",
description: "Get the weather",
parameters: z.object({
location: z.string().describe("The location to get the weather for"),
}),
},
),
new WikipediaTool(),
],
llm: anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: "claude-3-7-sonnet",
}),
});
async function main() {
const result = await workflow.run(
"What is the weather in New York? What's the history of New York from Wikipedia in 3 sentences?",
);
console.log(result.data);
const { message } = await agent.chat({
message:
"What is the weather in New York? What's the history of New York from Wikipedia in 3 sentences?",
});
console.log(message.content);
}
void main();
+51 -33
View File
@@ -1,47 +1,65 @@
import { gemini, GEMINI_MODEL } from "@llamaindex/google";
import { agent, tool } from "llamaindex";
import { Gemini, GEMINI_MODEL } from "@llamaindex/google";
import { FunctionTool, LLMAgent, Settings } from "llamaindex";
import { z } from "zod";
const sumNumbers = tool({
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: z.object({
a: z.number().describe("The first number"),
b: z.number().describe("The second number"),
}),
execute: ({ a, b }) => `${a + b}`,
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail);
});
const divideNumbers = tool({
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: z.object({
a: z.number().describe("The dividend a to divide"),
b: z.number().describe("The divisor b to divide by"),
}),
execute: ({ a, b }) => `${a / b}`,
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail);
});
const subtractNumbers = tool({
name: "subtractNumbers",
description: "Use this function to subtract two numbers",
parameters: z.object({
a: z.number().describe("The number to subtract from"),
b: z.number().describe("The number to subtract"),
}),
execute: ({ a, b }) => `${a - b}`,
});
const sumNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a + b}`,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: z.object({
a: z.number().describe("The first number"),
b: z.number().describe("The second number"),
}),
},
);
const divideNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a / b}`,
{
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: z.object({
a: z.number().describe("The dividend a to divide"),
b: z.number().describe("The divisor b to divide by"),
}),
},
);
const subtractNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a - b}`,
{
name: "subtractNumbers",
description: "Use this function to subtract two numbers",
parameters: z.object({
a: z.number().describe("The number to subtract from"),
b: z.number().describe("The number to subtract"),
}),
},
);
async function main() {
const myAgent = agent({
const gemini = new Gemini({
model: GEMINI_MODEL.GEMINI_PRO,
});
const agent = new LLMAgent({
llm: gemini,
tools: [sumNumbers, divideNumbers, subtractNumbers],
llm: gemini({ model: GEMINI_MODEL.GEMINI_PRO }),
});
const result = await myAgent.run(
"How much is 5 + 5? then divide by 2 then subtract 1",
);
console.log(result.data);
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2 then subtract 1",
});
console.log(response.message);
}
void main().then(() => {
+1 -1
View File
@@ -1,4 +1,4 @@
import { StartEvent, StopEvent, Workflow } from "llamaindex";
import { StartEvent, StopEvent, Workflow } from "@llamaindex/workflow";
type ContextData = {
counter: number;
+38 -39
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/examples",
"version": "0.2.10",
"version": "0.2.8",
"private": true,
"scripts": {
"lint": "eslint .",
@@ -11,44 +11,43 @@
"@azure/cosmos": "^4.1.1",
"@azure/identity": "^4.4.1",
"@azure/search-documents": "^12.1.0",
"@llamaindex/anthropic": "^0.2.6",
"@llamaindex/astra": "^0.0.13",
"@llamaindex/azure": "^0.1.8",
"@llamaindex/chroma": "^0.0.13",
"@llamaindex/clip": "^0.0.45",
"@llamaindex/cloud": "^3.0.9",
"@llamaindex/cohere": "^0.0.13",
"@llamaindex/core": "^0.5.8",
"@llamaindex/deepinfra": "^0.0.45",
"@llamaindex/anthropic": "^0.2.5",
"@llamaindex/astra": "^0.0.12",
"@llamaindex/azure": "^0.1.7",
"@llamaindex/chroma": "^0.0.12",
"@llamaindex/clip": "^0.0.43",
"@llamaindex/cloud": "^3.0.8",
"@llamaindex/cohere": "^0.0.12",
"@llamaindex/core": "^0.5.7",
"@llamaindex/deepinfra": "^0.0.43",
"@llamaindex/env": "^0.1.29",
"@llamaindex/firestore": "^1.0.6",
"@llamaindex/google": "^0.1.2",
"@llamaindex/groq": "^0.0.60",
"@llamaindex/huggingface": "^0.0.45",
"@llamaindex/milvus": "^0.1.8",
"@llamaindex/mistral": "^0.0.14",
"@llamaindex/mixedbread": "^0.0.13",
"@llamaindex/mongodb": "^0.0.13",
"@llamaindex/node-parser": "^1.0.8",
"@llamaindex/ollama": "^0.0.48",
"@llamaindex/openai": "^0.1.61",
"@llamaindex/pinecone": "^0.0.13",
"@llamaindex/portkey-ai": "^0.0.41",
"@llamaindex/postgres": "^0.0.41",
"@llamaindex/qdrant": "^0.1.8",
"@llamaindex/readers": "^2.0.8",
"@llamaindex/replicate": "^0.0.41",
"@llamaindex/upstash": "^0.0.13",
"@llamaindex/vercel": "^0.0.19",
"@llamaindex/vllm": "^0.0.31",
"@llamaindex/voyage-ai": "^1.0.5",
"@llamaindex/weaviate": "^0.0.13",
"@llamaindex/workflow": "^0.0.16",
"@llamaindex/deepseek": "^0.0.5",
"@llamaindex/fireworks": "^0.0.5",
"@llamaindex/together": "^0.0.5",
"@llamaindex/jinaai": "^0.0.5",
"@llamaindex/perplexity": "^0.0.2",
"@llamaindex/firestore": "^1.0.5",
"@llamaindex/google": "^0.1.0",
"@llamaindex/groq": "^0.0.58",
"@llamaindex/huggingface": "^0.0.43",
"@llamaindex/milvus": "^0.1.7",
"@llamaindex/mistral": "^0.0.12",
"@llamaindex/mixedbread": "^0.0.12",
"@llamaindex/mongodb": "^0.0.12",
"@llamaindex/node-parser": "^1.0.7",
"@llamaindex/ollama": "^0.0.47",
"@llamaindex/openai": "^0.1.59",
"@llamaindex/pinecone": "^0.0.12",
"@llamaindex/portkey-ai": "^0.0.40",
"@llamaindex/postgres": "^0.0.40",
"@llamaindex/qdrant": "^0.1.7",
"@llamaindex/readers": "^2.0.7",
"@llamaindex/replicate": "^0.0.40",
"@llamaindex/upstash": "^0.0.12",
"@llamaindex/vercel": "^0.0.18",
"@llamaindex/vllm": "^0.0.29",
"@llamaindex/voyage-ai": "^1.0.4",
"@llamaindex/weaviate": "^0.0.12",
"@llamaindex/workflow": "^0.0.15",
"@llamaindex/deepseek": "^0.0.3",
"@llamaindex/fireworks": "^0.0.3",
"@llamaindex/together": "^0.0.3",
"@llamaindex/jinaai": "^0.0.3",
"@notionhq/client": "^2.2.15",
"@pinecone-database/pinecone": "^4.0.0",
"@vercel/postgres": "^0.10.0",
@@ -57,7 +56,7 @@
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"js-tiktoken": "^1.0.14",
"llamaindex": "^0.9.11",
"llamaindex": "^0.9.9",
"mongodb": "6.7.0",
"postgres": "^3.4.4",
"wikipedia": "^2.1.2",
-44
View File
@@ -1,44 +0,0 @@
import { perplexity } from "@llamaindex/perplexity";
(async () => {
const perplexityLLM = perplexity({
apiKey: process.env.PERPLEXITY_API_KEY!,
model: "sonar",
});
// Chat API example
const response = await perplexityLLM.chat({
messages: [
{
role: "system",
content:
"You are a helpful AI assistant that provides accurate and concise answers",
},
{
role: "user",
content: "What is the capital of France?",
},
],
});
console.log("Chat response:", response.message.content);
// Streaming example
const stream = await perplexityLLM.chat({
messages: [
{
role: "system",
content: "You are a creative AI assistant that tells engaging stories",
},
{
role: "user",
content: "Tell me a short story",
},
],
stream: true,
});
console.log("\nStreaming response:");
for await (const chunk of stream) {
process.stdout.write(chunk.delta);
}
})();
+1 -1
View File
@@ -5,7 +5,7 @@ import {
StopEvent,
Workflow,
WorkflowEvent,
} from "llamaindex";
} from "@llamaindex/workflow";
const MAX_REVIEWS = 3;
+1 -1
View File
@@ -5,7 +5,7 @@ import {
StopEvent,
Workflow,
WorkflowEvent,
} from "llamaindex";
} from "@llamaindex/workflow";
// Create LLM instance
const llm = new OpenAI();
+6 -1
View File
@@ -1,5 +1,10 @@
import { OpenAI } from "@llamaindex/openai";
import { StartEvent, StopEvent, Workflow, WorkflowEvent } from "llamaindex";
import {
StartEvent,
StopEvent,
Workflow,
WorkflowEvent,
} from "@llamaindex/workflow";
// Create LLM instance
const llm = new OpenAI();
+1 -1
View File
@@ -5,7 +5,7 @@ import {
StopEvent,
Workflow,
WorkflowEvent,
} from "llamaindex";
} from "@llamaindex/workflow";
// Create LLM instance
const llm = new OpenAI();
+1 -1
View File
@@ -1,4 +1,4 @@
import { StartEvent, StopEvent, Workflow } from "llamaindex";
import { StartEvent, StopEvent, Workflow } from "@llamaindex/workflow";
const longRunning = async (_: unknown, ev: StartEvent<string>) => {
await new Promise((resolve) => setTimeout(resolve, 2000)); // Wait for 2 seconds
+6 -1
View File
@@ -1,5 +1,10 @@
import { OpenAI } from "@llamaindex/openai";
import { StartEvent, StopEvent, Workflow, WorkflowEvent } from "llamaindex";
import {
StartEvent,
StopEvent,
Workflow,
WorkflowEvent,
} from "@llamaindex/workflow";
// Create LLM instance
const llm = new OpenAI();
-15
View File
@@ -1,20 +1,5 @@
# @llamaindex/autotool
## 6.0.11
### Patch Changes
- llamaindex@0.9.11
## 6.0.10
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 6.0.9
### Patch Changes
@@ -1,22 +1,5 @@
# @llamaindex/autotool-01-node-example
## 0.0.92
### Patch Changes
- llamaindex@0.9.11
- @llamaindex/autotool@6.0.11
## 0.0.91
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
- @llamaindex/autotool@6.0.10
## 0.0.90
### Patch Changes
@@ -13,5 +13,5 @@
"scripts": {
"start": "node --import tsx --import @llamaindex/autotool/node ./src/index.ts"
},
"version": "0.0.92"
"version": "0.0.90"
}
+1 -1
View File
@@ -6,7 +6,7 @@
"url": "git+https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/autotool"
},
"version": "6.0.11",
"version": "6.0.9",
"description": "auto transpile your JS function to LLM Agent compatible",
"files": [
"dist",
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/cloud
## 3.0.9
### Patch Changes
- Updated dependencies [40ee761]
- @llamaindex/core@0.5.8
## 3.0.8
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloud",
"version": "3.0.9",
"version": "3.0.8",
"type": "module",
"license": "MIT",
"scripts": {
-1
View File
@@ -1,5 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/community
## 0.0.90
### Patch Changes
- Updated dependencies [40ee761]
- @llamaindex/core@0.5.8
## 0.0.89
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/community",
"description": "Community package for LlamaIndexTS",
"version": "0.0.90",
"version": "0.0.89",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
-6
View File
@@ -1,11 +1,5 @@
# @llamaindex/core
## 0.5.8
### Patch Changes
- 40ee761: Add factory methods tool, agent and multiAgent to simplify agent usage
## 0.5.7
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/core",
"type": "module",
"version": "0.5.8",
"version": "0.5.7",
"description": "LlamaIndex Core Module",
"exports": {
"./agent": {
+4 -49
View File
@@ -64,52 +64,12 @@ export class FunctionTool<
parameters: R;
},
): FunctionTool<T, JSONValue, AdditionalToolArgument>;
static from<
R extends z.ZodType,
AdditionalToolArgument extends object = object,
>(
config: Omit<ToolMetadata, "parameters"> & {
parameters: R;
execute: (
input: z.infer<R>,
additionalArg?: AdditionalToolArgument,
) => JSONValue | Promise<JSONValue>;
},
): FunctionTool<
z.infer<R>,
JSONValue | Promise<JSONValue>,
AdditionalToolArgument
>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static from(fnOrConfig: any, schema?: any): any {
// Handle the case where an object with execute function is passed
if (
typeof schema === "undefined" &&
typeof fnOrConfig === "object" &&
fnOrConfig.execute
) {
const { execute, parameters, ...restConfig } = fnOrConfig;
if (parameters instanceof z.ZodSchema) {
const jsonSchema = zodToJsonSchema(parameters);
return new FunctionTool(
execute,
{
...restConfig,
parameters: jsonSchema,
},
parameters,
);
}
return new FunctionTool(execute, fnOrConfig);
}
// Handle the original cases
if (schema && schema.parameters instanceof z.ZodSchema) {
static from(fn: any, schema: any): any {
if (schema.parameters instanceof z.ZodSchema) {
const jsonSchema = zodToJsonSchema(schema.parameters);
return new FunctionTool(
fnOrConfig,
fn,
{
...schema,
parameters: jsonSchema,
@@ -117,7 +77,7 @@ export class FunctionTool<
schema.parameters,
);
}
return new FunctionTool(fnOrConfig, schema);
return new FunctionTool(fn, schema);
}
get metadata(): BaseTool<T>["metadata"] {
@@ -162,8 +122,3 @@ export class FunctionTool<
return this.#fn.call(null, input, this.#additionalArg);
};
}
/**
* A simpler alias for creating a FunctionTool.
*/
export const tool = FunctionTool.from;
+1 -1
View File
@@ -1 +1 @@
export * from "./function-tool";
export { FunctionTool } from "./function-tool";
+1 -2
View File
@@ -80,9 +80,8 @@ export {
extractText,
imageToDataUrl,
messagesToHistory,
MockLLM,
toToolDescriptions,
} from "./llms";
export { MockLLM } from "./mock";
export { objectEntries } from "./object-entries";
+88
View File
@@ -2,6 +2,15 @@ import { fs } from "@llamaindex/env";
import { filetypemime } from "magic-bytes.js";
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
CompletionResponse,
LLM,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
LLMCompletionParamsNonStreaming,
LLMCompletionParamsStreaming,
LLMMetadata,
MessageContent,
MessageContentDetail,
MessageContentTextDetail,
@@ -143,3 +152,82 @@ export async function imageToDataUrl(
}
return await blobToDataUrl(input);
}
export class MockLLM implements LLM {
metadata: LLMMetadata;
options: {
timeBetweenToken: number;
responseMessage: string;
};
constructor(options?: {
timeBetweenToken?: number;
responseMessage?: string;
metadata?: LLMMetadata;
}) {
this.options = {
timeBetweenToken: options?.timeBetweenToken ?? 20,
responseMessage: options?.responseMessage ?? "This is a mock response",
};
this.metadata = options?.metadata ?? {
model: "MockLLM",
temperature: 0.5,
topP: 0.5,
contextWindow: 1024,
tokenizer: undefined,
};
}
chat(
params: LLMChatParamsStreaming<object, object>,
): Promise<AsyncIterable<ChatResponseChunk>>;
chat(
params: LLMChatParamsNonStreaming<object, object>,
): Promise<ChatResponse<object>>;
async chat(
params:
| LLMChatParamsStreaming<object, object>
| LLMChatParamsNonStreaming<object, object>,
): Promise<AsyncIterable<ChatResponseChunk> | ChatResponse<object>> {
const responseMessage = this.options.responseMessage;
const timeBetweenToken = this.options.timeBetweenToken;
if (params.stream) {
return (async function* () {
for (const char of responseMessage) {
yield { delta: char, raw: {} };
await new Promise((resolve) => setTimeout(resolve, timeBetweenToken));
}
})();
}
return {
message: { content: responseMessage, role: "assistant" },
raw: {},
};
}
async complete(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
async complete(
params: LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse>;
async complete(
params: LLMCompletionParamsStreaming | LLMCompletionParamsNonStreaming,
): Promise<AsyncIterable<CompletionResponse> | CompletionResponse> {
const responseMessage = this.options.responseMessage;
const timeBetweenToken = this.options.timeBetweenToken;
if (params.stream) {
return (async function* () {
for (const char of responseMessage) {
yield { delta: char, text: char, raw: {} };
await new Promise((resolve) => setTimeout(resolve, timeBetweenToken));
}
})();
}
return { text: responseMessage, raw: {} };
}
}
-93
View File
@@ -1,93 +0,0 @@
// TODO: move to a test package
import { ToolCallLLM } from "../llms/base";
import type {
ChatResponse,
ChatResponseChunk,
CompletionResponse,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
LLMCompletionParamsNonStreaming,
LLMCompletionParamsStreaming,
LLMMetadata,
} from "../llms/type";
export class MockLLM extends ToolCallLLM {
metadata: LLMMetadata;
options: {
timeBetweenToken: number;
responseMessage: string;
};
supportToolCall: boolean = false;
constructor(options?: {
timeBetweenToken?: number;
responseMessage?: string;
metadata?: LLMMetadata;
}) {
super();
this.options = {
timeBetweenToken: options?.timeBetweenToken ?? 20,
responseMessage: options?.responseMessage ?? "This is a mock response",
};
this.metadata = options?.metadata ?? {
model: "MockLLM",
temperature: 0.5,
topP: 0.5,
contextWindow: 1024,
tokenizer: undefined,
};
}
chat(
params: LLMChatParamsStreaming<object, object>,
): Promise<AsyncIterable<ChatResponseChunk>>;
chat(
params: LLMChatParamsNonStreaming<object, object>,
): Promise<ChatResponse<object>>;
async chat(
params:
| LLMChatParamsStreaming<object, object>
| LLMChatParamsNonStreaming<object, object>,
): Promise<AsyncIterable<ChatResponseChunk> | ChatResponse<object>> {
const responseMessage = this.options.responseMessage;
const timeBetweenToken = this.options.timeBetweenToken;
if (params.stream) {
return (async function* () {
for (const char of responseMessage) {
yield { delta: char, raw: {} };
await new Promise((resolve) => setTimeout(resolve, timeBetweenToken));
}
})();
}
return {
message: { content: responseMessage, role: "assistant" },
raw: {},
};
}
async complete(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
async complete(
params: LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse>;
async complete(
params: LLMCompletionParamsStreaming | LLMCompletionParamsNonStreaming,
): Promise<AsyncIterable<CompletionResponse> | CompletionResponse> {
const responseMessage = this.options.responseMessage;
const timeBetweenToken = this.options.timeBetweenToken;
if (params.stream) {
return (async function* () {
for (const char of responseMessage) {
yield { delta: char, text: char, raw: {} };
await new Promise((resolve) => setTimeout(resolve, timeBetweenToken));
}
})();
}
return { text: responseMessage, raw: {} };
}
}
+1 -36
View File
@@ -1,4 +1,4 @@
import { FunctionTool, tool } from "@llamaindex/core/tools";
import { FunctionTool } from "@llamaindex/core/tools";
import { describe, expect, test, vi } from "vitest";
import { z } from "zod";
@@ -27,41 +27,6 @@ describe("FunctionTool", () => {
});
});
test("create with execute attribute", async () => {
// Mock function to be passed as execute attribute
const mockExecute = vi.fn().mockImplementation(({ content }) => {
return `File saved with content: ${content}`;
});
const config = {
name: "saveFile",
description: "Save the content into a file",
parameters: z.object({
content: z.string({
description: "The content to save into a file",
}),
}),
execute: mockExecute,
};
// Create tool using an execute attribute
const saveTool = FunctionTool.from(config);
// Call the tool and verify
const result = await saveTool.call({ content: "test content" });
expect(mockExecute).toHaveBeenCalledOnce();
expect(mockExecute).toHaveBeenCalledWith(
{ content: "test content" },
undefined,
);
expect(result).toBe("File saved with content: test content");
// Test tool alias
const saveTool2 = tool(config);
const result2 = await saveTool2.call({ content: "test content" });
expect(result2).toBe("File saved with content: test content");
});
test("bind additional argument", () => {
type AdditionalHelloArgument = {
question?: string;
-1
View File
@@ -1,5 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
-1
View File
@@ -1,5 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
-15
View File
@@ -1,20 +1,5 @@
# @llamaindex/experimental
## 0.0.161
### Patch Changes
- llamaindex@0.9.11
## 0.0.160
### Patch Changes
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- Updated dependencies [40ee761]
- llamaindex@0.9.10
## 0.0.159
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.161",
"version": "0.0.159",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
-23
View File
@@ -1,28 +1,5 @@
# llamaindex
## 0.9.11
### Patch Changes
- Updated dependencies [a8c0637]
- @llamaindex/openai@0.1.61
## 0.9.10
### Patch Changes
- c1b5be5: feat: make AgentWorkflow llm param optional
- 40ee761: Add factory methods tool, agent and multiAgent to simplify agent usage
- 40ee761: feat: add asQueryTool to index
- Updated dependencies [aea550a]
- Updated dependencies [c1b5be5]
- Updated dependencies [40ee761]
- @llamaindex/openai@0.1.60
- @llamaindex/workflow@0.0.16
- @llamaindex/core@0.5.8
- @llamaindex/cloud@3.0.9
- @llamaindex/node-parser@1.0.8
## 0.9.9
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "llamaindex",
"version": "0.9.11",
"version": "0.9.9",
"license": "MIT",
"type": "module",
"keywords": [
-1
View File
@@ -67,7 +67,6 @@ export * from "@llamaindex/core/storage/index-store";
export * from "@llamaindex/core/storage/kv-store";
export * from "@llamaindex/core/utils";
export * from "@llamaindex/openai";
export * from "@llamaindex/workflow";
export * from "@llamaindex/workflow/agent";
export * from "./agent/index.js";
export * from "./cloud/index.js";
@@ -2,21 +2,15 @@ import type {
BaseChatEngine,
ContextChatEngineOptions,
} from "@llamaindex/core/chat-engine";
import type { ToolMetadata } from "@llamaindex/core/llms";
import type { BaseQueryEngine } from "@llamaindex/core/query-engine";
import type { BaseSynthesizer } from "@llamaindex/core/response-synthesizers";
import type { BaseRetriever } from "@llamaindex/core/retriever";
import type { BaseNode, Document } from "@llamaindex/core/schema";
import type { BaseDocumentStore } from "@llamaindex/core/storage/doc-store";
import type { BaseIndexStore } from "@llamaindex/core/storage/index-store";
import type { JSONSchemaType } from "ajv";
import { runTransformations } from "../ingestion/IngestionPipeline.js";
import { Settings } from "../Settings.js";
import type { StorageContext } from "../storage/StorageContext.js";
import {
type QueryEngineParam,
QueryEngineTool,
} from "../tools/QueryEngineTool.js";
export interface BaseIndexInit<T> {
storageContext: StorageContext;
@@ -25,24 +19,6 @@ export interface BaseIndexInit<T> {
indexStruct: T;
}
/**
* Common parameter type for queryTool and asQueryTool
*/
export type QueryToolParams = (
| {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any;
retriever?: never;
}
| {
options?: never;
retriever?: BaseRetriever;
}
) & {
responseSynthesizer?: BaseSynthesizer;
metadata?: ToolMetadata<JSONSchemaType<QueryEngineParam>> | undefined;
};
/**
* Indexes are the data structure that we store our nodes and embeddings in so
* they can be retrieved for our queries.
@@ -85,22 +61,6 @@ export abstract class BaseIndex<T> {
options?: Omit<ContextChatEngineOptions, "retriever">,
): BaseChatEngine;
/**
* Returns a query tool by calling asQueryEngine.
* Either options or retriever can be passed, but not both.
* If options are provided, they are passed to generate a retriever.
*/
asQueryTool(params: QueryToolParams): QueryEngineTool {
if (params.options) {
params.retriever = this.asRetriever(params.options);
}
return new QueryEngineTool({
queryEngine: this.asQueryEngine(params),
metadata: params?.metadata,
});
}
/**
* Insert a document into the index.
* @param document
@@ -116,33 +76,4 @@ export abstract class BaseIndex<T> {
refDocId: string,
deleteFromDocStore?: boolean,
): Promise<void>;
/**
* Alias for asRetriever
* @param options
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
retriever(options?: any): BaseRetriever {
return this.asRetriever(options);
}
/**
* Alias for asQueryEngine
* @param options you can supply your own custom Retriever and ResponseSynthesizer
*/
queryEngine(options?: {
retriever?: BaseRetriever;
responseSynthesizer?: BaseSynthesizer;
}): BaseQueryEngine {
return this.asQueryEngine(options);
}
/**
* Alias for asQueryTool
* Either options or retriever can be passed, but not both.
* If options are provided, they are passed to generate a retriever.
*/
queryTool(params: QueryToolParams): QueryEngineTool {
return this.asQueryTool(params);
}
}
@@ -19,7 +19,7 @@ const DEFAULT_PARAMETERS: JSONSchemaType<QueryEngineParam> = {
export type QueryEngineToolParams = {
queryEngine: BaseQueryEngine;
metadata?: ToolMetadata<JSONSchemaType<QueryEngineParam>> | undefined;
metadata: ToolMetadata<JSONSchemaType<QueryEngineParam>>;
};
export type QueryEngineParam = {
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/node-parser
## 1.0.8
### Patch Changes
- Updated dependencies [40ee761]
- @llamaindex/core@0.5.8
## 1.0.7
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/node-parser",
"version": "1.0.8",
"version": "1.0.7",
"description": "Node parser for LlamaIndex",
"type": "module",
"exports": {
-1
View File
@@ -1,5 +1,4 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
@@ -1,13 +1,5 @@
# @llamaindex/anthropic
## 0.2.6
### Patch Changes
- aea550a: Add factory convenience factory for each LLM provider, e.g. you can use openai instead of 'new OpenAI'
- Updated dependencies [40ee761]
- @llamaindex/core@0.5.8
## 0.2.5
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/anthropic",
"description": "Anthropic Adapter for LlamaIndex",
"version": "0.2.6",
"version": "0.2.5",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
+14 -2
View File
@@ -1,2 +1,14 @@
export * from "./agent";
export * from "./llm";
export {
AnthropicAgent,
AnthropicAgentWorker,
type AnthropicAgentParams,
} from "./agent";
export {
ALL_AVAILABLE_ANTHROPIC_LEGACY_MODELS,
ALL_AVAILABLE_ANTHROPIC_MODELS,
ALL_AVAILABLE_V3_5_MODELS,
ALL_AVAILABLE_V3_MODELS,
Anthropic,
AnthropicSession,
type AnthropicAdditionalChatOptions,
} from "./llm";
+1 -9
View File
@@ -60,7 +60,7 @@ const defaultAnthropicSession: {
* @param options
* @returns
*/
function getAnthropicSession(options: ClientOptions = {}) {
export function getAnthropicSession(options: ClientOptions = {}) {
let session = defaultAnthropicSession.find((session) => {
return isDeepEqual(session.options, options);
})?.session;
@@ -586,11 +586,3 @@ export class Anthropic extends ToolCallLLM<
};
}
}
/**
* Convenience function to create a new Anthropic instance.
* @param init - Optional initialization parameters for the Anthropic instance.
* @returns A new Anthropic instance.
*/
export const anthropic = (init?: ConstructorParameters<typeof Anthropic>[0]) =>
new Anthropic(init);
-16
View File
@@ -1,21 +1,5 @@
# @llamaindex/clip
## 0.0.45
### Patch Changes
- Updated dependencies [a8c0637]
- @llamaindex/openai@0.1.61
## 0.0.44
### Patch Changes
- Updated dependencies [aea550a]
- Updated dependencies [40ee761]
- @llamaindex/openai@0.1.60
- @llamaindex/core@0.5.8
## 0.0.43
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/clip",
"description": "Clip Embedding Adapter for LlamaIndex",
"version": "0.0.45",
"version": "0.0.43",
"type": "module",
"types": "dist/index.d.ts",
"main": "dist/index.cjs",
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/cohere
## 0.0.13
### Patch Changes
- Updated dependencies [40ee761]
- @llamaindex/core@0.5.8
## 0.0.12
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/cohere",
"description": "Cohere Adapter for LlamaIndex",
"version": "0.0.13",
"version": "0.0.12",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
-17
View File
@@ -1,22 +1,5 @@
# @llamaindex/deepinfra
## 0.0.45
### Patch Changes
- Updated dependencies [a8c0637]
- @llamaindex/openai@0.1.61
## 0.0.44
### Patch Changes
- aea550a: Add factory convenience factory for each LLM provider, e.g. you can use openai instead of 'new OpenAI'
- Updated dependencies [aea550a]
- Updated dependencies [40ee761]
- @llamaindex/openai@0.1.60
- @llamaindex/core@0.5.8
## 0.0.43
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/deepinfra",
"description": "Deepinfra Adapter for LlamaIndex",
"version": "0.0.45",
"version": "0.0.43",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
+1 -1
View File
@@ -3,4 +3,4 @@ export {
type DeepInfraEmbeddingResponse,
type InferenceStatus,
} from "./embedding";
export * from "./llm";
export { DeepInfra } from "./llm";
-8
View File
@@ -31,11 +31,3 @@ export class DeepInfra extends OpenAI {
});
}
}
/**
* Convenience function to create a new DeepInfra instance.
* @param init - Optional initialization parameters for the DeepInfra instance.
* @returns A new DeepInfra instance.
*/
export const deepinfra = (init?: ConstructorParameters<typeof DeepInfra>[0]) =>
new DeepInfra(init);
-15
View File
@@ -1,20 +1,5 @@
# @llamaindex/deepseek
## 0.0.5
### Patch Changes
- Updated dependencies [a8c0637]
- @llamaindex/openai@0.1.61
## 0.0.4
### Patch Changes
- aea550a: Add factory convenience factory for each LLM provider, e.g. you can use openai instead of 'new OpenAI'
- Updated dependencies [aea550a]
- @llamaindex/openai@0.1.60
## 0.0.3
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/deepseek",
"description": "DeepSeek Adapter for LlamaIndex",
"version": "0.0.5",
"version": "0.0.3",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
-8
View File
@@ -35,11 +35,3 @@ export class DeepSeekLLM extends OpenAI {
});
}
}
/**
* Convenience function to create a new DeepSeekLLM instance.
* @param init - Optional initialization parameters for the DeepSeekLLM instance.
* @returns A new DeepSeekLLM instance.
*/
export const deepseek = (init?: ConstructorParameters<typeof DeepSeekLLM>[0]) =>
new DeepSeekLLM(init);
-15
View File
@@ -1,20 +1,5 @@
# @llamaindex/fireworks
## 0.0.5
### Patch Changes
- Updated dependencies [a8c0637]
- @llamaindex/openai@0.1.61
## 0.0.4
### Patch Changes
- aea550a: Add factory convenience factory for each LLM provider, e.g. you can use openai instead of 'new OpenAI'
- Updated dependencies [aea550a]
- @llamaindex/openai@0.1.60
## 0.0.3
### Patch Changes

Some files were not shown because too many files have changed in this diff Show More