Compare commits

...

22 Commits

Author SHA1 Message Date
github-actions[bot] 9a71382243 Release 0.5.5 (#1046)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-17 16:26:38 +07:00
Thuc Pham b974eea341 feat: add MetadataFilter for SimpleVectorStore and Milvus (#1030)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-07-17 16:21:21 +07:00
github-actions[bot] e82632f83d Release 0.5.4 (#1043)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-16 14:53:20 -07:00
Fabian Wimmer 1a65ead849 feat: add vendorMultiModal params to LlamaParseReader (#1042) 2024-07-16 14:20:34 -07:00
Alex Yang 50b7d1b7bb refactor: put embedding into core (#1041) 2024-07-16 10:49:03 -07:00
github-actions[bot] 09beb72f5b Release 0.5.3 (#1038)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-16 10:25:27 -07:00
Fabian Wimmer 9bbbc67c8e feat: add a reader for Discord messages (#1040) 2024-07-16 10:19:48 -07:00
Brian Peiris b3681bf681 fix: DataCloneError when using FunctionTool (#1037) 2024-07-14 15:24:49 -07:00
Alex Yang b548b1443b chore: bump version (#1032) 2024-07-12 15:14:27 -07:00
github-actions[bot] 0e980d962d Release 0.5.2 (#1035)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-12 11:44:32 -07:00
Alex Yang 3ed6acc6a6 chore: bump cloud api (#1036) 2024-07-12 11:21:37 -07:00
Parham Saidi 56746c240f fix: bedrock handle empty content and added max tokens export (#1034) 2024-07-12 09:47:49 -07:00
Alex Yang 5c1c2c7f5b ci: only commit lock file (#1031) 2024-07-10 10:17:35 -07:00
github-actions[bot] a699086f46 Release 0.5.1 (#1028)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-09 15:36:20 -07:00
Alex Yang 454c204112 chore: bump version (#1029) 2024-07-09 13:42:09 -07:00
Julius Lipp 277468160d feat: add mixedbread ai integration (#953) 2024-07-09 09:36:43 -07:00
Ranjan Mangla a0f424e592 fix: corrected the regex in the ReactAgent (#1022)
Signed-off-by: ranjanmangla1 <ranjanmangla1@gmail.com>
2024-07-09 08:55:38 -07:00
github-actions[bot] 3ae832ca28 Release 0.5.0 (#1024)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: himself65 <himself65@users.noreply.github.com>
2024-07-08 17:16:46 -07:00
Alex Yang 16ef5dd631 feat: simplify callback manager (#1027) 2024-07-08 16:44:54 -07:00
Alex Yang c4bd0a5215 refactor: move llm & callback manager to core module (#1026) 2024-07-08 15:48:59 -07:00
Alex Yang f5c8ca7dfb chore: use bunchee bundler for all (#1025) 2024-07-08 09:45:55 -07:00
Sacha Bron 36ddec44af fix: typo in custom page separator parameter for LlamaParse (#1023) 2024-07-08 09:27:51 -07:00
177 changed files with 9684 additions and 6760 deletions
+1
View File
@@ -67,3 +67,4 @@ jobs:
with:
commit_message: "chore: update lock file"
branch: changeset-release/main
file_pattern: "pnpm-lock.yaml"
+48
View File
@@ -1,5 +1,53 @@
# docs
## 0.0.46
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.0.45
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.0.44
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.0.43
### Patch Changes
- llamaindex@0.5.2
## 0.0.42
### Patch Changes
- 2774681: Add mixedbread's embeddings and reranking API
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.0.41
### Patch Changes
- 36ddec4: fix: typo in custom page separator parameter for LlamaParse
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
- @llamaindex/examples@0.0.7
## 0.0.40
### Patch Changes
@@ -0,0 +1,34 @@
import CodeBlock from "@theme/CodeBlock";
import CodeSource from "!raw-loader!../../../../../examples/readers/src/discord";
# DiscordReader
DiscordReader is a simple data loader that reads all messages in a given Discord channel and returns them as Document objects.
It uses the [@discordjs/rest](https://github.com/discordjs/discord.js/tree/main/packages/rest) library to fetch the messages.
## Usage
First step is to create a Discord Application and generating a bot token [here](https://discord.com/developers/applications).
In your Discord Application, go to the `OAuth2` tab and generate an invite URL by selecting `bot` and click `Read Messages/View Channels` as wells as `Read Message History`.
This will invite the bot with the necessary permissions to read messages.
Copy the URL in your browser and select the server you want your bot to join.
<CodeBlock language="ts">{CodeSource}</CodeBlock>
### Params
#### DiscordReader()
- `discordToken?`: The Discord bot token.
- `makeRequest?`: Optionally provide a custom request function for edge environments, e.g. `fetch`. See discord.js for more info.
#### DiscordReader.loadData
- `channelIDs`: The ID(s) of discord channels as an array of strings.
- `limit?`: Optionally limit the number of messages to read
- `additionalInfo?`: An optional flag to include embedded messages and attachment urls in the document.
- `oldestFirst?`: An optional flag to return the oldest messages first.
## API Reference
- [DiscordReader](../../api/classes/DiscordReader.md)
@@ -27,25 +27,28 @@ They can be divided into two groups.
- `apiKey` is required. Can be set as an environment variable `LLAMA_CLOUD_API_KEY`
- `checkInterval` is the interval in seconds to check if the parsing is done. Default is `1`.
- `maxTimeout` is the maximum timout to wait for parsing to finish. Default is `2000`
- `maxTimeout` is the maximum timeout to wait for parsing to finish. Default is `2000`
- `verbose` shows progress of the parsing. Default is `true`
- `ignoreErrors` set to false to get errors while parsing. Default is `true` and returns an empty array on error.
#### Advanced params:
- `resultType` can be set to `markdown`, `text` or `json`. Defaults to `text`. More information about `json` mode on the next pages.
- `language` primarly helps with OCR recognition. Defaults to `en`. Click [here](../../../api/type-aliases/Language.md) for a list of supported languages.
- `language` primarily helps with OCR recognition. Defaults to `en`. Click [here](../../../api/type-aliases/Language.md) for a list of supported languages.
- `parsingInstructions?` Optional. Can help with complicated document structures. See this [LlamaIndex Blog Post](https://www.llamaindex.ai/blog/launching-the-first-genai-native-document-parsing-platform) for an example.
- `skipDiagonalText?` Optional. Set to true to ignore diagonal text. (Text that is not rotated 0, 90, 180 or 270 degrees)
- `invalidateCache?` Optional. Set to true to ignore the LlamaCloud cache. All document are kept in cache for 48hours after the job was completed to avoid processing the same document twice. Can be useful for testing when trying to re-parse the same document with, e.g. different `parsingInstructions`.
- `doNotCache?` Optional. Set to true to not cache the document.
- `fastMode?` Optional. Set to true to use the fast mode. This mode will skip OCR of images, and table/heading reconstruction. Note: Non-compatible with `gpt4oMode`.
- `doNotUnrollColumns?` Optional. Set to true to keep the text according to document layout. Reduce reconstruction accuracy, and LLM's/embedings performances in most cases.
- `pageSeperator?` Optional. The page seperator to use. Defaults is `\\n---\\n`.
- `gpt4oMode` set to true to use GPT-4o to extract content. Default is `false`.
- `gpt4oApiKey?` Optional. Set the GPT-4o API key. Lowers the cost of parsing by using your own API key. Your OpenAI account will be charged. Can also be set in the environment variable `LLAMA_CLOUD_GPT4O_API_KEY`.
- `doNotUnrollColumns?` Optional. Set to true to keep the text according to document layout. Reduce reconstruction accuracy, and LLMs/embeddings performances in most cases.
- `pageSeparator?` Optional. The page separator to use. Defaults is `\\n---\\n`.
- `gpt4oMode` Deprecated. Use vendorMultimodal params. Set to true to use GPT-4o to extract content. Default is `false`.
- `gpt4oApiKey?` Deprecated. Use vendorMultimodal params. Optional. Set the GPT-4o API key. Lowers the cost of parsing by using your own API key. Your OpenAI account will be charged. Can also be set in the environment variable `LLAMA_CLOUD_GPT4O_API_KEY`.
- `boundingBox?` Optional. Specify an area of the document to parse. Expects the bounding box margins as a string in clockwise order, e.g. `boundingBox = "0.1,0,0,0"` to not parse the top 10% of the document.
- `targetPages?` Optional. Specify which pages to parse by specifying them as a comma-seperated list. First page is `0`.
- `targetPages?` Optional. Specify which pages to parse by specifying them as a comma-separated list. First page is `0`.
- `useVendorMultimodalModel` set to true to use a multimodal model. Default is `false`.
- `vendorMultimodalModel?` Optional. Specify which multimodal model to use. Default is GPT4o. See [here](https://docs.cloud.llamaindex.ai/llamaparse/features/multimodal) for a list of available models and cost.
- `vendorMultimodalApiKey?` Optional. Set the multimodal model API key. Can also be set in the environment variable `LLAMA_CLOUD_VENDOR_MULTIMODAL_API_KEY`.
- `numWorkers` as in the python version, is set in `SimpleDirectoryReader`. Default is 1.
### LlamaParse with SimpleDirectoryReader
@@ -0,0 +1,100 @@
# MixedbreadAI
Welcome to the mixedbread embeddings guide! This guide will help you use the mixedbread ai's API to generate embeddings for your text documents, ensuring you get the most relevant information, just like picking the freshest bread from the bakery.
To find out more about the latest features, updates, and available models, visit [mixedbread.ai](https://mixedbread-ai.com/).
## Table of Contents
1. [Setup](#setup)
2. [Usage with LlamaIndex](#integration-with-llamaindex)
3. [Embeddings with Custom Parameters](#embeddings-with-custom-parameters)
## Setup
First, you will need to install the `llamaindex` package.
```bash
pnpm install llamaindex
```
Next, sign up for an API key at [mixedbread.ai](https://mixedbread.ai/). Once you have your API key, you can import the necessary modules and create a new instance of the `MixedbreadAIEmbeddings` class.
```ts
import { MixedbreadAIEmbeddings, Document, Settings } from "llamaindex";
```
## Usage with LlamaIndex
This section will guide you through integrating mixedbread embeddings with LlamaIndex for more advanced usage.
### Step 1: 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, like a variety of breads in a bakery.
```ts
Settings.embedModel = new MixedbreadAIEmbeddings({
apiKey: "<MIXEDBREAD_API_KEY>",
model: "mixedbread-ai/mxbai-embed-large-v1",
});
const document = new Document({
text: "The true source of happiness.",
id_: "bread",
});
const index = await VectorStoreIndex.fromDocuments([document]);
```
### Step 2: Create a Query Engine
Combine the retriever and the embed model to create a query engine. This setup ensures that your queries are processed to provide the best results, like arranging the bread in the order of freshness and quality.
Models can require prompts to generate embeddings for queries, in the 'mixedbread-ai/mxbai-embed-large-v1' model's case, the prompt is `Represent this sentence for searching relevant passages:`.
```ts
const queryEngine = index.asQueryEngine();
const query =
"Represent this sentence for searching relevant passages: What is bread?";
// Log the response
const results = await queryEngine.query(query);
console.log(results); // Serving up the freshest, most relevant results.
```
## Embeddings with Custom Parameters
This section will guide you through generating embeddings with custom parameters and usage with f.e. matryoshka and binary embeddings.
### Step 1: Create an Instance of MixedbreadAIEmbeddings
Create a new instance of the `MixedbreadAIEmbeddings` class with custom parameters. For example, to use the `mixedbread-ai/mxbai-embed-large-v1` model with a batch size of 64, normalized embeddings, and binary encoding format:
```ts
const embeddings = new MixedbreadAIEmbeddings({
apiKey: "<MIXEDBREAD_API_KEY>",
model: "mixedbread-ai/mxbai-embed-large-v1",
batchSize: 64,
normalized: true,
dimensions: 512,
encodingFormat: MixedbreadAI.EncodingFormat.Binary,
});
```
### Step 2: Define Texts
Define the texts you want to generate embeddings for.
```ts
const texts = ["Bread is life", "Bread is love"];
```
### Step 3: Generate Embeddings
Use the `embedDocuments` method to generate embeddings for the texts.
```ts
const result = await embeddings.embedDocuments(texts);
console.log(result); // Perfectly customized embeddings, ready to serve.
```
@@ -0,0 +1,164 @@
# MixedbreadAI
Welcome to the mixedbread ai reranker guide! This guide will help you use mixedbread ai's API to rerank search query results, ensuring you get the most relevant information, just like picking the freshest bread from the bakery.
To find out more about the latest features and updates, visit the [mixedbread.ai](https://mixedbread.ai/).
## Table of Contents
1. [Setup](#setup)
2. [Usage with LlamaIndex](#integration-with-llamaindex)
3. [Simple Reranking Guide](#simple-reranking-guide)
4. [Reranking with Objects](#reranking-with-objects)
## Setup
First, you will need to install the `llamaindex` package.
```bash
pnpm install llamaindex
```
Next, sign up for an API key at [mixedbread.ai](https://mixedbread.ai/). Once you have your API key, you can import the necessary modules and create a new instance of the `MixedbreadAIReranker` class.
```ts
import {
MixedbreadAIReranker,
Document,
OpenAI,
VectorStoreIndex,
Settings,
} from "llamaindex";
```
## Usage with LlamaIndex
This section will guide you through integrating mixedbread's reranker with LlamaIndex.
### Step 1: 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, like a variety of breads in a bakery.
```ts
const document = new Document({
text: "This is a sample document.",
id_: "sampleDoc",
});
Settings.llm = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0.1 });
const index = await VectorStoreIndex.fromDocuments([document]);
```
### Step 2: Increase Similarity TopK
The default value for `similarityTopK` is 2, which means only the most similar document will be returned. To get more results, like picking a variety of fresh breads, you can increase the value of `similarityTopK`.
```ts
const retriever = index.asRetriever();
retriever.similarityTopK = 5;
```
### Step 3: Create a MixedbreadAIReranker Instance
Create a new instance of the `MixedbreadAIReranker` class.
```ts
const nodePostprocessor = new MixedbreadAIReranker({
apiKey: "<MIXEDBREAD_API_KEY>",
topN: 4,
});
```
### Step 4: Create a Query Engine
Combine the retriever and node postprocessor to create a query engine. This setup ensures that your queries are processed and reranked to provide the best results, like arranging the bread in the order of freshness and quality.
```ts
const queryEngine = index.asQueryEngine({
retriever,
nodePostprocessors: [nodePostprocessor],
});
// Log the response
const response = await queryEngine.query("Where did the author grow up?");
console.log(response);
```
With mixedbread's Reranker, you're all set to serve up the most relevant and well-ordered results, just like a skilled baker arranging their best breads for eager customers. Enjoy the perfect blend of technology and culinary delight!
## Simple Reranking Guide
This section will guide you through a simple reranking process using mixedbread ai.
### Step 1: Create an Instance of MixedbreadAIReranker
Create a new instance of the `MixedbreadAIReranker` class, passing in your API key and the number of results you want to return. It's like setting up your bakery to offer a specific number of freshly baked items.
```ts
const reranker = new MixedbreadAIReranker({
apiKey: "<MIXEDBREAD_API_KEY>",
topN: 4,
});
```
### Step 2: Define Nodes and Query
Define the nodes (documents) you want to rerank and the query.
```ts
const nodes = [
{ node: new BaseNode("To bake bread you need flour") },
{ node: new BaseNode("To bake bread you need yeast") },
];
const query = "What do you need to bake bread?";
```
### Step 3: Perform Reranking
Use the `postprocessNodes` method to rerank the nodes based on the query.
```ts
const result = await reranker.postprocessNodes(nodes, query);
console.log(result); // Like pulling freshly baked nodes out of the oven.
```
## Reranking with Objects
This section will guide you through reranking when working with objects.
### Step 1: Create an Instance of MixedbreadAIReranker
Create a new instance of the `MixedbreadAIReranker` class, just like before.
```ts
const reranker = new MixedbreadAIReranker({
apiKey: "<MIXEDBREAD_API_KEY>",
model: "mixedbread-ai/mxbai-rerank-large-v1",
topK: 5,
rankFields: ["title", "content"],
returnInput: true,
maxRetries: 5,
});
```
### Step 2: Define Documents and Query
Define the documents (objects) you want to rerank and the query.
```ts
const documents = [
{ title: "Bread Recipe", content: "To bake bread you need flour" },
{ title: "Bread Recipe", content: "To bake bread you need yeast" },
];
const query = "What do you need to bake bread?";
```
### Step 3: Perform Reranking
Use the `rerank` method to reorder the documents based on the query.
```ts
const result = await reranker.rerank(documents, query);
console.log(result); // Perfectly customized results, ready to serve.
```
@@ -75,7 +75,7 @@ const queryEngine = index.asQueryEngine({
{
key: "dogId",
value: "2",
filterType: "ExactMatch",
operator: "==",
},
],
},
@@ -135,7 +135,7 @@ async function main() {
{
key: "dogId",
value: "2",
filterType: "ExactMatch",
operator: "==",
},
],
},
+19 -19
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.40",
"version": "0.0.46",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
@@ -15,29 +15,29 @@
"typecheck": "tsc"
},
"dependencies": {
"@docusaurus/core": "^3.3.2",
"@docusaurus/remark-plugin-npm2yarn": "^3.3.2",
"@docusaurus/core": "3.4.0",
"@docusaurus/remark-plugin-npm2yarn": "3.4.0",
"@llamaindex/examples": "workspace:*",
"@mdx-js/react": "^3.0.1",
"clsx": "^2.1.1",
"@mdx-js/react": "3.0.1",
"clsx": "2.1.1",
"llamaindex": "workspace:*",
"postcss": "^8.4.38",
"prism-react-renderer": "^2.3.1",
"raw-loader": "^4.0.2",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"postcss": "8.4.39",
"prism-react-renderer": "2.3.1",
"raw-loader": "4.0.2",
"react": "18.3.1",
"react-dom": "18.3.1"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.3.2",
"@docusaurus/preset-classic": "^3.3.2",
"@docusaurus/theme-classic": "^3.3.2",
"@docusaurus/types": "^3.3.2",
"@tsconfig/docusaurus": "^2.0.3",
"@docusaurus/module-type-aliases": "3.4.0",
"@docusaurus/preset-classic": "3.4.0",
"@docusaurus/theme-classic": "3.4.0",
"@docusaurus/types": "3.4.0",
"@tsconfig/docusaurus": "2.0.3",
"@types/node": "^20.12.11",
"docusaurus-plugin-typedoc": "^1.0.1",
"typedoc": "^0.25.13",
"typedoc-plugin-markdown": "^4.0.1",
"typescript": "^5.5.2"
"docusaurus-plugin-typedoc": "1.0.3",
"typedoc": "0.26.4",
"typedoc-plugin-markdown": "4.1.2",
"typescript": "^5.5.3"
},
"browserslist": {
"production": [
+10
View File
@@ -1,5 +1,15 @@
# examples
## 0.0.7
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
- @llamaindex/core@0.1.0
## 0.0.6
### Patch Changes
+1 -1
View File
@@ -2,7 +2,7 @@ import { Anthropic, FunctionTool, Settings, WikipediaTool } from "llamaindex";
import { AnthropicAgent } from "llamaindex/agent/anthropic";
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log("llm-tool-call", event.detail.payload.toolCall);
console.log("llm-tool-call", event.detail.toolCall);
});
const anthropic = new Anthropic({
+1 -1
View File
@@ -40,7 +40,7 @@ async function main() {
{
key: "dogId",
value: "2",
filterType: "ExactMatch",
operator: "==",
},
],
},
+40
View File
@@ -0,0 +1,40 @@
import { MilvusVectorStore, VectorStoreIndex } from "llamaindex";
const collectionName = "movie_reviews";
async function main() {
try {
const milvus = new MilvusVectorStore({ collection: collectionName });
const index = await VectorStoreIndex.fromVectorStore(milvus);
const retriever = index.asRetriever({ similarityTopK: 20 });
console.log("\n=====\nQuerying the index with filters");
const queryEngineWithFilters = index.asQueryEngine({
retriever,
preFilters: {
filters: [
{
key: "document_id",
value: "./data/movie_reviews.csv_37",
operator: "==",
},
{
key: "document_id",
value: "./data/movie_reviews.csv_37",
operator: "!=",
},
],
condition: "or",
},
});
const resultAfterFilter = await queryEngineWithFilters.query({
query: "Get all movie titles.",
});
console.log(`Query from ${resultAfterFilter.sourceNodes?.length} nodes`);
console.log(resultAfterFilter.response);
} catch (e) {
console.error(e);
}
}
void main();
+143
View File
@@ -0,0 +1,143 @@
import {
Document,
Settings,
SimpleDocumentStore,
VectorStoreIndex,
storageContextFromDefaults,
} from "llamaindex";
Settings.callbackManager.on("retrieve-end", (event) => {
const { nodes } = event.detail;
console.log("Number of retrieved nodes:", nodes.length);
});
async function getDataSource() {
const docs = [
new Document({
text: "The dog is brown",
metadata: {
dogId: "1",
private: true,
},
}),
new Document({
text: "The dog is yellow",
metadata: {
dogId: "2",
private: false,
},
}),
new Document({
text: "The dog is red",
metadata: {
dogId: "3",
private: false,
},
}),
];
const storageContext = await storageContextFromDefaults({
persistDir: "./cache",
});
const numberOfDocs = Object.keys(
(storageContext.docStore as SimpleDocumentStore).toDict(),
).length;
if (numberOfDocs === 0) {
// Generate the data source if it's empty
return await VectorStoreIndex.fromDocuments(docs, {
storageContext,
});
}
return await VectorStoreIndex.init({
storageContext,
});
}
async function main() {
const index = await getDataSource();
console.log(
"=============\nQuerying index with no filters. The output should be any color.",
);
const queryEngineNoFilters = index.asQueryEngine({
similarityTopK: 3,
});
const noFilterResponse = await queryEngineNoFilters.query({
query: "What is the color of the dog?",
});
console.log("No filter response:", noFilterResponse.toString());
console.log(
"\n=============\nQuerying index with dogId 2 and private false. The output always should be red.",
);
const queryEngineEQ = index.asQueryEngine({
preFilters: {
filters: [
{
key: "private",
value: "false",
operator: "==",
},
{
key: "dogId",
value: "3",
operator: "==",
},
],
},
similarityTopK: 3,
});
const responseEQ = await queryEngineEQ.query({
query: "What is the color of the dog?",
});
console.log("Filter with dogId 2 response:", responseEQ.toString());
console.log(
"\n=============\nQuerying index with dogId IN (1, 3). The output should be brown and red.",
);
const queryEngineIN = index.asQueryEngine({
preFilters: {
filters: [
{
key: "dogId",
value: ["1", "3"],
operator: "in",
},
],
},
similarityTopK: 3,
});
const responseIN = await queryEngineIN.query({
query: "What is the color of the dog?",
});
console.log("Filter with dogId IN (1, 3) response:", responseIN.toString());
console.log(
"\n=============\nQuerying index with dogId IN (1, 3). The output should be any.",
);
const queryEngineOR = index.asQueryEngine({
preFilters: {
filters: [
{
key: "private",
value: "false",
operator: "==",
},
{
key: "dogId",
value: ["1", "3"],
operator: "in",
},
],
condition: "or",
},
similarityTopK: 3,
});
const responseOR = await queryEngineOR.query({
query: "What is the color of the dog?",
});
console.log(
"Filter with dogId with OR operator response:",
responseOR.toString(),
);
}
void main();
+2 -3
View File
@@ -4,7 +4,6 @@ import {
NodeWithScore,
ObjectType,
OpenAI,
RetrievalEndEvent,
Settings,
VectorStoreIndex,
} from "llamaindex";
@@ -18,8 +17,8 @@ Settings.chunkOverlap = 20;
Settings.llm = new OpenAI({ model: "gpt-4-turbo", maxTokens: 512 });
// Update callbackManager
Settings.callbackManager.on("retrieve-end", (event: RetrievalEndEvent) => {
const { nodes, query } = event.detail.payload;
Settings.callbackManager.on("retrieve-end", (event) => {
const { nodes, query } = event.detail;
const imageNodes = nodes.filter(
(node: NodeWithScore) => node.node.type === ObjectType.IMAGE_DOCUMENT,
);
+2 -3
View File
@@ -1,7 +1,6 @@
import {
MultiModalResponseSynthesizer,
OpenAI,
RetrievalEndEvent,
Settings,
VectorStoreIndex,
} from "llamaindex";
@@ -15,8 +14,8 @@ Settings.chunkOverlap = 20;
Settings.llm = new OpenAI({ model: "gpt-4-turbo", maxTokens: 512 });
// Update callbackManager
Settings.callbackManager.on("retrieve-end", (event: RetrievalEndEvent) => {
const { nodes, query } = event.detail.payload;
Settings.callbackManager.on("retrieve-end", (event) => {
const { nodes, query } = event.detail;
console.log(`Retrieved ${nodes.length} nodes for query: ${query}`);
});
+5 -4
View File
@@ -1,26 +1,27 @@
{
"name": "@llamaindex/examples",
"private": true,
"version": "0.0.6",
"version": "0.0.7",
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@azure/identity": "^4.2.1",
"@datastax/astra-db-ts": "^1.2.1",
"@llamaindex/core": "^0.1.0",
"@notionhq/client": "^2.2.15",
"@pinecone-database/pinecone": "^2.2.2",
"@zilliz/milvus2-sdk-node": "^2.4.2",
"@zilliz/milvus2-sdk-node": "^2.4.4",
"chromadb": "^1.8.1",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"js-tiktoken": "^1.0.12",
"llamaindex": "^0.4.3",
"llamaindex": "^0.5.0",
"mongodb": "^6.7.0",
"pathe": "^1.1.2"
},
"devDependencies": {
"@types/node": "^20.14.1",
"tsx": "^4.15.6",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
},
"scripts": {
"lint": "eslint ."
+8 -9
View File
@@ -1,8 +1,8 @@
import * as dotenv from "dotenv";
import {
CallbackManager,
Document,
MetadataMode,
NodeWithScore,
QdrantVectorStore,
Settings,
VectorStoreIndex,
@@ -10,13 +10,12 @@ import {
} from "llamaindex";
// Update callback manager
Settings.callbackManager = new CallbackManager({
onRetrieve: (data) => {
console.log(
"The retrieved nodes are:",
data.nodes.map((node) => node.node.getContent(MetadataMode.NONE)),
);
},
Settings.callbackManager.on("retrieve-end", (event) => {
const { nodes } = event.detail;
console.log(
"The retrieved nodes are:",
nodes.map((node: NodeWithScore) => node.node.getContent(MetadataMode.NONE)),
);
});
// Load environment variables from local .env file
@@ -65,7 +64,7 @@ async function main() {
{
key: "dogId",
value: "2",
filterType: "ExactMatch",
operator: "==",
},
],
},
+3 -2
View File
@@ -12,7 +12,8 @@
"start:llamaparse": "node --import tsx ./src/llamaparse.ts",
"start:notion": "node --import tsx ./src/notion.ts",
"start:llamaparse-dir": "node --import tsx ./src/simple-directory-reader-with-llamaparse.ts",
"start:llamaparse-json": "node --import tsx ./src/llamaparse-json.ts"
"start:llamaparse-json": "node --import tsx ./src/llamaparse-json.ts",
"start:discord": "node --import tsx ./src/discord.ts"
},
"dependencies": {
"llamaindex": "*"
@@ -20,6 +21,6 @@
"devDependencies": {
"@types/node": "^20.12.11",
"tsx": "^4.15.6",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
}
}
+20
View File
@@ -0,0 +1,20 @@
import { DiscordReader } from "llamaindex";
async function main() {
// Create an instance of the DiscordReader. Set token here or DISCORD_TOKEN environment variable
const discordReader = new DiscordReader();
// Specify the channel IDs you want to read messages from as an arry of strings
const channelIds = ["721374320794009630", "719596376261918720"];
// Specify the number of messages to fetch per channel
const limit = 10;
// Load messages from the specified channel
const messages = await discordReader.loadData(channelIds, limit, true);
// Print out the messages
console.log(messages);
}
main().catch(console.error);
+5 -5
View File
@@ -1,7 +1,7 @@
import { extractText } from "@llamaindex/core/utils";
import { encodingForModel } from "js-tiktoken";
import { ChatMessage, OpenAI, type LLMStartEvent } from "llamaindex";
import { ChatMessage, OpenAI } from "llamaindex";
import { Settings } from "llamaindex/Settings";
import { extractText } from "llamaindex/llm/utils";
const encoding = encodingForModel("gpt-4-0125-preview");
@@ -12,8 +12,8 @@ const llm = new OpenAI({
let tokenCount = 0;
Settings.callbackManager.on("llm-start", (event: LLMStartEvent) => {
const { messages } = event.detail.payload;
Settings.callbackManager.on("llm-start", (event) => {
const { messages } = event.detail;
messages.reduce((count: number, message: ChatMessage) => {
return count + encoding.encode(extractText(message.content)).length;
}, 0);
@@ -24,7 +24,7 @@ Settings.callbackManager.on("llm-start", (event: LLMStartEvent) => {
});
Settings.callbackManager.on("llm-stream", (event) => {
const { chunk } = event.detail.payload;
const { chunk } = event.detail;
const { delta } = chunk;
tokenCount += encoding.encode(extractText(delta)).length;
if (tokenCount > 20) {
+5 -8
View File
@@ -21,27 +21,24 @@
"@changesets/cli": "^2.27.5",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.4",
"eslint-config-next": "^14.2.5",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "^2.0.5",
"eslint-plugin-react": "7.34.1",
"eslint-plugin-react": "7.34.3",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"madge": "^7.0.0",
"prettier": "^3.3.2",
"prettier-plugin-organize-imports": "^3.2.4",
"prettier-plugin-organize-imports": "^4.0.0",
"turbo": "^2.0.5",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
},
"packageManager": "pnpm@9.4.0",
"packageManager": "pnpm@9.5.0",
"pnpm": {
"overrides": {
"trim": "1.0.1",
"@babel/traverse": "7.23.2",
"protobufjs": "7.2.6"
},
"patchedDependencies": {
"bunchee@5.2.1": "patches/bunchee@5.2.1.patch"
}
},
"lint-staged": {
+9
View File
@@ -1,5 +1,14 @@
# @llamaindex/autotool
## 2.0.0
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 1.0.0
### Patch Changes
@@ -5,7 +5,7 @@
"dependencies": {
"@llamaindex/autotool": "workspace:*",
"llamaindex": "workspace:*",
"openai": "^4.52.0"
"openai": "^4.52.5"
},
"devDependencies": {
"tsx": "^4.15.6"
@@ -1,5 +1,56 @@
# @llamaindex/autotool-02-next-example
## 0.1.30
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
- @llamaindex/autotool@2.0.0
## 0.1.29
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
- @llamaindex/autotool@2.0.0
## 0.1.28
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
- @llamaindex/autotool@2.0.0
## 0.1.27
### Patch Changes
- llamaindex@0.5.2
- @llamaindex/autotool@2.0.0
## 0.1.26
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
- @llamaindex/autotool@2.0.0
## 0.1.25
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
- @llamaindex/autotool@2.0.0
## 0.1.24
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool-02-next-example",
"private": true,
"version": "0.1.24",
"version": "0.1.30",
"scripts": {
"dev": "next dev",
"build": "next build",
@@ -14,7 +14,7 @@
"class-variance-authority": "^0.7.0",
"dotenv": "^16.3.1",
"llamaindex": "workspace:*",
"lucide-react": "^0.378.0",
"lucide-react": "^0.407.0",
"next": "14.3.0-canary.51",
"react": "^18.3.1",
"react-dom": "^18.3.1",
@@ -32,6 +32,6 @@
"cross-env": "^7.0.3",
"postcss": "^8.4.32",
"tailwindcss": "^3.4.4",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
}
}
+7 -7
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool",
"type": "module",
"version": "1.0.0",
"version": "2.0.0",
"description": "auto transpile your JS function to LLM Agent compatible",
"files": [
"dist",
@@ -47,11 +47,11 @@
"dependencies": {
"@swc/core": "^1.6.3",
"jotai": "^2.8.3",
"typedoc": "^0.25.13",
"typedoc": "^0.26.4",
"unplugin": "^1.10.1"
},
"peerDependencies": {
"llamaindex": "^0.4.14",
"llamaindex": "^0.5.5",
"openai": "^4",
"typescript": "^4"
},
@@ -70,13 +70,13 @@
"@swc/types": "^0.1.8",
"@types/json-schema": "^7.0.15",
"@types/node": "^20.12.11",
"bunchee": "^5.2.1",
"bunchee": "5.3.0-beta.0",
"llamaindex": "workspace:*",
"next": "14.2.3",
"next": "14.2.5",
"rollup": "^4.18.0",
"tsx": "^4.15.6",
"typescript": "^5.5.2",
"vitest": "^1.6.0",
"typescript": "^5.5.3",
"vitest": "^2.0.2",
"webpack": "^5.92.1"
}
}
+12
View File
@@ -1,5 +1,17 @@
# @llamaindex/cloud
## 0.2.0
### Minor Changes
- 3ed6acc: feat: cloud api change
## 0.1.4
### Patch Changes
- 36ddec4: fix: typo in custom page separator parameter for LlamaParse
## 0.1.3
### Patch Changes
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloud",
"version": "0.1.3",
"version": "0.2.0",
"type": "module",
"license": "MIT",
"scripts": {
@@ -35,6 +35,6 @@
},
"devDependencies": {
"@hey-api/openapi-ts": "^0.48.0",
"bunchee": "^5.2.1"
"bunchee": "5.3.0-beta.0"
}
}
+29
View File
@@ -1,5 +1,34 @@
# @llamaindex/community
## 0.0.22
### Patch Changes
- Updated dependencies [b974eea]
- @llamaindex/core@0.1.2
## 0.0.21
### Patch Changes
- Updated dependencies [b3681bf]
- @llamaindex/core@0.1.1
## 0.0.20
### Patch Changes
- 56746c2: fix: llama3 patched to handle empty content (can happen with system) and added max tokens export
## 0.0.19
### Patch Changes
- 16ef5dd: refactor: depends on core pacakge instead of llamaindex
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- @llamaindex/core@0.1.0
## 0.0.18
### Patch Changes
+7 -12
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/community",
"description": "Community package for LlamaIndexTS",
"version": "0.0.18",
"version": "0.0.22",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
@@ -38,20 +38,15 @@
"directory": "packages/community"
},
"scripts": {
"build": "rm -rf ./dist && pnpm run build:code && pnpm run build:type",
"build:code": "tsup",
"build:type": "tsc -p tsconfig.json",
"dev": "concurrently \"pnpm run build:esm --watch\" \"pnpm run build:cjs --watch\" \"pnpm run build:type --watch\""
"build": "bunchee",
"dev": "bunchee --watch"
},
"devDependencies": {
"@swc/cli": "^0.3.12",
"@swc/core": "^1.6.3",
"concurrently": "^8.2.2",
"tsup": "^8.1.0"
"@types/node": "^20.14.2",
"bunchee": "5.3.0-beta.0"
},
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.600.0",
"@types/node": "^20.14.2",
"llamaindex": "workspace:*"
"@aws-sdk/client-bedrock-runtime": "^3.613.0",
"@llamaindex/core": "workspace:*"
}
}
+5 -1
View File
@@ -1 +1,5 @@
export { BEDROCK_MODELS, Bedrock } from "./llm/bedrock/base.js";
export {
BEDROCK_MODELS,
BEDROCK_MODEL_MAX_TOKENS,
Bedrock,
} from "./llm/bedrock/base.js";
+25 -12
View File
@@ -4,18 +4,19 @@ import {
InvokeModelCommand,
InvokeModelWithResponseStreamCommand,
} from "@aws-sdk/client-bedrock-runtime";
import type {
ChatMessage,
ChatResponse,
CompletionResponse,
LLMChatParamsNonStreaming,
LLMChatParamsStreaming,
LLMCompletionParamsNonStreaming,
LLMCompletionParamsStreaming,
LLMMetadata,
ToolCallLLMMessageOptions,
} from "llamaindex";
import { streamConverter, ToolCallLLM, wrapLLMEvent } from "llamaindex";
import {
type ChatMessage,
type ChatResponse,
type CompletionResponse,
type LLMChatParamsNonStreaming,
type LLMChatParamsStreaming,
type LLMCompletionParamsNonStreaming,
type LLMCompletionParamsStreaming,
type LLMMetadata,
ToolCallLLM,
type ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { streamConverter, wrapLLMEvent } from "@llamaindex/core/utils";
import {
type BedrockAdditionalChatOptions,
type BedrockChatStreamResponse,
@@ -149,6 +150,18 @@ export type BedrockModelParams = {
maxTokens?: number;
};
export const BEDROCK_MODEL_MAX_TOKENS: Partial<Record<BEDROCK_MODELS, number>> =
{
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_SONNET]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_OPUS]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET]: 4096,
[BEDROCK_MODELS.META_LLAMA2_13B_CHAT]: 2048,
[BEDROCK_MODELS.META_LLAMA2_70B_CHAT]: 2048,
[BEDROCK_MODELS.META_LLAMA3_8B_INSTRUCT]: 2048,
[BEDROCK_MODELS.META_LLAMA3_70B_INSTRUCT]: 2048,
};
const DEFAULT_BEDROCK_PARAMS = {
temperature: 0.1,
topP: 1,
@@ -8,9 +8,9 @@ import {
type ChatMessage,
type ChatResponseChunk,
type LLMMetadata,
streamConverter,
type ToolCallLLMMessageOptions,
} from "llamaindex";
} from "@llamaindex/core/llms";
import { streamConverter } from "@llamaindex/core/utils";
import type { ToolChoice } from "./types";
import { toUtf8 } from "./utils";
@@ -10,7 +10,7 @@ import type {
PartialToolCall,
ToolCall,
ToolCallLLMMessageOptions,
} from "llamaindex";
} from "@llamaindex/core/llms";
import {
type BedrockAdditionalChatOptions,
type BedrockChatStreamResponse,
@@ -2,7 +2,7 @@ import type {
InvokeModelCommandInput,
InvokeModelWithResponseStreamCommandInput,
} from "@aws-sdk/client-bedrock-runtime";
import type { ChatMessage, LLMMetadata } from "llamaindex";
import type { ChatMessage, LLMMetadata } from "@llamaindex/core/llms";
import type { MetaNoneStreamingResponse, MetaStreamEvent } from "../types";
import {
mapChatMessagesToMetaLlama2Messages,
+4 -4
View File
@@ -1,13 +1,13 @@
import type { JSONObject } from "@llamaindex/core/global";
import type {
BaseTool,
ChatMessage,
JSONObject,
MessageContent,
MessageContentDetail,
MessageContentTextDetail,
ToolCallLLMMessageOptions,
ToolMetadata,
} from "llamaindex";
} from "@llamaindex/core/llms";
import type {
AnthropicContent,
AnthropicImageContent,
@@ -154,10 +154,10 @@ export const mapChatMessagesToMetaMessages = <T extends ChatMessage>(
messages: T[],
): MetaMessage[] => {
return messages.map((msg) => {
let content: string;
let content: string = "";
if (typeof msg.content === "string") {
content = msg.content;
} else {
} else if (msg.content.length) {
content = (msg.content[0] as MessageContentTextDetail).text;
}
return {
-9
View File
@@ -1,9 +0,0 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/script/type",
"tsBuildInfoFile": "./dist/script/.tsbuildinfo",
"emitDeclarationOnly": true
},
"include": ["./tsup.config.ts"]
}
-9
View File
@@ -1,9 +0,0 @@
import { defineConfig } from "tsup";
export default defineConfig([
{
entry: ["src/index.ts", "src/llm/bedrock/base.ts"],
format: ["cjs", "esm"],
sourcemap: true,
},
]);
+27
View File
@@ -1,5 +1,32 @@
# @llamaindex/core
## 0.1.2
### Patch Changes
- b974eea: Add support for Metadata filters
## 0.1.1
### Patch Changes
- b3681bf: fix: DataCloneError when using FunctionTool
## 0.1.0
### Minor Changes
- 16ef5dd: refactor: simplify callback manager
Change `event.detail.payload` to `event.detail`
### Patch Changes
- 16ef5dd: refactor: move callback manager & llm to core module
For people who import `llamaindex/llms/base` or `llamaindex/llms/utils`,
use `@llamaindex/core/llms` and `@llamaindex/core/utils` instead.
## 0.0.3
### Patch Changes
+30 -2
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/core",
"type": "module",
"version": "0.0.3",
"version": "0.1.2",
"description": "LlamaIndex Core Module",
"exports": {
"./llms": {
@@ -32,6 +32,20 @@
"default": "./dist/decorator/index.js"
}
},
"./embeddings": {
"require": {
"types": "./dist/embeddings/index.d.cts",
"default": "./dist/embeddings/index.cjs"
},
"import": {
"types": "./dist/embeddings/index.d.ts",
"default": "./dist/embeddings/index.js"
},
"default": {
"types": "./dist/embeddings/index.d.ts",
"default": "./dist/embeddings/index.js"
}
},
"./global": {
"require": {
"types": "./dist/global/index.d.cts",
@@ -59,6 +73,20 @@
"types": "./dist/schema/index.d.ts",
"default": "./dist/schema/index.js"
}
},
"./utils": {
"require": {
"types": "./dist/utils/index.d.cts",
"default": "./dist/utils/index.cjs"
},
"import": {
"types": "./dist/utils/index.d.ts",
"default": "./dist/utils/index.js"
},
"default": {
"types": "./dist/utils/index.d.ts",
"default": "./dist/utils/index.js"
}
}
},
"files": [
@@ -75,7 +103,7 @@
},
"devDependencies": {
"ajv": "^8.16.0",
"bunchee": "^5.2.1"
"bunchee": "5.3.0-beta.0"
},
"dependencies": {
"@llamaindex/env": "workspace:*",
@@ -1,9 +1,8 @@
import type { MessageContentDetail } from "@llamaindex/core/llms";
import type { BaseNode } from "@llamaindex/core/schema";
import { MetadataMode } from "@llamaindex/core/schema";
import { type Tokenizers } from "@llamaindex/env";
import type { TransformComponent } from "../ingestion/types.js";
import { extractSingleText } from "../llm/utils.js";
import type { MessageContentDetail } from "../llms";
import type { TransformComponent } from "../schema";
import { BaseNode, MetadataMode } from "../schema";
import { extractSingleText } from "../utils";
import { truncateMaxTokens } from "./tokenizer.js";
import { SimilarityType, similarity } from "./utils.js";
@@ -17,7 +16,13 @@ export type EmbeddingInfo = {
tokenizer?: Tokenizers;
};
export abstract class BaseEmbedding implements TransformComponent {
export type BaseEmbeddingOptions = {
logProgress?: boolean;
};
export abstract class BaseEmbedding
implements TransformComponent<BaseEmbeddingOptions>
{
embedBatchSize = DEFAULT_EMBED_BATCH_SIZE;
embedInfo?: EmbeddingInfo;
@@ -45,7 +50,7 @@ export abstract class BaseEmbedding implements TransformComponent {
* Optionally override this method to retrieve multiple embeddings in a single request
* @param texts
*/
async getTextEmbeddings(texts: string[]): Promise<Array<number[]>> {
getTextEmbeddings = async (texts: string[]): Promise<Array<number[]>> => {
const embeddings: number[][] = [];
for (const text of texts) {
@@ -54,7 +59,7 @@ export abstract class BaseEmbedding implements TransformComponent {
}
return embeddings;
}
};
/**
* Get embeddings for a batch of texts
@@ -63,22 +68,23 @@ export abstract class BaseEmbedding implements TransformComponent {
*/
async getTextEmbeddingsBatch(
texts: string[],
options?: {
logProgress?: boolean;
},
options?: BaseEmbeddingOptions,
): Promise<Array<number[]>> {
return await batchEmbeddings(
texts,
this.getTextEmbeddings.bind(this),
this.getTextEmbeddings,
this.embedBatchSize,
options,
);
}
async transform(nodes: BaseNode[], _options?: any): Promise<BaseNode[]> {
async transform(
nodes: BaseNode[],
options?: BaseEmbeddingOptions,
): Promise<BaseNode[]> {
const texts = nodes.map((node) => node.getContent(MetadataMode.EMBED));
const embeddings = await this.getTextEmbeddingsBatch(texts, _options);
const embeddings = await this.getTextEmbeddingsBatch(texts, options);
for (let i = 0; i < nodes.length; i++) {
nodes[i].embedding = embeddings[i];
@@ -104,9 +110,7 @@ export async function batchEmbeddings<T>(
values: T[],
embedFunc: EmbedFunc<T>,
chunkSize: number,
options?: {
logProgress?: boolean;
},
options?: BaseEmbeddingOptions,
): Promise<Array<number[]>> {
const resultEmbeddings: Array<number[]> = [];
+4
View File
@@ -0,0 +1,4 @@
export { BaseEmbedding, batchEmbeddings } from "./base";
export type { BaseEmbeddingOptions, EmbeddingInfo } from "./base";
export { truncateMaxTokens } from "./tokenizer";
export { DEFAULT_SIMILARITY_TOP_K, SimilarityType, similarity } from "./utils";
+64
View File
@@ -0,0 +1,64 @@
export const DEFAULT_SIMILARITY_TOP_K = 2;
/**
* Similarity type
* Default is cosine similarity. Dot product and negative Euclidean distance are also supported.
*/
export enum SimilarityType {
DEFAULT = "cosine",
DOT_PRODUCT = "dot_product",
EUCLIDEAN = "euclidean",
}
/**
* The similarity between two embeddings.
* @param embedding1
* @param embedding2
* @param mode
* @returns similarity score with higher numbers meaning the two embeddings are more similar
*/
export function similarity(
embedding1: number[],
embedding2: number[],
mode: SimilarityType = SimilarityType.DEFAULT,
): number {
if (embedding1.length !== embedding2.length) {
throw new Error("Embedding length mismatch");
}
// NOTE I've taken enough Kahan to know that we should probably leave the
// numeric programming to numeric programmers. The naive approach here
// will probably cause some avoidable loss of floating point precision
// ml-distance is worth watching although they currently also use the naive
// formulas
function norm(x: number[]): number {
let result = 0;
for (let i = 0; i < x.length; i++) {
result += x[i] * x[i];
}
return Math.sqrt(result);
}
switch (mode) {
case SimilarityType.EUCLIDEAN: {
const difference = embedding1.map((x, i) => x - embedding2[i]);
return -norm(difference);
}
case SimilarityType.DOT_PRODUCT: {
let result = 0;
for (let i = 0; i < embedding1.length; i++) {
result += embedding1[i] * embedding2[i];
}
return result;
}
case SimilarityType.DEFAULT: {
return (
similarity(embedding1, embedding2, SimilarityType.DOT_PRODUCT) /
(norm(embedding1) * norm(embedding2))
);
}
default:
throw new Error("Not implemented yet");
}
}
+10
View File
@@ -1 +1,11 @@
export { Settings } from "./settings";
export { CallbackManager } from "./settings/callback-manager";
export type {
LLMEndEvent,
LLMStartEvent,
LLMStreamEvent,
LLMToolCallEvent,
LLMToolResultEvent,
LlamaIndexEventMaps,
} from "./settings/callback-manager";
export type { JSONArray, JSONObject, JSONValue } from "./type";
+21
View File
@@ -1,3 +1,9 @@
import {
type CallbackManager,
getCallbackManager,
setCallbackManager,
withCallbackManager,
} from "./settings/callback-manager";
import {
getChunkSize,
setChunkSize,
@@ -14,4 +20,19 @@ export const Settings = {
withChunkSize<Result>(chunkSize: number, fn: () => Result): Result {
return withChunkSize(chunkSize, fn);
},
get callbackManager(): CallbackManager {
return getCallbackManager();
},
set callbackManager(callbackManager: CallbackManager) {
setCallbackManager(callbackManager);
},
withCallbackManager<Result>(
callbackManager: CallbackManager,
fn: () => Result,
): Result {
return withCallbackManager(callbackManager, fn);
},
};
@@ -0,0 +1,138 @@
import { AsyncLocalStorage, CustomEvent } from "@llamaindex/env";
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
ToolCall,
ToolOutput,
} from "../../llms";
import { EventCaller, getEventCaller } from "../../utils/event-caller";
import type { UUID } from "../type";
export type LLMStartEvent = {
id: UUID;
messages: ChatMessage[];
};
export type LLMToolCallEvent = {
toolCall: ToolCall;
};
export type LLMToolResultEvent = {
toolCall: ToolCall;
toolResult: ToolOutput;
};
export type LLMEndEvent = {
id: UUID;
response: ChatResponse;
};
export type LLMStreamEvent = {
id: UUID;
chunk: ChatResponseChunk;
};
export interface LlamaIndexEventMaps {
"llm-start": LLMStartEvent;
"llm-end": LLMEndEvent;
"llm-tool-call": LLMToolCallEvent;
"llm-tool-result": LLMToolResultEvent;
"llm-stream": LLMStreamEvent;
}
export class LlamaIndexCustomEvent<T = any> extends CustomEvent<T> {
reason: EventCaller | null = null;
private constructor(
event: string,
options?: CustomEventInit & {
reason?: EventCaller | null;
},
) {
super(event, options);
this.reason = options?.reason ?? null;
}
static fromEvent<Type extends keyof LlamaIndexEventMaps>(
type: Type,
detail: LlamaIndexEventMaps[Type],
) {
return new LlamaIndexCustomEvent(type, {
detail: detail,
reason: getEventCaller(),
});
}
}
type EventHandler<Event> = (event: LlamaIndexCustomEvent<Event>) => void;
export class CallbackManager {
#handlers = new Map<keyof LlamaIndexEventMaps, EventHandler<any>[]>();
on<K extends keyof LlamaIndexEventMaps>(
event: K,
handler: EventHandler<LlamaIndexEventMaps[K]>,
) {
if (!this.#handlers.has(event)) {
this.#handlers.set(event, []);
}
this.#handlers.get(event)!.push(handler);
return this;
}
off<K extends keyof LlamaIndexEventMaps>(
event: K,
handler: EventHandler<LlamaIndexEventMaps[K]>,
) {
if (!this.#handlers.has(event)) {
return this;
}
const cbs = this.#handlers.get(event)!;
const index = cbs.indexOf(handler);
if (index > -1) {
cbs.splice(index, 1);
}
return this;
}
dispatchEvent<K extends keyof LlamaIndexEventMaps>(
event: K,
detail: LlamaIndexEventMaps[K],
) {
const cbs = this.#handlers.get(event);
if (!cbs) {
return;
}
queueMicrotask(() => {
cbs.forEach((handler) =>
handler(LlamaIndexCustomEvent.fromEvent(event, { ...detail })),
);
});
}
}
export const globalCallbackManager = new CallbackManager();
const callbackManagerAsyncLocalStorage =
new AsyncLocalStorage<CallbackManager>();
let currentCallbackManager: CallbackManager | null = null;
export function getCallbackManager(): CallbackManager {
return (
callbackManagerAsyncLocalStorage.getStore() ??
currentCallbackManager ??
globalCallbackManager
);
}
export function setCallbackManager(callbackManager: CallbackManager) {
currentCallbackManager = callbackManager;
}
export function withCallbackManager<Result>(
callbackManager: CallbackManager,
fn: () => Result,
): Result {
return callbackManagerAsyncLocalStorage.run(callbackManager, fn);
}
@@ -1,3 +1,5 @@
import { streamConverter } from "../utils";
import { extractText } from "../utils/llms";
import type {
ChatResponse,
ChatResponseChunk,
@@ -9,8 +11,7 @@ import type {
LLMCompletionParamsStreaming,
LLMMetadata,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { extractText, streamConverter } from "./utils.js";
} from "./type";
export abstract class BaseLLM<
AdditionalChatOptions extends object = object,
+1
View File
@@ -1,3 +1,4 @@
export { BaseLLM, ToolCallLLM } from "./base";
export type {
BaseTool,
BaseToolWithCall,
+1
View File
@@ -1,2 +1,3 @@
export * from "./node";
export type { TransformComponent } from "./type";
export * from "./zod";
+5
View File
@@ -0,0 +1,5 @@
import type { BaseNode } from "./node";
export interface TransformComponent<Options extends Record<string, unknown>> {
transform(nodes: BaseNode[], options?: Options): Promise<BaseNode[]>;
}
@@ -1,5 +1,14 @@
import { AsyncLocalStorage, randomUUID } from "@llamaindex/env";
import { isAsyncIterable, isIterable } from "../utils.js";
export const isAsyncIterable = (
obj: unknown,
): obj is AsyncIterable<unknown> => {
return obj != null && typeof obj === "object" && Symbol.asyncIterator in obj;
};
export const isIterable = (obj: unknown): obj is Iterable<unknown> => {
return obj != null && typeof obj === "object" && Symbol.iterator in obj;
};
const eventReasonAsyncLocalStorage = new AsyncLocalStorage<EventCaller>();
+54
View File
@@ -0,0 +1,54 @@
export { wrapEventCaller } from "./event-caller";
export async function* streamConverter<S, D>(
stream: AsyncIterable<S>,
converter: (s: S) => D | null,
): AsyncIterable<D> {
for await (const data of stream) {
const newData = converter(data);
if (newData === null) {
return;
}
yield newData;
}
}
export async function* streamCallbacks<S>(
stream: AsyncIterable<S>,
callbacks: {
finished?: (value?: S) => void;
},
): AsyncIterable<S> {
let value: S | undefined;
for await (value of stream) {
yield value;
}
if (callbacks.finished) {
callbacks.finished(value);
}
}
export async function* streamReducer<S, D>(params: {
stream: AsyncIterable<S>;
reducer: (previousValue: D, currentValue: S) => D;
initialValue: D;
finished?: (value: D) => void;
}): AsyncIterable<S> {
let value = params.initialValue;
for await (const data of params.stream) {
value = params.reducer(value, data);
yield data;
}
if (params.finished) {
params.finished(value);
}
}
export { wrapLLMEvent } from "./wrap-llm-event";
export {
extractDataUrlComponents,
extractImage,
extractSingleText,
extractText,
} from "./llms";
+79
View File
@@ -0,0 +1,79 @@
import type {
MessageContent,
MessageContentDetail,
MessageContentTextDetail,
} from "../llms";
import type { ImageType } from "../schema";
/**
* Extracts just the text from a multi-modal message or the message itself if it's just text.
*
* @param message The message to extract text from.
* @returns The extracted text
*/
export function extractText(message: MessageContent): string {
if (typeof message !== "string" && !Array.isArray(message)) {
console.warn(
"extractText called with non-MessageContent message, this is likely a bug.",
);
return `${message}`;
} else if (typeof message !== "string" && Array.isArray(message)) {
// message is of type MessageContentDetail[] - retrieve just the text parts and concatenate them
// so we can pass them to the context generator
return message
.filter((c): c is MessageContentTextDetail => c.type === "text")
.map((c) => c.text)
.join("\n\n");
} else {
return message;
}
}
/**
* Extracts a single text from a multi-modal message content
*
* @param message The message to extract images from.
* @returns The extracted images
*/
export function extractSingleText(
message: MessageContentDetail,
): string | null {
if (message.type === "text") {
return message.text;
}
return null;
}
/**
* Extracts an image from a multi-modal message content
*
* @param message The message to extract images from.
* @returns The extracted images
*/
export function extractImage(message: MessageContentDetail): ImageType | null {
if (message.type === "image_url") {
return new URL(message.image_url.url);
}
return null;
}
export const extractDataUrlComponents = (
dataUrl: string,
): {
mimeType: string;
base64: string;
} => {
const parts = dataUrl.split(";base64,");
if (parts.length !== 2 || !parts[0].startsWith("data:")) {
throw new Error("Invalid data URL");
}
const mimeType = parts[0].slice(5);
const base64 = parts[1];
return {
mimeType,
base64,
};
};
+80
View File
@@ -0,0 +1,80 @@
import { AsyncLocalStorage, randomUUID } from "@llamaindex/env";
import { getCallbackManager } from "../global/settings/callback-manager";
import type { ChatResponse, ChatResponseChunk, LLM, LLMChat } from "../llms";
export function wrapLLMEvent<
AdditionalChatOptions extends object = object,
AdditionalMessageOptions extends object = object,
>(
originalMethod: LLMChat<
AdditionalChatOptions,
AdditionalMessageOptions
>["chat"],
_context: ClassMethodDecoratorContext,
) {
return async function withLLMEvent(
this: LLM<AdditionalChatOptions, AdditionalMessageOptions>,
...params: Parameters<
LLMChat<AdditionalChatOptions, AdditionalMessageOptions>["chat"]
>
): ReturnType<
LLMChat<AdditionalChatOptions, AdditionalMessageOptions>["chat"]
> {
const id = randomUUID();
getCallbackManager().dispatchEvent("llm-start", {
id,
messages: params[0].messages,
});
const response = await originalMethod.call(this, ...params);
if (Symbol.asyncIterator in response) {
// save snapshot to restore it after the response is done
const snapshot = AsyncLocalStorage.snapshot();
const originalAsyncIterator = {
[Symbol.asyncIterator]: response[Symbol.asyncIterator].bind(response),
};
response[Symbol.asyncIterator] = async function* () {
const finalResponse = {
raw: [] as ChatResponseChunk[],
message: {
content: "",
role: "assistant",
options: {},
},
} satisfies ChatResponse;
let firstOne = false;
for await (const chunk of originalAsyncIterator) {
if (!firstOne) {
firstOne = true;
finalResponse.message.content = chunk.delta;
} else {
finalResponse.message.content += chunk.delta;
}
if (chunk.options) {
finalResponse.message.options = {
...finalResponse.message.options,
...chunk.options,
};
}
getCallbackManager().dispatchEvent("llm-stream", {
id,
chunk,
});
finalResponse.raw.push(chunk);
yield chunk;
}
snapshot(() => {
getCallbackManager().dispatchEvent("llm-end", {
id,
response: finalResponse,
});
});
};
} else {
getCallbackManager().dispatchEvent("llm-end", {
id,
response,
});
}
return response;
};
}
@@ -1,6 +1,6 @@
import { truncateMaxTokens } from "@llamaindex/core/embeddings";
import { Tokenizers, tokenizers } from "@llamaindex/env";
import { describe, expect, test } from "vitest";
import { truncateMaxTokens } from "../../src/embeddings/tokenizer.js";
describe("truncateMaxTokens", () => {
const tokenizer = tokenizers.tokenizer(Tokenizers.CL100K_BASE);
+89
View File
@@ -0,0 +1,89 @@
import { CallbackManager, Settings } from "@llamaindex/core/global";
import { beforeEach, describe, expect, expectTypeOf, test, vi } from "vitest";
declare module "@llamaindex/core/global" {
interface LlamaIndexEventMaps {
test: {
value: number;
};
functionTest: {
fn: ({ x }: { x: number }) => string;
};
}
}
describe("event system", () => {
beforeEach(() => {
Settings.callbackManager = new CallbackManager();
});
test("type system", () => {
Settings.callbackManager.on("test", (event) => {
const data = event.detail;
expectTypeOf(data).not.toBeAny();
expectTypeOf(data).toEqualTypeOf<{
value: number;
}>();
});
});
test("dispatch event", async () => {
let callback;
Settings.callbackManager.on(
"test",
(callback = vi.fn((event) => {
const data = event.detail;
expect(data.value).toBe(42);
})),
);
Settings.callbackManager.dispatchEvent("test", {
value: 42,
});
expect(callback).toHaveBeenCalledTimes(0);
await new Promise((resolve) => process.nextTick(resolve));
expect(callback).toHaveBeenCalledTimes(1);
});
test("dispatch function tool event", async () => {
const testFunction = ({ x }: { x: number }) => `${x * 2}`;
let callback;
Settings.callbackManager.on(
"functionTest",
(callback = vi.fn((event) => {
const data = event.detail;
expect(data.fn).toBe(testFunction);
})),
);
Settings.callbackManager.dispatchEvent("functionTest", {
fn: testFunction,
});
expect(callback).toHaveBeenCalledTimes(0);
await new Promise((resolve) => process.nextTick(resolve));
expect(callback).toHaveBeenCalledTimes(1);
});
// rollup doesn't support decorators for now
// test('wrap event caller', async () => {
// class A {
// @wrapEventCaller
// fn() {
// Settings.callbackManager.dispatchEvent('test', {
// value: 42
// });
// }
// }
// const a = new A();
// let callback;
// Settings.callbackManager.on('test', callback = vi.fn((event) => {
// const data = event.detail;
// expect(event.reason!.caller).toBe(a);
// expect(data.value).toBe(42);
// }));
// a.fn();
// expect(callback).toHaveBeenCalledTimes(0);
// await new Promise((resolve) => process.nextTick(resolve));
// expect(callback).toHaveBeenCalledTimes(1);
// })
});
+1 -1
View File
@@ -7,6 +7,6 @@
},
"devDependencies": {
"@llamaindex/core": "workspace:*",
"vitest": "^1.6.0"
"vitest": "^2.0.2"
}
}
+1
View File
@@ -8,6 +8,7 @@
"moduleResolution": "Bundler",
"skipLibCheck": true,
"strict": true,
"lib": ["ESNext", "DOM"],
"types": ["node"]
},
"include": ["./src"],
+2 -2
View File
@@ -68,11 +68,11 @@
},
"devDependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@swc/cli": "^0.3.12",
"@swc/cli": "^0.4.0",
"@swc/core": "^1.6.3",
"concurrently": "^8.2.2",
"pathe": "^1.1.2",
"vitest": "^1.6.0"
"vitest": "^2.0.2"
},
"dependencies": {
"@types/lodash": "^4.17.5",
+45
View File
@@ -1,5 +1,50 @@
# @llamaindex/experimental
## 0.0.55
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.0.54
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.0.53
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.0.52
### Patch Changes
- llamaindex@0.5.2
## 0.0.51
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.0.50
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 0.0.49
### Patch Changes
+2 -2
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.49",
"version": "0.0.55",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
@@ -56,7 +56,7 @@
},
"devDependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@swc/cli": "^0.3.12",
"@swc/cli": "^0.4.0",
"@swc/core": "^1.6.3",
"@types/jsonpath": "^0.2.4",
"concurrently": "^8.2.2",
+60 -1
View File
@@ -1,5 +1,64 @@
# llamaindex
## 0.5.5
### Patch Changes
- b974eea: Add support for Metadata filters
- Updated dependencies [b974eea]
- @llamaindex/core@0.1.2
## 0.5.4
### Patch Changes
- 1a65ead: feat: add vendorMultimodal params to LlamaParseReader
## 0.5.3
### Patch Changes
- 9bbbc67: feat: add a reader for Discord messages
- b3681bf: fix: DataCloneError when using FunctionTool
- Updated dependencies [b3681bf]
- @llamaindex/core@0.1.1
## 0.5.2
### Patch Changes
- Updated dependencies [3ed6acc]
- @llamaindex/cloud@0.2.0
## 0.5.1
### Patch Changes
- 2774681: Add mixedbread's embeddings and reranking API
- a0f424e: corrected the regex in the react.ts file in extractToolUse & extractJsonStr functions, as mentioned in https://github.com/run-llama/LlamaIndexTS/issues/1019
## 0.5.0
### Minor Changes
- 16ef5dd: refactor: simplify callback manager
Change `event.detail.payload` to `event.detail`
### Patch Changes
- 16ef5dd: refactor: move callback manager & llm to core module
For people who import `llamaindex/llms/base` or `llamaindex/llms/utils`,
use `@llamaindex/core/llms` and `@llamaindex/core/utils` instead.
- 36ddec4: fix: typo in custom page separator parameter for LlamaParse
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- @llamaindex/core@0.1.0
- @llamaindex/cloud@0.1.4
## 0.4.14
### Patch Changes
@@ -125,7 +184,7 @@
### Patch Changes
- 6bc5bdd: feat: add cache disabling, fast mode, do not unroll columns mode and custom page seperator to LlamaParseReader
- 6bc5bdd: feat: add cache disabling, fast mode, do not unroll columns mode and custom page separator to LlamaParseReader
- bf25ff6: fix: polyfill for cloudflare worker
- e6d6576: chore: use `unpdf`
@@ -1,5 +1,50 @@
# @llamaindex/cloudflare-worker-agent-test
## 0.0.39
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.0.38
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.0.37
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.0.36
### Patch Changes
- llamaindex@0.5.2
## 0.0.35
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.0.34
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 0.0.33
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloudflare-worker-agent-test",
"version": "0.0.33",
"version": "0.0.39",
"type": "module",
"private": true,
"scripts": {
@@ -12,13 +12,13 @@
"cf-typegen": "wrangler types"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.4.3",
"@cloudflare/workers-types": "^4.20240605.0",
"@vitest/runner": "1.3.0",
"@vitest/snapshot": "1.3.0",
"typescript": "^5.5.2",
"vitest": "1.3.0",
"wrangler": "^3.60.1"
"@cloudflare/vitest-pool-workers": "^0.4.10",
"@cloudflare/workers-types": "^4.20240620.0",
"@vitest/runner": "1.5.3",
"@vitest/snapshot": "1.5.3",
"typescript": "^5.5.3",
"vitest": "1.5.3",
"wrangler": "^3.63.2"
},
"dependencies": {
"llamaindex": "workspace:*"
@@ -1,5 +1,50 @@
# @llamaindex/next-agent-test
## 0.1.39
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.1.38
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.1.37
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.1.36
### Patch Changes
- llamaindex@0.5.2
## 0.1.35
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.1.34
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 0.1.33
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-agent-test",
"version": "0.1.33",
"version": "0.1.39",
"private": true,
"scripts": {
"dev": "next dev",
@@ -11,7 +11,7 @@
"dependencies": {
"ai": "^3.2.1",
"llamaindex": "workspace:*",
"next": "14.2.4",
"next": "14.2.5",
"react": "18.3.1",
"react-dom": "18.3.1"
},
@@ -20,9 +20,9 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"eslint": "^8.57.0",
"eslint-config-next": "14.2.3",
"eslint-config-next": "14.2.5",
"postcss": "^8",
"tailwindcss": "^3.4.4",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
}
}
@@ -1,5 +1,50 @@
# test-edge-runtime
## 0.1.38
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.1.37
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.1.36
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.1.35
### Patch Changes
- llamaindex@0.5.2
## 0.1.34
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.1.33
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 0.1.32
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/nextjs-edge-runtime-test",
"version": "0.1.32",
"version": "0.1.38",
"private": true,
"scripts": {
"dev": "next dev",
@@ -9,7 +9,7 @@
},
"dependencies": {
"llamaindex": "workspace:*",
"next": "14.2.4",
"next": "14.2.5",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
@@ -17,6 +17,6 @@
"@types/node": "^20.12.11",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
}
}
@@ -1,5 +1,50 @@
# @llamaindex/next-node-runtime
## 0.0.20
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.0.19
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.0.18
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.0.17
### Patch Changes
- llamaindex@0.5.2
## 0.0.16
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.0.15
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 0.0.14
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-node-runtime-test",
"version": "0.0.14",
"version": "0.0.20",
"private": true,
"scripts": {
"dev": "next dev",
@@ -10,7 +10,7 @@
},
"dependencies": {
"llamaindex": "workspace:*",
"next": "14.2.4",
"next": "14.2.5",
"react": "18.3.1",
"react-dom": "18.3.1"
},
@@ -19,9 +19,9 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"eslint": "^8.57.0",
"eslint-config-next": "14.2.3",
"eslint-config-next": "14.2.5",
"postcss": "^8",
"tailwindcss": "^3.4.4",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
}
}
@@ -19,10 +19,10 @@ Settings.embedModel = new HuggingFaceEmbedding({
quantized: false,
});
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail.payload);
console.log(event.detail);
});
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail.payload);
console.log(event.detail);
});
export async function getOpenAIModelRequest(query: string) {
@@ -1,5 +1,50 @@
# @llamaindex/waku-query-engine-test
## 0.0.39
### Patch Changes
- Updated dependencies [b974eea]
- llamaindex@0.5.5
## 0.0.38
### Patch Changes
- Updated dependencies [1a65ead]
- llamaindex@0.5.4
## 0.0.37
### Patch Changes
- Updated dependencies [9bbbc67]
- Updated dependencies [b3681bf]
- llamaindex@0.5.3
## 0.0.36
### Patch Changes
- llamaindex@0.5.2
## 0.0.35
### Patch Changes
- Updated dependencies [2774681]
- Updated dependencies [a0f424e]
- llamaindex@0.5.1
## 0.0.34
### Patch Changes
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- Updated dependencies [36ddec4]
- llamaindex@0.5.0
## 0.0.33
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/waku-query-engine-test",
"version": "0.0.33",
"version": "0.0.39",
"type": "module",
"private": true,
"scripts": {
@@ -10,16 +10,16 @@
},
"dependencies": {
"llamaindex": "workspace:*",
"react": "19.0.0-canary-e3ebcd54b-20240405",
"react-dom": "19.0.0-canary-e3ebcd54b-20240405",
"react-server-dom-webpack": "19.0.0-canary-e3ebcd54b-20240405",
"waku": "0.20.1"
"react": "19.0.0-beta-e7d213dfb0-20240507",
"react-dom": "19.0.0-beta-e7d213dfb0-20240507",
"react-server-dom-webpack": "19.0.0-beta-e7d213dfb0-20240507",
"waku": "0.20.2"
},
"devDependencies": {
"@types/react": "18.3.1",
"@types/react": "18.3.3",
"@types/react-dom": "18.3.0",
"autoprefixer": "10.4.19",
"tailwindcss": "3.4.3",
"typescript": "5.4.5"
"tailwindcss": "3.4.4",
"typescript": "5.5.3"
}
}
@@ -1,3 +1,4 @@
import { extractText } from "@llamaindex/core/utils";
import type {
ChatResponse,
ChatResponseChunk,
@@ -8,7 +9,6 @@ import type {
LLMCompletionParamsNonStreaming,
LLMCompletionParamsStreaming,
} from "llamaindex";
import { extractText } from "llamaindex/llm/utils";
import { deepStrictEqual, strictEqual } from "node:assert";
import { llmCompleteMockStorage } from "../../node/utils.js";
+1 -1
View File
@@ -1,7 +1,7 @@
import { extractText } from "@llamaindex/core/utils";
import { consola } from "consola";
import { Anthropic, FunctionTool, Settings, type LLM } from "llamaindex";
import { AnthropicAgent } from "llamaindex/agent/anthropic";
import { extractText } from "llamaindex/llm/utils";
import { ok } from "node:assert";
import { beforeEach, test } from "node:test";
import { getWeatherTool, sumNumbersTool } from "./fixtures/tools.js";
+1 -1
View File
@@ -1,3 +1,4 @@
import { extractText } from "@llamaindex/core/utils";
import { consola } from "consola";
import {
Document,
@@ -14,7 +15,6 @@ import {
VectorStoreIndex,
type LLM,
} from "llamaindex";
import { extractText } from "llamaindex/llm/utils";
import { ok, strictEqual } from "node:assert";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
+1 -1
View File
@@ -1,5 +1,5 @@
import { extractText } from "@llamaindex/core/utils";
import { OpenAI, ReActAgent, Settings, type LLM } from "llamaindex";
import { extractText } from "llamaindex/llm/utils";
import { ok } from "node:assert";
import { beforeEach, test } from "node:test";
import { getWeatherTool } from "./fixtures/tools.js";
+14 -13
View File
@@ -4,16 +4,17 @@ import {
type LLMEndEvent,
type LLMStartEvent,
type LLMStreamEvent,
} from "llamaindex";
} from "@llamaindex/core/global";
import { CustomEvent } from "@llamaindex/env";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type test } from "node:test";
import { fileURLToPath } from "node:url";
type MockStorage = {
llmEventStart: LLMStartEvent["detail"]["payload"][];
llmEventEnd: LLMEndEvent["detail"]["payload"][];
llmEventStream: LLMStreamEvent["detail"]["payload"][];
llmEventStart: LLMStartEvent[];
llmEventEnd: LLMEndEvent[];
llmEventStream: LLMStreamEvent[];
};
export const llmCompleteMockStorage: MockStorage = {
@@ -36,35 +37,35 @@ export async function mockLLMEvent(
llmEventStream: [],
};
function captureLLMStart(event: LLMStartEvent) {
idMap.set(event.detail.payload.id, `PRESERVE_${counter++}`);
function captureLLMStart(event: CustomEvent<LLMStartEvent>) {
idMap.set(event.detail.id, `PRESERVE_${counter++}`);
newLLMCompleteMockStorage.llmEventStart.push({
...event.detail.payload,
...event.detail,
// @ts-expect-error id is not UUID, but it is fine for testing
id: idMap.get(event.detail.payload.id)!,
});
}
function captureLLMEnd(event: LLMEndEvent) {
function captureLLMEnd(event: CustomEvent<LLMEndEvent>) {
newLLMCompleteMockStorage.llmEventEnd.push({
...event.detail.payload,
...event.detail,
// @ts-expect-error id is not UUID, but it is fine for testing
id: idMap.get(event.detail.payload.id)!,
response: {
...event.detail.payload.response,
...event.detail.response,
// hide raw object since it might too big
raw: null,
},
});
}
function captureLLMStream(event: LLMStreamEvent) {
function captureLLMStream(event: CustomEvent<LLMStreamEvent>) {
newLLMCompleteMockStorage.llmEventStream.push({
...event.detail.payload,
...event.detail,
// @ts-expect-error id is not UUID, but it is fine for testing
id: idMap.get(event.detail.payload.id)!,
chunk: {
...event.detail.payload.chunk,
...event.detail.chunk,
// hide raw object since it might too big
raw: null,
},
+1
View File
@@ -10,6 +10,7 @@
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@llamaindex/core": "workspace:*",
"@types/node": "^20.12.11",
"consola": "^3.2.3",
"llamaindex": "workspace:*",
+18 -15
View File
@@ -1,6 +1,6 @@
{
"name": "llamaindex",
"version": "0.4.14",
"version": "0.5.5",
"license": "MIT",
"type": "module",
"keywords": [
@@ -20,18 +20,20 @@
"llamaindex"
],
"dependencies": {
"@anthropic-ai/sdk": "^0.21.1",
"@anthropic-ai/sdk": "0.21.1",
"@aws-crypto/sha256-js": "^5.2.0",
"@azure/identity": "^4.2.1",
"@datastax/astra-db-ts": "^1.2.1",
"@discordjs/rest": "^2.3.0",
"@google-cloud/vertexai": "^1.2.0",
"@google/generative-ai": "^0.12.0",
"@grpc/grpc-js": "^1.10.8",
"@google/generative-ai": "0.12.0",
"@grpc/grpc-js": "^1.10.11",
"@huggingface/inference": "^2.7.0",
"@llamaindex/cloud": "workspace:*",
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*",
"@mistralai/mistralai": "^0.4.0",
"@mistralai/mistralai": "^0.5.0",
"@mixedbread-ai/sdk": "^2.2.11",
"@pinecone-database/pinecone": "^2.2.2",
"@qdrant/js-client-rest": "^1.9.0",
"@types/lodash": "^4.17.4",
@@ -39,11 +41,12 @@
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.6",
"@xenova/transformers": "^2.17.2",
"@zilliz/milvus2-sdk-node": "^2.4.2",
"@zilliz/milvus2-sdk-node": "^2.4.4",
"ajv": "^8.16.0",
"assemblyai": "^4.4.5",
"assemblyai": "^4.6.0",
"chromadb": "1.8.1",
"cohere-ai": "7.9.5",
"cohere-ai": "7.10.6",
"discord-api-types": "^0.37.92",
"groq-sdk": "^0.5.0",
"js-tiktoken": "^1.0.12",
"lodash": "^4.17.21",
@@ -52,16 +55,16 @@
"md-utils-ts": "^2.0.0",
"mongodb": "^6.7.0",
"notion-md-crawler": "^1.0.0",
"openai": "^4.52.0",
"openai": "^4.52.5",
"papaparse": "^5.4.1",
"pathe": "^1.1.2",
"pg": "^8.12.0",
"pgvector": "^0.1.8",
"portkey-ai": "^0.1.16",
"pgvector": "^0.2.0",
"portkey-ai": "0.1.16",
"rake-modified": "^1.0.8",
"string-strip-html": "^13.4.8",
"tiktoken": "^1.0.15",
"unpdf": "^0.10.1",
"unpdf": "^0.11.0",
"wikipedia": "^2.1.2",
"wink-nlp": "^2.3.0",
"zod": "^3.23.8"
@@ -76,11 +79,11 @@
},
"devDependencies": {
"@notionhq/client": "^2.2.15",
"@swc/cli": "^0.3.12",
"@swc/cli": "^0.4.0",
"@swc/core": "^1.6.3",
"concurrently": "^8.2.2",
"glob": "^10.4.2",
"typescript": "^5.5.2"
"glob": "^11.0.0",
"typescript": "^5.5.3"
},
"engines": {
"node": ">=18.0.0"
+1 -1
View File
@@ -1,9 +1,9 @@
import type { ChatMessage, LLM, MessageType } from "@llamaindex/core/llms";
import { extractText } from "@llamaindex/core/utils";
import { tokenizers, type Tokenizer } from "@llamaindex/env";
import type { SummaryPrompt } from "./Prompt.js";
import { defaultSummaryPrompt, messagesToHistoryStr } from "./Prompt.js";
import { OpenAI } from "./llm/openai.js";
import { extractText } from "./llm/utils.js";
/**
* A ChatHistory is used to keep the state of back and forth chat messages
+1 -1
View File
@@ -4,7 +4,7 @@ import type {
ChatResponseChunk,
} from "@llamaindex/core/llms";
import type { NodeWithScore } from "@llamaindex/core/schema";
import { extractText } from "./llm/utils.js";
import { extractText } from "@llamaindex/core/utils";
export class EngineResponse implements ChatResponse, ChatResponseChunk {
sourceNodes?: NodeWithScore[];
+1 -1
View File
@@ -1,7 +1,7 @@
import type { BaseEmbedding } from "@llamaindex/core/embeddings";
import type { LLM } from "@llamaindex/core/llms";
import { PromptHelper } from "./PromptHelper.js";
import { OpenAIEmbedding } from "./embeddings/OpenAIEmbedding.js";
import type { BaseEmbedding } from "./embeddings/types.js";
import { OpenAI } from "./llm/openai.js";
import { SimpleNodeParser } from "./nodeParsers/SimpleNodeParser.js";
import type { NodeParser } from "./nodeParsers/types.js";
+8 -11
View File
@@ -1,19 +1,16 @@
import { Settings as CoreSettings } from "@llamaindex/core/global";
import { CallbackManager } from "./callbacks/CallbackManager.js";
import {
type CallbackManager,
Settings as CoreSettings,
} from "@llamaindex/core/global";
import { OpenAI } from "./llm/openai.js";
import { PromptHelper } from "./PromptHelper.js";
import { SimpleNodeParser } from "./nodeParsers/SimpleNodeParser.js";
import type { BaseEmbedding } from "@llamaindex/core/embeddings";
import type { LLM } from "@llamaindex/core/llms";
import { AsyncLocalStorage, getEnv } from "@llamaindex/env";
import type { ServiceContext } from "./ServiceContext.js";
import type { BaseEmbedding } from "./embeddings/types.js";
import {
getCallbackManager,
setCallbackManager,
withCallbackManager,
} from "./internal/settings/CallbackManager.js";
import {
getEmbeddedModel,
setEmbeddedModel,
@@ -129,18 +126,18 @@ class GlobalSettings implements Config {
}
get callbackManager(): CallbackManager {
return getCallbackManager();
return CoreSettings.callbackManager;
}
set callbackManager(callbackManager: CallbackManager) {
setCallbackManager(callbackManager);
CoreSettings.callbackManager = callbackManager;
}
withCallbackManager<Result>(
callbackManager: CallbackManager,
fn: () => Result,
): Result {
return withCallbackManager(callbackManager, fn);
return CoreSettings.withCallbackManager(callbackManager, fn);
}
set chunkSize(chunkSize: number | undefined) {
+5 -10
View File
@@ -5,6 +5,7 @@ import type {
MessageContent,
ToolOutput,
} from "@llamaindex/core/llms";
import { wrapEventCaller } from "@llamaindex/core/utils";
import { ReadableStream, TransformStream, randomUUID } from "@llamaindex/env";
import { ChatHistory } from "../ChatHistory.js";
import { EngineResponse } from "../EngineResponse.js";
@@ -14,9 +15,7 @@ import {
type ChatEngineParamsNonStreaming,
type ChatEngineParamsStreaming,
} from "../engines/chat/index.js";
import { wrapEventCaller } from "../internal/context/EventCaller.js";
import { consoleLogger, emptyLogger } from "../internal/logger.js";
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
import { isAsyncIterable } from "../internal/utils.js";
import { ObjectRetriever } from "../objects/index.js";
import type {
@@ -69,10 +68,8 @@ export function createTaskOutputStream<
taskOutputs.push(output);
controller.enqueue(output);
};
getCallbackManager().dispatchEvent("agent-start", {
payload: {
startStep: step,
},
Settings.callbackManager.dispatchEvent("agent-start", {
startStep: step,
});
context.logger.log("Starting step(id, %s).", step.id);
@@ -93,10 +90,8 @@ export function createTaskOutputStream<
"Final step(id, %s) reached, closing task.",
step.id,
);
getCallbackManager().dispatchEvent("agent-end", {
payload: {
endStep: step,
},
Settings.callbackManager.dispatchEvent("agent-end", {
endStep: step,
});
controller.close();
}
+4 -4
View File
@@ -1,3 +1,4 @@
import type { JSONObject, JSONValue } from "@llamaindex/core/global";
import type {
BaseTool,
ChatMessage,
@@ -5,15 +6,14 @@ import type {
ChatResponseChunk,
LLM,
} from "@llamaindex/core/llms";
import { extractText } from "@llamaindex/core/utils";
import { randomUUID, ReadableStream } from "@llamaindex/env";
import { getReACTAgentSystemHeader } from "../internal/prompt/react.js";
import {
isAsyncIterable,
stringifyJSONToMessageContent,
} from "../internal/utils.js";
import { extractText } from "../llm/utils.js";
import { Settings } from "../Settings.js";
import type { JSONObject, JSONValue } from "../types.js";
import { AgentRunner, AgentWorker, type AgentParamsBase } from "./base.js";
import type { TaskHandler } from "./types.js";
import {
@@ -66,7 +66,7 @@ function reasonFormatter(reason: Reason): string | Promise<string> {
}
function extractJsonStr(text: string): string {
const pattern = /\{.*}/s;
const pattern = /\{.*\}/s;
const match = text.match(pattern);
if (!match) {
@@ -98,7 +98,7 @@ function extractToolUse(
inputText: string,
): [thought: string, action: string, input: string] {
const pattern =
/\s*Thought: (.*?)\nAction: ([a-zA-Z0-9_]+).*?\.*Input: .*?(\{.*?})/s;
/\s*Thought: (.*?)\nAction: ([a-zA-Z0-9_]+).*?\.*Input: .*?(\{.*?\})/s;
const match = inputText.match(pattern);
+4 -5
View File
@@ -9,7 +9,6 @@ import type {
} from "@llamaindex/core/llms";
import { ReadableStream } from "@llamaindex/env";
import type { Logger } from "../internal/logger.js";
import type { BaseEvent } from "../internal/type.js";
import type { UUID } from "../types.js";
export type AgentTaskContext<
@@ -90,9 +89,9 @@ export type TaskHandler<
) => void,
) => Promise<void>;
export type AgentStartEvent = BaseEvent<{
export type AgentStartEvent = {
startStep: TaskStep;
}>;
export type AgentEndEvent = BaseEvent<{
};
export type AgentEndEvent = {
endStep: TaskStep;
}>;
};
+10 -11
View File
@@ -1,3 +1,8 @@
import {
type JSONObject,
type JSONValue,
Settings,
} from "@llamaindex/core/global";
import type {
BaseTool,
ChatMessage,
@@ -14,13 +19,11 @@ import { baseToolWithCallSchema } from "@llamaindex/core/schema";
import { ReadableStream } from "@llamaindex/env";
import { z } from "zod";
import type { Logger } from "../internal/logger.js";
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
import {
isAsyncIterable,
prettifyError,
stringifyJSONToMessageContent,
} from "../internal/utils.js";
import type { JSONObject, JSONValue } from "../types.js";
import type { AgentParamsBase } from "./base.js";
import type { TaskHandler } from "./types.js";
@@ -221,10 +224,8 @@ export async function callTool(
};
}
try {
getCallbackManager().dispatchEvent("llm-tool-call", {
payload: {
toolCall: { ...toolCall, input },
},
Settings.callbackManager.dispatchEvent("llm-tool-call", {
toolCall: { ...toolCall, input },
});
output = await call.call(tool, input);
logger.log(
@@ -237,11 +238,9 @@ export async function callTool(
output,
isError: false,
};
getCallbackManager().dispatchEvent("llm-tool-result", {
payload: {
toolCall: { ...toolCall, input },
toolResult: { ...toolOutput },
},
Settings.callbackManager.dispatchEvent("llm-tool-result", {
toolCall: { ...toolCall, input },
toolResult: { ...toolOutput },
});
return toolOutput;
} catch (e) {
@@ -1,230 +0,0 @@
import type { Anthropic } from "@anthropic-ai/sdk";
import type { MessageContent } from "@llamaindex/core/llms";
import type { NodeWithScore } from "@llamaindex/core/schema";
import { CustomEvent } from "@llamaindex/env";
import type { AgentEndEvent, AgentStartEvent } from "../agent/types.js";
import {
EventCaller,
getEventCaller,
} from "../internal/context/EventCaller.js";
import type {
LLMEndEvent,
LLMStartEvent,
LLMStreamEvent,
LLMToolCallEvent,
LLMToolResultEvent,
RetrievalEndEvent,
RetrievalStartEvent,
} from "../llm/types.js";
export class LlamaIndexCustomEvent<T = any> extends CustomEvent<T> {
reason: EventCaller | null;
private constructor(
event: string,
options?: CustomEventInit & {
reason?: EventCaller | null;
},
) {
super(event, options);
this.reason = options?.reason ?? null;
}
static fromEvent<Type extends keyof LlamaIndexEventMaps>(
type: Type,
detail: LlamaIndexEventMaps[Type]["detail"],
) {
return new LlamaIndexCustomEvent(type, {
detail: detail,
reason: getEventCaller(),
});
}
}
/**
* This type is used to define the event maps.
*/
export interface LlamaIndexEventMaps {
/**
* @deprecated
*/
retrieve: CustomEvent<RetrievalCallbackResponse>;
"retrieve-start": RetrievalStartEvent;
"retrieve-end": RetrievalEndEvent;
/**
* @deprecated
*/
stream: CustomEvent<StreamCallbackResponse>;
// llm events
"llm-start": LLMStartEvent;
"llm-end": LLMEndEvent;
"llm-tool-call": LLMToolCallEvent;
"llm-tool-result": LLMToolResultEvent;
"llm-stream": LLMStreamEvent;
// agent events
"agent-start": AgentStartEvent;
"agent-end": AgentEndEvent;
}
//#region @deprecated remove in the next major version
//Specify StreamToken per mainstream LLM
export interface DefaultStreamToken {
id: string;
object: string;
created: number;
model: string;
choices: {
index: number;
delta: {
content?: string | null;
role?: "user" | "assistant" | "system" | "function" | "tool";
};
finish_reason: string | null;
}[];
}
//OpenAI stream token schema is the default.
//Note: Anthropic and Replicate also use similar token schemas.
export type OpenAIStreamToken = DefaultStreamToken;
export type AnthropicStreamToken = Anthropic.Completion;
//
//Callback Responses
//
//TODO: Write Embedding Callbacks
//StreamCallbackResponse should let practitioners implement callbacks out of the box...
//When custom streaming LLMs are involved, people are expected to write their own StreamCallbackResponses
export interface StreamCallbackResponse {
index: number;
isDone?: boolean;
token?: DefaultStreamToken;
}
export interface RetrievalCallbackResponse {
query: MessageContent;
nodes: NodeWithScore[];
}
interface CallbackManagerMethods {
/**
* onLLMStream is called when a token is streamed from the LLM. Defining this
* callback auto sets the stream = True flag on the openAI createChatCompletion request.
* @deprecated will be removed in the next major version
*/
onLLMStream: (params: StreamCallbackResponse) => Promise<void> | void;
/**
* onRetrieve is called as soon as the retriever finishes fetching relevant nodes.
* This callback allows you to handle the retrieved nodes even if the synthesizer
* is still running.
* @deprecated will be removed in the next major version
*/
onRetrieve: (params: RetrievalCallbackResponse) => Promise<void> | void;
}
//#endregion
const noop: (...args: any[]) => any = () => void 0;
type EventHandler<Event extends CustomEvent> = (
event: Event & {
reason: EventCaller | null;
},
) => void;
export class CallbackManager implements CallbackManagerMethods {
/**
* @deprecated will be removed in the next major version
*/
get onLLMStream(): CallbackManagerMethods["onLLMStream"] {
return async (response) => {
await Promise.all(
this.#handlers
.get("stream")!
.map((handler) =>
handler(LlamaIndexCustomEvent.fromEvent("stream", response)),
),
);
};
}
/**
* @deprecated will be removed in the next major version
*/
get onRetrieve(): CallbackManagerMethods["onRetrieve"] {
return async (response) => {
await Promise.all(
this.#handlers
.get("retrieve")!
.map((handler) =>
handler(LlamaIndexCustomEvent.fromEvent("retrieve", response)),
),
);
};
}
/**
* @deprecated will be removed in the next major version
*/
set onLLMStream(_: never) {
throw new Error("onLLMStream is deprecated. Use on('stream') instead");
}
/**
* @deprecated will be removed in the next major version
*/
set onRetrieve(_: never) {
throw new Error("onRetrieve is deprecated. Use `on('retrieve')` instead");
}
#handlers = new Map<keyof LlamaIndexEventMaps, EventHandler<CustomEvent>[]>();
constructor(handlers?: Partial<CallbackManagerMethods>) {
const onLLMStream = handlers?.onLLMStream ?? noop;
this.on("stream", (event) => onLLMStream(event.detail));
const onRetrieve = handlers?.onRetrieve ?? noop;
this.on("retrieve", (event) => onRetrieve(event.detail));
}
on<
K extends keyof LlamaIndexEventMaps,
H extends EventHandler<LlamaIndexEventMaps[K]>,
>(event: K, handler: H) {
if (!this.#handlers.has(event)) {
this.#handlers.set(event, []);
}
this.#handlers.get(event)!.push(handler);
return this;
}
off<
K extends keyof LlamaIndexEventMaps,
H extends EventHandler<LlamaIndexEventMaps[K]>,
>(event: K, handler: H) {
if (!this.#handlers.has(event)) {
return;
}
const handlers = this.#handlers.get(event)!;
const index = handlers.indexOf(handler);
if (index > -1) {
handlers.splice(index, 1);
}
return this;
}
dispatchEvent<K extends keyof LlamaIndexEventMaps>(
event: K,
detail: LlamaIndexEventMaps[K]["detail"],
) {
const handlers = this.#handlers.get(event);
if (!handlers) {
return;
}
queueMicrotask(() => {
handlers.forEach((handler) =>
handler(
LlamaIndexCustomEvent.fromEvent(event, {
...detail,
}),
),
);
});
}
}
@@ -1,7 +1,6 @@
import type { Document } from "@llamaindex/core/schema";
import type { Document, TransformComponent } from "@llamaindex/core/schema";
import type { BaseRetriever } from "../Retriever.js";
import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
import type { TransformComponent } from "../ingestion/types.js";
import type { BaseNodePostprocessor } from "../postprocessors/types.js";
import type { BaseSynthesizer } from "../synthesizers/types.js";
import type { QueryEngine } from "../types.js";
@@ -148,11 +147,11 @@ export class LlamaCloudIndex {
static async fromDocuments(
params: {
documents: Document[];
transformations?: TransformComponent[];
transformations?: TransformComponent<any>[];
verbose?: boolean;
} & CloudConstructorParams,
): Promise<LlamaCloudIndex> {
const defaultTransformations: TransformComponent[] = [
const defaultTransformations: TransformComponent<any>[] = [
new SimpleNodeParser(),
new OpenAIEmbedding({
apiKey: getEnv("OPENAI_API_KEY"),
@@ -4,12 +4,11 @@ import {
type RetrievalParams,
type TextNodeWithScore,
} from "@llamaindex/cloud/api";
import { Settings } from "@llamaindex/core/global";
import type { NodeWithScore } from "@llamaindex/core/schema";
import { jsonToNode, ObjectType } from "@llamaindex/core/schema";
import { extractText, wrapEventCaller } from "@llamaindex/core/utils";
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
import { wrapEventCaller } from "../internal/context/EventCaller.js";
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
import { extractText } from "../llm/utils.js";
import type { ClientParams, CloudConstructorParams } from "./constants.js";
import { DEFAULT_PROJECT_NAME } from "./constants.js";
import { initService } from "./utils.js";
@@ -92,11 +91,9 @@ export class LlamaCloudRetriever implements BaseRetriever {
const nodesWithScores = this.resultNodesToNodeWithScore(
results.retrieval_nodes,
);
getCallbackManager().dispatchEvent("retrieve-end", {
payload: {
query,
nodes: nodesWithScores,
},
Settings.callbackManager.dispatchEvent("retrieve-end", {
query,
nodes: nodesWithScores,
});
return nodesWithScores;
}
+3 -4
View File
@@ -3,20 +3,19 @@ import type {
PipelineCreate,
PipelineType,
} from "@llamaindex/cloud/api";
import { BaseNode } from "@llamaindex/core/schema";
import { BaseNode, type TransformComponent } from "@llamaindex/core/schema";
import { OpenAIEmbedding } from "../embeddings/OpenAIEmbedding.js";
import type { TransformComponent } from "../ingestion/types.js";
import { SimpleNodeParser } from "../nodeParsers/SimpleNodeParser.js";
export type GetPipelineCreateParams = {
pipelineName: string;
pipelineType: PipelineType;
transformations?: TransformComponent[];
transformations?: TransformComponent<any>[];
inputNodes?: BaseNode[];
};
function getTransformationConfig(
transformation: TransformComponent,
transformation: TransformComponent<any>,
): ConfiguredTransformationItem {
if (transformation instanceof SimpleNodeParser) {
return {
-1
View File
@@ -4,6 +4,5 @@ export const DEFAULT_NUM_OUTPUTS = 256;
export const DEFAULT_CHUNK_SIZE = 1024;
export const DEFAULT_CHUNK_OVERLAP = 20;
export const DEFAULT_CHUNK_OVERLAP_RATIO = 0.1;
export const DEFAULT_SIMILARITY_TOP_K = 2;
export const DEFAULT_PADDING = 5;
@@ -1,7 +1,7 @@
import { BaseEmbedding } from "@llamaindex/core/embeddings";
import type { MessageContentDetail } from "@llamaindex/core/llms";
import { extractSingleText } from "@llamaindex/core/utils";
import { getEnv } from "@llamaindex/env";
import { extractSingleText } from "../llm/utils.js";
import { BaseEmbedding } from "./types.js";
const DEFAULT_MODEL = "sentence-transformers/clip-ViT-B-32";
@@ -103,10 +103,10 @@ export class DeepInfraEmbedding extends BaseEmbedding {
}
}
async getTextEmbeddings(texts: string[]): Promise<number[][]> {
getTextEmbeddings = async (texts: string[]): Promise<number[][]> => {
const textsWithPrefix = mapPrefixWithInputs(this.textPrefix, texts);
return await this.getDeepInfraEmbedding(textsWithPrefix);
}
return this.getDeepInfraEmbedding(textsWithPrefix);
};
async getQueryEmbeddings(queries: string[]): Promise<number[][]> {
const queriesWithPrefix = mapPrefixWithInputs(this.queryPrefix, queries);
@@ -1,6 +1,6 @@
import { BaseEmbedding } from "@llamaindex/core/embeddings";
import { GeminiSession, GeminiSessionStore } from "../llm/gemini/base.js";
import { GEMINI_BACKENDS } from "../llm/gemini/types.js";
import { BaseEmbedding } from "./types.js";
export enum GEMINI_EMBEDDING_MODEL {
EMBEDDING_001 = "embedding-001",
@@ -1,6 +1,6 @@
import { HfInference } from "@huggingface/inference";
import { BaseEmbedding } from "@llamaindex/core/embeddings";
import { lazyLoadTransformers } from "../internal/deps/transformers.js";
import { BaseEmbedding } from "./types.js";
export enum HuggingFaceEmbeddingModelType {
XENOVA_ALL_MINILM_L6_V2 = "Xenova/all-MiniLM-L6-v2",
@@ -91,11 +91,11 @@ export class HuggingFaceInferenceAPIEmbedding extends BaseEmbedding {
return res as number[];
}
async getTextEmbeddings(texts: string[]): Promise<Array<number[]>> {
getTextEmbeddings = async (texts: string[]): Promise<Array<number[]>> => {
const res = await this.hf.featureExtraction({
model: this.model,
inputs: texts,
});
return res as number[][];
}
};
}
@@ -1,5 +1,5 @@
import { BaseEmbedding } from "@llamaindex/core/embeddings";
import { MistralAISession } from "../llm/mistral.js";
import { BaseEmbedding } from "./types.js";
export enum MistralAIEmbeddingModelType {
MISTRAL_EMBED = "mistral-embed",
@@ -0,0 +1,170 @@
import { BaseEmbedding, type EmbeddingInfo } from "@llamaindex/core/embeddings";
import { getEnv } from "@llamaindex/env";
import { MixedbreadAI, MixedbreadAIClient } from "@mixedbread-ai/sdk";
type EmbeddingsRequestWithoutInput = Omit<
MixedbreadAI.EmbeddingsRequest,
"input"
>;
/**
* Interface extending EmbeddingsParams with additional
* parameters specific to the MixedbreadAIEmbeddings class.
*/
export interface MixedbreadAIEmbeddingsParams
extends Omit<EmbeddingsRequestWithoutInput, "model"> {
/**
* The model to use for generating embeddings.
* @default {"mixedbread-ai/mxbai-embed-large-v1"}
*/
model?: string;
/**
* The API key to use.
* @default {process.env.MXBAI_API_KEY}
*/
apiKey?: string;
/**
* The base URL for the API.
*/
baseUrl?: string;
/**
* The maximum number of documents to embed in a single request.
* @default {128}
*/
embedBatchSize?: number;
/**
* The embed info for the model.
*/
embedInfo?: EmbeddingInfo;
/**
* The maximum number of retries to attempt.
* @default {3}
*/
maxRetries?: number;
/**
* Timeouts for the request.
*/
timeoutInSeconds?: number;
}
/**
* Class for generating embeddings using the mixedbread ai API.
*
* This class leverages the model "mixedbread-ai/mxbai-embed-large-v1" to generate
* embeddings for text documents. The embeddings can be used for various NLP tasks
* such as similarity comparison, clustering, or as features in machine learning models.
*
* @example
* const mxbai = new MixedbreadAIEmbeddings({ apiKey: 'your-api-key' });
* const texts = ["Baking bread is fun", "I love baking"];
* const result = await mxbai.getTextEmbeddings(texts);
* console.log(result);
*
* @example
* const mxbai = new MixedbreadAIEmbeddings({
* apiKey: 'your-api-key',
* model: 'mixedbread-ai/mxbai-embed-large-v1',
* encodingFormat: MixedbreadAI.EncodingFormat.Binary,
* dimensions: 512,
* normalized: true,
* });
* const query = "Represent this sentence for searching relevant passages: Is baking bread fun?";
* const result = await mxbai.getTextEmbedding(query);
* console.log(result);
*/
export class MixedbreadAIEmbeddings extends BaseEmbedding {
requestParams: EmbeddingsRequestWithoutInput;
requestOptions: MixedbreadAIClient.RequestOptions;
private client: MixedbreadAIClient;
/**
* Constructor for MixedbreadAIEmbeddings.
* @param {Partial<MixedbreadAIEmbeddingsParams>} params - An optional object with properties to configure the instance.
* @throws {Error} If the API key is not provided or found in the environment variables.
* @throws {Error} If the batch size exceeds 256.
*/
constructor(params?: Partial<MixedbreadAIEmbeddingsParams>) {
super();
const apiKey = params?.apiKey ?? getEnv("MXBAI_API_KEY");
if (!apiKey) {
throw new Error(
"mixedbread ai API key not found. Either provide it in the constructor or set the 'MXBAI_API_KEY' environment variable.",
);
}
if (params?.embedBatchSize && params?.embedBatchSize > 256) {
throw new Error(
"The maximum batch size for mixedbread ai embeddings API is 256.",
);
}
this.embedBatchSize = params?.embedBatchSize ?? 128;
this.embedInfo = params?.embedInfo;
this.requestParams = {
model: params?.model ?? "mixedbread-ai/mxbai-embed-large-v1",
normalized: params?.normalized,
dimensions: params?.dimensions,
encodingFormat: params?.encodingFormat,
truncationStrategy: params?.truncationStrategy,
prompt: params?.prompt,
};
this.requestOptions = {
timeoutInSeconds: params?.timeoutInSeconds,
maxRetries: params?.maxRetries ?? 3,
// Support for this already exists in the python sdk and will be added to the js sdk soon
// @ts-ignore
additionalHeaders: {
"user-agent": "@mixedbread-ai/llamaindex-ts-sdk",
},
};
this.client = new MixedbreadAIClient({
apiKey,
environment: params?.baseUrl,
});
}
/**
* Generates an embedding for a single text.
* @param {string} text - A string to generate an embedding for.
* @returns {Promise<number[]>} A Promise that resolves to an array of numbers representing the embedding.
*
* @example
* const query = "Represent this sentence for searching relevant passages: Is baking bread fun?";
* const result = await mxbai.getTextEmbedding(text);
* console.log(result);
*/
async getTextEmbedding(text: string): Promise<number[]> {
return (await this.getTextEmbeddings([text]))[0];
}
/**
* Generates embeddings for an array of texts.
* @param {string[]} texts - An array of strings to generate embeddings for.
* @returns {Promise<Array<number[]>>} A Promise that resolves to an array of embeddings.
*
* @example
* const texts = ["Baking bread is fun", "I love baking"];
* const result = await mxbai.getTextEmbeddings(texts);
* console.log(result);
*/
getTextEmbeddings = async (texts: string[]): Promise<Array<number[]>> => {
if (texts.length === 0) {
return [];
}
const response = await this.client.embeddings(
{
...this.requestParams,
input: texts,
},
this.requestOptions,
);
return response.data.map((d) => d.embedding as number[]);
};
}

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