Compare commits

..

71 Commits

Author SHA1 Message Date
Marcus Schiesser 2048698f77 docs: add interactive chat for anthropic 2024-03-05 11:15:50 +07:00
Emanuel Ferreira 9942979aa7 feat: Claude 3 (#604) 2024-03-04 15:02:18 -08:00
Alex Yang 3c2655a1f9 fix: .tsbuildinfo 2024-03-04 16:05:45 -06:00
Marcus Schiesser 552a61a66f Add quantized parameter to HuggingFaceEmbedding (#601) 2024-03-04 12:10:40 +07:00
Alex Yang d13143e322 RELEASING: Releasing 3 package(s)
Releases:
  llamaindex@0.1.20
  @llamaindex/env@0.0.5
  docs@0.0.4

[skip ci]
2024-03-02 18:42:24 -06:00
Alex Yang 5116ad8d08 fix: compatibility issue with Deno (#598) 2024-03-02 18:40:01 -06:00
Emanuel Ferreira 64683a55f3 fix: prefix messages always true (#596) 2024-03-01 21:45:02 -03:00
Emanuel Ferreira 698cd9c631 fix: step wise agent + examples (#594) 2024-03-01 21:28:02 -03:00
Alex Yang c744a99102 chore: bump @llamaindex/cloud (#595) 2024-03-01 17:22:50 -06:00
Huu Le (Lee) 2d2935085e feat: Add use LlamaParse option to create-llama (#591) 2024-03-01 16:54:06 +07:00
Marcus Schiesser 1b31e2c8cd chore: update @llamaindex/cloud to 0.0.2 2024-03-01 15:59:10 +07:00
Thuc Pham 7257751993 fix: empty store bugs (#592)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-03-01 15:04:10 +07:00
Marcus Schiesser de6bfdb1b1 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.19

[skip ci]
2024-02-29 15:35:13 +07:00
Marcus Schiesser 9e49f4411b fix: copy README and license 2024-02-29 15:34:27 +07:00
Thuc Pham 026d068ddf feat: enhance pinecone usage (#586) 2024-02-29 15:34:08 +07:00
Marcus Schiesser 7055d6fc3c docs: add OpenAIEmbedding to examples 2024-02-29 11:11:43 +07:00
Alex Yang e9c2366bf1 fix: allow passing model metadata (#588) 2024-02-29 10:41:06 +07:00
Alex Yang 6278152e49 fix: lazy import pg (#584) 2024-02-27 19:16:54 -06:00
Emanuel Ferreira 76010c0cea chore: remove duplicated example and minor example update (#582) 2024-02-27 09:02:37 -03:00
Emanuel Ferreira 889b84cfb9 docs: remove query engine from correctness evaluator (#581) 2024-02-27 08:15:41 -03:00
Marcus Schiesser a26681c416 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.18

[skip ci]
2024-02-27 13:45:28 +07:00
Thuc Pham 90027a7b44 fix: enable split long sentence by default (#568)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-02-27 13:44:04 +07:00
Emanuel Ferreira aab56faf88 refactor: qdrant minor updates (#580) 2024-02-26 22:13:45 -03:00
Emanuel Ferreira c57bd11c45 feat: update and refactor title extractor (#579) 2024-02-26 21:49:07 -03:00
Alex Yang 3fa1e29468 RELEASING: Releasing 3 package(s)
Releases:
  llamaindex@0.1.17
  @llamaindex/env@0.0.4
  docs@0.0.3

[skip ci]
2024-02-26 17:01:47 -06:00
Alex Yang cf87f84900 fix: type backward compatibility (#578) 2024-02-26 16:59:09 -06:00
Alex Yang 402d4ef013 docs: update tutorial (#576) 2024-02-26 14:11:09 -06:00
Alex Yang fc94906a1e fix: keep dynamic import in cjs (#575) 2024-02-26 12:27:17 -06:00
Alex Yang b83fcd11e4 fix(core): type generation (#574) 2024-02-26 12:15:40 -06:00
Emanuel Ferreira c28af7c7bc chore: remove storage context from multi_doc_agent example (#572) 2024-02-26 12:05:30 -03:00
Emanuel Ferreira dbc853bcc5 chore: fix paths and docs (#569) 2024-02-26 10:37:08 -03:00
Emanuel Ferreira c8396c5a3c feat: add base evaluator and correctness evaluator (#559) 2024-02-26 09:38:56 -03:00
Thuc Pham 65af8d3a26 fix: missing dependency for local development (#566) 2024-02-26 15:54:47 +07:00
Marcus Schiesser 329b6ec958 fix: SummaryIndex and VectorStoreIndex must be able to share storage context (#567) 2024-02-26 15:52:33 +07:00
Graden Rea 09bf27abd7 feat: Add Groq LLM integration (#561) 2024-02-26 13:46:27 +07:00
Alex Yang 2ec6a529c7 RELEASING: Releasing 2 package(s)
Releases:
  llamaindex@0.1.16
  @llamaindex/env@0.0.3

[skip ci]
2024-02-23 19:03:04 -06:00
Alex Yang e8e21a0e4e docs(changeset): build: set files in package.json 2024-02-23 19:02:42 -06:00
Alex Yang 88d243f145 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.15

[skip ci]
2024-02-23 18:57:10 -06:00
Alex Yang 3a6e287443 feat: enable verbatimModuleSyntax (#562) 2024-02-23 18:56:44 -06:00
Alex Yang beb3e5cd7f RELEASING: Releasing 2 package(s)
Releases:
  llamaindex@0.1.14
  @llamaindex/env@0.0.2

[skip ci]
2024-02-23 18:09:31 -06:00
Alex Yang 7416a87e10 build: cjs file not found 2024-02-23 18:09:15 -06:00
Alex Yang 65b85b237e RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.13

[skip ci]
2024-02-23 17:59:06 -06:00
Alex Yang b17a80014a fix: unset private package 2024-02-23 17:58:46 -06:00
Alex Yang ff87f99807 fix: avoid publishing test package 2024-02-23 17:56:38 -06:00
Alex Yang 65d834615d docs(changeset): feat: abstract @llamaindex/env package 2024-02-23 17:45:43 -06:00
Alex Yang b8be4c09e2 docs(changeset): build: use ESM as default 2024-02-23 17:45:01 -06:00
Alex Yang e5fb332538 build: leave code as-is (#560)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-02-23 17:39:25 -06:00
Marcus Schiesser 491033d534 fix: lint errors 2024-02-23 14:55:45 +07:00
Marcus Schiesser 885fa316a5 chore: add prefer const lint 2024-02-23 14:45:17 +07:00
Marcus Schiesser b6ed679771 RELEASING: Releasing 1 package(s)
Releases:
  create-llama@0.0.26

[skip ci]
2024-02-23 12:00:37 +07:00
Marcus Schiesser 68fc6e8b50 fix: don't need similarityTopK parameter for LlamaCloud 2024-02-23 11:43:22 +07:00
Marcus Schiesser ea0331ef5a refactor: simplify generated python code (#558)
Co-authored-by: leehuwuj <leehuwuj@gmail.com>
2024-02-23 11:14:02 +07:00
Marcus Schiesser 6827e245b8 fix: don't allow runApp for LlamaPacks 2024-02-22 16:20:57 +07:00
Huu Le (Lee) ef25d6960c Upgrade llama-index version to v0.10+ for create-llama (#556) 2024-02-22 13:50:53 +07:00
Marcus Schiesser f740f44cf2 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.12

[skip ci]
2024-02-22 12:02:35 +07:00
Marcus Schiesser a5e4e6d857 Add support for LlamaCloud (#554) 2024-02-22 11:59:32 +07:00
Thuc Pham cfdd6db530 feat: add pinecone support to create llama (#555) 2024-02-22 09:55:50 +07:00
Marcus Schiesser d4eda9f396 docs: add llamaparse docs (#553) 2024-02-21 14:37:13 +07:00
Alex Yang a433b107e0 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.11

[skip ci]
2024-02-20 12:58:54 -06:00
Yufei (Benny) Chen 59f9fb6c3f Add Fireworks to LlamaIndex (#539)
Co-authored-by: Emanuel Ferreira <contatoferreirads@gmail.com>
2024-02-20 10:53:09 -03:00
Thuc Pham 09d532ebcc feat(create-llama-pack): generate llama pack project from llama index (#549) 2024-02-20 08:22:54 +07:00
Emanuel Ferreira cfd6f3ca8c feat: prompt mixin (#543) 2024-02-18 18:44:08 -03:00
Emanuel Ferreira 95add73c38 feat: multi-document agents (#531) 2024-02-18 18:43:52 -03:00
Emanuel Ferreira 9ee036b160 docs: remove duplicate embedding (#545) 2024-02-13 23:21:13 -03:00
Erick Sosa Garcia c2b521199c fix: some errors in available llms examples (#544) 2024-02-13 21:16:47 -03:00
Emanuel Ferreira ee9f3f373a refactor: openai agent and utils (#542) 2024-02-11 20:24:36 -03:00
Emanuel Ferreira f205358587 feat: add markdown node parser (#541) 2024-02-11 20:23:49 -03:00
Emanuel Ferreira 255ae7dced fix: react agent history (#540) 2024-02-11 18:46:55 -03:00
Emanuel Ferreira b4c6d509a0 docs: available embeddings (#538) 2024-02-10 22:10:20 -03:00
Emanuel Ferreira 34cd57b639 feat(react): add react agent (#511) 2024-02-10 20:52:11 -03:00
yisding 50dfd7bf60 dep security vulns (#537) 2024-02-11 04:37:42 +08:00
360 changed files with 10306 additions and 5213 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"create-llama": patch
---
Add LlamaParse option when selecting a pdf file or a folder
+5
View File
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---
Add quantized parameter to HuggingFaceEmbedding
-5
View File
@@ -1,5 +0,0 @@
---
"llamaindex": patch
---
feat: add filtering of metadata to PGVectorStore
-5
View File
@@ -1,5 +0,0 @@
---
"llamaindex": patch
---
feat(reranker): cohere reranker
-5
View File
@@ -1,5 +0,0 @@
---
"llamaindex": patch
---
feat: use batching in vector store index
-5
View File
@@ -1,5 +0,0 @@
---
"create-llama": patch
---
update fastapi for CVE-2024-24762
-5
View File
@@ -1,5 +0,0 @@
---
"llamaindex": patch
---
Add reader for LlamaParse
+2 -1
View File
@@ -9,6 +9,7 @@ module.exports = {
},
rules: {
"max-params": ["error", 4],
"prefer-const": "error",
},
ignorePatterns: ["dist/"],
ignorePatterns: ["dist/", "lib/"],
};
+5 -2
View File
@@ -61,10 +61,13 @@ jobs:
run: pnpm run build --filter llamaindex
- name: Copy examples
run: rsync -rv --exclude=node_modules ./examples ${{ runner.temp }}
- name: Pack
- name: Pack @llamaindex/env
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/env
- name: Pack llamaindex
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/core
- name: Install llamaindex
- name: Install
run: npm add ${{ runner.temp }}/*.tgz
working-directory: ${{ runner.temp }}/examples
- name: Run Type Check
+1
View File
@@ -44,6 +44,7 @@ test-results/
playwright-report/
blob-report/
playwright/.cache/
.tsbuildinfo
# intellij
**/.idea
-1
View File
@@ -5,7 +5,6 @@
"[xml]": {
"editor.defaultFormatter": "redhat.vscode-xml"
},
"jest.rootPath": "./packages/core",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
+2
View File
@@ -125,8 +125,10 @@ module.exports = nextConfig;
- OpenAI GPT-3.5-turbo and GPT-4
- Anthropic Claude Instant and Claude 2
- Groq LLMs
- Llama2 Chat LLMs (70B, 13B, and 7B parameters)
- MistralAI Chat LLMs
- Fireworks Chat LLMs
## Contributing:
+15
View File
@@ -1,5 +1,20 @@
# docs
## 0.0.4
### Patch Changes
- Updated dependencies [5116ad8]
- @llamaindex/env@0.0.5
## 0.0.3
### Patch Changes
- 09bf27a: Add Groq LLM to LlamaIndex
- Updated dependencies [cf87f84]
- @llamaindex/env@0.0.4
## 0.0.2
### Patch Changes
-56
View File
@@ -1,56 +0,0 @@
---
sidebar_position: 1
---
# Starter Tutorial
Once you have [installed LlamaIndex.TS using NPM](installation) and set up your OpenAI key, you're ready to start your first app:
In a new folder:
```bash npm2yarn
npm install typescript
npm install @types/node
npx tsc --init # if needed
```
Create the file `example.ts`. This code will load some example data, create a document, index it (which creates embeddings using OpenAI), and then creates query engine to answer questions about the data.
```ts
// example.ts
import fs from "fs/promises";
import { Document, VectorStoreIndex } from "llamaindex";
async function main() {
// Load essay from abramov.txt in Node
const essay = await fs.readFile(
"node_modules/llamaindex/examples/abramov.txt",
"utf-8",
);
// Create Document object with essay
const document = new Document({ text: essay });
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments([document]);
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What did the author do in college?",
});
// Output response
console.log(response.toString());
}
main();
```
Then you can run it using
```bash
npx ts-node example.ts
```
Ready to learn more? Check out our NextJS playground at https://llama-playground.vercel.app/. The source is available at https://github.com/run-llama/ts-playground
@@ -0,0 +1,51 @@
---
sidebar_position: 1
---
import CodeBlock from "@theme/CodeBlock";
import CodeSource from "!raw-loader!../../../../examples/vectorIndex";
import TSConfigSource from "!!raw-loader!../../../../examples/tsconfig.json";
# Starter Tutorial
Make sure you have installed LlamaIndex.TS and have an OpenAI key. If you haven't, check out the [installation](installation) guide.
## From scratch(node.js + TypeScript):
In a new folder:
```bash npm2yarn
npm init
npm install -D typescript @types/node
```
Create the file `example.ts`. This code will load some example data, create a document, index it (which creates embeddings using OpenAI), and then creates query engine to answer questions about the data.
<CodeBlock language="ts">{CodeSource}</CodeBlock>
Create a `tsconfig.json` file in the same folder:
<CodeBlock language="json">{TSConfigSource}</CodeBlock>
Now you can run the code with
```bash
npx tsx example.ts
```
Also, you can clone our examples and try them out:
```bash npm2yarn
npx degit run-llama/LlamaIndexTS/examples my-new-project
cd my-new-project
npm install
npx tsx ./vectorIndex.ts
```
## From scratch (Next.js + TypeScript):
You just need one command to create a new Next.js project:
```bash npm2yarn
npx create-llama@latest
```
+1 -1
View File
@@ -37,7 +37,7 @@ For more complex applications, our lower-level APIs allow advanced users to cust
`npm install llamaindex`
Our documentation includes [Installation Instructions](./getting_started/installation.mdx) and a [Starter Tutorial](./getting_started/starter.md) to build your first application.
Our documentation includes [Installation Instructions](./getting_started/installation.mdx) and a [Starter Tutorial](./getting_started/starter.mdx) 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 Examples section on the sidebar.
@@ -1 +1,2 @@
label: "Agents"
position: 3
@@ -0,0 +1,314 @@
# Multi-Document Agent
In this guide, you learn towards setting up an agent that can effectively answer different types of questions over a larger set of documents.
These questions include the following
- QA over a specific doc
- QA comparing different docs
- Summaries over a specific doc
- Comparing summaries between different docs
We do this with the following architecture:
- setup a “document agent” over each Document: each doc agent can do QA/summarization within its doc
- setup a top-level agent over this set of document agents. Do tool retrieval and then do CoT over the set of tools to answer a question.
## Setup and Download Data
We first start by installing the necessary libraries and downloading the data.
```bash
pnpm i llamaindex
```
```ts
import {
Document,
ObjectIndex,
OpenAI,
OpenAIAgent,
QueryEngineTool,
SimpleNodeParser,
SimpleToolNodeMapping,
SummaryIndex,
VectorStoreIndex,
serviceContextFromDefaults,
storageContextFromDefaults,
} from "llamaindex";
```
And then for the data we will run through a list of countries and download the wikipedia page for each country.
```ts
import fs from "fs";
import path from "path";
const dataPath = path.join(__dirname, "tmp_data");
const extractWikipediaTitle = async (title: string) => {
const fileExists = fs.existsSync(path.join(dataPath, `${title}.txt`));
if (fileExists) {
console.log(`File already exists for the title: ${title}`);
return;
}
const queryParams = new URLSearchParams({
action: "query",
format: "json",
titles: title,
prop: "extracts",
explaintext: "true",
});
const url = `https://en.wikipedia.org/w/api.php?${queryParams}`;
const response = await fetch(url);
const data: any = await response.json();
const pages = data.query.pages;
const page = pages[Object.keys(pages)[0]];
const wikiText = page.extract;
await new Promise((resolve) => {
fs.writeFile(path.join(dataPath, `${title}.txt`), wikiText, (err: any) => {
if (err) {
console.error(err);
resolve(title);
return;
}
console.log(`${title} stored in file!`);
resolve(title);
});
});
};
```
```ts
export const extractWikipedia = async (titles: string[]) => {
if (!fs.existsSync(dataPath)) {
fs.mkdirSync(dataPath);
}
for await (const title of titles) {
await extractWikipediaTitle(title);
}
console.log("Extration finished!");
```
These files will be saved in the `tmp_data` folder.
Now we can call the function to download the data for each country.
```ts
await extractWikipedia([
"Brazil",
"United States",
"Canada",
"Mexico",
"Argentina",
"Chile",
"Colombia",
"Peru",
"Venezuela",
"Ecuador",
"Bolivia",
"Paraguay",
"Uruguay",
"Guyana",
"Suriname",
"French Guiana",
"Falkland Islands",
]);
```
## Load the data
Now that we have the data, we can load it into the LlamaIndex and store as a document.
```ts
import { Document } from "llamaindex";
const countryDocs: Record<string, Document> = {};
for (const title of wikiTitles) {
const path = `./agent/helpers/tmp_data/${title}.txt`;
const text = await fs.readFile(path, "utf-8");
const document = new Document({ text: text, id_: path });
countryDocs[title] = document;
}
```
## Setup LLM and StorageContext
We will be using gpt-4 for this example and we will use the `StorageContext` to store the documents in-memory.
```ts
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({ llm });
const storageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
```
## Building Multi-Document Agents
In this section we show you how to construct the multi-document agent. We first build a document agent for each document, and then define the top-level parent agent with an object index.
```ts
const documentAgents: Record<string, any> = {};
const queryEngines: Record<string, any> = {};
```
Now we iterate over each country and create a document agent for each one.
### Build Agent for each Document
In this section we define “document agents” for each document.
We define both a vector index (for semantic search) and summary index (for summarization) for each document. The two query engines are then converted into tools that are passed to an OpenAI function calling agent.
This document agent can dynamically choose to perform semantic search or summarization within a given document.
We create a separate document agent for each coutnry.
```ts
for (const title of wikiTitles) {
// parse the document into nodes
const nodes = new SimpleNodeParser({
chunkSize: 200,
chunkOverlap: 20,
}).getNodesFromDocuments([countryDocs[title]]);
// create the vector index for specific search
const vectorIndex = await VectorStoreIndex.init({
serviceContext: serviceContext,
storageContext: storageContext,
nodes,
});
// create the summary index for broader search
const summaryIndex = await SummaryIndex.init({
serviceContext: serviceContext,
nodes,
});
const vectorQueryEngine = summaryIndex.asQueryEngine();
const summaryQueryEngine = summaryIndex.asQueryEngine();
// create the query engines for each task
const queryEngineTools = [
new QueryEngineTool({
queryEngine: vectorQueryEngine,
metadata: {
name: "vector_tool",
description: `Useful for questions related to specific aspects of ${title} (e.g. the history, arts and culture, sports, demographics, or more).`,
},
}),
new QueryEngineTool({
queryEngine: summaryQueryEngine,
metadata: {
name: "summary_tool",
description: `Useful for any requests that require a holistic summary of EVERYTHING about ${title}. For questions about more specific sections, please use the vector_tool.`,
},
}),
];
// create the document agent
const agent = new OpenAIAgent({
tools: queryEngineTools,
llm,
verbose: true,
});
documentAgents[title] = agent;
queryEngines[title] = vectorIndex.asQueryEngine();
}
```
## Build Top-Level Agent
Now we define the top-level agent that can answer questions over the set of document agents.
This agent takes in all document agents as tools. This specific agent RetrieverOpenAIAgent performs tool retrieval before tool use (unlike a default agent that tries to put all tools in the prompt).
Here we use a top-k retriever, but we encourage you to customize the tool retriever method!
Firstly, we create a tool for each document agent
```ts
const allTools: QueryEngineTool[] = [];
```
```ts
for (const title of wikiTitles) {
const wikiSummary = `
This content contains Wikipedia articles about ${title}.
Use this tool if you want to answer any questions about ${title}
`;
const docTool = new QueryEngineTool({
queryEngine: documentAgents[title],
metadata: {
name: `tool_${title}`,
description: wikiSummary,
},
});
allTools.push(docTool);
}
```
Our top level agent will use this document agents as tools and use toolRetriever to retrieve the best tool to answer a question.
```ts
// map the tools to nodes
const toolMapping = SimpleToolNodeMapping.fromObjects(allTools);
// create the object index
const objectIndex = await ObjectIndex.fromObjects(
allTools,
toolMapping,
VectorStoreIndex,
{
serviceContext,
storageContext,
},
);
// create the top agent
const topAgent = new OpenAIAgent({
toolRetriever: await objectIndex.asRetriever({}),
llm,
verbose: true,
prefixMessages: [
{
content:
"You are an agent designed to answer queries about a set of given countries. Please always use the tools provided to answer a question. Do not rely on prior knowledge.",
role: "system",
},
],
});
```
## Use the Agent
Now we can use the agent to answer questions.
```ts
const response = await topAgent.chat({
message: "Tell me the differences between Brazil and Canada economics?",
});
// print output
console.log(response);
```
You can find the full code for this example [here](https://github.com/run-llama/LlamaIndexTS/tree/main/examples/agent/multi-document-agent.ts)
+6 -2
View File
@@ -1,3 +1,7 @@
---
sidebar_position: 0
---
# OpenAI Agent
OpenAI API that supports function calling, its never been easier to build your own agent!
@@ -82,7 +86,7 @@ const divideFunctionTool = new FunctionTool(divideNumbers, {
Now we can create an OpenAIAgent with the function tools.
```ts
const worker = new OpenAIAgent({
const agent = new OpenAIAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});
@@ -93,7 +97,7 @@ const worker = new OpenAIAgent({
Now we can chat with the agent.
```ts
const response = await worker.chat({
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});
@@ -1,3 +1,7 @@
---
sidebar_position: 1
---
# OpenAI Agent + QueryEngineTool
QueryEngineTool is a tool that allows you to query a vector index. In this example, we will create a vector index from a set of documents and then create a QueryEngineTool from the vector index. We will then create an OpenAIAgent with the QueryEngineTool and chat with the agent.
@@ -0,0 +1,203 @@
# ReAct Agent
The ReAct agent is an AI agent that can reason over the next action, construct an action command, execute the action, and repeat these steps in an iterative loop until the task is complete.
In this notebook tutorial, we showcase how to write your ReAct agent using the `llamaindex` package.
## 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
```
And then you can import the `OpenAIAgent` and `FunctionTool` from the `llamaindex` package.
```ts
import { FunctionTool, OpenAIAgent } from "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 ReAct
Now we can create an OpenAIAgent with the function tools.
```ts
const agent = new ReActAgent({
tools: [sumFunctionTool, divideFunctionTool],
verbose: true,
});
```
## Chat with the agent
Now we can chat with the agent.
```ts
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});
console.log(String(response));
```
The output will be:
```bash
Thought: I need to use a tool to help me answer the question.
Action: sumNumbers
Action Input: {"a":5,"b":5}
Observation: 10
Thought: I can answer without using any more tools.
Answer: The sum of 5 and 5 is 10, and when divided by 2, the result is 5.
The sum of 5 and 5 is 10, and when divided by 2, the result is 5.
```
## Full code
```ts
import { FunctionTool, ReActAgent } from "llamaindex";
// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}
// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}
// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};
// 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: "I want to sum 5 and 5 and then divide by 2",
});
// Print the response
console.log(String(response));
}
main().then(() => {
console.log("Done");
});
```
+13
View File
@@ -5,6 +5,7 @@ sidebar_position: 4
import CodeBlock from "@theme/CodeBlock";
import CodeSource from "!raw-loader!../../../../examples/readers/src/simple-directory-reader";
import CodeSource2 from "!raw-loader!../../../../examples/readers/src/custom-simple-directory-reader";
import CodeSource3 from "!raw-loader!../../../../examples/readers/src/llamaparse";
# Loader
@@ -30,6 +31,18 @@ Or pass new readers for `fileExtToReader` to support more file types.
{CodeSource2}
</CodeBlock>
### LlamaParse
LlamaParse is an API created by LlamaIndex to efficiently parse files, e.g. it's great at converting PDF tables into markdown.
To use it, first login and get an API key from https://cloud.llamaindex.ai. Make sure to store the key in the environment variable `LLAMA_CLOUD_API_KEY`.
Then, you can use the `LlamaParseReader` class to read a local PDF file and convert it into a markdown document that can be used by LlamaIndex:
<CodeBlock language="ts">{CodeSource3}</CodeBlock>
Alternatively, you can set the [`resultType`](../api/classes/LlamaParseReader.md#resulttype) option to `text` to get the parsed document as a text string.
## API Reference
- [SimpleDirectoryReader](../api/classes/SimpleDirectoryReader.md)
@@ -0,0 +1,2 @@
label: "Embeddings"
position: 3
@@ -0,0 +1 @@
label: "Available Embeddings"
@@ -0,0 +1,37 @@
# HuggingFace
To use HuggingFace embeddings, you need to import `HuggingFaceEmbedding` from `llamaindex`.
```ts
import { HuggingFaceEmbedding, serviceContextFromDefaults } from "llamaindex";
const huggingFaceEmbeds = new HuggingFaceEmbedding();
const serviceContext = serviceContextFromDefaults({ embedModel: openaiEmbeds });
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
Per default, `HuggingFaceEmbedding` is using the `Xenova/all-MiniLM-L6-v2` model. You can change the model by passing the `modelType` parameter to the constructor.
If you're not using a quantized model, set the `quantized` parameter to `false`.
For example, to use the not quantized `BAAI/bge-small-en-v1.5` model, you can use the following code:
```
const embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false,
});
```
@@ -0,0 +1,29 @@
# MistralAI
To use MistralAI embeddings, you need to import `MistralAIEmbedding` from `llamaindex`.
```ts
import { MistralAIEmbedding, serviceContextFromDefaults } from "llamaindex";
const mistralEmbedModel = new MistralAIEmbedding({
apiKey: "<YOUR_API_KEY>",
});
const serviceContext = serviceContextFromDefaults({
embedModel: mistralEmbedModel,
});
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
@@ -0,0 +1,27 @@
# Ollama
To use Ollama embeddings, you need to import `Ollama` from `llamaindex`.
```ts
import { Ollama, serviceContextFromDefaults } from "llamaindex";
const ollamaEmbedModel = new Ollama();
const serviceContext = serviceContextFromDefaults({
embedModel: ollamaEmbedModel,
});
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
@@ -0,0 +1,27 @@
# OpenAI
To use OpenAI embeddings, you need to import `OpenAIEmbedding` from `llamaindex`.
```ts
import { OpenAIEmbedding, serviceContextFromDefaults } from "llamaindex";
const openaiEmbedModel = new OpenAIEmbedding();
const serviceContext = serviceContextFromDefaults({
embedModel: openaiEmbedModel,
});
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
@@ -0,0 +1,29 @@
# Together
To use together embeddings, you need to import `TogetherEmbedding` from `llamaindex`.
```ts
import { TogetherEmbedding, serviceContextFromDefaults } from "llamaindex";
const togetherEmbedModel = new TogetherEmbedding({
apiKey: "<YOUR_API_KEY>",
});
const serviceContext = serviceContextFromDefaults({
embedModel: togetherEmbedModel,
});
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
@@ -1,7 +1,3 @@
---
sidebar_position: 4
---
# Embedding
The embedding model in LlamaIndex is responsible for creating numerical representations of text. By default, LlamaIndex will use the `text-embedding-ada-002` model from OpenAI.
@@ -16,7 +12,11 @@ const openaiEmbeds = new OpenAIEmbedding();
const serviceContext = serviceContextFromDefaults({ embedModel: openaiEmbeds });
```
## Local Embedding
For local embeddings, you can use the [HuggingFace](./available_embeddings/huggingface.md) embedding model.
## API Reference
- [OpenAIEmbedding](../api/classes/OpenAIEmbedding.md)
- [ServiceContext](../api/interfaces//ServiceContext.md)
- [OpenAIEmbedding](../../api/classes/OpenAIEmbedding.md)
- [ServiceContext](../../api/interfaces//ServiceContext.md)
@@ -0,0 +1,2 @@
label: "Evaluating"
position: 3
@@ -0,0 +1,32 @@
# Evaluating
## Concept
Evaluation and benchmarking are crucial concepts in LLM development. To improve the perfomance of an LLM app (RAG, agents) you must have a way to measure it.
LlamaIndex offers key modules to measure the quality of generated results. We also offer key modules to measure retrieval quality.
- **Response Evaluation**: Does the response match the retrieved context? Does it also match the query? Does it match the reference answer or guidelines?
- **Retrieval Evaluation**: Are the retrieved sources relevant to the query?
## Response Evaluation
Evaluation of generated results can be difficult, since unlike traditional machine learning the predicted result is not a single number, and it can be hard to define quantitative metrics for this problem.
LlamaIndex offers LLM-based evaluation modules to measure the quality of results. This uses a “gold” LLM (e.g. GPT-4) to decide whether the predicted answer is correct in a variety of ways.
Note that many of these current evaluation modules do not require ground-truth labels. Evaluation can be done with some combination of the query, context, response, and combine these with LLM calls.
These evaluation modules are in the following forms:
- **Correctness**: Whether the generated answer matches that of the reference answer given the query (requires labels).
- **Faithfulness**: Evaluates if the answer is faithful to the retrieved contexts (in other words, whether if theres hallucination).
- **Relevancy**: Evaluates if the response from a query engine matches any source nodes.
## Usage
- [Correctness Evaluator](./modules/correctness.md)
- [Faithfulness Evaluator](./modules/faithfulness.md)
- [Relevancy Evaluator](./modules/relevancy.md)
@@ -0,0 +1 @@
label: "Modules"
@@ -0,0 +1,68 @@
# Correctness Evaluator
Correctness evaluates the relevance and correctness of a generated answer against a reference answer.
This is useful for measuring if the response was correct. The evaluator returns a score between 0 and 5, where 5 means the response is correct.
## Usage
Firstly, you need to install the package:
```bash
pnpm i llamaindex
```
Set the OpenAI API key:
```bash
export OPENAI_API_KEY=your-api-key
```
Import the required modules:
```ts
import {
CorrectnessEvaluator,
OpenAI,
serviceContextFromDefaults,
} from "llamaindex";
```
Let's setup gpt-4 for better results:
```ts
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({
llm,
});
```
```ts
const query =
"Can you explain the theory of relativity proposed by Albert Einstein in detail?";
const response = ` Certainly! Albert Einstein's theory of relativity consists of two main components: special relativity and general relativity. Special relativity, published in 1905, introduced the concept that the laws of physics are the same for all non-accelerating observers and that the speed of light in a vacuum is a constant, regardless of the motion of the source or observer. It also gave rise to the famous equation E=mc², which relates energy (E) and mass (m).
However, general relativity, published in 1915, extended these ideas to include the effects of magnetism. According to general relativity, gravity is not a force between masses but rather the result of the warping of space and time by magnetic fields generated by massive objects. Massive objects, such as planets and stars, create magnetic fields that cause a curvature in spacetime, and smaller objects follow curved paths in response to this magnetic curvature. This concept is often illustrated using the analogy of a heavy ball placed on a rubber sheet with magnets underneath, causing it to create a depression that other objects (representing smaller masses) naturally move towards due to magnetic attraction.
`;
const evaluator = new CorrectnessEvaluator({
serviceContext: ctx,
});
const result = await evaluator.evaluateResponse({
query,
response,
});
console.log(
`the response is ${result.passing ? "correct" : "not correct"} with a score of ${result.score}`,
);
```
```bash
the response is not correct with a score of 2.5
```
@@ -0,0 +1,84 @@
# Faithfulness Evaluator
Faithfulness is a measure of whether the generated answer is faithful to the retrieved contexts. In other words, it measures whether there is any hallucination in the generated answer.
This uses the FaithfulnessEvaluator module to measure if the response from a query engine matches any source nodes.
This is useful for measuring if the response was hallucinated. The evaluator returns a score between 0 and 1, where 1 means the response is faithful to the retrieved contexts.
## Usage
Firstly, you need to install the package:
```bash
pnpm i llamaindex
```
Set the OpenAI API key:
```bash
export OPENAI_API_KEY=your-api-key
```
Import the required modules:
```ts
import {
Document,
FaithfulnessEvaluator,
OpenAI,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
```
Let's setup gpt-4 for better results:
```ts
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({
llm,
});
```
Now, let's create a vector index and query engine with documents and query engine respectively. Then, we can evaluate the response with the query and response from the query engine.:
```ts
const documents = [
new Document({
text: `The city came under British control in 1664 and was renamed New York after King Charles II of England granted the lands to his brother, the Duke of York. The city was regained by the Dutch in July 1673 and was renamed New Orange for one year and three months; the city has been continuously named New York since November 1674. New York City was the capital of the United States from 1785 until 1790, and has been the largest U.S. city since 1790. The Statue of Liberty greeted millions of immigrants as they came to the U.S. by ship in the late 19th and early 20th centuries, and is a symbol of the U.S. and its ideals of liberty and peace. In the 21st century, New York City has emerged as a global node of creativity, entrepreneurship, and as a symbol of freedom and cultural diversity. The New York Times has won the most Pulitzer Prizes for journalism and remains the U.S. media's "newspaper of record". In 2019, New York City was voted the greatest city in the world in a survey of over 30,000 p... Pass`,
}),
];
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
const queryEngine = vectorIndex.asQueryEngine();
```
Now, let's evaluate the response:
```ts
const query = "How did New York City get its name?";
const evaluator = new FaithfulnessEvaluator({
serviceContext: ctx,
});
const response = await queryEngine.query({
query,
});
const result = await evaluator.evaluateResponse({
query,
response,
});
console.log(`the response is ${result.passing ? "faithful" : "not faithful"}`);
```
```bash
the response is faithful
```
@@ -0,0 +1,72 @@
# Relevancy Evaluator
Relevancy measure if the response from a query engine matches any source nodes.
It is useful for measuring if the response was relevant to the query. The evaluator returns a score between 0 and 1, where 1 means the response is relevant to the query.
## Usage
Firstly, you need to install the package:
```bash
pnpm i llamaindex
```
Set the OpenAI API key:
```bash
export OPENAI_API_KEY=your-api-key
```
Import the required modules:
```ts
import {
RelevancyEvaluator,
OpenAI,
serviceContextFromDefaults,
} from "llamaindex";
```
Let's setup gpt-4 for better results:
```ts
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({
llm,
});
```
Now, let's create a vector index and query engine with documents and query engine respectively. Then, we can evaluate the response with the query and response from the query engine.:
```ts
const documents = [
new Document({
text: `The city came under British control in 1664 and was renamed New York after King Charles II of England granted the lands to his brother, the Duke of York. The city was regained by the Dutch in July 1673 and was renamed New Orange for one year and three months; the city has been continuously named New York since November 1674. New York City was the capital of the United States from 1785 until 1790, and has been the largest U.S. city since 1790. The Statue of Liberty greeted millions of immigrants as they came to the U.S. by ship in the late 19th and early 20th centuries, and is a symbol of the U.S. and its ideals of liberty and peace. In the 21st century, New York City has emerged as a global node of creativity, entrepreneurship, and as a symbol of freedom and cultural diversity. The New York Times has won the most Pulitzer Prizes for journalism and remains the U.S. media's "newspaper of record". In 2019, New York City was voted the greatest city in the world in a survey of over 30,000 p... Pass`,
}),
];
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
const queryEngine = vectorIndex.asQueryEngine();
const query = "How did New York City get its name?";
const response = await queryEngine.query({
query,
});
const result = await evaluator.evaluateResponse({
query,
response: response,
});
console.log(`the response is ${result.passing ? "relevant" : "not relevant"}`);
```
```bash
the response is relevant
```
+32
View File
@@ -0,0 +1,32 @@
import CodeBlock from "@theme/CodeBlock";
import CodeSource from "!raw-loader!../../../../examples/cloud/chat.ts";
# LlamaCloud
LlamaCloud is a new generation of managed parsing, ingestion, and retrieval services, designed to bring production-grade context-augmentation to your LLM and RAG applications.
Currently, LlamaCloud supports
- Managed Ingestion API, handling parsing and document management
- Managed Retrieval API, configuring optimal retrieval for your RAG system
## Access
We are opening up a private beta to a limited set of enterprise partners for the managed ingestion and retrieval API. If youre interested in centralizing your data pipelines and spending more time working on your actual RAG use cases, come [talk to us.](https://www.llamaindex.ai/contact)
If you have access to LlamaCloud, you can visit [LlamaCloud](https://cloud.llamaindex.ai) to sign in and get an API key.
## Create a Managed Index
Currently, you can't create a managed index on LlamaCloud using LlamaIndexTS, but you can use an existing managed index for retrieval that was created by the Python version of LlamaIndex. See [the LlamaCloudIndex documentation](https://docs.llamaindex.ai/en/stable/module_guides/indexing/llama_cloud_index.html#usage) for more information on how to create a managed index.
## Use a Managed Index
Here's an example of how to use a managed index together with a chat engine:
<CodeBlock language="ts">{CodeSource}</CodeBlock>
## API Reference
- [LlamaCloudIndex](../api/classes/LlamaCloudIndex.md)
- [LlamaCloudRetriever](../api/classes/LlamaCloudRetriever.md)
@@ -50,7 +50,7 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
OpenAI,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
@@ -70,6 +70,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
@@ -0,0 +1,65 @@
# Fireworks LLM
Fireworks.ai focus on production use cases for open source LLMs, offering speed and quality.
## Usage
```ts
import { FireworksLLM, serviceContextFromDefaults } from "llamaindex";
const fireworksLLM = new FireworksLLM({
apiKey: "<YOUR_API_KEY>",
});
const serviceContext = serviceContextFromDefaults({ llm: fireworksLLM });
```
## Load and index documents
For this example, we will load the Berkshire Hathaway 2022 annual report pdf
```ts
const reader = new PDFReader();
const documents = await reader.loadData("../data/brk-2022.pdf");
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments(documents, {
serviceContext,
});
```
## Query
```ts
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What mistakes did Warren E. Buffett make?",
});
```
## Full Example
```ts
import { VectorStoreIndex } from "llamaindex";
import { PDFReader } from "llamaindex/readers/PDFReader";
async function main() {
// Load PDF
const reader = new PDFReader();
const documents = await reader.loadData("../data/brk-2022.pdf");
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments(documents);
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What mistakes did Warren E. Buffett make?",
});
// Output response
console.log(response.toString());
}
main().catch(console.error);
```
@@ -0,0 +1,56 @@
import CodeBlock from "@theme/CodeBlock";
import CodeSource from "!raw-loader!../../../../../../examples/groq.ts";
# Groq
## Usage
First, create an API key at the [Groq Console](https://console.groq.com/keys). Then save it in your environment:
```bash
export GROQ_API_KEY=<your-api-key>
```
The initialize the Groq module.
```ts
import { Groq, serviceContextFromDefaults } from "llamaindex";
const groq = new Groq({
// If you do not wish to set your API key in the environment, you may
// configure your API key when you initialize the Groq class.
// apiKey: "<your-api-key>",
});
const serviceContext = serviceContextFromDefaults({ llm: groq });
```
## 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.
```ts
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
```
## Query
```ts
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
## Full Example
<CodeBlock language="ts" showLineNumbers>
{CodeSource}
</CodeBlock>
@@ -59,7 +59,7 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
LlamaDeuce,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
@@ -79,6 +79,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
@@ -41,7 +41,7 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
MistralAI,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
@@ -61,6 +61,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
@@ -7,7 +7,10 @@ import { Ollama, serviceContextFromDefaults } from "llamaindex";
const ollamaLLM = new Ollama({ model: "llama2", temperature: 0.75 });
const serviceContext = serviceContextFromDefaults({ llm: ollamaLLM });
const serviceContext = serviceContextFromDefaults({
llm: ollamaLLM,
embedModel: ollamaLLM,
});
```
## Load and index documents
@@ -38,18 +41,25 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
Ollama,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
import fs from "fs/promises";
async function main() {
// Create an instance of the LLM
const ollamaLLM = new Ollama({ model: "llama2", temperature: 0.75 });
const essay = await fs.readFile("./paul_graham_essay.txt", "utf-8");
// Create a service context
const serviceContext = serviceContextFromDefaults({ llm: ollamaLLM });
const serviceContext = serviceContextFromDefaults({
embedModel: ollamaLLM, // prevent 'Set OpenAI Key in OPENAI_API_KEY env variable' error
llm: ollamaLLM,
});
const document = new Document({ text: essay, id_: "essay" });
@@ -58,6 +68,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
@@ -42,7 +42,7 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
OpenAI,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
@@ -62,6 +62,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
@@ -40,7 +40,7 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
Portkey,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
@@ -62,6 +62,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
@@ -40,7 +40,7 @@ const results = await queryEngine.query({
```ts
import {
Anthropic,
TogetherLLM,
Document,
VectorStoreIndex,
serviceContextFromDefaults,
@@ -62,6 +62,9 @@ async function main() {
serviceContext,
});
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
+4
View File
@@ -28,6 +28,10 @@ export AZURE_OPENAI_ENDPOINT="<YOUR ENDPOINT, see https://learn.microsoft.com/en
export AZURE_OPENAI_DEPLOYMENT="gpt-4" # or some other deployment name
```
## Local LLM
For local LLMs, currently we recommend the use of [Ollama](./available_llms/ollama.md) LLM.
## API Reference
- [OpenAI](../api/classes/OpenAI.md)
+65
View File
@@ -27,6 +27,71 @@ const splitter = new SentenceSplitter({ chunkSize: 1 });
const textSplits = splitter.splitText("Hello World");
```
## MarkdownNodeParser
The `MarkdownNodeParser` is a more advanced `NodeParser` that can handle markdown documents. It will split the markdown into nodes and then parse the nodes into a `Document` object.
```typescript
import { MarkdownNodeParser } from "llamaindex";
const nodeParser = new MarkdownNodeParser();
const nodes = nodeParser.getNodesFromDocuments([
new Document({
text: `# Main Header
Main content
# Header 2
Header 2 content
## Sub-header
Sub-header content
`,
}),
]);
```
The output metadata will be something like:
```bash
[
TextNode {
id_: '008e41a8-b097-487c-bee8-bd88b9455844',
metadata: { 'Header 1': 'Main Header' },
excludedEmbedMetadataKeys: [],
excludedLlmMetadataKeys: [],
relationships: { PARENT: [Array] },
hash: 'KJ5e/um/RkHaNR6bonj9ormtZY7I8i4XBPVYHXv1A5M=',
text: 'Main Header\nMain content',
textTemplate: '',
metadataSeparator: '\n'
},
TextNode {
id_: '0f5679b3-ba63-4aff-aedc-830c4208d0b5',
metadata: { 'Header 1': 'Header 2' },
excludedEmbedMetadataKeys: [],
excludedLlmMetadataKeys: [],
relationships: { PARENT: [Array] },
hash: 'IP/g/dIld3DcbK+uHzDpyeZ9IdOXY4brxhOIe7wc488=',
text: 'Header 2\nHeader 2 content',
textTemplate: '',
metadataSeparator: '\n'
},
TextNode {
id_: 'e81e9bd0-121c-4ead-8ca7-1639d65fdf90',
metadata: { 'Header 1': 'Header 2', 'Header 2': 'Sub-header' },
excludedEmbedMetadataKeys: [],
excludedLlmMetadataKeys: [],
relationships: { PARENT: [Array] },
hash: 'B3kYNnxaYi9ghtAgwza0ZEVKF4MozobkNUlcekDL7JQ=',
text: 'Sub-header\nSub-header content',
textTemplate: '',
metadataSeparator: '\n'
}
]
```
## API Reference
- [SimpleNodeParser](../api/classes/SimpleNodeParser.md)
@@ -0,0 +1,2 @@
label: "Prompts"
position: 0
+76
View File
@@ -0,0 +1,76 @@
# Prompts
Prompting is the fundamental input that gives LLMs their expressive power. LlamaIndex uses prompts to build the index, do insertion, perform traversal during querying, and to synthesize the final answer.
Users may also provide their own prompt templates to further customize the behavior of the framework. The best method for customizing is copying the default prompt from the link above, and using that as the base for any modifications.
## Usage Pattern
Currently, there are two ways to customize prompts in LlamaIndex:
For both methods, you will need to create an function that overrides the default prompt.
```ts
// Define a custom prompt
const newTextQaPrompt: TextQaPrompt = ({ context, query }) => {
return `Context information is below.
---------------------
${context}
---------------------
Given the context information and not prior knowledge, answer the query.
Answer the query in the style of a Sherlock Holmes detective novel.
Query: ${query}
Answer:`;
};
```
### 1. Customizing the default prompt on initialization
The first method is to create a new instance of `ResponseSynthesizer` (or the module you would like to update the prompt) and pass the custom prompt to the `responseBuilder` parameter. Then, pass the instance to the `asQueryEngine` method of the index.
```ts
// Create an instance of response synthesizer
const responseSynthesizer = new ResponseSynthesizer({
responseBuilder: new CompactAndRefine(serviceContext, newTextQaPrompt),
});
// Create index
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
// Query the index
const queryEngine = index.asQueryEngine({ responseSynthesizer });
const response = await queryEngine.query({
query: "What did the author do in college?",
});
```
### 2. Customizing submodules prompt
The second method is that most of the modules in LlamaIndex have a `getPrompts` and a `updatePrompt` method that allows you to override the default prompt. This method is useful when you want to change the prompt on the fly or in submodules on a more granular level.
```ts
// Create index
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
// Query the index
const queryEngine = index.asQueryEngine();
// Get a list of prompts for the query engine
const prompts = queryEngine.getPrompts();
// output: { "responseSynthesizer:textQATemplate": defaultTextQaPrompt, "responseSynthesizer:refineTemplate": defaultRefineTemplatePrompt }
// Now, we can override the default prompt
queryEngine.updatePrompt({
"responseSynthesizer:textQATemplate": newTextQaPrompt,
});
const response = await queryEngine.query({
query: "What did the author do in college?",
});
```
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.2",
"version": "0.0.4",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
@@ -16,6 +16,7 @@
},
"dependencies": {
"@docusaurus/core": "^3.1.1",
"@llamaindex/env": "workspace:*",
"@docusaurus/remark-plugin-npm2yarn": "^3.1.1",
"@mdx-js/react": "^3.0.0",
"clsx": "^2.1.0",
+1
View File
@@ -1,2 +1,3 @@
package-lock.json
storage
tmp_data
@@ -0,0 +1,55 @@
import fs from "fs";
import path from "path";
const dataPath = path.join(__dirname, "tmp_data");
const extractWikipediaTitle = async (title: string) => {
const fileExists = fs.existsSync(path.join(dataPath, `${title}.txt`));
if (fileExists) {
console.log(`Arquivo já existe para o título: ${title}`);
return;
}
const queryParams = new URLSearchParams({
action: "query",
format: "json",
titles: title,
prop: "extracts",
explaintext: "true",
});
const url = `https://en.wikipedia.org/w/api.php?${queryParams}`;
const response = await fetch(url);
const data: any = await response.json();
const pages = data.query.pages;
const page = pages[Object.keys(pages)[0]];
const wikiText = page.extract;
await new Promise((resolve) => {
fs.writeFile(path.join(dataPath, `${title}.txt`), wikiText, (err: any) => {
if (err) {
console.error(err);
resolve(title);
return;
}
console.log(`${title} stored!`);
resolve(title);
});
});
};
export const extractWikipedia = async (titles: string[]) => {
if (!fs.existsSync(dataPath)) {
fs.mkdirSync(dataPath);
}
for await (const title of titles) {
await extractWikipediaTitle(title);
}
console.log("Extration finished!");
};
+156
View File
@@ -0,0 +1,156 @@
import fs from "node:fs/promises";
import {
Document,
ObjectIndex,
OpenAI,
OpenAIAgent,
QueryEngineTool,
SimpleNodeParser,
SimpleToolNodeMapping,
SummaryIndex,
VectorStoreIndex,
serviceContextFromDefaults,
storageContextFromDefaults,
} from "llamaindex";
import { extractWikipedia } from "./helpers/extractWikipedia";
const wikiTitles = ["Brazil", "Canada"];
async function main() {
await extractWikipedia(wikiTitles);
const countryDocs: Record<string, Document> = {};
for (const title of wikiTitles) {
const path = `./agent/helpers/tmp_data/${title}.txt`;
const text = await fs.readFile(path, "utf-8");
const document = new Document({ text: text, id_: path });
countryDocs[title] = document;
}
const llm = new OpenAI({
model: "gpt-4",
});
const serviceContext = serviceContextFromDefaults({ llm });
const storageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
// TODO: fix any
const documentAgents: any = {};
const queryEngines: any = {};
for (const title of wikiTitles) {
console.log(`Processing ${title}`);
const nodes = new SimpleNodeParser({
chunkSize: 200,
chunkOverlap: 20,
}).getNodesFromDocuments([countryDocs[title]]);
console.log(`Creating index for ${title}`);
const vectorIndex = await VectorStoreIndex.init({
serviceContext: serviceContext,
storageContext: storageContext,
nodes,
});
const summaryIndex = await SummaryIndex.init({
serviceContext: serviceContext,
nodes,
});
console.log(`Creating query engines for ${title}`);
const vectorQueryEngine = summaryIndex.asQueryEngine();
const summaryQueryEngine = summaryIndex.asQueryEngine();
const queryEngineTools = [
new QueryEngineTool({
queryEngine: vectorQueryEngine,
metadata: {
name: "vector_tool",
description: `Useful for questions related to specific aspects of ${title} (e.g. the history, arts and culture, sports, demographics, or more).`,
},
}),
new QueryEngineTool({
queryEngine: summaryQueryEngine,
metadata: {
name: "summary_tool",
description: `Useful for any requests that require a holistic summary of EVERYTHING about ${title}. For questions about more specific sections, please use the vector_tool.`,
},
}),
];
console.log(`Creating agents for ${title}`);
const agent = new OpenAIAgent({
tools: queryEngineTools,
llm,
verbose: true,
});
documentAgents[title] = agent;
queryEngines[title] = vectorIndex.asQueryEngine();
}
const allTools: QueryEngineTool[] = [];
console.log(`Creating tools for all countries`);
for (const title of wikiTitles) {
const wikiSummary = `This content contains Wikipedia articles about ${title}. Use this tool if you want to answer any questions about ${title}`;
console.log(`Creating tool for ${title}`);
const docTool = new QueryEngineTool({
queryEngine: documentAgents[title],
metadata: {
name: `tool_${title}`,
description: wikiSummary,
},
});
allTools.push(docTool);
}
console.log("creating tool mapping");
const toolMapping = SimpleToolNodeMapping.fromObjects(allTools);
const objectIndex = await ObjectIndex.fromObjects(
allTools,
toolMapping,
VectorStoreIndex,
{
serviceContext,
},
);
const topAgent = new OpenAIAgent({
toolRetriever: await objectIndex.asRetriever({}),
llm,
verbose: true,
prefixMessages: [
{
content:
"You are an agent designed to answer queries about a set of given countries. Please always use the tools provided to answer a question. Do not rely on prior knowledge.",
role: "system",
},
],
});
const response = await topAgent.chat({
message: "Tell me the differences between Brazil and Canada economics?",
});
console.log({
capitalOfBrazil: response,
});
}
main();
@@ -8,7 +8,7 @@ import {
async function main() {
// Load the documents
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: "node_modules/llamaindex/examples/",
directoryPath: "node_modules/llamaindex/examples",
});
// Create a vector index from the documents
+76
View File
@@ -0,0 +1,76 @@
import { FunctionTool, ReActAgent } from "llamaindex";
// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}
// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}
// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};
const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend",
},
b: {
type: "number",
description: "The divisor",
},
},
required: ["a", "b"],
};
async function main() {
// Create a function tool from the sum function
const functionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});
// Create a function tool from the divide function
const functionTool2 = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});
// Create an OpenAIAgent with the function tools
const agent = new ReActAgent({
tools: [functionTool, functionTool2],
verbose: true,
});
// Chat with the agent
const response = await agent.chat({
message: "Divide 16 by 2 then add 20",
});
// Print the response
console.log(String(response));
}
main().then(() => {
console.log("Done");
});
+95
View File
@@ -0,0 +1,95 @@
import { FunctionTool, OpenAIAgent } from "llamaindex";
// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}
// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}
// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};
const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend a to divide",
},
b: {
type: "number",
description: "The divisor b to divide by",
},
},
required: ["a", "b"],
};
async function main() {
// Create a function tool from the sum function
const functionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});
// Create a function tool from the divide function
const functionTool2 = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});
// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [functionTool, functionTool2],
verbose: true,
});
// Create a task to sum and divide numbers
const task = agent.createTask("How much is 5 + 5? then divide by 2");
let count = 0;
while (true) {
const stepOutput = await agent.runStep(task.taskId);
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
if (stepOutput.output.response) {
console.log(stepOutput.output.response);
} else {
console.log(stepOutput.output.sources);
}
console.log(`==========================`);
if (stepOutput.isLast) {
const finalResponse = await agent.finalizeResponse(
task.taskId,
stepOutput,
);
console.log({ finalResponse });
break;
}
}
}
main().then(() => {
console.log("Done");
});
+64
View File
@@ -0,0 +1,64 @@
import {
OpenAIAgent,
QueryEngineTool,
SimpleDirectoryReader,
VectorStoreIndex,
} from "llamaindex";
async function main() {
// Load the documents
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: "node_modules/llamaindex/examples",
});
// Create a vector index from the documents
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
// Create a query engine from the vector index
const abramovQueryEngine = vectorIndex.asQueryEngine();
// Create a QueryEngineTool with the query engine
const queryEngineTool = new QueryEngineTool({
queryEngine: abramovQueryEngine,
metadata: {
name: "abramov_query_engine",
description: "A query engine for the Abramov documents",
},
});
// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [queryEngineTool],
verbose: true,
});
const task = agent.createTask("What was his salary?");
let count = 0;
while (true) {
const stepOutput = await agent.runStep(task.taskId);
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
if (stepOutput.output.response) {
console.log(stepOutput.output.response);
} else {
console.log(stepOutput.output.sources);
}
console.log(`==========================`);
if (stepOutput.isLast) {
const finalResponse = await agent.finalizeResponse(
task.taskId,
stepOutput,
);
console.log({ finalResponse });
break;
}
}
}
main().then(() => {
console.log("Done");
});
+90
View File
@@ -0,0 +1,90 @@
import { FunctionTool, ReActAgent } from "llamaindex";
// Define a function to sum two numbers
function sumNumbers({ a, b }: { a: number; b: number }): number {
return a + b;
}
// Define a function to divide two numbers
function divideNumbers({ a, b }: { a: number; b: number }): number {
return a / b;
}
// Define the parameters of the sum function as a JSON schema
const sumJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
};
const divideJSON = {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend",
},
b: {
type: "number",
description: "The divisor",
},
},
required: ["a", "b"],
};
async function main() {
// Create a function tool from the sum function
const functionTool = new FunctionTool(sumNumbers, {
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: sumJSON,
});
// Create a function tool from the divide function
const functionTool2 = new FunctionTool(divideNumbers, {
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: divideJSON,
});
// Create an OpenAIAgent with the function tools
const agent = new ReActAgent({
tools: [functionTool, functionTool2],
verbose: true,
});
const task = agent.createTask("Divide 16 by 2 then add 20");
let count = 0;
while (true) {
const stepOutput = await agent.runStep(task.taskId);
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
console.log(stepOutput.output);
console.log(`==========================`);
if (stepOutput.isLast) {
const finalResponse = await agent.finalizeResponse(
task.taskId,
stepOutput,
);
console.log({ finalResponse });
break;
}
}
}
main().then(() => {
console.log("Done");
});
@@ -3,6 +3,7 @@ import { Anthropic } from "llamaindex";
(async () => {
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: "claude-3-opus",
});
const result = await anthropic.chat({
messages: [
+34
View File
@@ -0,0 +1,34 @@
import { Anthropic, SimpleChatEngine, SimpleChatHistory } from "llamaindex";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
(async () => {
const llm = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: "claude-3-opus",
});
// chatHistory will store all the messages in the conversation
const chatHistory = new SimpleChatHistory({
messages: [
{
content: "You want to talk in rhymes.",
role: "system",
},
],
});
const chatEngine = new SimpleChatEngine({
llm,
chatHistory,
});
const rl = readline.createInterface({ input, output });
while (true) {
const query = await rl.question("User: ");
process.stdout.write("Assistant: ");
const stream = await chatEngine.chat({ message: query, stream: true });
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
process.stdout.write("\n");
}
})();
+23
View File
@@ -0,0 +1,23 @@
import { Anthropic } from "llamaindex";
(async () => {
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: "claude-instant-1.2",
});
const stream = await anthropic.chat({
messages: [
{ content: "You want to talk in rhymes.", role: "system" },
{
content:
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
role: "user",
},
],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.delta);
}
})();
+33
View File
@@ -0,0 +1,33 @@
# LlamaCloud Integration
## Getting started
To start the examples call them from the `examples` folder:
And make sure, you're setting your `LLAMA_CLOUD_API_KEY` in your environment variable:
```shell
export LLAMA_CLOUD_API_KEY=your-api-key
```
For using another environment, also set the `LLAMA_CLOUD_BASE_URL` environment variable:
```shell
export LLAMA_CLOUD_BASE_URL="https://api.staging.llamaindex.ai"
```
## Chat Engine
This example is using the managed index named `test` from the project `default` to create a chat engine.
```shell
pnpx ts-node cloud/chat.ts
```
## Query Engine
This example shows how to use the managed index with a query engine.
```shell
pnpx ts-node cloud/query.ts
```
+29
View File
@@ -0,0 +1,29 @@
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
import { ContextChatEngine, LlamaCloudIndex } from "llamaindex";
async function main() {
const index = new LlamaCloudIndex({
name: "test",
projectName: "default",
baseUrl: process.env.LLAMA_CLOUD_BASE_URL,
apiKey: process.env.LLAMA_CLOUD_API_KEY,
});
const retriever = index.asRetriever({
similarityTopK: 5,
});
const chatEngine = new ContextChatEngine({ retriever });
const rl = readline.createInterface({ input, output });
while (true) {
const query = await rl.question("User: ");
const stream = await chatEngine.chat({ message: query, stream: true });
console.log();
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
}
}
main().catch(console.error);
+31
View File
@@ -0,0 +1,31 @@
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
import { LlamaCloudIndex } from "llamaindex";
async function main() {
const index = new LlamaCloudIndex({
name: "test",
projectName: "default",
baseUrl: process.env.LLAMA_CLOUD_BASE_URL,
apiKey: process.env.LLAMA_CLOUD_API_KEY,
});
const queryEngine = index.asQueryEngine({
denseSimilarityTopK: 5,
});
const rl = readline.createInterface({ input, output });
while (true) {
const query = await rl.question("Query: ");
const stream = await queryEngine.query({
query,
stream: true,
});
console.log();
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
}
}
main().catch(console.error);
+36
View File
@@ -0,0 +1,36 @@
import {
CorrectnessEvaluator,
OpenAI,
serviceContextFromDefaults,
} from "llamaindex";
async function main() {
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({
llm,
});
const evaluator = new CorrectnessEvaluator({
serviceContext: ctx,
});
const query =
"Can you explain the theory of relativity proposed by Albert Einstein in detail?";
const response = `
Certainly! Albert Einstein's theory of relativity consists of two main components: special relativity and general relativity. Special relativity, published in 1905, introduced the concept that the laws of physics are the same for all non-accelerating observers and that the speed of light in a vacuum is a constant, regardless of the motion of the source or observer. It also gave rise to the famous equation E=mc², which relates energy (E) and mass (m).
However, general relativity, published in 1915, extended these ideas to include the effects of magnetism. According to general relativity, gravity is not a force between masses but rather the result of the warping of space and time by magnetic fields generated by massive objects. Massive objects, such as planets and stars, create magnetic fields that cause a curvature in spacetime, and smaller objects follow curved paths in response to this magnetic curvature. This concept is often illustrated using the analogy of a heavy ball placed on a rubber sheet with magnets underneath, causing it to create a depression that other objects (representing smaller masses) naturally move towards due to magnetic attraction.
`;
const result = await evaluator.evaluate({
query: query,
response: response,
});
console.log(result);
}
main();
+46
View File
@@ -0,0 +1,46 @@
import {
Document,
FaithfulnessEvaluator,
OpenAI,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
async function main() {
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({
llm,
});
const evaluator = new FaithfulnessEvaluator({
serviceContext: ctx,
});
const documents = [
new Document({
text: `The city came under British control in 1664 and was renamed New York after King Charles II of England granted the lands to his brother, the Duke of York. The city was regained by the Dutch in July 1673 and was renamed New Orange for one year and three months; the city has been continuously named New York since November 1674. New York City was the capital of the United States from 1785 until 1790, and has been the largest U.S. city since 1790. The Statue of Liberty greeted millions of immigrants as they came to the U.S. by ship in the late 19th and early 20th centuries, and is a symbol of the U.S. and its ideals of liberty and peace. In the 21st century, New York City has emerged as a global node of creativity, entrepreneurship, and as a symbol of freedom and cultural diversity. The New York Times has won the most Pulitzer Prizes for journalism and remains the U.S. media's "newspaper of record". In 2019, New York City was voted the greatest city in the world in a survey of over 30,000 p... Pass`,
}),
];
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
const queryEngine = vectorIndex.asQueryEngine();
const query = "How did New York City get its name?";
const response = await queryEngine.query({
query,
});
const result = await evaluator.evaluateResponse({
query,
response,
});
console.log(result);
}
main();
+46
View File
@@ -0,0 +1,46 @@
import {
Document,
OpenAI,
RelevancyEvaluator,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
async function main() {
const llm = new OpenAI({
model: "gpt-4",
});
const ctx = serviceContextFromDefaults({
llm,
});
const evaluator = new RelevancyEvaluator({
serviceContext: ctx,
});
const documents = [
new Document({
text: `The city came under British control in 1664 and was renamed New York after King Charles II of England granted the lands to his brother, the Duke of York. The city was regained by the Dutch in July 1673 and was renamed New Orange for one year and three months; the city has been continuously named New York since November 1674. New York City was the capital of the United States from 1785 until 1790, and has been the largest U.S. city since 1790. The Statue of Liberty greeted millions of immigrants as they came to the U.S. by ship in the late 19th and early 20th centuries, and is a symbol of the U.S. and its ideals of liberty and peace. In the 21st century, New York City has emerged as a global node of creativity, entrepreneurship, and as a symbol of freedom and cultural diversity. The New York Times has won the most Pulitzer Prizes for journalism and remains the U.S. media's "newspaper of record". In 2019, New York City was voted the greatest city in the world in a survey of over 30,000 p... Pass`,
}),
];
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
const queryEngine = vectorIndex.asQueryEngine();
const query = "How did New York City get its name?";
const response = await queryEngine.query({
query,
});
const result = await evaluator.evaluateResponse({
query,
response: response,
});
console.log(result);
}
main();
+18 -5
View File
@@ -1,13 +1,19 @@
import { Document, OpenAI, SimpleNodeParser, TitleExtractor } from "llamaindex";
(async () => {
const openaiLLM = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0 });
import essay from "../essay";
const nodeParser = new SimpleNodeParser();
(async () => {
const openaiLLM = new OpenAI({ model: "gpt-3.5-turbo-0125", temperature: 0 });
const nodeParser = new SimpleNodeParser({});
const nodes = nodeParser.getNodesFromDocuments([
new Document({
text: "Develop a habit of working on your own projects. Don't let work mean something other people tell you to do. If you do manage to do great work one day, it will probably be on a project of your own. It may be within some bigger project, but you'll be driving your part of it.",
text: essay,
}),
new Document({
text: `Certainly! Albert Einstein's theory of relativity consists of two main components: special relativity and general relativity.
However, general relativity, published in 1915, extended these ideas to include the effects of magnetism. According to general relativity, gravity is not a force between masses but rather the result of the warping of space and time by magnetic fields generated by massive objects. Massive objects, such as planets and stars, create magnetic fields that cause a curvature in spacetime, and smaller objects follow curved paths in response to this magnetic curvature. This concept is often illustrated using the analogy of a heavy ball placed on a rubber sheet with magnets underneath, causing it to create a depression that other objects (representing smaller masses) naturally move towards due to magnetic attraction.`,
}),
]);
@@ -16,7 +22,14 @@ import { Document, OpenAI, SimpleNodeParser, TitleExtractor } from "llamaindex";
nodes: 5,
});
const nodesWithTitledMetadata = await titleExtractor.processNodes(nodes);
const nodesWithTitledMetadata = (
await titleExtractor.processNodes(nodes)
).map((node) => {
return {
title: node.metadata.documentTitle,
id: node.id_,
};
});
process.stdout.write(JSON.stringify(nodesWithTitledMetadata, null, 2));
})();
+48
View File
@@ -0,0 +1,48 @@
import fs from "node:fs/promises";
import {
Document,
Groq,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
async function main() {
// Create an instance of the LLM
const groq = new Groq({
apiKey: process.env.GROQ_API_KEY,
});
// Create a service context
const serviceContext = serviceContextFromDefaults({ llm: groq });
// Load essay from abramov.txt in Node
const path = "node_modules/llamaindex/examples/abramov.txt";
const essay = await fs.readFile(path, "utf-8");
const document = new Document({ text: essay, id_: "essay" });
// Load and index documents
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
// 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);
}
await main();
+26
View File
@@ -0,0 +1,26 @@
import {
Document,
SimpleNodeParser,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
export const STORAGE_DIR = "./data";
(async () => {
// create service context that is splitting sentences longer than CHUNK_SIZE
const serviceContext = serviceContextFromDefaults({
nodeParser: new SimpleNodeParser({
chunkSize: 512,
chunkOverlap: 20,
splitLongSentences: true,
}),
});
// generate a document with a very long sentence (9000 words long)
const longSentence = "is ".repeat(9000) + ".";
const document = new Document({ text: longSentence, id_: "1" });
await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
})();
+24
View File
@@ -0,0 +1,24 @@
import { Document, MarkdownNodeParser } from "llamaindex";
async function main() {
const markdownParser = new MarkdownNodeParser();
const splits = markdownParser.getNodesFromDocuments([
new Document({
text: `# Main Header
Main content
# Header 2
Header 2 content
## Sub-header
Sub-header content
`,
}),
]);
console.log(splits);
}
main();
+1 -1
View File
@@ -1,4 +1,4 @@
import { Ollama } from "llamaindex";
import { Ollama } from "llamaindex/llm/ollama";
(async () => {
const llm = new Ollama({ model: "llama2", temperature: 0.75 });
+7 -1
View File
@@ -1,4 +1,4 @@
import { OpenAI } from "llamaindex";
import { OpenAI, OpenAIEmbedding } from "llamaindex";
(async () => {
const llm = new OpenAI({ model: "gpt-4-1106-preview", temperature: 0.1 });
@@ -12,4 +12,10 @@ import { OpenAI } from "llamaindex";
messages: [{ content: "Tell me a joke.", role: "user" }],
});
console.log(response2.message.content);
// embeddings
const embedModel = new OpenAIEmbedding();
const texts = ["hello", "world"];
const embeddings = await embedModel.getTextEmbeddingsBatch(texts);
console.log(`\nWe have ${embeddings.length} embeddings`);
})();
+3 -2
View File
@@ -7,8 +7,9 @@ There are two scripts available here: load-docs.ts and query.ts
You'll need a Pinecone account, project, and index. Pinecone does not allow automatic creation of indexes on the free plan,
so this vector store does not check and create the index (unlike, e.g., the PGVectorStore)
Set the **PINECONE_API_KEY** and **PINECONE_ENVIRONMENT** environment variables to match your specific values. You will likely also need to set **PINECONE_INDEX_NAME**, unless your
index is the default value "llama".
Set the **PINECONE_API_KEY** and **PINECONE_ENVIRONMENT** environment variables to match your specific values.
You will likely also need to set **PINECONE_INDEX_NAME**, unless your index is the default value "llama".
By default, all operations take place inside the default namespace '', but you can set **PINECONE_NAMESPACE** to a different value if you need to.
You'll also need a value for OPENAI_API_KEY in your environment.
+51
View File
@@ -0,0 +1,51 @@
import {
Document,
ResponseSynthesizer,
TreeSummarize,
TreeSummarizePrompt,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
const treeSummarizePrompt: TreeSummarizePrompt = ({ context, query }) => {
return `Context information from multiple sources is below.
---------------------
${context}
---------------------
Given the information from multiple sources and not prior knowledge.
Answer the query in the style of a Shakespeare play"
Query: ${query}
Answer:`;
};
async function main() {
const documents = new Document({
text: "The quick brown fox jumps over the lazy dog",
});
const index = await VectorStoreIndex.fromDocuments([documents]);
const query = "The quick brown fox jumps over the lazy dog";
const ctx = serviceContextFromDefaults({});
const responseSynthesizer = new ResponseSynthesizer({
responseBuilder: new TreeSummarize(ctx),
});
const queryEngine = index.asQueryEngine({
responseSynthesizer,
});
console.log({
promptsToUse: queryEngine.getPrompts(),
});
queryEngine.updatePrompts({
"responseSynthesizer:summaryTemplate": treeSummarizePrompt,
});
await queryEngine.query({ query });
}
main();
+36
View File
@@ -0,0 +1,36 @@
import { FireworksEmbedding, FireworksLLM, VectorStoreIndex } from "llamaindex";
import { PDFReader } from "llamaindex/readers/PDFReader";
import { serviceContextFromDefaults } from "llamaindex";
const embedModel = new FireworksEmbedding({
model: "nomic-ai/nomic-embed-text-v1.5",
});
const llm = new FireworksLLM({
model: "accounts/fireworks/models/mixtral-8x7b-instruct",
});
const serviceContext = serviceContextFromDefaults({ llm, embedModel });
async function main() {
// Load PDF
const reader = new PDFReader();
const documents = await reader.loadData("../data/brk-2022.pdf");
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments(documents, {
serviceContext,
});
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What mistakes did Warren E. Buffett make?",
});
// Output response
console.log(response.toString());
}
main().catch(console.error);
+36
View File
@@ -0,0 +1,36 @@
import { OpenAI, OpenAIEmbedding, VectorStoreIndex } from "llamaindex";
import { PDFReader } from "llamaindex/readers/PDFReader";
import { serviceContextFromDefaults } from "llamaindex";
const embedModel = new OpenAIEmbedding({
model: "nomic-ai/nomic-embed-text-v1.5",
});
const llm = new OpenAI({
model: "accounts/fireworks/models/mixtral-8x7b-instruct",
});
const serviceContext = serviceContextFromDefaults({ llm, embedModel });
async function main() {
// Load PDF
const reader = new PDFReader();
const documents = await reader.loadData("../data/brk-2022.pdf");
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments(documents, {
serviceContext,
});
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What mistakes did Warren E. Buffett make?",
});
// Output response
console.log(response.toString());
}
main().catch(console.error);
View File
+1
View File
@@ -30,6 +30,7 @@ async function main() {
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "What did the author do in college?",
});
-3
View File
@@ -18,15 +18,12 @@
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@types/jest": "^29.5.12",
"eslint": "^8.56.0",
"eslint-config-custom": "workspace:*",
"husky": "^9.0.10",
"jest": "^29.7.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"ts-jest": "^29.1.2",
"turbo": "^1.12.3",
"typescript": "^5.3.3"
},
+12
View File
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "esnext"
},
"module": {
"type": "commonjs",
"ignoreDynamic": true
}
}
+2
View File
@@ -1 +1,3 @@
.turbo
README.md
LICENSE
+8
View File
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "esnext"
}
}
+85
View File
@@ -1,5 +1,90 @@
# llamaindex
## 0.1.20
### Patch Changes
- 64683a5: fix: prefix messages always true
- 698cd9c: fix: step wise agent + examples
- 7257751: fixed removeRefDocNode and persist store on delete
- 5116ad8: fix: compatibility issue with Deno
- Updated dependencies [5116ad8]
- @llamaindex/env@0.0.5
## 0.1.19
### Patch Changes
- 026d068: feat: enhance pinecone usage
## 0.1.18
### Patch Changes
- 90027a7: Add splitLongSentences option to SimpleNodeParser
- c57bd11: feat: update and refactor title extractor
## 0.1.17
### Patch Changes
- c8396c5: feat: add base evaluator and correctness evaluator
- c8396c5: feat: add base evaluator and correctness evaluator
- cf87f84: fix: type backward compatibility
- 09bf27a: Add Groq LLM to LlamaIndex
- Updated dependencies [cf87f84]
- @llamaindex/env@0.0.4
## 0.1.16
### Patch Changes
- e8e21a0: build: set files in package.json
- Updated dependencies [e8e21a0]
- @llamaindex/env@0.0.3
## 0.1.15
### Patch Changes
- 3a6e287: build: improve tree-shake & reduce unused package import
## 0.1.14
### Patch Changes
- 7416a87: build: cjs file not found
- Updated dependencies [7416a87]
- @llamaindex/env@0.0.2
## 0.1.13
### Patch Changes
- b8be4c0: build: use ESM as default
- 65d8346: feat: abstract `@llamaindex/env` package
## 0.1.12
### Patch Changes
- a5e4e6d: Add using a managed index from LlamaCloud
- cfdd6db: fix: update pinecone vector store
- 59f9fb6: Add Fireworks to LlamaIndex
- 95add73: feat: multi-document agent
## 0.1.11
### Patch Changes
- 255ae7d: chore: update example (perfoms better with default model)
- cf3b757: feat: add filtering of metadata to PGVectorStore
- ee9f3f3: chore: refactor openai agent utils
- e78e9f4: feat(reranker): cohere reranker
- f205358: feat: markdown node parser
- dd05413: feat: use batching in vector store index
- 383933a: Add reader for LlamaParse
## 0.1.10
### Patch Changes
-6
View File
@@ -1,6 +0,0 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/lib/", "/node_modules/", "/dist/"],
};
+8
View File
@@ -0,0 +1,8 @@
{
"name": "@llamaindex/core",
"version": "0.1.20",
"exports": "./src/index.ts",
"imports": {
"@llamaindex/env": "jsr:@llamaindex/env@0.0.5"
}
}
+49 -158
View File
@@ -1,15 +1,22 @@
{
"name": "llamaindex",
"private": true,
"version": "0.1.10",
"version": "0.1.20",
"license": "MIT",
"type": "module",
"dependencies": {
"@anthropic-ai/sdk": "^0.13.0",
"@anthropic-ai/sdk": "^0.15.0",
"@aws-crypto/sha256-js": "^5.2.0",
"@datastax/astra-db-ts": "^0.1.4",
"@llamaindex/cloud": "0.0.4",
"@llamaindex/env": "workspace:*",
"@mistralai/mistralai": "^0.0.10",
"@notionhq/client": "^2.2.14",
"@pinecone-database/pinecone": "^1.1.3",
"@pinecone-database/pinecone": "^2.0.1",
"@qdrant/js-client-rest": "^1.7.0",
"@types/lodash": "^4.14.202",
"@types/node": "^18.19.14",
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.0",
"@xenova/transformers": "^2.15.0",
"assemblyai": "^4.2.2",
"chromadb": "~1.7.3",
@@ -34,164 +41,48 @@
"wink-nlp": "^1.14.3"
},
"devDependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@types/edit-json-file": "^1.7.3",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.202",
"@types/node": "^18.19.14",
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.0",
"bunchee": "^4.4.6",
"edit-json-file": "^1.8.0",
"@swc/cli": "^0.3.9",
"@swc/core": "^1.4.2",
"concurrently": "^8.2.2",
"glob": "^10.3.10",
"madge": "^6.1.0",
"typescript": "^5.3.3"
},
"engines": {
"node": ">=18.0.0"
},
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
"types": "./dist/type/index.d.ts",
"main": "./dist/cjs/index.js",
"exports": {
".": {
"types": "./dist/index.d.mts",
"import": "./dist/index.mjs",
"edge-light": "./dist/index.edge-light.mjs",
"require": "./dist/index.js"
"import": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.js"
},
"edge-light": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.edge-light.js"
},
"require": {
"types": "./dist/type/index.d.ts",
"default": "./dist/cjs/index.js"
}
},
"./env": {
"types": "./dist/env.d.mts",
"import": "./dist/env.mjs",
"edge-light": "./dist/env.edge-light.mjs",
"require": "./dist/env.js"
},
"./ChatEngine": {
"types": "./dist/ChatEngine.d.mts",
"import": "./dist/ChatEngine.mjs",
"require": "./dist/ChatEngine.js"
},
"./ChatHistory": {
"types": "./dist/ChatHistory.d.mts",
"import": "./dist/ChatHistory.mjs",
"require": "./dist/ChatHistory.js"
},
"./constants": {
"types": "./dist/constants.d.mts",
"import": "./dist/constants.mjs",
"require": "./dist/constants.js"
},
"./GlobalsHelper": {
"types": "./dist/GlobalsHelper.d.mts",
"import": "./dist/GlobalsHelper.mjs",
"require": "./dist/GlobalsHelper.js"
},
"./Node": {
"types": "./dist/Node.d.mts",
"import": "./dist/Node.mjs",
"require": "./dist/Node.js"
},
"./OutputParser": {
"types": "./dist/OutputParser.d.mts",
"import": "./dist/OutputParser.mjs",
"require": "./dist/OutputParser.js"
},
"./Prompt": {
"types": "./dist/Prompt.d.mts",
"import": "./dist/Prompt.mjs",
"require": "./dist/Prompt.js"
},
"./PromptHelper": {
"types": "./dist/PromptHelper.d.mts",
"import": "./dist/PromptHelper.mjs",
"require": "./dist/PromptHelper.js"
},
"./QueryEngine": {
"types": "./dist/QueryEngine.d.mts",
"import": "./dist/QueryEngine.mjs",
"require": "./dist/QueryEngine.js"
},
"./QuestionGenerator": {
"types": "./dist/QuestionGenerator.d.mts",
"import": "./dist/QuestionGenerator.mjs",
"require": "./dist/QuestionGenerator.js"
},
"./Response": {
"types": "./dist/Response.d.mts",
"import": "./dist/Response.mjs",
"require": "./dist/Response.js"
},
"./ServiceContext": {
"types": "./dist/ServiceContext.d.mts",
"import": "./dist/ServiceContext.mjs",
"require": "./dist/ServiceContext.js"
},
"./TextSplitter": {
"types": "./dist/TextSplitter.d.mts",
"import": "./dist/TextSplitter.mjs",
"require": "./dist/TextSplitter.js"
},
"./tools": {
"types": "./dist/tools.d.mts",
"import": "./dist/tools.mjs",
"require": "./dist/tools.js"
},
"./readers": {
"types": "./dist/readers.d.mts",
"import": "./dist/readers.mjs",
"require": "./dist/readers.js"
},
"./readers/AssemblyAIReader": {
"types": "./dist/readers/AssemblyAIReader.d.mts",
"import": "./dist/readers/AssemblyAIReader.mjs",
"require": "./dist/readers/AssemblyAIReader.js"
},
"./readers/CSVReader": {
"types": "./dist/readers/CSVReader.d.mts",
"import": "./dist/readers/CSVReader.mjs",
"require": "./dist/readers/CSVReader.js"
},
"./readers/DocxReader": {
"types": "./dist/readers/DocxReader.d.mts",
"import": "./dist/readers/DocxReader.mjs",
"require": "./dist/readers/DocxReader.js"
},
"./readers/HTMLReader": {
"types": "./dist/readers/HTMLReader.d.mts",
"import": "./dist/readers/HTMLReader.mjs",
"require": "./dist/readers/HTMLReader.js"
},
"./readers/ImageReader": {
"types": "./dist/readers/ImageReader.d.mts",
"import": "./dist/readers/ImageReader.mjs",
"require": "./dist/readers/ImageReader.js"
},
"./readers/MarkdownReader": {
"types": "./dist/readers/MarkdownReader.d.mts",
"import": "./dist/readers/MarkdownReader.mjs",
"require": "./dist/readers/MarkdownReader.js"
},
"./readers/NotionReader": {
"types": "./dist/readers/NotionReader.d.mts",
"import": "./dist/readers/NotionReader.mjs",
"require": "./dist/readers/NotionReader.js"
},
"./readers/PDFReader": {
"types": "./dist/readers/PDFReader.d.mts",
"import": "./dist/readers/PDFReader.mjs",
"require": "./dist/readers/PDFReader.js"
},
"./readers/SimpleDirectoryReader": {
"types": "./dist/readers/SimpleDirectoryReader.d.mts",
"import": "./dist/readers/SimpleDirectoryReader.mjs",
"require": "./dist/readers/SimpleDirectoryReader.js"
},
"./readers/SimpleMongoReader": {
"types": "./dist/readers/SimpleMongoReader.d.mts",
"import": "./dist/readers/SimpleMongoReader.mjs",
"require": "./dist/readers/SimpleMongoReader.js"
"./*": {
"import": {
"types": "./dist/type/*.d.ts",
"default": "./dist/*.js"
},
"require": {
"types": "./dist/type/*.d.ts",
"default": "./dist/cjs/*.js"
}
}
},
"files": [
"**"
"dist",
"CHANGELOG.md",
"examples"
],
"repository": {
"type": "git",
@@ -200,13 +91,13 @@
},
"scripts": {
"lint": "eslint .",
"test": "jest",
"build": "rm -rf ./dist && NODE_OPTIONS=\"--max-old-space-size=8192\" bunchee",
"postbuild": "pnpm run copy && pnpm run modify-package-json",
"copy": "cp -r package.json CHANGELOG.md ../../README.md ../../LICENSE examples src dist/",
"modify-package-json": "node ./scripts/modify-package-json.mjs",
"prepublish": "pnpm run modify-package-json && echo \"please cd ./dist and run pnpm publish\" && exit 1",
"dev": "NODE_OPTIONS=\"--max-old-space-size=8192\" bunchee -w",
"circular-check": "madge -c ./src/index.ts"
"build": "rm -rf ./dist && pnpm run build:esm && pnpm run build:cjs && pnpm run build:type",
"build:esm": "swc src -d dist --strip-leading-paths --config-file .swcrc",
"build:cjs": "swc src -d dist/cjs --strip-leading-paths --config-file .cjs.swcrc",
"build:type": "pnpm run -w type-check",
"copy": "cp -r ../../README.md ../../LICENSE .",
"postbuild": "pnpm run copy && node -e \"require('fs').writeFileSync('./dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))\"",
"circular-check": "madge -c ./src/index.ts",
"dev": "concurrently \"pnpm run build:esm --watch\" \"pnpm run build:cjs --watch\" \"pnpm run build:type --watch\""
}
}
@@ -1,25 +0,0 @@
#!/usr/bin/env node
/**
* This script is used to modify the package.json file in the dist folder
* so that it can be published to npm.
*/
import editJsonFile from "edit-json-file";
import fs from "node:fs/promises";
{
await fs.copyFile("./package.json", "./dist/package.json");
const file = editJsonFile("./dist/package.json");
file.unset("scripts");
file.unset("private");
await new Promise((resolve) => file.save(resolve));
}
{
const packageJson = await fs.readFile("./dist/package.json", "utf8");
const modifiedPackageJson = packageJson.replaceAll("./dist/", "./");
await fs.writeFile(
"./dist/package.json",
JSON.stringify(JSON.parse(modifiedPackageJson), null, 2),
"utf8",
);
}
+4 -7
View File
@@ -1,10 +1,7 @@
import { OpenAI } from "./llm/LLM";
import { ChatMessage, LLM, MessageType } from "./llm/types";
import {
defaultSummaryPrompt,
messagesToHistoryStr,
SummaryPrompt,
} from "./Prompt";
import { OpenAI } from "./llm/LLM.js";
import type { ChatMessage, LLM, MessageType } from "./llm/types.js";
import type { SummaryPrompt } from "./Prompt.js";
import { defaultSummaryPrompt, messagesToHistoryStr } from "./Prompt.js";
/**
* A ChatHistory is used to keep the state of back and forth chat messages
+8 -4
View File
@@ -1,7 +1,11 @@
import { encodingForModel } from "js-tiktoken";
import { Event, EventTag, EventType } from "./callbacks/CallbackManager";
import { randomUUID } from "./env";
import { randomUUID } from "@llamaindex/env";
import type {
Event,
EventTag,
EventType,
} from "./callbacks/CallbackManager.js";
export enum Tokenizers {
CL100K_BASE = "cl100k_base",
@@ -32,7 +36,7 @@ class GlobalsHelper {
};
}
tokenizer(encoding?: string) {
tokenizer(encoding?: Tokenizers) {
if (encoding && encoding !== Tokenizers.CL100K_BASE) {
throw new Error(`Tokenizer encoding ${encoding} not yet supported`);
}
@@ -43,7 +47,7 @@ class GlobalsHelper {
return this.defaultTokenizer!.encode.bind(this.defaultTokenizer);
}
tokenizerDecoder(encoding?: string) {
tokenizerDecoder(encoding?: Tokenizers) {
if (encoding && encoding !== Tokenizers.CL100K_BASE) {
throw new Error(`Tokenizer encoding ${encoding} not yet supported`);
}
+6 -5
View File
@@ -1,5 +1,5 @@
import { createSHA256, path, randomUUID } from "@llamaindex/env";
import _ from "lodash";
import { createSHA256, path, randomUUID } from "./env";
export enum NodeRelationship {
SOURCE = "SOURCE",
@@ -65,7 +65,8 @@ export abstract class BaseNode<T extends Metadata = Metadata> {
abstract getContent(metadataMode: MetadataMode): string;
abstract getMetadataStr(metadataMode: MetadataMode): string;
abstract setContent(value: any): void;
// todo: set value as a generic type
abstract setContent(value: unknown): void;
get sourceNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.SOURCE];
@@ -353,10 +354,10 @@ export function splitNodesByType(nodes: BaseNode[]): {
imageNodes: ImageNode[];
textNodes: TextNode[];
} {
let imageNodes: ImageNode[] = [];
let textNodes: TextNode[] = [];
const imageNodes: ImageNode[] = [];
const textNodes: TextNode[] = [];
for (let node of nodes) {
for (const node of nodes) {
if (node instanceof ImageNode) {
imageNodes.push(node);
} else if (node instanceof TextNode) {
+4 -4
View File
@@ -1,5 +1,5 @@
import { SubQuestion } from "./engines/query/types";
import { BaseOutputParser, StructuredOutput } from "./types";
import type { SubQuestion } from "./engines/query/types.js";
import type { BaseOutputParser, StructuredOutput } from "./types.js";
/**
* Error class for output parsing. Due to the nature of LLMs, anytime we use LLM
@@ -44,8 +44,8 @@ export function parseJsonMarkdown(text: string) {
const left_square = text.indexOf("[");
const left_brace = text.indexOf("{");
var left: number;
var right: number;
let left: number;
let right: number;
if (left_square < left_brace && left_square != -1) {
left = left_square;
right = text.lastIndexOf("]");
+3 -3
View File
@@ -1,6 +1,6 @@
import { SubQuestion } from "./engines/query/types";
import { ChatMessage } from "./llm/types";
import { ToolMetadata } from "./types";
import type { SubQuestion } from "./engines/query/types.js";
import type { ChatMessage } from "./llm/types.js";
import type { ToolMetadata } from "./types.js";
/**
* A SimplePrompt is a function that takes a dictionary of inputs and returns a string.
+4 -4
View File
@@ -1,12 +1,12 @@
import { globalsHelper } from "./GlobalsHelper";
import { SimplePrompt } from "./Prompt";
import { SentenceSplitter } from "./TextSplitter";
import { globalsHelper } from "./GlobalsHelper.js";
import type { SimplePrompt } from "./Prompt.js";
import { SentenceSplitter } from "./TextSplitter.js";
import {
DEFAULT_CHUNK_OVERLAP_RATIO,
DEFAULT_CONTEXT_WINDOW,
DEFAULT_NUM_OUTPUTS,
DEFAULT_PADDING,
} from "./constants";
} from "./constants.js";
export function getEmptyPromptTxt(prompt: SimplePrompt) {
return prompt({});
+35 -11
View File
@@ -1,28 +1,52 @@
import { SubQuestionOutputParser } from "./OutputParser";
import {
SubQuestionPrompt,
buildToolsText,
defaultSubQuestionPrompt,
} from "./Prompt";
import { BaseQuestionGenerator, SubQuestion } from "./engines/query/types";
import { OpenAI } from "./llm/LLM";
import { LLM } from "./llm/types";
import { BaseOutputParser, StructuredOutput, ToolMetadata } from "./types";
import { SubQuestionOutputParser } from "./OutputParser.js";
import type { SubQuestionPrompt } from "./Prompt.js";
import { buildToolsText, defaultSubQuestionPrompt } from "./Prompt.js";
import type {
BaseQuestionGenerator,
SubQuestion,
} from "./engines/query/types.js";
import { OpenAI } from "./llm/LLM.js";
import type { LLM } from "./llm/types.js";
import { PromptMixin } from "./prompts/index.js";
import type {
BaseOutputParser,
StructuredOutput,
ToolMetadata,
} from "./types.js";
/**
* LLMQuestionGenerator uses the LLM to generate new questions for the LLM using tools and a user query.
*/
export class LLMQuestionGenerator implements BaseQuestionGenerator {
export class LLMQuestionGenerator
extends PromptMixin
implements BaseQuestionGenerator
{
llm: LLM;
prompt: SubQuestionPrompt;
outputParser: BaseOutputParser<StructuredOutput<SubQuestion[]>>;
constructor(init?: Partial<LLMQuestionGenerator>) {
super();
this.llm = init?.llm ?? new OpenAI();
this.prompt = init?.prompt ?? defaultSubQuestionPrompt;
this.outputParser = init?.outputParser ?? new SubQuestionOutputParser();
}
protected _getPrompts(): { [x: string]: SubQuestionPrompt } {
return {
subQuestion: this.prompt,
};
}
protected _updatePrompts(promptsDict: {
subQuestion: SubQuestionPrompt;
}): void {
if ("subQuestion" in promptsDict) {
this.prompt = promptsDict.subQuestion;
}
}
async generate(tools: ToolMetadata[], query: string): Promise<SubQuestion[]> {
const toolsStr = buildToolsText(tools);
const queryStr = query;
+1 -1
View File
@@ -1,4 +1,4 @@
import { BaseNode } from "./Node";
import type { BaseNode } from "./Node.js";
/**
* Response is the output of a LLM
+3 -3
View File
@@ -1,6 +1,6 @@
import { Event } from "./callbacks/CallbackManager";
import { NodeWithScore } from "./Node";
import { ServiceContext } from "./ServiceContext";
import type { Event } from "./callbacks/CallbackManager.js";
import type { NodeWithScore } from "./Node.js";
import type { ServiceContext } from "./ServiceContext.js";
/**
* Retrievers retrieve the nodes that most closely match our query in similarity.
+8 -5
View File
@@ -1,8 +1,11 @@
import { CallbackManager } from "./callbacks/CallbackManager";
import { BaseEmbedding, OpenAIEmbedding } from "./embeddings";
import { LLM, OpenAI } from "./llm";
import { NodeParser, SimpleNodeParser } from "./nodeParsers";
import { PromptHelper } from "./PromptHelper";
import { PromptHelper } from "./PromptHelper.js";
import { CallbackManager } from "./callbacks/CallbackManager.js";
import { OpenAIEmbedding } from "./embeddings/OpenAIEmbedding.js";
import type { BaseEmbedding } from "./embeddings/types.js";
import type { LLM } from "./llm/index.js";
import { OpenAI } from "./llm/index.js";
import { SimpleNodeParser } from "./nodeParsers/SimpleNodeParser.js";
import type { NodeParser } from "./nodeParsers/types.js";
/**
* The ServiceContext is a collection of components that are used in different parts of the application.
+13 -13
View File
@@ -1,7 +1,7 @@
import { EOL } from "./env";
import { EOL } from "@llamaindex/env";
// GitHub translated
import { globalsHelper } from "./GlobalsHelper";
import { DEFAULT_CHUNK_OVERLAP, DEFAULT_CHUNK_SIZE } from "./constants";
import { globalsHelper } from "./GlobalsHelper.js";
import { DEFAULT_CHUNK_OVERLAP, DEFAULT_CHUNK_SIZE } from "./constants.js";
class TextSplit {
textChunk: string;
@@ -130,7 +130,7 @@ export class SentenceSplitter {
getParagraphSplits(text: string, effectiveChunkSize?: number): string[] {
// get paragraph splits
let paragraphSplits: string[] = text.split(this.paragraphSeparator);
const paragraphSplits: string[] = text.split(this.paragraphSeparator);
let idx = 0;
if (effectiveChunkSize == undefined) {
return paragraphSplits;
@@ -155,9 +155,9 @@ export class SentenceSplitter {
}
getSentenceSplits(text: string, effectiveChunkSize?: number): string[] {
let paragraphSplits = this.getParagraphSplits(text, effectiveChunkSize);
const paragraphSplits = this.getParagraphSplits(text, effectiveChunkSize);
// Next we split the text using the chunk tokenizer fn/
let splits = [];
const splits = [];
for (const parText of paragraphSplits) {
const sentenceSplits = this.chunkingTokenizerFn(parText);
@@ -194,9 +194,9 @@ export class SentenceSplitter {
}));
}
let newSplits: SplitRep[] = [];
const newSplits: SplitRep[] = [];
for (const split of sentenceSplits) {
let splitTokens = this.tokenizer(split);
const splitTokens = this.tokenizer(split);
const splitLen = splitTokens.length;
if (splitLen <= effectiveChunkSize) {
newSplits.push({ text: split, numTokens: splitLen });
@@ -219,7 +219,7 @@ export class SentenceSplitter {
// go through sentence splits, combine to chunks that are within the chunk size
// docs represents final list of text chunks
let docs: TextSplit[] = [];
const docs: TextSplit[] = [];
// curChunkSentences represents the current list of sentence splits (that)
// will be merged into a chunk
let curChunkSentences: SplitRep[] = [];
@@ -287,18 +287,18 @@ export class SentenceSplitter {
return [];
}
let effectiveChunkSize = this.getEffectiveChunkSize(extraInfoStr);
let sentenceSplits = this.getSentenceSplits(text, effectiveChunkSize);
const effectiveChunkSize = this.getEffectiveChunkSize(extraInfoStr);
const sentenceSplits = this.getSentenceSplits(text, effectiveChunkSize);
// Check if any sentences exceed the chunk size. If they don't,
// force split by tokenizer
let newSentenceSplits = this.processSentenceSplits(
const newSentenceSplits = this.processSentenceSplits(
sentenceSplits,
effectiveChunkSize,
);
// combine sentence splits into chunks of text that can then be returned
let combinedTextSplits = this.combineTextSplits(
const combinedTextSplits = this.combineTextSplits(
newSentenceSplits,
effectiveChunkSize,
);

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