Skip to content

Commit

Permalink
Merge branch 'mfortman11/astra' of github.com:run-llama/LlamaIndexTS …
Browse files Browse the repository at this point in the history
…into astra
  • Loading branch information
mfortman11 committed Feb 5, 2024
2 parents 8d34e13 + e0885f8 commit 790bc34
Show file tree
Hide file tree
Showing 121 changed files with 4,443 additions and 618 deletions.
5 changes: 0 additions & 5 deletions .changeset/bright-tips-develop.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/smart-eels-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

fix: update `VectorIndexRetriever` constructor parameters' type.
6 changes: 6 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ jobs:
- name: Build create-llama
run: pnpm run build
working-directory: ./packages/create-llama
- name: Pack
run: pnpm pack --pack-destination ./output
working-directory: ./packages/create-llama
- name: Extract Pack
run: tar -xvzf ./output/*.tgz -C ./output
working-directory: ./packages/create-llama
- name: Run Playwright tests
run: pnpm exec playwright test
env:
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
"jest.rootPath": "./packages/core",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ main();
Then you can run it using

```bash
pnpx ts-node example.ts
pnpm dlx ts-node example.ts
```

## Playground
Expand Down
6 changes: 6 additions & 0 deletions apps/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# docs

## 0.0.2

### Patch Changes

- 0f64084: docs: update API references

## 0.0.1

### Patch Changes
Expand Down
85 changes: 85 additions & 0 deletions apps/docs/docs/examples/agent.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Agents

A built-in agent that can take decisions and reasoning based on the tools provided to it.

## OpenAI Agent

```ts
import { FunctionTool, OpenAIAgent } from "llamaindex";

// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}

// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}

// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};

// Define the parameters of the divide function as a JSON schema
const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend to divide",
},
b: {
type: "number",
description: "The divisor to divide by",
},
},
required: ["a", "b"],
};

async function main() {
// Create a function tool from the sum function
const sumFunctionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});

// Create a function tool from the divide function
const divideFunctionTool = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers"
parameters: divideJSON,
});

// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});

// Chat with the agent
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});

// Print the response
console.log(String(response));
}

main().then(() => {
console.log("Done");
});
```
4 changes: 2 additions & 2 deletions apps/docs/docs/getting_started/concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ LlamaIndex.TS help you prepare the knowledge base with a suite of data connector
[**Data Loaders**](../modules/data_loader.md):
A data connector (i.e. `Reader`) ingest data from different data sources and data formats into a simple `Document` representation (text and simple metadata).

[**Documents / Nodes**](../modules/documents_and_nodes.md): A `Document` is a generic container around any data source - for instance, a PDF, an API output, or retrieved data from a database. A `Node` is the atomic unit of data in LlamaIndex and represents a "chunk" of a source `Document`. It's a rich representation that includes metadata and relationships (to other nodes) to enable accurate and expressive retrieval operations.
[**Documents / Nodes**](../modules/documents_and_nodes/index.md): A `Document` is a generic container around any data source - for instance, a PDF, an API output, or retrieved data from a database. A `Node` is the atomic unit of data in LlamaIndex and represents a "chunk" of a source `Document`. It's a rich representation that includes metadata and relationships (to other nodes) to enable accurate and expressive retrieval operations.

[**Data Indexes**](../modules/data_index.md):
Once you've ingested your data, LlamaIndex helps you index data into a format that's easy to retrieve.
Expand Down Expand Up @@ -69,7 +69,7 @@ A response synthesizer generates a response from an LLM, using a user query and

#### Pipelines

[**Query Engines**](../modules/query_engine.md):
[**Query Engines**](../modules/query_engines):
A query engine is an end-to-end pipeline that allow you to ask question over your data.
It takes in a natural language query, and returns a response, along with reference context retrieved and passed to the LLM.

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/docs/getting_started/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ Our examples use OpenAI by default. You'll need to set up your Open AI key like
export OPENAI_API_KEY="sk-......" # Replace with your key from https://platform.openai.com/account/api-keys
```

If you want to have it automatically loaded every time, add it to your .zshrc/.bashrc.
If you want to have it automatically loaded every time, add it to your `.zshrc/.bashrc`.

WARNING: do not check in your OpenAI key into version control.
6 changes: 3 additions & 3 deletions apps/docs/docs/getting_started/starter.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);
const response = await queryEngine.query({
query: "What did the author do in college?",
});

// Output response
console.log(response.toString());
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ For more complex applications, our lower-level APIs allow advanced users to cust

`npm install llamaindex`

Our documentation includes [Installation Instructions](./installation.mdx) and a [Starter Tutorial](./starter.md) to build your first application.
Our documentation includes [Installation Instructions](./getting_started/installation.mdx) and a [Starter Tutorial](./getting_started/starter.md) to build your first application.

Once you're up and running, [High-Level Concepts](./getting_started/concepts.md) has an overview of LlamaIndex's modular architecture. For more hands-on practical examples, look through our [End-to-End Tutorials](./end_to_end.md).
Once you're up and running, [High-Level Concepts](./getting_started/concepts.md) has an overview of LlamaIndex's modular architecture. For more hands-on practical examples, look through our Examples section on the sidebar.

## 🗺️ Ecosystem

Expand Down
1 change: 1 addition & 0 deletions apps/docs/docs/modules/agent/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label: "Agents"
14 changes: 14 additions & 0 deletions apps/docs/docs/modules/agent/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Agents

An “agent” is an automated reasoning and decision engine. It takes in a user input/query and can make internal decisions for executing that query in order to return the correct result. The key agent components can include, but are not limited to:

- Breaking down a complex question into smaller ones
- Choosing an external Tool to use + coming up with parameters for calling the Tool
- Planning out a set of tasks
- Storing previously completed tasks in a memory module

## Getting Started

LlamaIndex.TS comes with a few built-in agents, but you can also create your own. The built-in agents include:

- [OpenAI Agent](./openai.mdx)
183 changes: 183 additions & 0 deletions apps/docs/docs/modules/agent/openai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# OpenAI Agent

OpenAI API that supports function calling, it’s never been easier to build your own agent!

In this notebook tutorial, we showcase how to write your own OpenAI agent

## Setup

First, you need to install the `llamaindex` package. You can do this by running the following command in your terminal:

```bash
pnpm i llamaindex
```

Then we can define a function to sum two numbers and another function to divide two numbers.

```ts
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}

// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}
```

## Create a function tool

Now we can create a function tool from the sum function and another function tool from the divide function.

For the parameters of the sum function, we can define a JSON schema.

### JSON Schema

```ts
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};

const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend a to divide",
},
b: {
type: "number",
description: "The divisor b to divide by",
},
},
required: ["a", "b"],
};

const sumFunctionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});

const divideFunctionTool = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});
```

## Create an OpenAIAgent

Now we can create an OpenAIAgent with the function tools.

```ts
const worker = new OpenAIAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});
```

## Chat with the agent

Now we can chat with the agent.

```ts
const response = await worker.chat({
message: "How much is 5 + 5? then divide by 2",
});

console.log(String(response));
```

## Full code

```ts
import { FunctionTool, OpenAIAgent } from "llamaindex";

// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}

// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}

// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};

// Define the parameters of the divide function as a JSON schema
const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The argument a to divide",
},
b: {
type: "number",
description: "The argument b to divide",
},
},
required: ["a", "b"],
};

async function main() {
// Create a function tool from the sum function
const sumFunctionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});

// Create a function tool from the divide function
const divideFunctionTool = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});

// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});

// Chat with the agent
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});

// Print the response
console.log(String(response));
}

main().then(() => {
console.log("Done");
});
```
Loading

0 comments on commit 790bc34

Please sign in to comment.