Compare commits

...

35 Commits

Author SHA1 Message Date
github-actions[bot] 2afcbe6587 Release 0.5.20 (#1132)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-28 10:43:26 +07:00
Marcus Schiesser 22ff486fbe fix: Add tiktoken WASM to withLlamaIndex (#1134)
Co-authored-by: Thuc Pham <51660321+thucpn@users.noreply.github.com>
2024-08-28 10:39:14 +07:00
Thuc Pham eed0b0415d fix: use metadata mode LLM for generating context (#1133)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-08-23 22:56:18 +07:00
Sebastian van Gerwen d9d6c56ed5 pgvectorstore support new conditions and operations (#1131)
Co-authored-by: Sebastian van Gerwen <svangerwen@invertigro.com>
Co-authored-by: Marcus Schiesser <marcus.schiesser@googlemail.com>
2024-08-23 14:40:39 +07:00
github-actions[bot] f99a237093 Release 0.5.19 (#1128)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-19 14:04:47 +07:00
Thuc Pham fcbf18344c feat: implement llamacloud file service (#1125) 2024-08-19 14:01:41 +07:00
github-actions[bot] bf8cbeb6c5 Release 0.5.18 (#1124)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-19 12:53:28 +09:00
Alex Yang e27e7dd054 chore: bump natural to 8.0.1 (#1126) 2024-08-17 07:15:08 -07:00
Thuc Pham 8b66cf4341 feat: support organization id in llamacloud index (#1123)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-08-15 13:51:48 +07:00
github-actions[bot] 6f4549bdea Release 0.5.17 (#1117)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-12 17:45:29 +07:00
Thuc Pham c654398f75 feat: implement Weaviate Vector Store in TS (#1109) 2024-08-12 17:41:05 +07:00
github-actions[bot] 0664a99945 Release 0.5.16 (#1115)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-09 22:09:34 -07:00
Alex Yang 58abc5731b chore: update changeset 2024-08-09 22:06:43 -07:00
github-actions[bot] 7498b1e0f1 Release 0.5.15 (#1108)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-09 14:36:47 -07:00
Alex Yang 07a275fea5 chore: bump openai (#1113) 2024-08-09 12:56:30 -07:00
Alex Yang 1b6263e08d fix: export schema in top level (#1112) 2024-08-09 10:10:12 -07:00
Alex Yang 089f1d49c0 refactor: migrate reader type into core (#1111) 2024-08-09 09:53:50 -07:00
Thuc Pham 01c184c608 feat: add is_empty operator for filtering vector store (#1107) 2024-08-09 14:50:57 +07:00
github-actions[bot] 1752463ee6 Release 0.5.14 (#1103)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-07 17:03:00 -07:00
Peter Goldstein c825a2f743 Add gpt-4o-mini to Azure. Add 2024-06-01 API version for Azure (#1102) 2024-08-06 14:23:28 +07:00
github-actions[bot] ba058dc8d4 Release 0.5.13 (#1100)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-08-02 09:50:07 -07:00
Philipp Serrer 04b2f8e062 fix: metadata should not be included after sentence splitter (#1099) 2024-08-02 09:22:04 -07:00
Alex Yang 62b874e14f fix: enforce no-base-to-string (#1097) 2024-08-01 14:05:19 -07:00
github-actions[bot] 9c9e9b4e03 Release 0.5.12 (#1091)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-30 15:46:39 -07:00
Alex Yang e3c307ab55 chore: fix changeset 2024-07-30 15:32:24 -07:00
André Lago b1b2baa969 docs: fix minor typo (#1092) 2024-07-30 15:07:46 -07:00
Marcus Schiesser 0452af91cc fix: handling errors in splitBySentenceTokenizer (#1087)
Co-authored-by: Alex Yang <himself65@outlook.com>
2024-07-30 09:36:58 -07:00
Marcus Schiesser da5cfc42e5 fix: integrate with create-llama (#1088)
Co-authored-by: Alex Yang <himself65@outlook.com>
2024-07-30 08:19:32 -07:00
Alex Yang eb89223386 chore: bump bunchee@5.3.1 (#1090) 2024-07-30 08:19:01 -07:00
Alex Yang 93dc3a31b3 fix: lock hey-api version (#1089) 2024-07-30 08:00:05 -07:00
Fabian Wimmer 345300f110 feat: add split by page mode to LlamaParseReader (#924) 2024-07-29 16:16:46 +07:00
github-actions[bot] f322c5d202 Release 0.5.11 (#1082)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-07-26 21:26:11 -07:00
Parham Saidi 376d29a78f feat: meta3.1 405b tool calling support (#1080) 2024-07-26 19:53:24 -07:00
Parham Saidi 224d507ab5 fix: prevent anthropic tool calling getting mixed with conversation (#1078) 2024-07-26 09:55:22 -07:00
Alex Yang 1f680d731d chore: bump llamacloud api (#1081) 2024-07-26 09:53:50 -07:00
107 changed files with 4176 additions and 2593 deletions
+6 -1
View File
@@ -31,7 +31,12 @@ module.exports = {
"@typescript-eslint/ban-types": "off",
"no-array-constructor": "off",
"@typescript-eslint/no-array-constructor": "off",
"@typescript-eslint/no-base-to-string": "off",
"@typescript-eslint/no-base-to-string": [
"error",
{
ignoredTypeNames: ["Error", "RegExp", "URL", "URLSearchParams"],
},
],
"@typescript-eslint/no-duplicate-enum-values": "off",
"@typescript-eslint/no-duplicate-type-constituents": "off",
"@typescript-eslint/no-explicit-any": "off",
+1 -1
View File
@@ -164,7 +164,7 @@ Check out our NextJS playground at https://llama-playground.vercel.app/. The sou
- [Node](/packages/llamaindex/src/Node.ts): The basic data building block. Most commonly, these are parts of the document split into manageable pieces that are small enough to be fed into an embedding model and LLM.
- [Embedding](/packages/llamaindex/src/embeddings/OpenAIEmbedding.ts): Embeddings are sets of floating point numbers which represent the data in a Node. By comparing the similarity of embeddings, we can derive an understanding of the similarity of two pieces of data. One use case is to compare the embedding of a question with the embeddings of our Nodes to see which Nodes may contain the data needed to answer that quesiton. Because the default service context is OpenAI, the default embedding is `OpenAIEmbedding`. If using different models, say through Ollama, use this [Embedding](/packages/llamaindex/src/embeddings/OllamaEmbedding.ts) (see all [here](/packages/llamaindex/src/embeddings)).
- [Embedding](/packages/llamaindex/src/embeddings/OpenAIEmbedding.ts): Embeddings are sets of floating point numbers which represent the data in a Node. By comparing the similarity of embeddings, we can derive an understanding of the similarity of two pieces of data. One use case is to compare the embedding of a question with the embeddings of our Nodes to see which Nodes may contain the data needed to answer that question. Because the default service context is OpenAI, the default embedding is `OpenAIEmbedding`. If using different models, say through Ollama, use this [Embedding](/packages/llamaindex/src/embeddings/OllamaEmbedding.ts) (see all [here](/packages/llamaindex/src/embeddings)).
- [Indices](/packages/llamaindex/src/indices/): Indices store the Nodes and the embeddings of those nodes. QueryEngines retrieve Nodes from these Indices using embedding similarity.
+74
View File
@@ -1,5 +1,79 @@
# docs
## 0.0.61
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.0.60
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.0.59
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.0.58
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.0.57
### Patch Changes
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.0.56
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.0.55
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.0.54
### Patch Changes
- llamaindex@0.5.13
## 0.0.53
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.0.52
### Patch Changes
- 376d29a: feat: added tool calling and agent support for llama3.1 504B
- llamaindex@0.5.11
## 0.0.51
### Patch Changes
+10 -3
View File
@@ -6,10 +6,17 @@ sidebar_position: 2
We support Node.JS versions 18, 20 and 22, with experimental support for Deno, Bun and Vercel Edge functions.
## NextJS App Router
## NextJS
If you're using NextJS App Router route handlers/serverless functions, you'll need to use the NodeJS mode:
If you're using NextJS you'll need to add `withLlamaIndex` to your `next.config.js` file. This will add the necessary configuration for included 3rd-party libraries to your build:
```js
export const runtime = "nodejs"; // default
// next.config.js
const withLlamaIndex = require("llamaindex/next");
module.exports = withLlamaIndex({
// your next.js config
});
```
For details, check the latest [withLlamaIndex](https://github.com/run-llama/LlamaIndexTS/blob/main/packages/llamaindex/src/next.ts) implementation.
+1
View File
@@ -15,6 +15,7 @@ LlamaIndex.TS comes with a few built-in agents, but you can also create your own
- Anthropic Agent both via Anthropic and Bedrock (in `@llamaIndex/community`)
- Gemini Agent
- ReACT Agent
- Meta3.1 504B via Bedrock (in `@llamaIndex/community`)
## Examples
@@ -48,6 +48,7 @@ They can be divided into two groups.
- `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-separated list. First page is `0`.
- `splitByPage` Wether to split the results, creating one document per page. Uses the set `pageSeparator` or `\n---\n` as fallback. Default is true.
- `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`.
@@ -31,7 +31,7 @@ META_LLAMA3_8B_INSTRUCT = "meta.llama3-8b-instruct-v1:0";
META_LLAMA3_70B_INSTRUCT = "meta.llama3-70b-instruct-v1:0";
META_LLAMA3_1_8B_INSTRUCT = "meta.llama3-1-8b-instruct-v1:0"; // available on us-west-2
META_LLAMA3_1_70B_INSTRUCT = "meta.llama3-1-70b-instruct-v1:0"; // available on us-west-2
META_LLAMA3_1_405B_INSTRUCT = "meta.llama3-1-405b-instruct-v1:0"; // preview only, available on us-west-2
META_LLAMA3_1_405B_INSTRUCT = "meta.llama3-1-405b-instruct-v1:0"; // preview only, available on us-west-2, tool calling supported
```
Sonnet, Haiku and Opus are multimodal, image_url only supports base64 data url format, e.g. `data:image/jpeg;base64,SGVsbG8sIFdvcmxkIQ==`
@@ -67,3 +67,72 @@ async function main() {
console.log(response.response);
}
```
## Agent Example
```ts
import { BEDROCK_MODELS, Bedrock } from "@llamaindex/community";
import { FunctionTool, LLMAgent } from "llamaindex";
const sumNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a + b}`,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
},
},
);
const divideNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a / b}`,
{
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend a to divide",
},
b: {
type: "number",
description: "The divisor b to divide by",
},
},
required: ["a", "b"],
},
},
);
const bedrock = new Bedrock({
model: BEDROCK_MODELS.META_LLAMA3_1_405B_INSTRUCT,
...
});
async function main() {
const agent = new LLMAgent({
llm: bedrock,
tools: [sumNumbers, divideNumbers],
});
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});
console.log(response.message);
}
```
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.51",
"version": "0.0.61",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
+31
View File
@@ -0,0 +1,31 @@
# Weaviate Vector Store
Here are two sample scripts which work with loading and querying data from a Weaviate Vector Store.
## Prerequisites
- An Weaviate Vector Database
- Hosted https://weaviate.io/
- Self Hosted https://weaviate.io/developers/weaviate/installation/docker-compose#starter-docker-compose-file
- An OpenAI API Key
## Setup
1. Set your env variables:
- `WEAVIATE_CLUSTER_URL`: Address of your Weaviate Vector Store (like localhost:8080)
- `WEAVIATE_API_KEY`: Your Weaviate API key
- `OPENAI_API_KEY`: Your OpenAI key
2. `cd` Into the `examples` directory
3. run `npm i`
## Load the data
This sample loads the same dataset of movie reviews as sample dataset
run `npx tsx weaviate/load`
## Use RAG to Query the data
run `npx tsx weaviate/query`
+23
View File
@@ -0,0 +1,23 @@
import {
PapaCSVReader,
storageContextFromDefaults,
VectorStoreIndex,
WeaviateVectorStore,
} from "llamaindex";
const indexName = "MovieReviews";
async function main() {
try {
const reader = new PapaCSVReader(false);
const docs = await reader.loadData("./data/movie_reviews.csv");
const vectorStore = new WeaviateVectorStore({ indexName });
const storageContext = await storageContextFromDefaults({ vectorStore });
await VectorStoreIndex.fromDocuments(docs, { storageContext });
console.log("Successfully loaded data into Weaviate");
} catch (e) {
console.error(e);
}
}
void main();
+46
View File
@@ -0,0 +1,46 @@
import { VectorStoreIndex, WeaviateVectorStore } from "llamaindex";
const indexName = "MovieReviews";
async function main() {
try {
const query = "Get all movie titles.";
const vectorStore = new WeaviateVectorStore({ indexName });
const index = await VectorStoreIndex.fromVectorStore(vectorStore);
const retriever = index.asRetriever({ similarityTopK: 20 });
const queryEngine = index.asQueryEngine({ retriever });
const results = await queryEngine.query({ query });
console.log(`Query from ${results.sourceNodes?.length} nodes`);
console.log(results.response);
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_21",
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();
+8
View File
@@ -1,5 +1,13 @@
# @llamaindex/autotool
## 2.0.1
### Patch Changes
- 58abc57: fix: align version
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 2.0.0
### Patch Changes
@@ -1,5 +1,88 @@
# @llamaindex/autotool-02-next-example
## 0.1.45
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
- @llamaindex/autotool@2.0.1
## 0.1.44
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
- @llamaindex/autotool@2.0.1
## 0.1.43
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
- @llamaindex/autotool@2.0.1
## 0.1.42
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
- @llamaindex/autotool@2.0.1
## 0.1.41
### Patch Changes
- Updated dependencies [58abc57]
- @llamaindex/autotool@2.0.1
- llamaindex@0.5.16
## 0.1.40
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
- @llamaindex/autotool@2.0.0
## 0.1.39
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
- @llamaindex/autotool@2.0.0
## 0.1.38
### Patch Changes
- llamaindex@0.5.13
- @llamaindex/autotool@2.0.0
## 0.1.37
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
- @llamaindex/autotool@2.0.0
## 0.1.36
### Patch Changes
- llamaindex@0.5.11
- @llamaindex/autotool@2.0.0
## 0.1.35
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool-02-next-example",
"private": true,
"version": "0.1.35",
"version": "0.1.45",
"scripts": {
"dev": "next dev",
"build": "next build",
+3 -3
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool",
"type": "module",
"version": "2.0.0",
"version": "2.0.1",
"description": "auto transpile your JS function to LLM Agent compatible",
"files": [
"dist",
@@ -51,7 +51,7 @@
"unplugin": "^1.10.1"
},
"peerDependencies": {
"llamaindex": "^0.5.10",
"llamaindex": "^0.5.20",
"openai": "^4",
"typescript": "^4"
},
@@ -70,7 +70,7 @@
"@swc/types": "^0.1.8",
"@types/json-schema": "^7.0.15",
"@types/node": "^20.12.11",
"bunchee": "5.3.0-beta.0",
"bunchee": "5.3.1",
"llamaindex": "workspace:*",
"next": "14.2.5",
"rollup": "^4.18.0",
+12
View File
@@ -1,5 +1,17 @@
# @llamaindex/cloud
## 0.2.2
### Patch Changes
- 58abc57: fix: align version
## 0.2.1
### Patch Changes
- 1f680d7: chore: bump llamacloud api
## 0.2.0
### Minor Changes
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -1,10 +1,10 @@
{
"name": "@llamaindex/cloud",
"version": "0.2.0",
"version": "0.2.2",
"type": "module",
"license": "MIT",
"scripts": {
"generate": "pnpm dlx @hey-api/openapi-ts",
"generate": "pnpm dlx @hey-api/openapi-ts@0.49.0",
"build": "pnpm run generate && bunchee"
},
"files": [
@@ -35,6 +35,6 @@
},
"devDependencies": {
"@hey-api/openapi-ts": "^0.48.0",
"bunchee": "5.3.0-beta.0"
"bunchee": "5.3.1"
}
}
+37
View File
@@ -1,5 +1,42 @@
# @llamaindex/community
## 0.0.30
### Patch Changes
- Updated dependencies [e27e7dd]
- @llamaindex/core@0.1.9
## 0.0.29
### Patch Changes
- 58abc57: fix: align version
- Updated dependencies [58abc57]
- @llamaindex/core@0.1.8
- @llamaindex/env@0.1.9
## 0.0.28
### Patch Changes
- Updated dependencies [04b2f8e]
- @llamaindex/core@0.1.7
## 0.0.27
### Patch Changes
- Updated dependencies [0452af9]
- @llamaindex/core@0.1.6
## 0.0.26
### Patch Changes
- 224d507: fix: prevent tool calling getting mixed with conversation
- 376d29a: feat: added tool calling and agent support for llama3.1 504B
## 0.0.25
### Patch Changes
+1
View File
@@ -6,6 +6,7 @@
- Bedrock support for the Anthropic Claude Models [usage](https://ts.llamaindex.ai/modules/llms/available_llms/bedrock)
- Bedrock support for the Meta LLama 2, 3 and 3.1 Models [usage](https://ts.llamaindex.ai/modules/llms/available_llms/bedrock)
- Meta LLama3.1 405b tool call support
## LICENSE
+6 -5
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/community",
"description": "Community package for LlamaIndexTS",
"version": "0.0.25",
"version": "0.0.30",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
@@ -19,11 +19,11 @@
"./llm/bedrock": {
"import": {
"types": "./dist/type/llm/bedrock.d.ts",
"default": "./dist/llm/bedrock/base.js"
"default": "./dist/llm/bedrock/index.js"
},
"require": {
"types": "./dist/type/llm/bedrock.d.ts",
"default": "./dist/llm/bedrock/base.cjs"
"default": "./dist/llm/bedrock/index.cjs"
}
}
},
@@ -43,10 +43,11 @@
},
"devDependencies": {
"@types/node": "^20.14.2",
"bunchee": "5.3.0-beta.0"
"bunchee": "5.3.1"
},
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.613.0",
"@llamaindex/core": "workspace:*"
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*"
}
}
+1 -1
View File
@@ -2,4 +2,4 @@ export {
BEDROCK_MODELS,
BEDROCK_MODEL_MAX_TOKENS,
Bedrock,
} from "./llm/bedrock/base.js";
} from "./llm/bedrock/index.js";
@@ -16,17 +16,18 @@ import {
type BedrockChatStreamResponse,
Provider,
} from "../provider";
import { toUtf8 } from "../utils";
import type {
AnthropicNoneStreamingResponse,
AnthropicStreamEvent,
AnthropicTextContent,
ToolBlock,
} from "../types";
} from "./types";
import {
mapBaseToolsToAnthropicTools,
mapChatMessagesToAnthropicMessages,
toUtf8,
} from "../utils";
} from "./utils";
export class AnthropicProvider extends Provider<AnthropicStreamEvent> {
getResultFromResponse(
@@ -69,6 +70,7 @@ export class AnthropicProvider extends Provider<AnthropicStreamEvent> {
let tool: ToolBlock | undefined = undefined;
// #TODO this should be broken down into a separate consumer
for await (const response of stream) {
const delta = this.getTextFromStreamResponse(response);
const event = this.getStreamingEventResponse(response);
if (
event?.type === "content_block_start" &&
@@ -114,11 +116,10 @@ export class AnthropicProvider extends Provider<AnthropicStreamEvent> {
};
}
}
const delta = this.getTextFromStreamResponse(response);
if (!delta && !options) continue;
yield {
delta,
delta: options ? "" : delta,
options,
raw: response,
};
@@ -0,0 +1,142 @@
import type { ToolMetadata } from "@llamaindex/core/llms";
import type { InvocationMetrics } from "../types";
type Usage = {
input_tokens: number;
output_tokens: number;
};
type Message = {
id: string;
type: string;
role: string;
content: string[];
model: string;
stop_reason: string | null;
stop_sequence: string | null;
usage: Usage;
};
export type ToolBlock = {
id: string;
input: unknown;
name: string;
type: "tool_use";
};
export type TextBlock = {
type: "text";
text: string;
};
type ContentBlockStart = {
type: "content_block_start";
index: number;
content_block: ToolBlock | TextBlock;
};
type Delta =
| {
type: "text_delta";
text: string;
}
| {
type: "input_json_delta";
partial_json: string;
};
type ContentBlockDelta = {
type: "content_block_delta";
index: number;
delta: Delta;
};
type ContentBlockStop = {
type: "content_block_stop";
index: number;
};
type MessageDelta = {
type: "message_delta";
delta: {
stop_reason: string;
stop_sequence: string | null;
};
usage: Usage;
};
export type MessageStop = {
type: "message_stop";
"amazon-bedrock-invocationMetrics": InvocationMetrics;
};
export type AnthropicStreamEvent =
| { type: "message_start"; message: Message }
| ContentBlockStart
| ContentBlockDelta
| ContentBlockStop
| MessageDelta
| MessageStop;
export type AnthropicContent =
| AnthropicTextContent
| AnthropicImageContent
| AnthropicToolContent
| AnthropicToolResultContent;
export type AnthropicTextContent = {
type: "text";
text: string;
};
export type AnthropicToolContent = {
type: "tool_use";
id: string;
name: string;
input: Record<string, unknown>;
};
export type AnthropicToolResultContent = {
type: "tool_result";
tool_use_id: string;
content: string;
};
export type AnthropicMediaTypes =
| "image/jpeg"
| "image/png"
| "image/webp"
| "image/gif";
export type AnthropicImageSource = {
type: "base64";
media_type: AnthropicMediaTypes;
data: string; // base64 encoded image bytes
};
export type AnthropicImageContent = {
type: "image";
source: AnthropicImageSource;
};
export type AnthropicMessage = {
role: "user" | "assistant";
content: AnthropicContent[];
};
export type AnthropicNoneStreamingResponse = {
id: string;
type: "message";
role: "assistant";
content: AnthropicContent[];
model: string;
stop_reason: "end_turn" | "max_tokens" | "stop_sequence";
stop_sequence?: string;
usage: { input_tokens: number; output_tokens: number };
};
export type AnthropicTool = {
name: string;
description: string;
input_schema: ToolMetadata["parameters"];
};
@@ -0,0 +1,186 @@
import type { JSONObject } from "@llamaindex/core/global";
import type {
BaseTool,
ChatMessage,
MessageContent,
MessageContentDetail,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { mapMessageContentToMessageContentDetails } from "../utils";
import type {
AnthropicContent,
AnthropicImageContent,
AnthropicMediaTypes,
AnthropicMessage,
AnthropicTextContent,
AnthropicTool,
} from "./types.js";
const ACCEPTED_IMAGE_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
];
export const mergeNeighboringSameRoleMessages = (
messages: AnthropicMessage[],
): AnthropicMessage[] => {
return messages.reduce(
(result: AnthropicMessage[], current: AnthropicMessage, index: number) => {
if (index > 0 && messages[index - 1].role === current.role) {
result[result.length - 1].content = [
...result[result.length - 1].content,
...current.content,
];
} else {
result.push(current);
}
return result;
},
[],
);
};
export const mapMessageContentDetailToAnthropicContent = <
T extends MessageContentDetail,
>(
detail: T,
): AnthropicContent => {
let content: AnthropicContent;
if (detail.type === "text") {
content = mapTextContent(detail.text);
} else if (detail.type === "image_url") {
content = mapImageContent(detail.image_url.url);
} else {
throw new Error("Unsupported content detail type");
}
return content;
};
export const mapMessageContentToAnthropicContent = <T extends MessageContent>(
content: T,
): AnthropicContent[] => {
return mapMessageContentToMessageContentDetails(content).map(
mapMessageContentDetailToAnthropicContent,
);
};
export const mapBaseToolsToAnthropicTools = (
tools?: BaseTool[],
): AnthropicTool[] => {
if (!tools) return [];
return tools.map((tool: BaseTool) => {
const {
metadata: { parameters, ...options },
} = tool;
return {
...options,
input_schema: parameters,
};
});
};
export const mapChatMessagesToAnthropicMessages = <
T extends ChatMessage<ToolCallLLMMessageOptions>,
>(
messages: T[],
): AnthropicMessage[] => {
const mapped = messages
.flatMap((msg: T): AnthropicMessage[] => {
if (msg.options && "toolCall" in msg.options) {
return [
{
role: "assistant",
content: msg.options.toolCall.map((call) => ({
type: "tool_use",
id: call.id,
name: call.name,
input: call.input as JSONObject,
})),
},
];
}
if (msg.options && "toolResult" in msg.options) {
return [
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: msg.options.toolResult.id,
content: msg.options.toolResult.result,
},
],
},
];
}
return mapMessageContentToMessageContentDetails(msg.content).map(
(detail: MessageContentDetail): AnthropicMessage => {
const content = mapMessageContentDetailToAnthropicContent(detail);
return {
role: msg.role === "assistant" ? "assistant" : "user",
content: [content],
};
},
);
})
.filter((message: AnthropicMessage) => {
const content = message.content[0];
if (content.type === "text" && !content.text) return false;
if (content.type === "image" && !content.source.data) return false;
if (content.type === "image" && message.role === "assistant")
return false;
return true;
});
return mergeNeighboringSameRoleMessages(mapped);
};
export const mapTextContent = (text: string): AnthropicTextContent => {
return { type: "text", text };
};
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,
};
};
export const mapImageContent = (imageUrl: string): AnthropicImageContent => {
if (!imageUrl.startsWith("data:"))
throw new Error(
"For Anthropic please only use base64 data url, e.g.: data:image/jpeg;base64,SGVsbG8sIFdvcmxkIQ==",
);
const { mimeType, base64: data } = extractDataUrlComponents(imageUrl);
if (!ACCEPTED_IMAGE_MIME_TYPES.includes(mimeType))
throw new Error(
`Anthropic only accepts the following mimeTypes: ${ACCEPTED_IMAGE_MIME_TYPES.join("\n")}`,
);
return {
type: "image",
source: {
type: "base64",
media_type: mimeType as AnthropicMediaTypes,
data,
},
};
};
@@ -22,8 +22,16 @@ import {
type BedrockChatStreamResponse,
Provider,
} from "./provider";
import { PROVIDERS } from "./providers";
import { mapMessageContentToMessageContentDetails } from "./utils.js";
import { mapMessageContentToMessageContentDetails } from "./utils";
import { AnthropicProvider } from "./anthropic/provider";
import { MetaProvider } from "./meta/provider";
// Other providers should go here
export const PROVIDERS: { [key: string]: Provider } = {
anthropic: new AnthropicProvider(),
meta: new MetaProvider(),
};
export type BedrockChatParamsStreaming = LLMChatParamsStreaming<
BedrockAdditionalChatOptions,
@@ -140,6 +148,7 @@ export const TOOL_CALL_MODELS = [
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_OPUS,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET,
BEDROCK_MODELS.META_LLAMA3_1_405B_INSTRUCT,
];
const getProvider = (model: string): Provider => {
@@ -0,0 +1,3 @@
export const TOKENS = {
TOOL_CALL: "<|python_tag|>",
};
@@ -0,0 +1,136 @@
import type {
InvokeModelCommandInput,
InvokeModelWithResponseStreamCommandInput,
ResponseStream,
} from "@aws-sdk/client-bedrock-runtime";
import type {
BaseTool,
ChatMessage,
LLMMetadata,
ToolCall,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { toUtf8 } from "../utils";
import type { MetaNoneStreamingResponse, MetaStreamEvent } from "./types";
import { randomUUID } from "@llamaindex/env";
import { Provider, type BedrockChatStreamResponse } from "../provider";
import { TOKENS } from "./constants";
import {
mapChatMessagesToMetaLlama2Messages,
mapChatMessagesToMetaLlama3Messages,
} from "./utils";
export class MetaProvider extends Provider<MetaStreamEvent> {
getResultFromResponse(
response: Record<string, any>,
): MetaNoneStreamingResponse {
return JSON.parse(toUtf8(response.body));
}
getToolsFromResponse<ToolContent>(
response: Record<string, any>,
): ToolContent[] {
const result = this.getResultFromResponse(response);
if (!result.generation.trim().startsWith(TOKENS.TOOL_CALL)) return [];
const tool = JSON.parse(
result.generation.trim().split(TOKENS.TOOL_CALL)[1],
);
return [
{
id: randomUUID(),
name: tool.name,
input: tool.parameters,
} as ToolContent,
];
}
getTextFromResponse(response: Record<string, any>): string {
const result = this.getResultFromResponse(response);
if (result.generation.trim().startsWith(TOKENS.TOOL_CALL)) return "";
return result.generation;
}
getTextFromStreamResponse(response: Record<string, any>): string {
const event = this.getStreamingEventResponse(response);
if (event?.generation) {
return event.generation;
}
return "";
}
async *reduceStream(
stream: AsyncIterable<ResponseStream>,
): BedrockChatStreamResponse {
const collecting: string[] = [];
let toolId: string | undefined = undefined;
for await (const response of stream) {
const event = this.getStreamingEventResponse(response);
const delta = this.getTextFromStreamResponse(response);
// odd quirk of llama3.1, start token is \n\n
if (
!event?.generation.trim() &&
event?.generation_token_count === 1 &&
event.prompt_token_count !== null
)
continue;
if (delta === TOKENS.TOOL_CALL) {
toolId = randomUUID();
continue;
}
let options: undefined | ToolCallLLMMessageOptions = undefined;
if (toolId && event?.stop_reason === "stop") {
const tool = JSON.parse(collecting.join(""));
options = {
toolCall: [
{
id: toolId,
name: tool.name,
input: tool.parameters,
} as ToolCall,
],
};
} else if (toolId && !event?.stop_reason) {
collecting.push(delta);
continue;
}
if (!delta && !options) continue;
yield {
delta: options ? "" : delta,
options,
raw: response,
};
}
}
getRequestBody<T extends ChatMessage>(
metadata: LLMMetadata,
messages: T[],
tools?: BaseTool[],
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput {
let prompt: string = "";
if (metadata.model.startsWith("meta.llama3")) {
prompt = mapChatMessagesToMetaLlama3Messages(messages, tools);
} else if (metadata.model.startsWith("meta.llama2")) {
prompt = mapChatMessagesToMetaLlama2Messages(messages);
} else {
throw new Error(`Meta model ${metadata.model} is not supported`);
}
return {
modelId: metadata.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({
prompt,
max_gen_len: metadata.maxTokens,
temperature: metadata.temperature,
top_p: metadata.topP,
}),
};
}
}
@@ -0,0 +1,21 @@
import type { InvocationMetrics } from "../types";
export type MetaTextContent = string;
export type MetaMessage = {
role: "user" | "assistant" | "system" | "ipython";
content: MetaTextContent;
};
type MetaResponse = {
generation: string;
prompt_token_count: number;
generation_token_count: number;
stop_reason: "stop" | "length";
};
export type MetaStreamEvent = MetaResponse & {
"amazon-bedrock-invocationMetrics": InvocationMetrics;
};
export type MetaNoneStreamingResponse = MetaResponse;
@@ -0,0 +1,198 @@
import type {
BaseTool,
ChatMessage,
MessageContentTextDetail,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import type { MetaMessage } from "./types";
const getToolCallInstructionString = (tool: BaseTool): string => {
return `Use the function '${tool.metadata.name}' to '${tool.metadata.description}'`;
};
const getToolCallParametersString = (tool: BaseTool): string => {
return JSON.stringify({
name: tool.metadata.name,
description: tool.metadata.description,
parameters: tool.metadata.parameters
? Object.entries(tool.metadata.parameters.properties).map(
([name, definition]) => ({ [name]: definition }),
)
: {},
});
};
// ported from https://github.com/meta-llama/llama-agentic-system/blob/main/llama_agentic_system/system_prompt.py
// NOTE: using json instead of the above xml style tool calling works more reliability
export const getToolsPrompt = (tools?: BaseTool[]) => {
if (!tools?.length) return "";
const customToolParams = tools.map((tool) => {
return [
getToolCallInstructionString(tool),
getToolCallParametersString(tool),
].join("\n\n");
});
return `
Environment: node
# Tool Instructions
- Never use ipython, always use javascript in node
Cutting Knowledge Date: December 2023
Today Date: ${new Date().toLocaleString("en-US", { year: "numeric", month: "long" })}
You have access to the following functions:
${customToolParams}
Think very carefully before calling functions.
If a you choose to call a function ONLY reply in the following json format:
{
"name": function_name,
"parameters": parameters,
}
where
{
"name": function_name,
"parameters": parameters, => a JSON dict with the function argument name as key and function argument value as value.
}
Here is an example,
{
"name": "example_function_name",
"parameters": {"example_name": "example_value"}
}
Reminder:
- Function calls MUST follow the specified format
- Required parameters MUST be specified
- Only call one function at a time
- Put the entire function call reply on one line
- Always add your sources when using search results to answer the user query
`;
};
export const mapChatRoleToMetaRole = (
role: ChatMessage["role"],
): MetaMessage["role"] => {
if (role === "assistant") return "assistant";
if (role === "user") return "user";
return "system";
};
export const mapChatMessagesToMetaMessages = <
T extends ChatMessage<ToolCallLLMMessageOptions>,
>(
messages: T[],
): MetaMessage[] => {
return messages.flatMap((msg) => {
if (msg.options && "toolCall" in msg.options) {
return msg.options.toolCall.map((call) => ({
role: "assistant",
content: JSON.stringify({
id: call.id,
name: call.name,
parameters: call.input,
}),
}));
}
if (msg.options && "toolResult" in msg.options) {
return {
role: "ipython",
content: JSON.stringify(msg.options.toolResult),
};
}
let content: string = "";
if (typeof msg.content === "string") {
content = msg.content;
} else if (msg.content.length) {
content = (msg.content[0] as MessageContentTextDetail).text;
}
return {
role: mapChatRoleToMetaRole(msg.role),
content,
};
});
};
/**
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3
*/
export const mapChatMessagesToMetaLlama3Messages = <T extends ChatMessage>(
messages: T[],
tools?: BaseTool[],
): string => {
const parts: string[] = [];
if (tools?.length) {
parts.push(
"<|begin_of_text|>",
"<|start_header_id|>system<|end_header_id|>",
getToolsPrompt(tools),
"<|eot_id|>",
);
}
const mapped = mapChatMessagesToMetaMessages(messages).map((message) => {
return [
"<|start_header_id|>",
message.role,
"<|end_header_id|>",
message.content,
"<|eot_id|>",
].join("\n");
});
parts.push(
"<|begin_of_text|>",
...mapped,
"<|start_header_id|>assistant<|end_header_id|>",
);
return parts.join("\n");
};
/**
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2
*/
export const mapChatMessagesToMetaLlama2Messages = <T extends ChatMessage>(
messages: T[],
): string => {
const mapped = mapChatMessagesToMetaMessages(messages);
let output = "<s>";
let insideInst = false;
let needsStartAgain = false;
for (const message of mapped) {
if (needsStartAgain) {
output += "<s>";
needsStartAgain = false;
}
const text = message.content;
if (message.role === "system") {
if (!insideInst) {
output += "[INST] ";
insideInst = true;
}
output += `<<SYS>>\n${text}\n<</SYS>>\n`;
} else if (message.role === "user") {
output += text;
if (insideInst) {
output += " [/INST]";
insideInst = false;
}
} else if (message.role === "assistant") {
if (insideInst) {
output += " [/INST]";
insideInst = false;
}
output += ` ${text} </s>\n`;
needsStartAgain = true;
}
}
return output;
};
@@ -23,6 +23,7 @@ export type BedrockChatStreamResponse = AsyncIterable<
export abstract class Provider<ProviderStreamEvent extends {} = {}> {
abstract getTextFromResponse(response: Record<string, any>): string;
// Return tool calls from none streaming calls
abstract getToolsFromResponse<T extends {} = {}>(
response: Record<string, any>,
): T[];
@@ -1,9 +0,0 @@
import { Provider } from "../provider";
import { AnthropicProvider } from "./anthropic";
import { MetaProvider } from "./meta";
// Other providers should go here
export const PROVIDERS: { [key: string]: Provider } = {
anthropic: new AnthropicProvider(),
meta: new MetaProvider(),
};
@@ -1,69 +0,0 @@
import type {
InvokeModelCommandInput,
InvokeModelWithResponseStreamCommandInput,
} from "@aws-sdk/client-bedrock-runtime";
import type { ChatMessage, LLMMetadata } from "@llamaindex/core/llms";
import type { MetaNoneStreamingResponse, MetaStreamEvent } from "../types";
import {
mapChatMessagesToMetaLlama2Messages,
mapChatMessagesToMetaLlama3Messages,
toUtf8,
} from "../utils";
import { Provider } from "../provider";
export class MetaProvider extends Provider<MetaStreamEvent> {
constructor() {
super();
}
getResultFromResponse(
response: Record<string, any>,
): MetaNoneStreamingResponse {
return JSON.parse(toUtf8(response.body));
}
getToolsFromResponse(_response: Record<string, any>): never {
throw new Error("Not supported by this provider.");
}
getTextFromResponse(response: Record<string, any>): string {
const result = this.getResultFromResponse(response);
return result.generation;
}
getTextFromStreamResponse(response: Record<string, any>): string {
const event = this.getStreamingEventResponse(response);
if (event?.generation) {
return event.generation;
}
return "";
}
getRequestBody<T extends ChatMessage>(
metadata: LLMMetadata,
messages: T[],
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput {
let promptFunction: (messages: ChatMessage[]) => string;
if (metadata.model.startsWith("meta.llama3")) {
promptFunction = mapChatMessagesToMetaLlama3Messages;
} else if (metadata.model.startsWith("meta.llama2")) {
promptFunction = mapChatMessagesToMetaLlama2Messages;
} else {
throw new Error(`Meta model ${metadata.model} is not supported`);
}
return {
modelId: metadata.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({
prompt: promptFunction(messages),
max_gen_len: metadata.maxTokens,
temperature: metadata.temperature,
top_p: metadata.topP,
}),
};
}
}
+1 -155
View File
@@ -1,165 +1,11 @@
type Usage = {
input_tokens: number;
output_tokens: number;
};
type Message = {
id: string;
type: string;
role: string;
content: string[];
model: string;
stop_reason: string | null;
stop_sequence: string | null;
usage: Usage;
};
export type ToolBlock = {
id: string;
input: unknown;
name: string;
type: "tool_use";
};
export type TextBlock = {
type: "text";
text: string;
};
type ContentBlockStart = {
type: "content_block_start";
index: number;
content_block: ToolBlock | TextBlock;
};
type Delta =
| {
type: "text_delta";
text: string;
}
| {
type: "input_json_delta";
partial_json: string;
};
type ContentBlockDelta = {
type: "content_block_delta";
index: number;
delta: Delta;
};
type ContentBlockStop = {
type: "content_block_stop";
index: number;
};
type MessageDelta = {
type: "message_delta";
delta: {
stop_reason: string;
stop_sequence: string | null;
};
usage: Usage;
};
type InvocationMetrics = {
export type InvocationMetrics = {
inputTokenCount: number;
outputTokenCount: number;
invocationLatency: number;
firstByteLatency: number;
};
type MessageStop = {
type: "message_stop";
"amazon-bedrock-invocationMetrics": InvocationMetrics;
};
export type ToolChoice =
| { type: "any" }
| { type: "auto" }
| { type: "tool"; name: string };
export type AnthropicStreamEvent =
| { type: "message_start"; message: Message }
| ContentBlockStart
| ContentBlockDelta
| ContentBlockStop
| MessageDelta
| MessageStop;
export type AnthropicContent =
| AnthropicTextContent
| AnthropicImageContent
| AnthropicToolContent
| AnthropicToolResultContent;
export type MetaTextContent = string;
export type AnthropicTextContent = {
type: "text";
text: string;
};
export type AnthropicToolContent = {
type: "tool_use";
id: string;
name: string;
input: Record<string, unknown>;
};
export type AnthropicToolResultContent = {
type: "tool_result";
tool_use_id: string;
content: string;
};
export type AnthropicMediaTypes =
| "image/jpeg"
| "image/png"
| "image/webp"
| "image/gif";
export type AnthropicImageSource = {
type: "base64";
media_type: AnthropicMediaTypes;
data: string; // base64 encoded image bytes
};
export type AnthropicImageContent = {
type: "image";
source: AnthropicImageSource;
};
export type AnthropicMessage = {
role: "user" | "assistant";
content: AnthropicContent[];
};
export type MetaMessage = {
role: "user" | "assistant" | "system";
content: MetaTextContent;
};
export type AnthropicNoneStreamingResponse = {
id: string;
type: "message";
role: "assistant";
content: AnthropicContent[];
model: string;
stop_reason: "end_turn" | "max_tokens" | "stop_sequence";
stop_sequence?: string;
usage: { input_tokens: number; output_tokens: number };
};
type MetaResponse = {
generation: string;
prompt_token_count: number;
generation_token_count: number;
stop_reason: "stop" | "length";
};
export type MetaStreamEvent = MetaResponse & {
"amazon-bedrock-invocationMetrics": InvocationMetrics;
};
export type MetaNoneStreamingResponse = MetaResponse;
-266
View File
@@ -1,28 +1,7 @@
import type { JSONObject } from "@llamaindex/core/global";
import type {
BaseTool,
ChatMessage,
MessageContent,
MessageContentDetail,
MessageContentTextDetail,
ToolCallLLMMessageOptions,
ToolMetadata,
} from "@llamaindex/core/llms";
import type {
AnthropicContent,
AnthropicImageContent,
AnthropicMediaTypes,
AnthropicMessage,
AnthropicTextContent,
MetaMessage,
} from "./types.js";
const ACCEPTED_IMAGE_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
];
export const mapMessageContentToMessageContentDetails = (
content: MessageContent,
@@ -30,250 +9,5 @@ export const mapMessageContentToMessageContentDetails = (
return Array.isArray(content) ? content : [{ type: "text", text: content }];
};
export const mergeNeighboringSameRoleMessages = (
messages: AnthropicMessage[],
): AnthropicMessage[] => {
return messages.reduce(
(result: AnthropicMessage[], current: AnthropicMessage, index: number) => {
if (index > 0 && messages[index - 1].role === current.role) {
result[result.length - 1].content = [
...result[result.length - 1].content,
...current.content,
];
} else {
result.push(current);
}
return result;
},
[],
);
};
export const mapMessageContentDetailToAnthropicContent = <
T extends MessageContentDetail,
>(
detail: T,
): AnthropicContent => {
let content: AnthropicContent;
if (detail.type === "text") {
content = mapTextContent(detail.text);
} else if (detail.type === "image_url") {
content = mapImageContent(detail.image_url.url);
} else {
throw new Error("Unsupported content detail type");
}
return content;
};
export const mapMessageContentToAnthropicContent = <T extends MessageContent>(
content: T,
): AnthropicContent[] => {
return mapMessageContentToMessageContentDetails(content).map(
mapMessageContentDetailToAnthropicContent,
);
};
type AnthropicTool = {
name: string;
description: string;
input_schema: ToolMetadata["parameters"];
};
export const mapBaseToolsToAnthropicTools = (
tools?: BaseTool[],
): AnthropicTool[] => {
if (!tools) return [];
return tools.map((tool: BaseTool) => {
const {
metadata: { parameters, ...options },
} = tool;
return {
...options,
input_schema: parameters,
};
});
};
export const mapChatMessagesToAnthropicMessages = <
T extends ChatMessage<ToolCallLLMMessageOptions>,
>(
messages: T[],
): AnthropicMessage[] => {
const mapped = messages
.flatMap((msg: T): AnthropicMessage[] => {
if (msg.options && "toolCall" in msg.options) {
return [
{
role: "assistant",
content: msg.options.toolCall.map((call) => ({
type: "tool_use",
id: call.id,
name: call.name,
input: call.input as JSONObject,
})),
},
];
}
if (msg.options && "toolResult" in msg.options) {
return [
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: msg.options.toolResult.id,
content: msg.options.toolResult.result,
},
],
},
];
}
return mapMessageContentToMessageContentDetails(msg.content).map(
(detail: MessageContentDetail): AnthropicMessage => {
const content = mapMessageContentDetailToAnthropicContent(detail);
return {
role: msg.role === "assistant" ? "assistant" : "user",
content: [content],
};
},
);
})
.filter((message: AnthropicMessage) => {
const content = message.content[0];
if (content.type === "text" && !content.text) return false;
if (content.type === "image" && !content.source.data) return false;
return true;
});
return mergeNeighboringSameRoleMessages(mapped);
};
export const mapChatMessagesToMetaMessages = <T extends ChatMessage>(
messages: T[],
): MetaMessage[] => {
return messages.map((msg) => {
let content: string = "";
if (typeof msg.content === "string") {
content = msg.content;
} else if (msg.content.length) {
content = (msg.content[0] as MessageContentTextDetail).text;
}
return {
role:
msg.role === "assistant"
? "assistant"
: msg.role === "user"
? "user"
: "system",
content,
};
});
};
/**
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3
*/
export const mapChatMessagesToMetaLlama3Messages = <T extends ChatMessage>(
messages: T[],
): string => {
const mapped = mapChatMessagesToMetaMessages(messages).map((message) => {
const text = message.content;
return `<|start_header_id|>${message.role}<|end_header_id|>\n${text}\n<|eot_id|>\n`;
});
return (
"<|begin_of_text|>" +
mapped.join("\n") +
"\n<|start_header_id|>assistant<|end_header_id|>\n"
);
};
/**
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2
*/
export const mapChatMessagesToMetaLlama2Messages = <T extends ChatMessage>(
messages: T[],
): string => {
const mapped = mapChatMessagesToMetaMessages(messages);
let output = "<s>";
let insideInst = false;
let needsStartAgain = false;
for (const message of mapped) {
if (needsStartAgain) {
output += "<s>";
needsStartAgain = false;
}
const text = message.content;
if (message.role === "system") {
if (!insideInst) {
output += "[INST] ";
insideInst = true;
}
output += `<<SYS>>\n${text}\n<</SYS>>\n`;
} else if (message.role === "user") {
output += text;
if (insideInst) {
output += " [/INST]";
insideInst = false;
}
} else if (message.role === "assistant") {
if (insideInst) {
output += " [/INST]";
insideInst = false;
}
output += ` ${text} </s>\n`;
needsStartAgain = true;
}
}
return output;
};
export const mapTextContent = (text: string): AnthropicTextContent => {
return { type: "text", text };
};
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,
};
};
export const mapImageContent = (imageUrl: string): AnthropicImageContent => {
if (!imageUrl.startsWith("data:"))
throw new Error(
"For Anthropic please only use base64 data url, e.g.: data:image/jpeg;base64,SGVsbG8sIFdvcmxkIQ==",
);
const { mimeType, base64: data } = extractDataUrlComponents(imageUrl);
if (!ACCEPTED_IMAGE_MIME_TYPES.includes(mimeType))
throw new Error(
`Anthropic only accepts the following mimeTypes: ${ACCEPTED_IMAGE_MIME_TYPES.join("\n")}`,
);
return {
type: "image",
source: {
type: "base64",
media_type: mimeType as AnthropicMediaTypes,
data,
},
};
};
export const toUtf8 = (input: Uint8Array): string =>
new TextDecoder("utf-8").decode(input);
+26
View File
@@ -1,5 +1,31 @@
# @llamaindex/core
## 0.1.9
### Patch Changes
- e27e7dd: chore: bump `natural` to 8.0.1
## 0.1.8
### Patch Changes
- 58abc57: fix: align version
- Updated dependencies [58abc57]
- @llamaindex/env@0.1.9
## 0.1.7
### Patch Changes
- 04b2f8e: Fix issue with metadata included after sentence splitter
## 0.1.6
### Patch Changes
- 0452af9: fix: handling errors in splitBySentenceTokenizer
## 0.1.5
### Patch Changes
+3 -3
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/core",
"type": "module",
"version": "0.1.5",
"version": "0.1.9",
"description": "LlamaIndex Core Module",
"exports": {
"./node-parser": {
@@ -131,8 +131,8 @@
},
"devDependencies": {
"ajv": "^8.16.0",
"bunchee": "5.3.0-beta.0",
"natural": "^7.1.0"
"bunchee": "5.3.1",
"natural": "^8.0.1"
},
"dependencies": {
"@llamaindex/env": "workspace:*",
+1 -1
View File
@@ -140,7 +140,7 @@ export abstract class MetadataAwareTextSplitter extends TextSplitter {
return nodes.reduce<TextNode[]>((allNodes, node) => {
const metadataStr = this.getMetadataString(node);
const splits = this.splitTextMetadataAware(
node.getContent(MetadataMode.ALL),
node.getContent(MetadataMode.NONE),
metadataStr,
);
return allNodes.concat(buildNodeFromSplits(splits, node));
File diff suppressed because it is too large Load Diff
@@ -1,4 +1,5 @@
declare class SentenceTokenizer {
constructor(abbreviations?: string[]);
tokenize(text: string): string[];
}
@@ -0,0 +1,222 @@
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) =>
function __require() {
return (
mod ||
(0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod),
mod.exports
);
};
// lib/natural/tokenizers/tokenizer.js
var require_tokenizer = __commonJS({
"lib/natural/tokenizers/tokenizer.js"(exports, module) {
"use strict";
var Tokenizer = class {
trim(array) {
while (array[array.length - 1] === "") {
array.pop();
}
while (array[0] === "") {
array.shift();
}
return array;
}
};
module.exports = Tokenizer;
},
});
// lib/natural/tokenizers/sentence_tokenizer.js
var require_sentence_tokenizer = __commonJS({
"lib/natural/tokenizers/sentence_tokenizer.js"(exports, module) {
var Tokenizer = require_tokenizer();
var NUM = "NUMBER";
var DELIM = "DELIM";
var URI = "URI";
var ABBREV = "ABBREV";
var DEBUG = false;
function generateUniqueCode(base, index) {
return `{{${base}_${index}}}`;
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
var SentenceTokenizer = class extends Tokenizer {
constructor(abbreviations) {
super();
if (abbreviations) {
this.abbreviations = abbreviations;
} else {
this.abbreviations = [];
}
this.replacementMap = null;
this.replacementCounter = 0;
}
replaceUrisWithPlaceholders(text) {
const urlPattern =
/(https?:\/\/\S+|www\.\S+|ftp:\/\/\S+|(mailto:)?[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|file:\/\/\S+)/gi;
const modifiedText = text.replace(urlPattern, (match) => {
const placeholder = generateUniqueCode(
URI,
this.replacementCounter++,
);
this.replacementMap.set(placeholder, match);
return placeholder;
});
return modifiedText;
}
replaceAbbreviations(text) {
if (this.abbreviations.length === 0) {
return text;
}
const pattern = new RegExp(
`(${this.abbreviations.map((abbrev) => escapeRegExp(abbrev)).join("|")})`,
"gi",
);
const replacedText = text.replace(pattern, (match) => {
const code = generateUniqueCode(ABBREV, this.replacementCounter++);
this.replacementMap.set(code, match);
return code;
});
return replacedText;
}
replaceDelimitersWithPlaceholders(text) {
const delimiterPattern = /([.?!… ]*)([.?!…])(["'”’)}\]]?)/g;
const modifiedText = text.replace(
delimiterPattern,
(match, p1, p2, p3) => {
const placeholder = generateUniqueCode(
DELIM,
this.replacementCounter++,
);
this.delimiterMap.set(placeholder, p1 + p2 + p3);
return placeholder;
},
);
return modifiedText;
}
splitOnPlaceholders(text, placeholders) {
if (this.delimiterMap.size === 0) {
return [text];
}
const keys = Array.from(this.delimiterMap.keys());
const pattern = new RegExp(`(${keys.map(escapeRegExp).join("|")})`);
const parts = text.split(pattern);
const sentences = [];
for (let i = 0; i < parts.length; i += 2) {
const sentence = parts[i];
const placeholder = parts[i + 1] || "";
sentences.push(sentence + placeholder);
}
return sentences;
}
replaceNumbersWithCode(text) {
const numberPattern = /\b\d{1,3}(?:,\d{3})*(?:\.\d+)?\b/g;
const replacedText = text.replace(numberPattern, (match) => {
const code = generateUniqueCode(NUM, this.replacementCounter++);
this.replacementMap.set(code, match);
return code;
});
return replacedText;
}
revertReplacements(text) {
let originalText = text;
for (const [
placeholder,
replacement,
] of this.replacementMap.entries()) {
const pattern = new RegExp(escapeRegExp(placeholder), "g");
originalText = originalText.replace(pattern, replacement);
}
return originalText;
}
revertDelimiters(text) {
let originalText = text;
for (const [placeholder, replacement] of this.delimiterMap.entries()) {
const pattern = new RegExp(escapeRegExp(placeholder), "g");
originalText = originalText.replace(pattern, replacement);
}
return originalText;
}
tokenize(text) {
this.replacementCounter = 0;
this.replacementMap = /* @__PURE__ */ new Map();
this.delimiterMap = /* @__PURE__ */ new Map();
DEBUG &&
console.log(
"---Start of sentence tokenization-----------------------",
);
DEBUG && console.log("Original input: >>>" + text + "<<<");
const result1 = this.replaceAbbreviations(text);
DEBUG &&
console.log(
"Phase 1: replacing abbreviations: " + JSON.stringify(result1),
);
const result2 = this.replaceUrisWithPlaceholders(result1);
DEBUG &&
console.log("Phase 2: replacing URIs: " + JSON.stringify(result2));
const result3 = this.replaceNumbersWithCode(result2);
DEBUG &&
console.log(
"Phase 3: replacing numbers with placeholders: " +
JSON.stringify(result3),
);
const result4 = this.replaceDelimitersWithPlaceholders(result3);
DEBUG &&
console.log(
"Phase 4: replacing delimiters with placeholders: " +
JSON.stringify(result4),
);
const sentences = this.splitOnPlaceholders(result4);
DEBUG &&
console.log(
"Phase 5: splitting into sentences on placeholders: " +
JSON.stringify(sentences),
);
const newSentences = sentences.map((s) => {
const s1 = this.revertReplacements(s);
return this.revertDelimiters(s1);
});
DEBUG &&
console.log(
"Phase 6: replacing back abbreviations, URIs, numbers and delimiters: " +
JSON.stringify(newSentences),
);
const trimmedSentences = this.trim(newSentences);
DEBUG &&
console.log(
"Phase 7: trimming array of empty sentences: " +
JSON.stringify(trimmedSentences),
);
const trimmedSentences2 = trimmedSentences.map((sent) => sent.trim());
DEBUG &&
console.log(
"Phase 8: trimming sentences from surrounding whitespace: " +
JSON.stringify(trimmedSentences2),
);
DEBUG &&
console.log(
"---End of sentence tokenization--------------------------",
);
DEBUG &&
console.log(
"---Replacement map---------------------------------------",
);
DEBUG && console.log([...this.replacementMap.entries()]);
DEBUG &&
console.log(
"---Delimiter map-----------------------------------------",
);
DEBUG && console.log([...this.delimiterMap.entries()]);
DEBUG &&
console.log(
"---------------------------------------------------------",
);
return trimmedSentences2;
}
};
module.exports = SentenceTokenizer;
},
});
export default require_sentence_tokenizer();
+14 -4
View File
@@ -1,5 +1,5 @@
import type { TextSplitter } from "./base";
import SentenceTokenizerNew from "./sentence-tokenizer-parser.js";
import SentenceTokenizer from "./sentence_tokenizer";
export type TextSplitterFn = (text: string) => string[];
@@ -31,15 +31,25 @@ export const splitByChar = (): TextSplitterFn => {
return (text: string) => text.split("");
};
let sentenceTokenizer: SentenceTokenizerNew | null = null;
let sentenceTokenizer: SentenceTokenizer | null = null;
export const splitBySentenceTokenizer = (): TextSplitterFn => {
if (!sentenceTokenizer) {
sentenceTokenizer = new SentenceTokenizerNew();
sentenceTokenizer = new SentenceTokenizer([
"i.e.",
"etc.",
"vs.",
"Inc.",
"A.S.A.P.",
]);
}
const tokenizer = sentenceTokenizer;
return (text: string) => {
return tokenizer.tokenize(text);
try {
return tokenizer.tokenize(text);
} catch {
return [text];
}
};
};
+1 -1
View File
@@ -1,4 +1,4 @@
export * from "./node";
export { TransformComponent } from "./type";
export { FileReader, TransformComponent, type BaseReader } from "./type";
export { EngineResponse } from "./type/engineresponse";
export * from "./zod";
+36 -2
View File
@@ -1,5 +1,5 @@
import { randomUUID } from "@llamaindex/env";
import type { BaseNode } from "./node";
import { fs, path, randomUUID } from "@llamaindex/env";
import type { BaseNode, Document } from "./node";
interface TransformComponentSignature {
<Options extends Record<string, unknown>>(
@@ -28,3 +28,37 @@ export class TransformComponent {
return transform;
}
}
/**
* A reader takes imports data into Document objects.
*/
export interface BaseReader {
loadData(...args: unknown[]): Promise<Document[]>;
}
/**
* A FileReader takes file paths and imports data into Document objects.
*/
export abstract class FileReader implements BaseReader {
abstract loadDataAsContent(
fileContent: Uint8Array,
fileName?: string,
): Promise<Document[]>;
async loadData(filePath: string): Promise<Document[]> {
const fileContent = await fs.readFile(filePath);
const fileName = path.basename(filePath);
const docs = await this.loadDataAsContent(fileContent, fileName);
docs.forEach(FileReader.addMetaData(filePath));
return docs;
}
static addMetaData(filePath: string) {
return (doc: Document, index: number) => {
// generate id as loadDataAsContent is only responsible for the content
doc.id_ = `${filePath}_${index + 1}`;
doc.metadata["file_path"] = path.resolve(filePath);
doc.metadata["file_name"] = path.basename(filePath);
};
}
}
@@ -1,7 +1,10 @@
import { SentenceSplitter } from "@llamaindex/core/node-parser";
import {
SentenceSplitter,
splitBySentenceTokenizer,
} from "@llamaindex/core/node-parser";
import { describe, expect, test } from "vitest";
describe("SentenceSplitter", () => {
describe("sentence splitter", () => {
test("initializes", () => {
const sentenceSplitter = new SentenceSplitter();
expect(sentenceSplitter).toBeDefined();
@@ -105,4 +108,11 @@ describe("SentenceSplitter", () => {
"因为他照了人类,连我都在内。",
]);
});
test("issue 1087 - edge case when input with brackets", () => {
const text =
"A card must be of uniform thickness and made of unfolded and uncreased paper or cardstock of approximately the quality and weight of a stamped card (i.e., a card available from USPS).";
const split = splitBySentenceTokenizer();
expect(split(text)).toEqual([text]);
});
});
+6
View File
@@ -1,5 +1,11 @@
# @llamaindex/env
## 0.1.9
### Patch Changes
- 58abc57: fix: align version
## 0.1.8
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/env",
"description": "environment wrapper, supports all JS environment including node, deno, bun, edge runtime, and cloudflare worker",
"version": "0.1.8",
"version": "0.1.9",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
+74
View File
@@ -1,5 +1,79 @@
# @llamaindex/experimental
## 0.0.70
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.0.69
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.0.68
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.0.67
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.0.66
### Patch Changes
- 58abc57: fix: align version
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.0.65
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.0.64
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.0.63
### Patch Changes
- llamaindex@0.5.13
## 0.0.62
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.0.61
### Patch Changes
- llamaindex@0.5.11
## 0.0.60
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.60",
"version": "0.0.70",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
@@ -162,18 +162,18 @@ export class JSONQueryEngine implements QueryEngine {
const schema = this.getSchemaContext();
const jsonPathResponseStr = await this.serviceContext.llm.complete({
const { text: jsonPathResponse } = await this.serviceContext.llm.complete({
prompt: this.jsonPathPrompt({ query, schema }),
});
if (this.verbose) {
console.log(
`> JSONPath Instructions:\n\`\`\`\n${jsonPathResponseStr}\n\`\`\`\n`,
`> JSONPath Instructions:\n\`\`\`\n${jsonPathResponse}\n\`\`\`\n`,
);
}
const jsonPathOutput = await this.outputProcessor({
llmOutput: jsonPathResponseStr.text,
llmOutput: jsonPathResponse,
jsonValue: this.jsonValue,
});
@@ -188,7 +188,7 @@ export class JSONQueryEngine implements QueryEngine {
prompt: this.responseSynthesisPrompt({
query,
jsonSchema: schema,
jsonPath: jsonPathResponseStr.text,
jsonPath: jsonPathResponse,
jsonPathValue: JSON.stringify(jsonPathOutput),
}),
});
@@ -199,7 +199,7 @@ export class JSONQueryEngine implements QueryEngine {
}
const responseMetadata = {
jsonPathResponseStr,
jsonPathResponse,
};
const response = EngineResponse.fromResponse(responseStr, false);
+75
View File
@@ -1,5 +1,80 @@
# llamaindex
## 0.5.20
### Patch Changes
- d9d6c56: Add support for MetadataFilters for PostgreSQL
- 22ff486: Add tiktoken WASM to withLlamaIndex
- eed0b04: fix: use LLM metadata mode for generating context of ContextChatEngine
## 0.5.19
### Patch Changes
- fcbf183: implement llamacloud file service
## 0.5.18
### Patch Changes
- 8b66cf4: feat: support organization id in llamacloud index
- Updated dependencies [e27e7dd]
- @llamaindex/core@0.1.9
## 0.5.17
### Patch Changes
- c654398: Implement Weaviate Vector Store in TS
## 0.5.16
### Patch Changes
- 58abc57: fix: align version
- Updated dependencies [58abc57]
- @llamaindex/cloud@0.2.2
- @llamaindex/core@0.1.8
- @llamaindex/env@0.1.9
## 0.5.15
### Patch Changes
- 01c184c: Add is_empty operator for filtering vector store
- 07a275f: chore: bump openai
## 0.5.14
### Patch Changes
- c825a2f: Add gpt-4o-mini to Azure. Add 2024-06-01 API version for Azure
## 0.5.13
### Patch Changes
- Updated dependencies [04b2f8e]
- @llamaindex/core@0.1.7
## 0.5.12
### Patch Changes
- 345300f: feat: add splitByPage mode to LlamaParseReader
- da5cfc4: Add metadatafilter options to retriever constructors
- da5cfc4: Fix system prompt not used in ContextChatEngine
- Updated dependencies [0452af9]
- @llamaindex/core@0.1.6
## 0.5.11
### Patch Changes
- Updated dependencies [1f680d7]
- @llamaindex/cloud@0.2.1
## 0.5.10
### Patch Changes
@@ -1,5 +1,78 @@
# @llamaindex/cloudflare-worker-agent-test
## 0.0.54
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.0.53
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.0.52
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.0.51
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.0.50
### Patch Changes
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.0.49
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.0.48
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.0.47
### Patch Changes
- llamaindex@0.5.13
## 0.0.46
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.0.45
### Patch Changes
- llamaindex@0.5.11
## 0.0.44
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloudflare-worker-agent-test",
"version": "0.0.44",
"version": "0.0.54",
"type": "module",
"private": true,
"scripts": {
@@ -1,5 +1,78 @@
# @llamaindex/next-agent-test
## 0.1.54
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.1.53
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.1.52
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.1.51
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.1.50
### Patch Changes
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.1.49
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.1.48
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.1.47
### Patch Changes
- llamaindex@0.5.13
## 0.1.46
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.1.45
### Patch Changes
- llamaindex@0.5.11
## 0.1.44
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-agent-test",
"version": "0.1.44",
"version": "0.1.54",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,78 @@
# test-edge-runtime
## 0.1.53
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.1.52
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.1.51
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.1.50
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.1.49
### Patch Changes
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.1.48
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.1.47
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.1.46
### Patch Changes
- llamaindex@0.5.13
## 0.1.45
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.1.44
### Patch Changes
- llamaindex@0.5.11
## 0.1.43
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/nextjs-edge-runtime-test",
"version": "0.1.43",
"version": "0.1.53",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,78 @@
# @llamaindex/next-node-runtime
## 0.0.35
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.0.34
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.0.33
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.0.32
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.0.31
### Patch Changes
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.0.30
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.0.29
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.0.28
### Patch Changes
- llamaindex@0.5.13
## 0.0.27
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.0.26
### Patch Changes
- llamaindex@0.5.11
## 0.0.25
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-node-runtime-test",
"version": "0.0.25",
"version": "0.0.35",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,78 @@
# @llamaindex/waku-query-engine-test
## 0.0.54
### Patch Changes
- Updated dependencies [d9d6c56]
- Updated dependencies [22ff486]
- Updated dependencies [eed0b04]
- llamaindex@0.5.20
## 0.0.53
### Patch Changes
- Updated dependencies [fcbf183]
- llamaindex@0.5.19
## 0.0.52
### Patch Changes
- Updated dependencies [8b66cf4]
- llamaindex@0.5.18
## 0.0.51
### Patch Changes
- Updated dependencies [c654398]
- llamaindex@0.5.17
## 0.0.50
### Patch Changes
- Updated dependencies [58abc57]
- llamaindex@0.5.16
## 0.0.49
### Patch Changes
- Updated dependencies [01c184c]
- Updated dependencies [07a275f]
- llamaindex@0.5.15
## 0.0.48
### Patch Changes
- Updated dependencies [c825a2f]
- llamaindex@0.5.14
## 0.0.47
### Patch Changes
- llamaindex@0.5.13
## 0.0.46
### Patch Changes
- Updated dependencies [345300f]
- Updated dependencies [da5cfc4]
- Updated dependencies [da5cfc4]
- llamaindex@0.5.12
## 0.0.45
### Patch Changes
- llamaindex@0.5.11
## 0.0.44
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/waku-query-engine-test",
"version": "0.0.44",
"version": "0.0.54",
"type": "module",
"private": true,
"scripts": {
+3 -2
View File
@@ -1,6 +1,6 @@
{
"name": "llamaindex",
"version": "0.5.10",
"version": "0.5.20",
"license": "MIT",
"type": "module",
"keywords": [
@@ -55,7 +55,7 @@
"md-utils-ts": "^2.0.0",
"mongodb": "^6.7.0",
"notion-md-crawler": "^1.0.0",
"openai": "^4.52.5",
"openai": "^4.55.3",
"papaparse": "^5.4.1",
"pathe": "^1.1.2",
"pg": "^8.12.0",
@@ -65,6 +65,7 @@
"string-strip-html": "^13.4.8",
"tiktoken": "^1.0.15",
"unpdf": "^0.11.0",
"weaviate-client": "^3.1.4",
"wikipedia": "^2.1.2",
"wink-nlp": "^2.3.0",
"zod": "^3.23.8"
@@ -0,0 +1,99 @@
import {
FilesService,
PipelinesService,
ProjectsService,
} from "@llamaindex/cloud/api";
import { initService } from "./utils.js";
export class LLamaCloudFileService {
/**
* Get list of projects, each project contains a list of pipelines
*/
public static async getAllProjectsWithPipelines() {
initService();
try {
const projects = await ProjectsService.listProjectsApiV1ProjectsGet();
const pipelines =
await PipelinesService.searchPipelinesApiV1PipelinesGet();
return projects.map((project) => ({
...project,
pipelines: pipelines.filter((p) => p.project_id === project.id),
}));
} catch (error) {
console.error("Error listing projects and pipelines:", error);
return [];
}
}
/**
* Upload a file to a pipeline in LlamaCloud
*/
public static async addFileToPipeline(
projectId: string,
pipelineId: string,
uploadFile: File | Blob,
customMetadata: Record<string, any> = {},
) {
initService();
const file = await FilesService.uploadFileApiV1FilesPost({
projectId,
formData: {
upload_file: uploadFile,
},
});
const files = [
{
file_id: file.id,
custom_metadata: { file_id: file.id, ...customMetadata },
},
];
await PipelinesService.addFilesToPipelineApiV1PipelinesPipelineIdFilesPut({
pipelineId,
requestBody: files,
});
// Wait 2s for the file to be processed
const maxAttempts = 20;
let attempt = 0;
while (attempt < maxAttempts) {
const result =
await PipelinesService.getPipelineFileStatusApiV1PipelinesPipelineIdFilesFileIdStatusGet(
{
pipelineId,
fileId: file.id,
},
);
if (result.status === "ERROR") {
throw new Error(`File processing failed: ${JSON.stringify(result)}`);
}
if (result.status === "SUCCESS") {
// File is ingested - return the file id
return file.id;
}
attempt += 1;
await new Promise((resolve) => setTimeout(resolve, 100)); // Sleep for 100ms
}
throw new Error(
`File processing did not complete after ${maxAttempts} attempts.`,
);
}
/**
* Get download URL for a file in LlamaCloud
*/
public static async getFileUrl(pipelineId: string, filename: string) {
initService();
const allPipelineFiles =
await PipelinesService.listPipelineFilesApiV1PipelinesPipelineIdFilesGet({
pipelineId,
});
const file = allPipelineFiles.find((file) => file.name === filename);
if (!file?.file_id) return null;
const fileContent =
await FilesService.readFileContentApiV1FilesIdContentGet({
id: file.file_id,
projectId: file.project_id,
});
return fileContent.url;
}
}
@@ -8,7 +8,7 @@ import type { CloudRetrieveParams } from "./LlamaCloudRetriever.js";
import { LlamaCloudRetriever } from "./LlamaCloudRetriever.js";
import { getPipelineCreate } from "./config.js";
import type { CloudConstructorParams } from "./constants.js";
import { getAppBaseUrl, initService } from "./utils.js";
import { getAppBaseUrl, getProjectId, initService } from "./utils.js";
import { PipelinesService, ProjectsService } from "@llamaindex/cloud/api";
import { SentenceSplitter } from "@llamaindex/core/node-parser";
@@ -132,18 +132,28 @@ export class LlamaCloudIndex {
await this.waitForPipelineIngestion(verbose, raiseOnError);
}
private async getPipelineId(
name: string,
projectName: string,
public async getPipelineId(
name?: string,
projectName?: string,
): Promise<string> {
const pipelines = await PipelinesService.searchPipelinesApiV1PipelinesGet({
projectName,
pipelineName: name,
projectId: await this.getProjectId(projectName),
pipelineName: name ?? this.params.name,
});
return pipelines[0].id;
}
public async getProjectId(
projectName?: string,
organizationId?: string,
): Promise<string> {
return await getProjectId(
projectName ?? this.params.projectName,
organizationId ?? this.params.organizationId,
);
}
static async fromDocuments(
params: {
documents: Document[];
@@ -168,6 +178,7 @@ export class LlamaCloudIndex {
});
const project = await ProjectsService.upsertProjectApiV1ProjectsPut({
organizationId: params.organizationId,
requestBody: {
name: params.projectName ?? "default",
},
@@ -11,16 +11,17 @@ import { extractText, wrapEventCaller } from "@llamaindex/core/utils";
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
import type { ClientParams, CloudConstructorParams } from "./constants.js";
import { DEFAULT_PROJECT_NAME } from "./constants.js";
import { initService } from "./utils.js";
import { getProjectId, initService } from "./utils.js";
export type CloudRetrieveParams = Omit<
RetrievalParams,
"query" | "searchFilters" | "className" | "denseSimilarityTopK"
> & { similarityTopK?: number };
"query" | "search_filters" | "dense_similarity_top_k"
> & { similarityTopK?: number; filters?: MetadataFilters };
export class LlamaCloudRetriever implements BaseRetriever {
clientParams: ClientParams;
retrieveParams: CloudRetrieveParams;
organizationId?: string;
projectName: string = DEFAULT_PROJECT_NAME;
pipelineName: string;
@@ -49,6 +50,9 @@ export class LlamaCloudRetriever implements BaseRetriever {
if (params.projectName) {
this.projectName = params.projectName;
}
if (params.organizationId) {
this.organizationId = params.organizationId;
}
}
@wrapEventCaller
@@ -57,7 +61,7 @@ export class LlamaCloudRetriever implements BaseRetriever {
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
const pipelines = await PipelinesService.searchPipelinesApiV1PipelinesGet({
projectName: this.projectName,
projectId: await getProjectId(this.projectName, this.organizationId),
pipelineName: this.pipelineName,
});
@@ -84,7 +88,9 @@ export class LlamaCloudRetriever implements BaseRetriever {
requestBody: {
...this.retrieveParams,
query: extractText(query),
search_filters: preFilters as MetadataFilters,
search_filters:
this.retrieveParams.filters ?? (preFilters as MetadataFilters),
dense_similarity_top_k: this.retrieveParams.similarityTopK,
},
});
@@ -8,5 +8,6 @@ export type ClientParams = { apiKey?: string; baseUrl?: string };
export type CloudConstructorParams = {
name: string;
projectName: string;
organizationId?: string;
serviceContext?: ServiceContext;
} & ClientParams;
+1
View File
@@ -1,4 +1,5 @@
export type { CloudConstructorParams } from "./constants.js";
export { LLamaCloudFileService } from "./LLamaCloudFileService.js";
export { LlamaCloudIndex } from "./LlamaCloudIndex.js";
export {
LlamaCloudRetriever,
+29 -1
View File
@@ -1,4 +1,4 @@
import { OpenAPI } from "@llamaindex/cloud/api";
import { OpenAPI, ProjectsService } from "@llamaindex/cloud/api";
import { getEnv } from "@llamaindex/env";
import type { ClientParams } from "./constants.js";
import { DEFAULT_BASE_URL } from "./constants.js";
@@ -20,3 +20,31 @@ export function initService({ apiKey, baseUrl }: ClientParams = {}) {
);
}
}
export async function getProjectId(
projectName: string,
organizationId?: string,
): Promise<string> {
const projects = await ProjectsService.listProjectsApiV1ProjectsGet({
projectName: projectName,
organizationId: organizationId,
});
if (projects.length === 0) {
throw new Error(
`Unknown project name ${projectName}. Please confirm a managed project with this name exists.`,
);
} else if (projects.length > 1) {
throw new Error(
`Multiple projects found with name ${projectName}. Please specify organization_id.`,
);
}
const project = projects[0];
if (!project.id) {
throw new Error(`No project found with name ${projectName}`);
}
return project.id;
}
@@ -79,7 +79,7 @@ export class JinaAIEmbedding extends MultiModalEmbedding {
private async getImageInput(
image: ImageType,
): Promise<{ bytes: string } | { url: string }> {
if (isLocal(image)) {
if (isLocal(image) || image instanceof Blob) {
const base64 = await imageToDataUrl(image);
const bytes = base64.split(",")[1];
return { bytes };
@@ -4,7 +4,7 @@ import type {
MessageContent,
MessageType,
} from "@llamaindex/core/llms";
import { EngineResponse } from "@llamaindex/core/schema";
import { EngineResponse, MetadataMode } from "@llamaindex/core/schema";
import {
extractText,
streamConverter,
@@ -53,6 +53,7 @@ export class ContextChatEngine extends PromptMixin implements ChatEngine {
contextSystemPrompt: init?.contextSystemPrompt,
nodePostprocessors: init?.nodePostprocessors,
contextRole: init?.contextRole,
metadataMode: MetadataMode.LLM,
});
this.systemPrompt = init.systemPrompt;
}
@@ -126,7 +127,7 @@ export class ContextChatEngine extends PromptMixin implements ChatEngine {
if (!this.systemPrompt) return message;
return {
...message,
content: this.systemPrompt.trim() + "\n" + message.content,
content: this.systemPrompt.trim() + "\n" + extractText(message.content),
};
}
}
@@ -1,5 +1,5 @@
import type { MessageContent, MessageType } from "@llamaindex/core/llms";
import { type NodeWithScore } from "@llamaindex/core/schema";
import { MetadataMode, type NodeWithScore } from "@llamaindex/core/schema";
import type { BaseNodePostprocessor } from "../../postprocessors/index.js";
import type { ContextSystemPrompt } from "../../Prompt.js";
import { defaultContextSystemPrompt } from "../../Prompt.js";
@@ -16,12 +16,14 @@ export class DefaultContextGenerator
contextSystemPrompt: ContextSystemPrompt;
nodePostprocessors: BaseNodePostprocessor[];
contextRole: MessageType;
metadataMode?: MetadataMode;
constructor(init: {
retriever: BaseRetriever;
contextSystemPrompt?: ContextSystemPrompt;
nodePostprocessors?: BaseNodePostprocessor[];
contextRole?: MessageType;
metadataMode?: MetadataMode;
}) {
super();
@@ -30,6 +32,7 @@ export class DefaultContextGenerator
init?.contextSystemPrompt ?? defaultContextSystemPrompt;
this.nodePostprocessors = init.nodePostprocessors || [];
this.contextRole = init.contextRole ?? "system";
this.metadataMode = init.metadataMode ?? MetadataMode.NONE;
}
protected _getPrompts(): { contextSystemPrompt: ContextSystemPrompt } {
@@ -75,6 +78,8 @@ export class DefaultContextGenerator
const content = await createMessageContent(
this.contextSystemPrompt,
nodes.map((r) => r.node),
undefined,
this.metadataMode,
);
return {
@@ -133,7 +133,7 @@ export class RouterQueryEngine extends PromptMixin implements QueryEngine {
const responses: EngineResponse[] = [];
for (let i = 0; i < result.selections.length; i++) {
const engineInd = result.selections[i];
const logStr = `Selecting query engine ${engineInd}: ${result.selections[i]}.`;
const logStr = `Selecting query engine ${engineInd.index}: ${result.selections[i].index}.`;
if (this.verbose) {
console.log(logStr + "\n");
@@ -119,15 +119,15 @@ export class SubQuestionQueryEngine
return null;
}
const responseText = await queryEngine?.call?.({
const responseValue = await queryEngine?.call?.({
query: question,
});
if (!responseText) {
if (responseValue == null) {
return null;
}
const nodeText = `Sub question: ${question}\nResponse: ${responseText}`;
const nodeText = `Sub question: ${question}\nResponse: ${typeof responseValue === "string" ? responseValue : JSON.stringify(responseValue)}`;
const node = new TextNode({ text: nodeText });
return { node, score: 0 };
} catch (error) {
@@ -78,7 +78,7 @@ export class RelevancyEvaluator extends PromptMixin implements BaseEvaluator {
serviceContext: this.serviceContext,
});
const queryResponse = `Question: ${query}\nResponse: ${response}`;
const queryResponse = `Question: ${extractText(query)}\nResponse: ${response}`;
const queryEngine = index.asQueryEngine();
+2
View File
@@ -1,6 +1,8 @@
import type { AgentEndEvent, AgentStartEvent } from "./agent/types.js";
import type { RetrievalEndEvent, RetrievalStartEvent } from "./llm/types.js";
export * from "@llamaindex/core/schema";
declare module "@llamaindex/core/global" {
export interface LlamaIndexEventMaps {
"retrieve-start": RetrievalStartEvent;
@@ -386,6 +386,7 @@ export type VectorIndexRetrieverOptions = {
index: VectorStoreIndex;
similarityTopK?: number;
topK?: TopKMap;
filters?: MetadataFilters;
};
export class VectorIndexRetriever implements BaseRetriever {
@@ -393,14 +394,21 @@ export class VectorIndexRetriever implements BaseRetriever {
topK: TopKMap;
serviceContext?: ServiceContext;
filters?: MetadataFilters;
constructor({ index, similarityTopK, topK }: VectorIndexRetrieverOptions) {
constructor({
index,
similarityTopK,
topK,
filters,
}: VectorIndexRetrieverOptions) {
this.index = index;
this.serviceContext = this.index.serviceContext;
this.topK = topK ?? {
[ModalityType.TEXT]: similarityTopK ?? DEFAULT_SIMILARITY_TOP_K,
[ModalityType.IMAGE]: DEFAULT_SIMILARITY_TOP_K,
};
this.filters = filters;
}
/**
@@ -443,7 +451,7 @@ export class VectorIndexRetriever implements BaseRetriever {
query: MessageContent,
type: ModalityType,
vectorStore: VectorStore,
preFilters?: MetadataFilters,
filters?: MetadataFilters,
): Promise<NodeWithScore[]> {
// convert string message to multi-modal format
if (typeof query === "string") {
@@ -460,7 +468,7 @@ export class VectorIndexRetriever implements BaseRetriever {
queryEmbedding,
mode: VectorStoreQueryMode.DEFAULT,
similarityTopK: this.topK[type],
filters: preFilters ?? undefined,
filters: this.filters ?? filters ?? undefined,
});
nodes = nodes.concat(this.buildNodeListFromQueryResult(result));
}
@@ -1,4 +1,4 @@
import type { TransformComponent } from "@llamaindex/core/schema";
import type { BaseReader, TransformComponent } from "@llamaindex/core/schema";
import {
ModalityType,
splitNodesByType,
@@ -6,7 +6,6 @@ import {
type Document,
type Metadata,
} from "@llamaindex/core/schema";
import type { BaseReader } from "../readers/type.js";
import type { BaseDocumentStore } from "../storage/docStore/types.js";
import type {
VectorStore,
@@ -107,6 +106,7 @@ export class IngestionPipeline {
inputNodes.push(this.documents);
}
if (this.reader) {
// fixme: empty parameter might cause error
inputNodes.push(await this.reader.loadData());
}
return inputNodes.flat();
+6
View File
@@ -18,6 +18,7 @@ const ALL_AZURE_OPENAI_CHAT_MODELS = {
openAIModel: "gpt-3.5-turbo-16k",
},
"gpt-4o": { contextWindow: 128000, openAIModel: "gpt-4o" },
"gpt-4o-mini": { contextWindow: 128000, openAIModel: "gpt-4o-mini" },
"gpt-4": { contextWindow: 8192, openAIModel: "gpt-4" },
"gpt-4-32k": { contextWindow: 32768, openAIModel: "gpt-4-32k" },
"gpt-4-turbo": {
@@ -40,6 +41,10 @@ const ALL_AZURE_OPENAI_CHAT_MODELS = {
contextWindow: 128000,
openAIModel: "gpt-4o-2024-05-13",
},
"gpt-4o-mini-2024-07-18": {
contextWindow: 128000,
openAIModel: "gpt-4o-mini-2024-07-18",
},
};
const ALL_AZURE_OPENAI_EMBEDDING_MODELS = {
@@ -73,6 +78,7 @@ const ALL_AZURE_API_VERSIONS = [
"2024-03-01-preview",
"2024-04-01-preview",
"2024-05-01-preview",
"2024-06-01",
];
const DEFAULT_API_VERSION = "2023-05-15";
+19 -8
View File
@@ -6,6 +6,7 @@ import type {
ClientOptions as OpenAIClientOptions,
} from "openai";
import { AzureOpenAI, OpenAI as OrigOpenAI } from "openai";
import type { ChatModel } from "openai/resources/chat/chat";
import {
type BaseTool,
@@ -108,16 +109,24 @@ export const GPT4_MODELS = {
"gpt-4o-2024-05-13": { contextWindow: 128000 },
"gpt-4o-mini": { contextWindow: 128000 },
"gpt-4o-mini-2024-07-18": { contextWindow: 128000 },
"gpt-4o-2024-08-06": { contextWindow: 128000 },
"gpt-4o-2024-09-14": { contextWindow: 128000 },
"gpt-4o-2024-10-14": { contextWindow: 128000 },
"gpt-4-0613": { contextWindow: 128000 },
"gpt-4-turbo-2024-04-09": { contextWindow: 128000 },
"gpt-4-0314": { contextWindow: 128000 },
"gpt-4-32k-0314": { contextWindow: 32768 },
};
// NOTE we don't currently support gpt-3.5-turbo-instruct and don't plan to in the near future
export const GPT35_MODELS = {
"gpt-3.5-turbo": { contextWindow: 4096 },
"gpt-3.5-turbo": { contextWindow: 16385 },
"gpt-3.5-turbo-0613": { contextWindow: 4096 },
"gpt-3.5-turbo-16k": { contextWindow: 16384 },
"gpt-3.5-turbo-16k-0613": { contextWindow: 16384 },
"gpt-3.5-turbo-1106": { contextWindow: 16384 },
"gpt-3.5-turbo-0125": { contextWindow: 16384 },
"gpt-3.5-turbo-16k": { contextWindow: 16385 },
"gpt-3.5-turbo-16k-0613": { contextWindow: 16385 },
"gpt-3.5-turbo-1106": { contextWindow: 16385 },
"gpt-3.5-turbo-0125": { contextWindow: 16385 },
"gpt-3.5-turbo-0301": { contextWindow: 16385 },
};
/**
@@ -126,7 +135,7 @@ export const GPT35_MODELS = {
export const ALL_AVAILABLE_OPENAI_MODELS = {
...GPT4_MODELS,
...GPT35_MODELS,
};
} satisfies Record<ChatModel, { contextWindow: number }>;
export function isFunctionCallingModel(llm: LLM): llm is OpenAI {
let model: string;
@@ -157,8 +166,10 @@ export type OpenAIAdditionalChatOptions = Omit<
>;
export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
// Per completion OpenAI params
model: keyof typeof ALL_AVAILABLE_OPENAI_MODELS | string;
model:
| ChatModel
// string & {} is a hack to allow any string, but still give autocomplete
| (string & {});
temperature: number;
topP: number;
maxTokens?: number;
+7
View File
@@ -17,6 +17,13 @@
*/
export default function withLlamaIndex(config: any) {
config.experimental = config.experimental ?? {};
// copy tiktoken WASM files to the NextJS build
config.experimental.outputFileTracingIncludes =
config.experimental.outputFileTracingIncludes ?? {};
config.experimental.outputFileTracingIncludes["/**/*"] = [
"./node_modules/tiktoken/*.wasm",
];
// needed for transformers, see https://huggingface.co/docs/transformers.js/en/tutorials/next#step-2-install-and-configure-transformersjs
config.experimental.serverComponentsExternalPackages =
config.experimental.serverComponentsExternalPackages ?? [];
config.experimental.serverComponentsExternalPackages.push(
@@ -1,4 +1,4 @@
import { Document } from "@llamaindex/core/schema";
import { type BaseReader, Document } from "@llamaindex/core/schema";
import { getEnv } from "@llamaindex/env";
import type {
BaseServiceParams,
@@ -8,7 +8,6 @@ import type {
TranscriptSentence,
} from "assemblyai";
import { AssemblyAI } from "assemblyai";
import type { BaseReader } from "./type.js";
type AssemblyAIOptions = Partial<BaseServiceParams>;
const defaultOptions = {
+1 -2
View File
@@ -1,7 +1,6 @@
import { Document } from "@llamaindex/core/schema";
import { type BaseReader, Document, FileReader } from "@llamaindex/core/schema";
import type { ParseConfig } from "papaparse";
import Papa from "papaparse";
import { FileReader } from "./type.js";
/**
* papaparse-based csv parser
@@ -1,5 +1,5 @@
import { REST, type RESTOptions } from "@discordjs/rest";
import { Document } from "@llamaindex/core/schema";
import { Document, type BaseReader } from "@llamaindex/core/schema";
import { getEnv } from "@llamaindex/env";
import { Routes, type APIEmbed, type APIMessage } from "discord-api-types/v10";
@@ -7,7 +7,7 @@ import { Routes, type APIEmbed, type APIMessage } from "discord-api-types/v10";
* Represents a reader for Discord messages using @discordjs/rest
* See https://github.com/discordjs/discord.js/tree/main/packages/rest
*/
export class DiscordReader {
export class DiscordReader implements BaseReader {
private client: REST;
constructor(
@@ -1,6 +1,5 @@
import { Document } from "@llamaindex/core/schema";
import { Document, FileReader } from "@llamaindex/core/schema";
import mammoth from "mammoth";
import { FileReader } from "./type.js";
export class DocxReader extends FileReader {
/** DocxParser */
@@ -1,6 +1,4 @@
import { Document } from "@llamaindex/core/schema";
import { FileReader } from "./type.js";
import { Document, FileReader } from "@llamaindex/core/schema";
/**
* Extract the significant text from an arbitrary HTML document.
* The contents of any head, script, style, and xml tags are removed completely.
@@ -1,6 +1,5 @@
import type { Document } from "@llamaindex/core/schema";
import { ImageDocument } from "@llamaindex/core/schema";
import { FileReader } from "./type.js";
import { FileReader, ImageDocument } from "@llamaindex/core/schema";
/**
* Reads the content of an image file into a Document object (which stores the image file as a Blob).
@@ -1,7 +1,5 @@
import type { JSONValue } from "@llamaindex/core/global";
import { Document } from "@llamaindex/core/schema";
import { FileReader } from "./type.js";
import { Document, FileReader } from "@llamaindex/core/schema";
export interface JSONReaderOptions {
/**
* Whether to ensure only ASCII characters.
@@ -185,7 +183,7 @@ export class JSONReader<T extends JSONValue> extends FileReader {
return jsonStr;
} catch (e) {
throw new JSONStringifyError(
`Error stringifying JSON: ${e} in "${data}"`,
`Error stringifying JSON: ${e} in "${JSON.stringify(data)}"`,
);
}
}
@@ -1,7 +1,92 @@
import { Document } from "@llamaindex/core/schema";
import { Document, FileReader } from "@llamaindex/core/schema";
import { fs, getEnv } from "@llamaindex/env";
import { filetypeinfo } from "magic-bytes.js";
import { FileReader, type Language, type ResultType } from "./type.js";
export type ResultType = "text" | "markdown" | "json";
export type Language =
| "abq"
| "ady"
| "af"
| "ang"
| "ar"
| "as"
| "ava"
| "az"
| "be"
| "bg"
| "bh"
| "bho"
| "bn"
| "bs"
| "ch_sim"
| "ch_tra"
| "che"
| "cs"
| "cy"
| "da"
| "dar"
| "de"
| "en"
| "es"
| "et"
| "fa"
| "fr"
| "ga"
| "gom"
| "hi"
| "hr"
| "hu"
| "id"
| "inh"
| "is"
| "it"
| "ja"
| "kbd"
| "kn"
| "ko"
| "ku"
| "la"
| "lbe"
| "lez"
| "lt"
| "lv"
| "mah"
| "mai"
| "mi"
| "mn"
| "mr"
| "ms"
| "mt"
| "ne"
| "new"
| "nl"
| "no"
| "oc"
| "pi"
| "pl"
| "pt"
| "ro"
| "ru"
| "rs_cyrillic"
| "rs_latin"
| "sck"
| "sk"
| "sl"
| "sq"
| "sv"
| "sw"
| "ta"
| "tab"
| "te"
| "th"
| "tjk"
| "tl"
| "tr"
| "ug"
| "uk"
| "ur"
| "uz"
| "vi";
const SUPPORT_FILE_EXT: string[] = [
".pdf",
@@ -143,6 +228,8 @@ export class LlamaParseReader extends FileReader {
targetPages?: string;
// Whether or not to ignore and skip errors raised during parsing.
ignoreErrors: boolean = true;
// Whether to split by page using the pageSeparator or '\n---\n' as default.
splitByPage: boolean = true;
// Whether to use the vendor multimodal API.
useVendorMultimodalModel: boolean = false;
// The model name for the vendor multimodal API
@@ -326,10 +413,17 @@ export class LlamaParseReader extends FileReader {
}
// Return results as Document objects
const resultJson = await this.getJobResult(jobId, this.resultType);
const jobResults = await this.getJobResult(jobId, this.resultType);
const resultText = jobResults[this.resultType];
// Split the text by separator if splitByPage is true
if (this.splitByPage) {
return this.splitTextBySeparator(resultText);
}
return [
new Document({
text: resultJson[this.resultType],
text: resultText,
}),
];
} catch (e) {
@@ -485,6 +579,17 @@ export class LlamaParseReader extends FileReader {
return filteredParams;
}
private splitTextBySeparator(text: string): Document[] {
const separator = this.pageSeparator ?? "\n---\n";
const textChunks = text.split(separator);
return textChunks.map(
(docChunk: string) =>
new Document({
text: docChunk,
}),
);
}
static async getMimeType(
data: Uint8Array,
): Promise<{ mime: string; extension: string }> {
@@ -1,5 +1,4 @@
import { Document } from "@llamaindex/core/schema";
import { FileReader } from "./type.js";
import { Document, FileReader } from "@llamaindex/core/schema";
type MarkdownTuple = [string | null, string];
@@ -1,7 +1,7 @@
import type { BaseReader } from "@llamaindex/core/schema";
import { Document } from "@llamaindex/core/schema";
import type { Crawler, CrawlerOptions, Page } from "notion-md-crawler";
import { crawler, pageToString } from "notion-md-crawler";
import type { BaseReader } from "./type.js";
type NotionReaderOptions = Pick<CrawlerOptions, "client" | "serializers">;
+5 -2
View File
@@ -1,11 +1,14 @@
import { Document } from "@llamaindex/core/schema";
import { FileReader } from "./type.js";
import { Document, FileReader } from "@llamaindex/core/schema";
/**
* Read the text of a PDF
*/
export class PDFReader extends FileReader {
async loadDataAsContent(content: Uint8Array): Promise<Document[]> {
// XXX: create a new Uint8Array to prevent "Please provide binary data as `Uint8Array`, rather than `Buffer`." error if a Buffer passed
if (content instanceof Buffer) {
content = new Uint8Array(content);
}
const { totalPages, text } = await readPDF(content);
return text.map((text, page) => {
const metadata = {
@@ -1,8 +1,8 @@
import type { BaseReader, FileReader } from "@llamaindex/core/schema";
import { Document } from "@llamaindex/core/schema";
import { path } from "@llamaindex/env";
import { walk } from "../storage/FileSystem.js";
import { TextFileReader } from "./TextFileReader.js";
import type { BaseReader, FileReader } from "./type.js";
import pLimit from "./utils.js";
type ReaderCallback = (
@@ -1,3 +1,4 @@
import type { FileReader } from "@llamaindex/core/schema";
import { Document } from "@llamaindex/core/schema";
import { PapaCSVReader } from "./CSVReader.js";
import { DocxReader } from "./DocxReader.js";
@@ -10,7 +11,6 @@ import {
type SimpleDirectoryReaderLoadDataParams,
} from "./SimpleDirectoryReader.edge.js";
import { TextFileReader } from "./TextFileReader.js";
import type { FileReader } from "./type.js";
export const FILE_EXT_TO_READER: Record<string, FileReader> = {
txt: new TextFileReader(),
@@ -1,7 +1,6 @@
import type { Metadata } from "@llamaindex/core/schema";
import { Document } from "@llamaindex/core/schema";
import { type BaseReader, Document } from "@llamaindex/core/schema";
import type { MongoClient } from "mongodb";
import type { BaseReader } from "./type.js";
/**
* Read in from MongoDB
@@ -1,6 +1,4 @@
import { Document } from "@llamaindex/core/schema";
import { FileReader } from "./type.js";
import { Document, FileReader } from "@llamaindex/core/schema";
/**
* Read a .txt file
*/
-125
View File
@@ -1,125 +0,0 @@
import type { Document } from "@llamaindex/core/schema";
import { fs, path } from "@llamaindex/env";
/**
* A reader takes imports data into Document objects.
*/
export interface BaseReader {
loadData(...args: unknown[]): Promise<Document[]>;
}
/**
* A FileReader takes file paths and imports data into Document objects.
*/
export abstract class FileReader implements BaseReader {
abstract loadDataAsContent(
fileContent: Uint8Array,
fileName?: string,
): Promise<Document[]>;
async loadData(filePath: string): Promise<Document[]> {
// XXX: create a new Uint8Array to prevent "Please provide binary data as `Uint8Array`, rather than `Buffer`." error in PDFReader
const fileContent = new Uint8Array(await fs.readFile(filePath));
const fileName = path.basename(filePath);
const docs = await this.loadDataAsContent(fileContent, fileName);
docs.forEach(FileReader.addMetaData(filePath));
return docs;
}
static addMetaData(filePath: string) {
return (doc: Document, index: number) => {
// generate id as loadDataAsContent is only responsible for the content
doc.id_ = `${filePath}_${index + 1}`;
doc.metadata["file_path"] = path.resolve(filePath);
doc.metadata["file_name"] = path.basename(filePath);
};
}
}
// For LlamaParseReader.ts
export type ResultType = "text" | "markdown" | "json";
export type Language =
| "abq"
| "ady"
| "af"
| "ang"
| "ar"
| "as"
| "ava"
| "az"
| "be"
| "bg"
| "bh"
| "bho"
| "bn"
| "bs"
| "ch_sim"
| "ch_tra"
| "che"
| "cs"
| "cy"
| "da"
| "dar"
| "de"
| "en"
| "es"
| "et"
| "fa"
| "fr"
| "ga"
| "gom"
| "hi"
| "hr"
| "hu"
| "id"
| "inh"
| "is"
| "it"
| "ja"
| "kbd"
| "kn"
| "ko"
| "ku"
| "la"
| "lbe"
| "lez"
| "lt"
| "lv"
| "mah"
| "mai"
| "mi"
| "mn"
| "mr"
| "ms"
| "mt"
| "ne"
| "new"
| "nl"
| "no"
| "oc"
| "pi"
| "pl"
| "pt"
| "ro"
| "ru"
| "rs_cyrillic"
| "rs_latin"
| "sck"
| "sk"
| "sl"
| "sq"
| "sv"
| "sw"
| "ta"
| "tab"
| "te"
| "th"
| "tjk"
| "tl"
| "tr"
| "ug"
| "uk"
| "ur"
| "uz"
| "vi";
+1
View File
@@ -18,3 +18,4 @@ export { PineconeVectorStore } from "./vectorStore/PineconeVectorStore.js";
export { QdrantVectorStore } from "./vectorStore/QdrantVectorStore.js";
export { SimpleVectorStore } from "./vectorStore/SimpleVectorStore.js";
export * from "./vectorStore/types.js";
export { WeaviateVectorStore } from "./vectorStore/WeaviateVectorStore.js";
@@ -1,13 +1,19 @@
import type pg from "pg";
import {
FilterCondition,
FilterOperator,
VectorStoreBase,
type IEmbedModel,
type MetadataFilter,
type MetadataFilterValue,
type VectorStoreNoEmbedModel,
type VectorStoreQuery,
type VectorStoreQueryResult,
} from "./types.js";
import { escapeLikeString } from "./utils.js";
import type { BaseNode, Metadata } from "@llamaindex/core/schema";
import { Document, MetadataMode } from "@llamaindex/core/schema";
@@ -246,6 +252,120 @@ export class PGVectorStore
return Promise.resolve();
}
private toPostgresCondition(condition: `${FilterCondition}`) {
if (condition === FilterCondition.AND) {
return "AND";
}
if (condition === FilterCondition.OR) {
return "OR";
}
// fallback to AND
else {
return "AND";
}
}
private toPostgresOperator(operator: `${FilterOperator}`) {
if (operator === FilterOperator.EQ) {
return "=";
}
if (operator === FilterOperator.GT) {
return ">";
}
if (operator === FilterOperator.LT) {
return "<";
}
if (operator === FilterOperator.NE) {
return "!=";
}
if (operator === FilterOperator.GTE) {
return ">=";
}
if (operator === FilterOperator.LTE) {
return "<=";
}
if (operator === FilterOperator.IN) {
return "= ANY";
}
if (operator === FilterOperator.NIN) {
return "!= ANY";
}
if (operator === FilterOperator.CONTAINS) {
return "@>";
}
if (operator === FilterOperator.ANY) {
return "?|";
}
if (operator === FilterOperator.ALL) {
return "?&";
}
// fallback to "="
return "=";
}
private buildFilterClause(
filter: MetadataFilter,
paramIndex: number,
): {
clause: string;
param: string | string[] | number | number[] | undefined;
} {
if (
filter.operator === FilterOperator.IN ||
filter.operator === FilterOperator.NIN
) {
return {
clause: `metadata->>'${filter.key}' ${this.toPostgresOperator(filter.operator)}($${paramIndex})`,
param: filter.value,
};
}
if (
filter.operator === FilterOperator.ALL ||
filter.operator === FilterOperator.ANY
) {
return {
clause: `metadata->'${filter.key}' ${this.toPostgresOperator(filter.operator)} $${paramIndex}::text[]`,
param: filter.value,
};
}
if (filter.operator === FilterOperator.CONTAINS) {
return {
clause: `metadata->'${filter.key}' ${this.toPostgresOperator(filter.operator)} $${paramIndex}::jsonb`,
param: JSON.stringify([filter.value]),
};
}
if (filter.operator === FilterOperator.IS_EMPTY) {
return {
clause: `(NOT (metadata ? '${filter.key}') OR metadata->>'${filter.key}' IS NULL OR metadata->>'${filter.key}' = '' OR metadata->'${filter.key}' = '[]'::jsonb)`,
param: undefined,
};
}
if (filter.operator === FilterOperator.TEXT_MATCH) {
const escapedValue = escapeLikeString(filter.value as string);
return {
clause: `metadata->>'${filter.key}' LIKE $${paramIndex}`,
param: `%${escapedValue}%`,
};
}
// if value is number, coerce metadata value to float
if (typeof filter.value === "number") {
return {
clause: `(metadata->>'${filter.key}')::float ${this.toPostgresOperator(filter.operator)} $${paramIndex}`,
param: filter.value,
};
}
return {
clause: `metadata->>'${filter.key}' ${this.toPostgresOperator(filter.operator)} $${paramIndex}`,
param: filter.value,
};
}
/**
* Query the vector store for the closest matching data to the query embeddings
* @param query The VectorStoreQuery to be used
@@ -265,19 +385,27 @@ export class PGVectorStore
const max = query.similarityTopK ?? 2;
const whereClauses = this.collection.length ? ["collection = $2"] : [];
const params: Array<string | number> = this.collection.length
const params: Array<MetadataFilterValue> = this.collection.length
? [embedding, this.collection]
: [embedding];
const filterClauses: string[] = [];
query.filters?.filters.forEach((filter, index) => {
const paramIndex = params.length + 1;
whereClauses.push(`metadata->>'${filter.key}' = $${paramIndex}`);
// TODO: support filter with other operators
if (!Array.isArray(filter.value)) {
params.push(filter.value);
const { clause, param } = this.buildFilterClause(filter, paramIndex);
filterClauses.push(clause);
if (param) {
params.push(param);
}
});
if (filterClauses.length > 0) {
const condition = this.toPostgresCondition(
query.filters?.condition ?? FilterCondition.AND,
);
whereClauses.push(`(${filterClauses.join(` ${condition} `)})`);
}
const where =
whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
@@ -36,7 +36,7 @@ type MetadataValue = Record<string, any>;
// Mapping of filter operators to metadata filter functions
const OPERATOR_TO_FILTER: {
[key in FilterOperator]: (
[key in FilterOperator]?: (
{ key, value }: MetadataFilter,
metadata: MetadataValue,
) => boolean;
@@ -94,7 +94,20 @@ const buildFilterFn = (
const queryCondition = condition || "and"; // default to and
const itemFilterFn = (filter: MetadataFilter): boolean => {
if (metadata[filter.key] === undefined) return false; // always return false if the metadata key is not present
if (filter.operator === FilterOperator.IS_EMPTY) {
// for `is_empty` operator, return true if the metadata key is not present or the value is empty
const value = metadata[filter.key];
return (
value === undefined ||
value === null ||
value === "" ||
(Array.isArray(value) && value.length === 0)
);
}
if (metadata[filter.key] === undefined) {
// for other operators, always return false if the metadata key is not present
return false;
}
const metadataLookupFn = OPERATOR_TO_FILTER[filter.operator];
if (!metadataLookupFn)
throw new Error(`Unsupported operator: ${filter.operator}`);
@@ -0,0 +1,339 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { BaseNode, MetadataMode, type Metadata } from "@llamaindex/core/schema";
import weaviate, {
Filters,
type Collection,
type DeleteManyOptions,
type FilterValue,
type WeaviateClient,
type WeaviateNonGenericObject,
} from "weaviate-client";
import {
VectorStoreBase,
VectorStoreQueryMode,
type IEmbedModel,
type MetadataFilter,
type MetadataFilters,
type VectorStoreNoEmbedModel,
type VectorStoreQuery,
type VectorStoreQueryResult,
} from "./types.js";
import {
metadataDictToNode,
nodeToMetadata,
parseArrayValue,
parseNumberValue,
} from "./utils.js";
const NODE_SCHEMA = [
{
dataType: ["text"],
description: "Text property",
name: "text",
},
{
dataType: ["text"],
description: "The ref_doc_id of the Node",
name: "ref_doc_id",
},
{
dataType: ["text"],
description: "node_info (in JSON)",
name: "node_info",
},
{
dataType: ["text"],
description: "The relationships of the node (in JSON)",
name: "relationships",
},
];
const SIMILARITY_KEYS: {
[key: string]: "distance" | "score";
} = {
[VectorStoreQueryMode.DEFAULT]: "distance",
[VectorStoreQueryMode.HYBRID]: "score",
};
const buildFilterItem = (
collection: Collection,
filter: MetadataFilter,
): FilterValue => {
const { key, operator, value } = filter;
switch (operator) {
case "==": {
return collection.filter.byProperty(key).equal(value);
}
case "!=": {
return collection.filter.byProperty(key).notEqual(value);
}
case ">": {
return collection.filter
.byProperty(key)
.greaterThan(parseNumberValue(value));
}
case "<": {
return collection.filter
.byProperty(key)
.lessThan(parseNumberValue(value));
}
case ">=": {
return collection.filter
.byProperty(key)
.greaterOrEqual(parseNumberValue(value));
}
case "<=": {
return collection.filter
.byProperty(key)
.lessOrEqual(parseNumberValue(value));
}
case "any": {
return collection.filter
.byProperty(key)
.containsAny(parseArrayValue(value).map(String));
}
case "all": {
return collection.filter
.byProperty(key)
.containsAll(parseArrayValue(value).map(String));
}
default: {
throw new Error(`Operator ${operator} is not supported.`);
}
}
};
const toWeaviateFilter = (
collection: Collection,
standardFilters?: MetadataFilters,
): FilterValue | undefined => {
if (!standardFilters?.filters.length) return undefined;
const filtersList = standardFilters.filters.map((filter) =>
buildFilterItem(collection, filter),
);
if (filtersList.length === 1) return filtersList[0];
const condition = standardFilters.condition ?? "and";
return Filters[condition](...filtersList);
};
export class WeaviateVectorStore
extends VectorStoreBase
implements VectorStoreNoEmbedModel
{
public storesText: boolean = true;
private flatMetadata: boolean = true;
private weaviateClient?: WeaviateClient;
private clusterURL!: string;
private apiKey!: string;
private indexName: string;
private idKey: string;
private contentKey: string;
private embeddingKey: string;
private metadataKey: string;
constructor(
init?: Partial<IEmbedModel> & {
weaviateClient?: WeaviateClient;
cloudOptions?: {
clusterURL?: string;
apiKey?: string;
};
indexName?: string;
idKey?: string;
contentKey?: string;
metadataKey?: string;
embeddingKey?: string;
},
) {
super(init?.embedModel);
if (init?.weaviateClient) {
// Use the provided client
this.weaviateClient = init.weaviateClient;
} else {
// Load client cloud options from config or env
const clusterURL =
init?.cloudOptions?.clusterURL ?? process.env.WEAVIATE_CLUSTER_URL;
const apiKey = init?.cloudOptions?.apiKey ?? process.env.WEAVIATE_API_KEY;
if (!clusterURL || !apiKey) {
throw new Error(
"Must specify WEAVIATE_CLUSTER_URL and WEAVIATE_API_KEY via env variable.",
);
}
this.clusterURL = clusterURL;
this.apiKey = apiKey;
}
this.checkIndexName(init?.indexName);
this.indexName = init?.indexName ?? "LlamaIndex";
this.idKey = init?.idKey ?? "id";
this.contentKey = init?.contentKey ?? "text";
this.embeddingKey = init?.embeddingKey ?? "vectors";
this.metadataKey = init?.metadataKey ?? "node_info";
}
public client() {
return this.getClient();
}
public async add(nodes: BaseNode<Metadata>[]): Promise<string[]> {
const collection = await this.ensureCollection({ createIfNotExists: true });
const result = await collection.data.insertMany(
nodes.map((node) => {
const metadata = nodeToMetadata(
node,
true,
this.contentKey,
this.flatMetadata,
);
const body = {
[this.idKey]: node.id_,
[this.embeddingKey]: node.getEmbedding(),
properties: {
...metadata,
[this.contentKey]: node.getContent(MetadataMode.NONE),
[this.metadataKey]: JSON.stringify(metadata),
relationships: JSON.stringify({ ref_doc_id: metadata.ref_doc_id }),
},
};
return body;
}),
);
return Object.values(result.uuids);
}
public async delete(
refDocId: string,
deleteOptions?: DeleteManyOptions<boolean>,
): Promise<void> {
const collection = await this.ensureCollection();
await collection.data.deleteMany(
collection.filter.byProperty("ref_doc_id").like(refDocId),
deleteOptions,
);
}
public async query(query: VectorStoreQuery): Promise<VectorStoreQueryResult> {
const collection = await this.ensureCollection();
const allProperties = await this.getAllProperties();
let filters: FilterValue | undefined = undefined;
if (query.docIds) {
filters = collection.filter
.byProperty("doc_id")
.containsAny(query.docIds);
}
if (query.filters) {
filters = toWeaviateFilter(collection, query.filters);
}
const queryResult = await collection.query.hybrid(query.queryStr!, {
vector: query.queryEmbedding,
alpha: this.getQueryAlpha(query),
limit: query.similarityTopK,
returnMetadata: Object.values(SIMILARITY_KEYS),
returnProperties: allProperties,
includeVector: true,
filters,
});
const entries = queryResult.objects;
const similarityKey = SIMILARITY_KEYS[query.mode];
const nodes: BaseNode<Metadata>[] = [];
const similarities: number[] = [];
const ids: string[] = [];
entries.forEach((entry, index) => {
if (index < query.similarityTopK && entry.metadata) {
const node = metadataDictToNode(entry.properties);
node.setContent(entry.properties[this.contentKey]);
nodes.push(node);
ids.push(entry.uuid);
similarities.push(this.getNodeSimilarity(entry, similarityKey));
}
});
return {
nodes,
similarities,
ids,
};
}
private async getClient(): Promise<WeaviateClient> {
if (this.weaviateClient) return this.weaviateClient;
const client = await weaviate.connectToWeaviateCloud(this.clusterURL, {
authCredentials: new weaviate.ApiKey(this.apiKey),
});
this.weaviateClient = client;
return client;
}
private async ensureCollection({ createIfNotExists = false } = {}) {
const client = await this.getClient();
const exists = await this.doesCollectionExist();
if (!exists) {
if (createIfNotExists) {
await this.createCollection();
} else {
throw new Error(`Collection ${this.indexName} does not exist.`);
}
}
return client.collections.get(this.indexName);
}
private async doesCollectionExist() {
const client = await this.getClient();
return client.collections.exists(this.indexName);
}
private async createCollection() {
const client = await this.getClient();
return await client.collections.createFromSchema({
class: this.indexName,
description: `Collection for ${this.indexName}`,
properties: NODE_SCHEMA,
});
}
private getQueryAlpha(query: VectorStoreQuery): number | undefined {
if (!query.queryEmbedding) return undefined;
if (query.mode === VectorStoreQueryMode.DEFAULT) return 1;
if (query.mode === VectorStoreQueryMode.HYBRID && query.queryStr)
return query.alpha;
return undefined;
}
private async getAllProperties(): Promise<string[]> {
const collection = await this.ensureCollection();
const properties = (await collection.config.get()).properties;
return properties.map((p) => p.name);
}
private checkIndexName(indexName?: string) {
if (indexName && indexName[0] !== indexName[0].toUpperCase()) {
throw new Error(
"Index name must start with a capital letter, e.g. 'LlamaIndex'",
);
}
}
private getNodeSimilarity(
entry: WeaviateNonGenericObject,
similarityKey: "distance" | "score" = "distance",
): number {
const distance = entry.metadata?.[similarityKey];
if (distance === undefined) return 1;
// convert distance https://forum.weaviate.io/t/distance-vs-certainty-scores/258
return 1 - distance;
}
}

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