mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-19 18:43:34 -04:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 28b877e31f | |||
| 4389b80a52 | |||
| d3bc663951 | |||
| 4810364788 | |||
| 2dcad52dd9 | |||
| 0bf8d80b12 | |||
| e4bba02aec | |||
| 1caa0da657 | |||
| 711c814bb2 | |||
| 5b832eb927 | |||
| 49988431f6 | |||
| 72d65dd51a | |||
| 553bc55b19 | |||
| fc6f69833c |
@@ -19,6 +19,11 @@ jobs:
|
|||||||
matrix:
|
matrix:
|
||||||
node-version: [18.x, 20.x, 22.x]
|
node-version: [18.x, 20.x, 22.x]
|
||||||
name: E2E on Node.js ${{ matrix.node-version }}
|
name: E2E on Node.js ${{ matrix.node-version }}
|
||||||
|
|
||||||
|
env: POSTGRES_DB=vectordb
|
||||||
|
POSTGRES_USER=testuser
|
||||||
|
POSTGRES_PASSWORD=testpwd
|
||||||
|
POSTGRES_HOST_AUTH_METHOD=trust
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -45,6 +50,16 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
- uses: ankane/setup-postgres@v1
|
||||||
|
with:
|
||||||
|
database: llamaindex_node_test
|
||||||
|
dev-files: true
|
||||||
|
- run: |
|
||||||
|
cd /tmp
|
||||||
|
git clone --branch v0.7.0 https://github.com/pgvector/pgvector.git
|
||||||
|
cd pgvector
|
||||||
|
make
|
||||||
|
sudo make install
|
||||||
- uses: pnpm/action-setup@v4
|
- uses: pnpm/action-setup@v4
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v4
|
||||||
|
|||||||
@@ -36,9 +36,44 @@ For now, browser support is limited due to the lack of support for [AsyncLocalSt
|
|||||||
npm install llamaindex
|
npm install llamaindex
|
||||||
pnpm install llamaindex
|
pnpm install llamaindex
|
||||||
yarn add llamaindex
|
yarn add llamaindex
|
||||||
jsr install @llamaindex/core
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Setup TypeScript
|
||||||
|
|
||||||
|
```json5
|
||||||
|
{
|
||||||
|
compilerOptions: {
|
||||||
|
// ⬇️ add this line to your tsconfig.json
|
||||||
|
moduleResolution: "bundler", // or "node16"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Why?</summary>
|
||||||
|
We are shipping both ESM and CJS module, and compatible with Vercel Edge, Cloudflare Workers, and other serverless platforms.
|
||||||
|
|
||||||
|
So we are using [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) to support all environments.
|
||||||
|
|
||||||
|
This is a kind of modern way of shipping packages, but might cause TypeScript type check to fail because of legacy module resolution.
|
||||||
|
|
||||||
|
Imaging you put output file into `/dist/openai.js` but you are importing `llamaindex/openai` in your code, and set `package.json` like this:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"exports": {
|
||||||
|
"./openai": "./dist/openai.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In old module resolution, TypeScript will not be able to find the module because it is not follow the file structure, even you run `node index.js` successfully. (on Node.js >=16)
|
||||||
|
|
||||||
|
See more about [moduleResolution](https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution) or
|
||||||
|
[TypeScript 5.0 blog](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#--moduleresolution-bundler7).
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
### Node.js
|
### Node.js
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# docs
|
# docs
|
||||||
|
|
||||||
|
## 0.0.66
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.0.65
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.0.64
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.0.63
|
## 0.0.63
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -50,10 +50,10 @@ We want to see what our agent is up to, so we're going to hook into some events
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Settings.callbackManager.on("llm-tool-call", (event) => {
|
Settings.callbackManager.on("llm-tool-call", (event) => {
|
||||||
console.log(event.detail.payload);
|
console.log(event.detail);
|
||||||
});
|
});
|
||||||
Settings.callbackManager.on("llm-tool-result", (event) => {
|
Settings.callbackManager.on("llm-tool-result", (event) => {
|
||||||
console.log(event.detail.payload);
|
console.log(event.detail);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "docs",
|
"name": "docs",
|
||||||
"version": "0.0.63",
|
"version": "0.0.66",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"docusaurus": "docusaurus",
|
"docusaurus": "docusaurus",
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { DeepInfraEmbedding } from "llamaindex";
|
|||||||
async function main() {
|
async function main() {
|
||||||
// API token can be provided as an environment variable too
|
// API token can be provided as an environment variable too
|
||||||
// using DEEPINFRA_API_TOKEN variable
|
// using DEEPINFRA_API_TOKEN variable
|
||||||
const apiToken = "YOUR_API_TOKEN" ?? process.env.DEEPINFRA_API_TOKEN;
|
const apiToken = process.env.DEEPINFRA_API_TOKEN ?? "YOUR_API_TOKEN";
|
||||||
const model = "BAAI/bge-large-en-v1.5";
|
const model = "BAAI/bge-large-en-v1.5";
|
||||||
const embedModel = new DeepInfraEmbedding({
|
const embedModel = new DeepInfraEmbedding({
|
||||||
model,
|
model,
|
||||||
|
|||||||
@@ -38,6 +38,9 @@
|
|||||||
"overrides": {
|
"overrides": {
|
||||||
"trim": "1.0.1",
|
"trim": "1.0.1",
|
||||||
"protobufjs": "7.2.6"
|
"protobufjs": "7.2.6"
|
||||||
|
},
|
||||||
|
"patchedDependencies": {
|
||||||
|
"python-format-js@1.4.3": "patches/python-format-js@1.4.3.patch"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
|
|||||||
@@ -1,5 +1,28 @@
|
|||||||
# @llamaindex/autotool-01-node-example
|
# @llamaindex/autotool-01-node-example
|
||||||
|
|
||||||
|
## 0.0.6
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
- @llamaindex/autotool@2.0.1
|
||||||
|
|
||||||
|
## 0.0.5
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
- @llamaindex/autotool@2.0.1
|
||||||
|
|
||||||
|
## 0.0.4
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
- @llamaindex/autotool@2.0.1
|
||||||
|
|
||||||
## 0.0.3
|
## 0.0.3
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -13,5 +13,5 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node --import tsx --import @llamaindex/autotool/node ./src/index.ts"
|
"start": "node --import tsx --import @llamaindex/autotool/node ./src/index.ts"
|
||||||
},
|
},
|
||||||
"version": "0.0.3"
|
"version": "0.0.6"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ const openai = new OpenAI();
|
|||||||
stream: false,
|
stream: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const toolCalls = response.choices[0].message.tool_calls ?? [];
|
const toolCalls = response.choices[0]!.message.tool_calls ?? [];
|
||||||
for (const toolCall of toolCalls) {
|
for (const toolCall of toolCalls) {
|
||||||
toolCall.function.name;
|
toolCall.function.name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,28 @@
|
|||||||
# @llamaindex/autotool-02-next-example
|
# @llamaindex/autotool-02-next-example
|
||||||
|
|
||||||
|
## 0.1.50
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
- @llamaindex/autotool@2.0.1
|
||||||
|
|
||||||
|
## 0.1.49
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
- @llamaindex/autotool@2.0.1
|
||||||
|
|
||||||
|
## 0.1.48
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
- @llamaindex/autotool@2.0.1
|
||||||
|
|
||||||
## 0.1.47
|
## 0.1.47
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/autotool-02-next-example",
|
"name": "@llamaindex/autotool-02-next-example",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.1.47",
|
"version": "0.1.50",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
"unplugin": "^1.12.2"
|
"unplugin": "^1.12.2"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"llamaindex": "^0.5.22",
|
"llamaindex": "^0.5.25",
|
||||||
"openai": "^4",
|
"openai": "^4",
|
||||||
"typescript": "^4"
|
"typescript": "^4"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -16,11 +16,16 @@ const openaiToolsAtom = atom<ChatCompletionTool[]>((get) => {
|
|||||||
const metadata = get(toolMetadataAtom);
|
const metadata = get(toolMetadataAtom);
|
||||||
return metadata.map(([metadata]) => ({
|
return metadata.map(([metadata]) => ({
|
||||||
type: "function",
|
type: "function",
|
||||||
function: {
|
function: metadata.parameters
|
||||||
parameters: metadata.parameters,
|
? {
|
||||||
name: metadata.name,
|
parameters: metadata.parameters,
|
||||||
description: metadata.description,
|
name: metadata.name,
|
||||||
},
|
description: metadata.description,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
name: metadata.name,
|
||||||
|
description: metadata.description,
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export type Info = {
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export type InfoString = {
|
export type InfoString = {
|
||||||
originalFunction?: string;
|
originalFunction: string | undefined;
|
||||||
parameterMapping: Record<string, number>;
|
parameterMapping: Record<string, number>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,17 @@
|
|||||||
# @llamaindex/cloud
|
# @llamaindex/cloud
|
||||||
|
|
||||||
|
## 0.2.4
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 4810364: fix: bump version
|
||||||
|
|
||||||
|
## 0.2.3
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 0bf8d80: fix: bump version
|
||||||
|
|
||||||
## 0.2.2
|
## 0.2.2
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ export default defineConfig({
|
|||||||
// you can download this file to get the latest version of the OpenAPI document
|
// you can download this file to get the latest version of the OpenAPI document
|
||||||
// @link https://api.cloud.llamaindex.ai/api/openapi.json
|
// @link https://api.cloud.llamaindex.ai/api/openapi.json
|
||||||
input: "./openapi.json",
|
input: "./openapi.json",
|
||||||
|
client: "@hey-api/client-fetch",
|
||||||
output: {
|
output: {
|
||||||
path: "./src/client",
|
path: "./src/client",
|
||||||
format: "prettier",
|
format: "prettier",
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/cloud",
|
"name": "@llamaindex/cloud",
|
||||||
"version": "0.2.2",
|
"version": "0.2.4",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"generate": "pnpm dlx @hey-api/openapi-ts@0.49.0",
|
"generate": "pnpx @hey-api/openapi-ts@0.53.0",
|
||||||
"build": "pnpm run generate && bunchee"
|
"build": "pnpm run generate && bunchee"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
@@ -34,7 +34,8 @@
|
|||||||
"directory": "packages/cloud"
|
"directory": "packages/cloud"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@hey-api/openapi-ts": "^0.52.11",
|
"@hey-api/client-fetch": "^0.2.4",
|
||||||
|
"@hey-api/openapi-ts": "^0.53.0",
|
||||||
"bunchee": "5.3.2"
|
"bunchee": "5.3.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
# @llamaindex/community
|
# @llamaindex/community
|
||||||
|
|
||||||
|
## 0.0.33
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [711c814]
|
||||||
|
- @llamaindex/core@0.1.12
|
||||||
|
|
||||||
## 0.0.32
|
## 0.0.32
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/community",
|
"name": "@llamaindex/community",
|
||||||
"description": "Community package for LlamaIndexTS",
|
"description": "Community package for LlamaIndexTS",
|
||||||
"version": "0.0.32",
|
"version": "0.0.33",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"types": "dist/type/index.d.ts",
|
"types": "dist/type/index.d.ts",
|
||||||
"main": "dist/cjs/index.js",
|
"main": "dist/cjs/index.js",
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ export const mergeNeighboringSameRoleMessages = (
|
|||||||
): AnthropicMessage[] => {
|
): AnthropicMessage[] => {
|
||||||
return messages.reduce(
|
return messages.reduce(
|
||||||
(result: AnthropicMessage[], current: AnthropicMessage, index: number) => {
|
(result: AnthropicMessage[], current: AnthropicMessage, index: number) => {
|
||||||
if (index > 0 && messages[index - 1].role === current.role) {
|
if (index > 0 && messages[index - 1]!.role === current.role) {
|
||||||
result[result.length - 1].content = [
|
result[result.length - 1]!.content = [
|
||||||
...result[result.length - 1].content,
|
...result[result.length - 1]!.content,
|
||||||
...current.content,
|
...current.content,
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
@@ -128,7 +128,7 @@ export const mapChatMessagesToAnthropicMessages = <
|
|||||||
);
|
);
|
||||||
})
|
})
|
||||||
.filter((message: AnthropicMessage) => {
|
.filter((message: AnthropicMessage) => {
|
||||||
const content = message.content[0];
|
const content = message.content[0]!;
|
||||||
if (content.type === "text" && !content.text) return false;
|
if (content.type === "text" && !content.text) return false;
|
||||||
if (content.type === "image" && !content.source.data) return false;
|
if (content.type === "image" && !content.source.data) return false;
|
||||||
if (content.type === "image" && message.role === "assistant")
|
if (content.type === "image" && message.role === "assistant")
|
||||||
@@ -151,12 +151,12 @@ export const extractDataUrlComponents = (
|
|||||||
} => {
|
} => {
|
||||||
const parts = dataUrl.split(";base64,");
|
const parts = dataUrl.split(";base64,");
|
||||||
|
|
||||||
if (parts.length !== 2 || !parts[0].startsWith("data:")) {
|
if (parts.length !== 2 || !parts[0]!.startsWith("data:")) {
|
||||||
throw new Error("Invalid data URL");
|
throw new Error("Invalid data URL");
|
||||||
}
|
}
|
||||||
|
|
||||||
const mimeType = parts[0].slice(5);
|
const mimeType = parts[0]!.slice(5);
|
||||||
const base64 = parts[1];
|
const base64 = parts[1]!;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mimeType,
|
mimeType,
|
||||||
|
|||||||
@@ -153,12 +153,15 @@ export const TOOL_CALL_MODELS = [
|
|||||||
|
|
||||||
const getProvider = (model: string): Provider => {
|
const getProvider = (model: string): Provider => {
|
||||||
const providerName = model.split(".")[0];
|
const providerName = model.split(".")[0];
|
||||||
|
if (!providerName) {
|
||||||
|
throw new Error(`Model ${model} is not supported`);
|
||||||
|
}
|
||||||
if (!(providerName in PROVIDERS)) {
|
if (!(providerName in PROVIDERS)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Provider ${providerName} for model ${model} is not supported`,
|
`Provider ${providerName} for model ${model} is not supported`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return PROVIDERS[providerName];
|
return PROVIDERS[providerName]!;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BedrockModelParams = {
|
export type BedrockModelParams = {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ export class MetaProvider extends Provider<MetaStreamEvent> {
|
|||||||
const result = this.getResultFromResponse(response);
|
const result = this.getResultFromResponse(response);
|
||||||
if (!result.generation.trim().startsWith(TOKENS.TOOL_CALL)) return [];
|
if (!result.generation.trim().startsWith(TOKENS.TOOL_CALL)) return [];
|
||||||
const tool = JSON.parse(
|
const tool = JSON.parse(
|
||||||
result.generation.trim().split(TOKENS.TOOL_CALL)[1],
|
result.generation.trim().split(TOKENS.TOOL_CALL)[1]!,
|
||||||
);
|
);
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
# @llamaindex/core
|
# @llamaindex/core
|
||||||
|
|
||||||
|
## 0.1.12
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 711c814: fix: patch `python-format-js`
|
||||||
|
|
||||||
## 0.1.11
|
## 0.1.11
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/core",
|
"name": "@llamaindex/core",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.1.11",
|
"version": "0.1.12",
|
||||||
"description": "LlamaIndex Core Module",
|
"description": "LlamaIndex Core Module",
|
||||||
"exports": {
|
"exports": {
|
||||||
"./node-parser": {
|
"./node-parser": {
|
||||||
@@ -129,6 +129,20 @@
|
|||||||
"types": "./dist/prompts/index.d.ts",
|
"types": "./dist/prompts/index.d.ts",
|
||||||
"default": "./dist/prompts/index.js"
|
"default": "./dist/prompts/index.js"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"./indices": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/indices/index.d.cts",
|
||||||
|
"default": "./dist/indices/index.cjs"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/indices/index.d.ts",
|
||||||
|
"default": "./dist/indices/index.js"
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"types": "./dist/indices/index.d.ts",
|
||||||
|
"default": "./dist/indices/index.js"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ export abstract class BaseEmbedding extends TransformComponent {
|
|||||||
const embeddings = await this.getTextEmbeddingsBatch(texts, options);
|
const embeddings = await this.getTextEmbeddingsBatch(texts, options);
|
||||||
|
|
||||||
for (let i = 0; i < nodes.length; i++) {
|
for (let i = 0; i < nodes.length; i++) {
|
||||||
nodes[i].embedding = embeddings[i];
|
nodes[i]!.embedding = embeddings[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodes;
|
return nodes;
|
||||||
@@ -120,7 +120,7 @@ export async function batchEmbeddings<T>(
|
|||||||
const curBatch: T[] = [];
|
const curBatch: T[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < queue.length; i++) {
|
for (let i = 0; i < queue.length; i++) {
|
||||||
curBatch.push(queue[i]);
|
curBatch.push(queue[i]!);
|
||||||
if (i == queue.length - 1 || curBatch.length == chunkSize) {
|
if (i == queue.length - 1 || curBatch.length == chunkSize) {
|
||||||
const embeddings = await embedFunc(curBatch);
|
const embeddings = await embedFunc(curBatch);
|
||||||
|
|
||||||
|
|||||||
@@ -35,20 +35,20 @@ export function similarity(
|
|||||||
function norm(x: number[]): number {
|
function norm(x: number[]): number {
|
||||||
let result = 0;
|
let result = 0;
|
||||||
for (let i = 0; i < x.length; i++) {
|
for (let i = 0; i < x.length; i++) {
|
||||||
result += x[i] * x[i];
|
result += x[i]! * x[i]!;
|
||||||
}
|
}
|
||||||
return Math.sqrt(result);
|
return Math.sqrt(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case SimilarityType.EUCLIDEAN: {
|
case SimilarityType.EUCLIDEAN: {
|
||||||
const difference = embedding1.map((x, i) => x - embedding2[i]);
|
const difference = embedding1.map((x, i) => x - embedding2[i]!);
|
||||||
return -norm(difference);
|
return -norm(difference);
|
||||||
}
|
}
|
||||||
case SimilarityType.DOT_PRODUCT: {
|
case SimilarityType.DOT_PRODUCT: {
|
||||||
let result = 0;
|
let result = 0;
|
||||||
for (let i = 0; i < embedding1.length; i++) {
|
for (let i = 0; i < embedding1.length; i++) {
|
||||||
result += embedding1[i] * embedding2[i];
|
result += embedding1[i]! * embedding2[i]!;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Tokenizer } from "@llamaindex/env";
|
import type { Tokenizer } from "@llamaindex/env";
|
||||||
|
import type { LLM } from "../llms";
|
||||||
import {
|
import {
|
||||||
type CallbackManager,
|
type CallbackManager,
|
||||||
getCallbackManager,
|
getCallbackManager,
|
||||||
@@ -10,6 +11,7 @@ import {
|
|||||||
setChunkSize,
|
setChunkSize,
|
||||||
withChunkSize,
|
withChunkSize,
|
||||||
} from "./settings/chunk-size";
|
} from "./settings/chunk-size";
|
||||||
|
import { getLLM, setLLM, withLLM } from "./settings/llm";
|
||||||
import {
|
import {
|
||||||
getTokenizer,
|
getTokenizer,
|
||||||
setTokenizer,
|
setTokenizer,
|
||||||
@@ -17,6 +19,15 @@ import {
|
|||||||
} from "./settings/tokenizer";
|
} from "./settings/tokenizer";
|
||||||
|
|
||||||
export const Settings = {
|
export const Settings = {
|
||||||
|
get llm() {
|
||||||
|
return getLLM();
|
||||||
|
},
|
||||||
|
set llm(llm) {
|
||||||
|
setLLM(llm);
|
||||||
|
},
|
||||||
|
withLLM<Result>(llm: LLM, fn: () => Result): Result {
|
||||||
|
return withLLM(llm, fn);
|
||||||
|
},
|
||||||
get tokenizer() {
|
get tokenizer() {
|
||||||
return getTokenizer();
|
return getTokenizer();
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { AsyncLocalStorage } from "@llamaindex/env";
|
||||||
|
import type { LLM } from "../../llms";
|
||||||
|
|
||||||
|
const llmAsyncLocalStorage = new AsyncLocalStorage<LLM>();
|
||||||
|
let globalLLM: LLM | undefined;
|
||||||
|
|
||||||
|
export function getLLM(): LLM {
|
||||||
|
const currentLLM = globalLLM ?? llmAsyncLocalStorage.getStore();
|
||||||
|
if (!currentLLM) {
|
||||||
|
throw new Error(
|
||||||
|
"Cannot find LLM, please set `Settings.llm = ...` on the top of your code",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return currentLLM;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setLLM(llm: LLM): void {
|
||||||
|
globalLLM = llm;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function withLLM<Result>(llm: LLM, fn: () => Result): Result {
|
||||||
|
return llmAsyncLocalStorage.run(llm, fn);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
export {
|
||||||
|
PromptHelper,
|
||||||
|
getBiggestPrompt,
|
||||||
|
type PromptHelperOptions,
|
||||||
|
} from "./prompt-helper";
|
||||||
+33
-19
@@ -1,17 +1,17 @@
|
|||||||
|
import { type Tokenizer, tokenizers } from "@llamaindex/env";
|
||||||
import {
|
import {
|
||||||
DEFAULT_CHUNK_OVERLAP_RATIO,
|
DEFAULT_CHUNK_OVERLAP_RATIO,
|
||||||
DEFAULT_CONTEXT_WINDOW,
|
DEFAULT_CONTEXT_WINDOW,
|
||||||
DEFAULT_NUM_OUTPUTS,
|
DEFAULT_NUM_OUTPUTS,
|
||||||
DEFAULT_PADDING,
|
DEFAULT_PADDING,
|
||||||
} from "@llamaindex/core/global";
|
} from "../global";
|
||||||
import { SentenceSplitter } from "@llamaindex/core/node-parser";
|
import { SentenceSplitter } from "../node-parser";
|
||||||
import type { PromptTemplate } from "@llamaindex/core/prompts";
|
import type { PromptTemplate } from "../prompts";
|
||||||
import { type Tokenizer, tokenizers } from "@llamaindex/env";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the empty prompt text given a prompt.
|
* Get the empty prompt text given a prompt.
|
||||||
*/
|
*/
|
||||||
export function getEmptyPromptTxt(prompt: PromptTemplate) {
|
function getEmptyPromptTxt(prompt: PromptTemplate) {
|
||||||
return prompt.format({
|
return prompt.format({
|
||||||
...Object.fromEntries(
|
...Object.fromEntries(
|
||||||
[...prompt.templateVars.keys()].map((key) => [key, ""]),
|
[...prompt.templateVars.keys()].map((key) => [key, ""]),
|
||||||
@@ -23,14 +23,23 @@ export function getEmptyPromptTxt(prompt: PromptTemplate) {
|
|||||||
* Get biggest empty prompt size from a list of prompts.
|
* Get biggest empty prompt size from a list of prompts.
|
||||||
* Used to calculate the maximum size of inputs to the LLM.
|
* Used to calculate the maximum size of inputs to the LLM.
|
||||||
*/
|
*/
|
||||||
export function getBiggestPrompt(prompts: PromptTemplate[]) {
|
export function getBiggestPrompt(prompts: PromptTemplate[]): PromptTemplate {
|
||||||
const emptyPromptTexts = prompts.map(getEmptyPromptTxt);
|
const emptyPromptTexts = prompts.map(getEmptyPromptTxt);
|
||||||
const emptyPromptLengths = emptyPromptTexts.map((text) => text.length);
|
const emptyPromptLengths = emptyPromptTexts.map((text) => text.length);
|
||||||
const maxEmptyPromptLength = Math.max(...emptyPromptLengths);
|
const maxEmptyPromptLength = Math.max(...emptyPromptLengths);
|
||||||
const maxEmptyPromptIndex = emptyPromptLengths.indexOf(maxEmptyPromptLength);
|
const maxEmptyPromptIndex = emptyPromptLengths.indexOf(maxEmptyPromptLength);
|
||||||
return prompts[maxEmptyPromptIndex];
|
return prompts[maxEmptyPromptIndex]!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PromptHelperOptions = {
|
||||||
|
contextWindow?: number;
|
||||||
|
numOutput?: number;
|
||||||
|
chunkOverlapRatio?: number;
|
||||||
|
chunkSizeLimit?: number;
|
||||||
|
tokenizer?: Tokenizer;
|
||||||
|
separator?: string;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A collection of helper functions for working with prompts.
|
* A collection of helper functions for working with prompts.
|
||||||
*/
|
*/
|
||||||
@@ -38,19 +47,19 @@ export class PromptHelper {
|
|||||||
contextWindow = DEFAULT_CONTEXT_WINDOW;
|
contextWindow = DEFAULT_CONTEXT_WINDOW;
|
||||||
numOutput = DEFAULT_NUM_OUTPUTS;
|
numOutput = DEFAULT_NUM_OUTPUTS;
|
||||||
chunkOverlapRatio = DEFAULT_CHUNK_OVERLAP_RATIO;
|
chunkOverlapRatio = DEFAULT_CHUNK_OVERLAP_RATIO;
|
||||||
chunkSizeLimit?: number;
|
chunkSizeLimit: number | undefined;
|
||||||
tokenizer: Tokenizer;
|
tokenizer: Tokenizer;
|
||||||
separator = " ";
|
separator = " ";
|
||||||
|
|
||||||
// eslint-disable-next-line max-params
|
constructor(options: PromptHelperOptions = {}) {
|
||||||
constructor(
|
const {
|
||||||
contextWindow = DEFAULT_CONTEXT_WINDOW,
|
contextWindow = DEFAULT_CONTEXT_WINDOW,
|
||||||
numOutput = DEFAULT_NUM_OUTPUTS,
|
numOutput = DEFAULT_NUM_OUTPUTS,
|
||||||
chunkOverlapRatio = DEFAULT_CHUNK_OVERLAP_RATIO,
|
chunkOverlapRatio = DEFAULT_CHUNK_OVERLAP_RATIO,
|
||||||
chunkSizeLimit?: number,
|
chunkSizeLimit,
|
||||||
tokenizer?: Tokenizer,
|
tokenizer,
|
||||||
separator = " ",
|
separator = " ",
|
||||||
) {
|
} = options;
|
||||||
this.contextWindow = contextWindow;
|
this.contextWindow = contextWindow;
|
||||||
this.numOutput = numOutput;
|
this.numOutput = numOutput;
|
||||||
this.chunkOverlapRatio = chunkOverlapRatio;
|
this.chunkOverlapRatio = chunkOverlapRatio;
|
||||||
@@ -79,7 +88,7 @@ export class PromptHelper {
|
|||||||
prompt: PromptTemplate,
|
prompt: PromptTemplate,
|
||||||
numChunks = 1,
|
numChunks = 1,
|
||||||
padding = 5,
|
padding = 5,
|
||||||
) {
|
): number {
|
||||||
const availableContextSize = this.getAvailableContextSize(prompt);
|
const availableContextSize = this.getAvailableContextSize(prompt);
|
||||||
|
|
||||||
const result = Math.floor(availableContextSize / numChunks) - padding;
|
const result = Math.floor(availableContextSize / numChunks) - padding;
|
||||||
@@ -104,7 +113,12 @@ export class PromptHelper {
|
|||||||
throw new Error("Got 0 as available chunk size");
|
throw new Error("Got 0 as available chunk size");
|
||||||
}
|
}
|
||||||
const chunkOverlap = this.chunkOverlapRatio * chunkSize;
|
const chunkOverlap = this.chunkOverlapRatio * chunkSize;
|
||||||
return new SentenceSplitter({ chunkSize, chunkOverlap });
|
return new SentenceSplitter({
|
||||||
|
chunkSize,
|
||||||
|
chunkOverlap,
|
||||||
|
separator: this.separator,
|
||||||
|
tokenizer: this.tokenizer,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,7 +103,7 @@ export type LLMMetadata = {
|
|||||||
model: string;
|
model: string;
|
||||||
temperature: number;
|
temperature: number;
|
||||||
topP: number;
|
topP: number;
|
||||||
maxTokens?: number;
|
maxTokens?: number | undefined;
|
||||||
contextWindow: number;
|
contextWindow: number;
|
||||||
tokenizer: Tokenizers | undefined;
|
tokenizer: Tokenizers | undefined;
|
||||||
};
|
};
|
||||||
@@ -141,7 +141,7 @@ export interface LLMCompletionParamsStreaming extends LLMCompletionParamsBase {
|
|||||||
|
|
||||||
export interface LLMCompletionParamsNonStreaming
|
export interface LLMCompletionParamsNonStreaming
|
||||||
extends LLMCompletionParamsBase {
|
extends LLMCompletionParamsBase {
|
||||||
stream?: false | null;
|
stream?: false | null | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MessageContentTextDetail = {
|
export type MessageContentTextDetail = {
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ export abstract class MetadataAwareTextSplitter extends TextSplitter {
|
|||||||
throw new TypeError("`texts` and `metadata` must have the same length");
|
throw new TypeError("`texts` and `metadata` must have the same length");
|
||||||
}
|
}
|
||||||
return texts.flatMap((text, i) =>
|
return texts.flatMap((text, i) =>
|
||||||
this.splitTextMetadataAware(text, metadata[i]),
|
this.splitTextMetadataAware(text, metadata[i]!),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ export class MarkdownNodeParser extends NodeParser {
|
|||||||
}
|
}
|
||||||
metadata = this.updateMetadata(
|
metadata = this.updateMetadata(
|
||||||
metadata,
|
metadata,
|
||||||
headerMatch[2],
|
headerMatch[2]!,
|
||||||
headerMatch[1].trim().length,
|
headerMatch[1]!.trim().length,
|
||||||
);
|
);
|
||||||
currentSection = `${headerMatch[2]}\n`;
|
currentSection = `${headerMatch[2]}\n`;
|
||||||
} else {
|
} else {
|
||||||
@@ -63,7 +63,7 @@ export class MarkdownNodeParser extends NodeParser {
|
|||||||
for (let i = 1; i < newHeaderLevel; i++) {
|
for (let i = 1; i < newHeaderLevel; i++) {
|
||||||
const key = `Header_${i}`;
|
const key = `Header_${i}`;
|
||||||
if (key in headersMetadata) {
|
if (key in headersMetadata) {
|
||||||
updatedHeaders[key] = headersMetadata[key];
|
updatedHeaders[key] = headersMetadata[key]!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,10 +76,10 @@ export class MarkdownNodeParser extends NodeParser {
|
|||||||
node: TextNode,
|
node: TextNode,
|
||||||
metadata: Metadata,
|
metadata: Metadata,
|
||||||
): TextNode {
|
): TextNode {
|
||||||
const newNode = buildNodeFromSplits([textSplit], node, undefined)[0];
|
const newNode = buildNodeFromSplits([textSplit], node, undefined)[0]!;
|
||||||
|
|
||||||
if (this.includeMetadata) {
|
if (this.includeMetadata) {
|
||||||
newNode.metadata = { ...newNode.metadata, ...metadata };
|
newNode.metadata = { ...newNode!.metadata, ...metadata };
|
||||||
}
|
}
|
||||||
|
|
||||||
return newNode;
|
return newNode;
|
||||||
|
|||||||
@@ -168,9 +168,9 @@ export class SentenceSplitter extends MetadataAwareTextSplitter {
|
|||||||
let lastIndex = lastChunk.length - 1;
|
let lastIndex = lastChunk.length - 1;
|
||||||
while (
|
while (
|
||||||
lastIndex >= 0 &&
|
lastIndex >= 0 &&
|
||||||
currentChunkLength + lastChunk[lastIndex][1] <= this.chunkOverlap
|
currentChunkLength + lastChunk[lastIndex]![1] <= this.chunkOverlap
|
||||||
) {
|
) {
|
||||||
const [text, length] = lastChunk[lastIndex];
|
const [text, length] = lastChunk[lastIndex]!;
|
||||||
currentChunkLength += length;
|
currentChunkLength += length;
|
||||||
currentChunk.unshift([text, length]);
|
currentChunk.unshift([text, length]);
|
||||||
lastIndex -= 1;
|
lastIndex -= 1;
|
||||||
@@ -178,7 +178,7 @@ export class SentenceSplitter extends MetadataAwareTextSplitter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
while (splits.length > 0) {
|
while (splits.length > 0) {
|
||||||
const curSplit = splits[0];
|
const curSplit = splits[0]!;
|
||||||
if (curSplit.tokenSize > chunkSize) {
|
if (curSplit.tokenSize > chunkSize) {
|
||||||
throw new Error("Single token exceeded chunk size");
|
throw new Error("Single token exceeded chunk size");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ export type TextSplitterFn = (text: string) => string[];
|
|||||||
|
|
||||||
const truncateText = (text: string, textSplitter: TextSplitter): string => {
|
const truncateText = (text: string, textSplitter: TextSplitter): string => {
|
||||||
const chunks = textSplitter.splitText(text);
|
const chunks = textSplitter.splitText(text);
|
||||||
return chunks[0];
|
return chunks[0] ?? text;
|
||||||
};
|
};
|
||||||
|
|
||||||
const splitTextKeepSeparator = (text: string, separator: string): string[] => {
|
const splitTextKeepSeparator = (text: string, separator: string): string[] => {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export type BasePromptTemplateOptions<
|
|||||||
// loose type for better type inference
|
// loose type for better type inference
|
||||||
| readonly string[];
|
| readonly string[];
|
||||||
options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>;
|
options?: Partial<Record<TemplatesVar[number] | (string & {}), string>>;
|
||||||
outputParser?: BaseOutputParser;
|
outputParser?: BaseOutputParser | undefined;
|
||||||
templateVarMappings?: Partial<
|
templateVarMappings?: Partial<
|
||||||
Record<Vars[number] | (string & {}), TemplatesVar[number] | (string & {})>
|
Record<Vars[number] | (string & {}), TemplatesVar[number] | (string & {})>
|
||||||
>;
|
>;
|
||||||
@@ -34,7 +34,7 @@ export abstract class BasePromptTemplate<
|
|||||||
metadata: Metadata = {};
|
metadata: Metadata = {};
|
||||||
templateVars: Set<string> = new Set();
|
templateVars: Set<string> = new Set();
|
||||||
options: Partial<Record<TemplatesVar[number] | (string & {}), string>> = {};
|
options: Partial<Record<TemplatesVar[number] | (string & {}), string>> = {};
|
||||||
outputParser?: BaseOutputParser;
|
outputParser: BaseOutputParser | undefined;
|
||||||
templateVarMappings: Partial<
|
templateVarMappings: Partial<
|
||||||
Record<Vars[number] | (string & {}), TemplatesVar[number] | (string & {})>
|
Record<Vars[number] | (string & {}), TemplatesVar[number] | (string & {})>
|
||||||
> = {};
|
> = {};
|
||||||
|
|||||||
@@ -45,21 +45,21 @@ export abstract class PromptMixin {
|
|||||||
|
|
||||||
for (const key in prompts) {
|
for (const key in prompts) {
|
||||||
if (key.includes(":")) {
|
if (key.includes(":")) {
|
||||||
const [module_name, sub_key] = key.split(":");
|
const [moduleName, subKey] = key.split(":") as [string, string];
|
||||||
|
|
||||||
if (!subPrompt[module_name]) {
|
if (!subPrompt[moduleName]) {
|
||||||
subPrompt[module_name] = {};
|
subPrompt[moduleName] = {};
|
||||||
}
|
}
|
||||||
subPrompt[module_name][sub_key] = prompts[key];
|
subPrompt[moduleName][subKey] = prompts[key]!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [module_name, subPromptDict] of Object.entries(subPrompt)) {
|
for (const [moduleName, subPromptDict] of Object.entries(subPrompt)) {
|
||||||
if (!promptModules[module_name]) {
|
if (!promptModules[moduleName]) {
|
||||||
throw new Error(`Module ${module_name} not found.`);
|
throw new Error(`Module ${moduleName} not found.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const moduleToUpdate = promptModules[module_name];
|
const moduleToUpdate = promptModules[moduleName];
|
||||||
|
|
||||||
moduleToUpdate.updatePrompts(subPromptDict);
|
moduleToUpdate.updatePrompts(subPromptDict);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,13 +38,15 @@ export type RelatedNodeType<T extends Metadata = Metadata> =
|
|||||||
| RelatedNodeInfo<T>[];
|
| RelatedNodeInfo<T>[];
|
||||||
|
|
||||||
export type BaseNodeParams<T extends Metadata = Metadata> = {
|
export type BaseNodeParams<T extends Metadata = Metadata> = {
|
||||||
id_?: string;
|
id_?: string | undefined;
|
||||||
metadata?: T;
|
metadata?: T | undefined;
|
||||||
excludedEmbedMetadataKeys?: string[];
|
excludedEmbedMetadataKeys?: string[] | undefined;
|
||||||
excludedLlmMetadataKeys?: string[];
|
excludedLlmMetadataKeys?: string[] | undefined;
|
||||||
relationships?: Partial<Record<NodeRelationship, RelatedNodeType<T>>>;
|
relationships?:
|
||||||
hash?: string;
|
| Partial<Record<NodeRelationship, RelatedNodeType<T>>>
|
||||||
embedding?: number[];
|
| undefined;
|
||||||
|
hash?: string | undefined;
|
||||||
|
embedding?: number[] | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,7 +60,7 @@ export abstract class BaseNode<T extends Metadata = Metadata> {
|
|||||||
* Set to a UUID by default.
|
* Set to a UUID by default.
|
||||||
*/
|
*/
|
||||||
id_: string;
|
id_: string;
|
||||||
embedding?: number[];
|
embedding: number[] | undefined;
|
||||||
|
|
||||||
// Metadata fields
|
// Metadata fields
|
||||||
metadata: T;
|
metadata: T;
|
||||||
@@ -198,11 +200,11 @@ export abstract class BaseNode<T extends Metadata = Metadata> {
|
|||||||
|
|
||||||
export type TextNodeParams<T extends Metadata = Metadata> =
|
export type TextNodeParams<T extends Metadata = Metadata> =
|
||||||
BaseNodeParams<T> & {
|
BaseNodeParams<T> & {
|
||||||
text?: string;
|
text?: string | undefined;
|
||||||
textTemplate?: string;
|
textTemplate?: string | undefined;
|
||||||
startCharIdx?: number;
|
startCharIdx?: number | undefined;
|
||||||
endCharIdx?: number;
|
endCharIdx?: number | undefined;
|
||||||
metadataSeparator?: string;
|
metadataSeparator?: string | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -418,7 +420,7 @@ export class ImageDocument<T extends Metadata = Metadata> extends ImageNode<T> {
|
|||||||
*/
|
*/
|
||||||
export interface NodeWithScore<T extends Metadata = Metadata> {
|
export interface NodeWithScore<T extends Metadata = Metadata> {
|
||||||
node: BaseNode<T>;
|
node: BaseNode<T>;
|
||||||
score?: number;
|
score?: number | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum ModalityType {
|
export enum ModalityType {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { extractText } from "../../utils";
|
|||||||
import type { Metadata, NodeWithScore } from "../node";
|
import type { Metadata, NodeWithScore } from "../node";
|
||||||
|
|
||||||
export class EngineResponse implements ChatResponse, ChatResponseChunk {
|
export class EngineResponse implements ChatResponse, ChatResponseChunk {
|
||||||
sourceNodes?: NodeWithScore[];
|
sourceNodes: NodeWithScore[] | undefined;
|
||||||
|
|
||||||
metadata: Metadata = {};
|
metadata: Metadata = {};
|
||||||
|
|
||||||
|
|||||||
@@ -74,12 +74,12 @@ export const extractDataUrlComponents = (
|
|||||||
} => {
|
} => {
|
||||||
const parts = dataUrl.split(";base64,");
|
const parts = dataUrl.split(";base64,");
|
||||||
|
|
||||||
if (parts.length !== 2 || !parts[0].startsWith("data:")) {
|
if (parts.length !== 2 || !parts[0]!.startsWith("data:")) {
|
||||||
throw new Error("Invalid data URL");
|
throw new Error("Invalid data URL");
|
||||||
}
|
}
|
||||||
|
|
||||||
const mimeType = parts[0].slice(5);
|
const mimeType = parts[0]!.slice(5);
|
||||||
const base64 = parts[1];
|
const base64 = parts[1]!;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
mimeType,
|
mimeType,
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ Header 2 content
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
expect(splits.length).toBe(2);
|
expect(splits.length).toBe(2);
|
||||||
expect(splits[0].metadata).toEqual({ Header_1: "Main Header" });
|
expect(splits[0]!.metadata).toEqual({ Header_1: "Main Header" });
|
||||||
expect(splits[1].metadata).toEqual({ Header_1: "Header 2" });
|
expect(splits[1]!.metadata).toEqual({ Header_1: "Header 2" });
|
||||||
expect(splits[0].getContent(MetadataMode.NONE)).toStrictEqual(
|
expect(splits[0]!.getContent(MetadataMode.NONE)).toStrictEqual(
|
||||||
"Main Header\n\nHeader 1 content",
|
"Main Header\n\nHeader 1 content",
|
||||||
);
|
);
|
||||||
expect(splits[1].getContent(MetadataMode.NONE)).toStrictEqual(
|
expect(splits[1]!.getContent(MetadataMode.NONE)).toStrictEqual(
|
||||||
"Header 2\nHeader 2 content",
|
"Header 2\nHeader 2 content",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -89,16 +89,16 @@ Content
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
expect(splits.length).toBe(4);
|
expect(splits.length).toBe(4);
|
||||||
expect(splits[0].metadata).toEqual({ Header_1: "Main Header" });
|
expect(splits[0]!.metadata).toEqual({ Header_1: "Main Header" });
|
||||||
expect(splits[1].metadata).toEqual({
|
expect(splits[1]!.metadata).toEqual({
|
||||||
Header_1: "Main Header",
|
Header_1: "Main Header",
|
||||||
Header_2: "Sub-header",
|
Header_2: "Sub-header",
|
||||||
});
|
});
|
||||||
expect(splits[2].metadata).toEqual({
|
expect(splits[2]!.metadata).toEqual({
|
||||||
Header_1: "Main Header",
|
Header_1: "Main Header",
|
||||||
Header_2: "Sub-header",
|
Header_2: "Sub-header",
|
||||||
Header_3: "Sub-sub header",
|
Header_3: "Sub-sub header",
|
||||||
});
|
});
|
||||||
expect(splits[3].metadata).toEqual({ Header_1: "New title" });
|
expect(splits[3]!.metadata).toEqual({ Header_1: "New title" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ describe("SentenceSplitter", () => {
|
|||||||
});
|
});
|
||||||
const result = sentenceSplitter.getNodesFromDocuments([doc]);
|
const result = sentenceSplitter.getNodesFromDocuments([doc]);
|
||||||
expect(result.length).toEqual(1);
|
expect(result.length).toEqual(1);
|
||||||
const node = result[0];
|
const node = result[0]!;
|
||||||
// check not the same object
|
// check not the same object
|
||||||
expect(node.metadata).not.toBe(doc.metadata);
|
expect(node.metadata).not.toBe(doc.metadata);
|
||||||
expect(node.excludedLlmMetadataKeys).not.toBe(doc.excludedLlmMetadataKeys);
|
expect(node.excludedLlmMetadataKeys).not.toBe(doc.excludedLlmMetadataKeys);
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# @llamaindex/experimental
|
# @llamaindex/experimental
|
||||||
|
|
||||||
|
## 0.0.75
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.0.74
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.0.73
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.0.72
|
## 0.0.72
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/experimental",
|
"name": "@llamaindex/experimental",
|
||||||
"description": "Experimental package for LlamaIndexTS",
|
"description": "Experimental package for LlamaIndexTS",
|
||||||
"version": "0.0.72",
|
"version": "0.0.75",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"types": "dist/type/index.d.ts",
|
"types": "dist/type/index.d.ts",
|
||||||
"main": "dist/cjs/index.js",
|
"main": "dist/cjs/index.js",
|
||||||
|
|||||||
@@ -1,5 +1,31 @@
|
|||||||
# llamaindex
|
# llamaindex
|
||||||
|
|
||||||
|
## 0.5.25
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 4810364: fix: handle `RouterQueryEngine` with string query
|
||||||
|
- d3bc663: refactor: export vector store only in nodejs environment on top level
|
||||||
|
|
||||||
|
If you see some missing modules error, please change vector store related imports to `llamaindex/vector-store`
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- @llamaindex/cloud@0.2.4
|
||||||
|
|
||||||
|
## 0.5.24
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [0bf8d80]
|
||||||
|
- @llamaindex/cloud@0.2.3
|
||||||
|
|
||||||
|
## 0.5.23
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [711c814]
|
||||||
|
- @llamaindex/core@0.1.12
|
||||||
|
|
||||||
## 0.5.22
|
## 0.5.22
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# @llamaindex/cloudflare-worker-agent-test
|
# @llamaindex/cloudflare-worker-agent-test
|
||||||
|
|
||||||
|
## 0.0.59
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.0.58
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.0.57
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.0.56
|
## 0.0.56
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/cloudflare-worker-agent-test",
|
"name": "@llamaindex/cloudflare-worker-agent-test",
|
||||||
"version": "0.0.56",
|
"version": "0.0.59",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# @llamaindex/next-agent-test
|
# @llamaindex/next-agent-test
|
||||||
|
|
||||||
|
## 0.1.59
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.1.58
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.1.57
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.1.56
|
## 0.1.56
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/next-agent-test",
|
"name": "@llamaindex/next-agent-test",
|
||||||
"version": "0.1.56",
|
"version": "0.1.59",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# test-edge-runtime
|
# test-edge-runtime
|
||||||
|
|
||||||
|
## 0.1.58
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.1.57
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.1.56
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.1.55
|
## 0.1.55
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/nextjs-edge-runtime-test",
|
"name": "@llamaindex/nextjs-edge-runtime-test",
|
||||||
"version": "0.1.55",
|
"version": "0.1.58",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# @llamaindex/next-node-runtime
|
# @llamaindex/next-node-runtime
|
||||||
|
|
||||||
|
## 0.0.40
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.0.39
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.0.38
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.0.37
|
## 0.0.37
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/next-node-runtime-test",
|
"name": "@llamaindex/next-node-runtime-test",
|
||||||
"version": "0.0.37",
|
"version": "0.0.40",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev",
|
"dev": "next dev",
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
# @llamaindex/waku-query-engine-test
|
# @llamaindex/waku-query-engine-test
|
||||||
|
|
||||||
|
## 0.0.59
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- Updated dependencies [4810364]
|
||||||
|
- Updated dependencies [d3bc663]
|
||||||
|
- llamaindex@0.5.25
|
||||||
|
|
||||||
|
## 0.0.58
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.24
|
||||||
|
|
||||||
|
## 0.0.57
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- llamaindex@0.5.23
|
||||||
|
|
||||||
## 0.0.56
|
## 0.0.56
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@llamaindex/waku-query-engine-test",
|
"name": "@llamaindex/waku-query-engine-test",
|
||||||
"version": "0.0.56",
|
"version": "0.0.59",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export class OpenAIEmbedding
|
|||||||
extends TransformComponent
|
extends TransformComponent
|
||||||
implements BaseEmbedding
|
implements BaseEmbedding
|
||||||
{
|
{
|
||||||
embedInfo?: EmbeddingInfo | undefined;
|
embedInfo?: EmbeddingInfo;
|
||||||
embedBatchSize = 512;
|
embedBatchSize = 512;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ export class OpenAI implements LLM {
|
|||||||
llmCompleteMockStorage.llmEventStart.shift()!["messages"];
|
llmCompleteMockStorage.llmEventStart.shift()!["messages"];
|
||||||
strictEqual(params.messages.length, chatMessage.length);
|
strictEqual(params.messages.length, chatMessage.length);
|
||||||
for (let i = 0; i < chatMessage.length; i++) {
|
for (let i = 0; i < chatMessage.length; i++) {
|
||||||
strictEqual(params.messages[i].role, chatMessage[i].role);
|
strictEqual(params.messages[i]!.role, chatMessage[i]!.role);
|
||||||
deepStrictEqual(params.messages[i].content, chatMessage[i].content);
|
deepStrictEqual(params.messages[i]!.content, chatMessage[i]!.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (llmCompleteMockStorage.llmEventEnd.length > 0) {
|
if (llmCompleteMockStorage.llmEventEnd.length > 0) {
|
||||||
@@ -64,7 +64,7 @@ export class OpenAI implements LLM {
|
|||||||
if (idx === -1) {
|
if (idx === -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const chunk = llmCompleteMockStorage.llmEventStream[idx].chunk;
|
const chunk = llmCompleteMockStorage.llmEventStream[idx]!.chunk;
|
||||||
llmCompleteMockStorage.llmEventStream.splice(idx, 1);
|
llmCompleteMockStorage.llmEventStream.splice(idx, 1);
|
||||||
yield chunk;
|
yield chunk;
|
||||||
}
|
}
|
||||||
@@ -90,8 +90,8 @@ export class OpenAI implements LLM {
|
|||||||
const chatMessage =
|
const chatMessage =
|
||||||
llmCompleteMockStorage.llmEventStart.shift()!["messages"];
|
llmCompleteMockStorage.llmEventStart.shift()!["messages"];
|
||||||
strictEqual(1, chatMessage.length);
|
strictEqual(1, chatMessage.length);
|
||||||
strictEqual("user", chatMessage[0].role);
|
strictEqual("user", chatMessage[0]!.role);
|
||||||
strictEqual(params.prompt, chatMessage[0].content);
|
strictEqual(params.prompt, chatMessage[0]!.content);
|
||||||
}
|
}
|
||||||
if (llmCompleteMockStorage.llmEventEnd.length > 0) {
|
if (llmCompleteMockStorage.llmEventEnd.length > 0) {
|
||||||
const response = llmCompleteMockStorage.llmEventEnd.shift()!["response"];
|
const response = llmCompleteMockStorage.llmEventEnd.shift()!["response"];
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import { LLMSingleSelector, Settings } from "llamaindex";
|
||||||
|
import assert from "node:assert";
|
||||||
|
import { test } from "node:test";
|
||||||
|
import { mockLLMEvent } from "./utils.js";
|
||||||
|
|
||||||
|
await test("#1177", async (t) => {
|
||||||
|
await mockLLMEvent(t, "#1177");
|
||||||
|
await t.test(async () => {
|
||||||
|
const selector = new LLMSingleSelector({
|
||||||
|
llm: Settings.llm,
|
||||||
|
});
|
||||||
|
{
|
||||||
|
const result = await selector.select(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
description: "Math calculation",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Search from google",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"calculate 2 + 2",
|
||||||
|
);
|
||||||
|
assert.equal(result.selections.length, 1);
|
||||||
|
assert.equal(result.selections.at(0)!.index, 0);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const result = await selector.select(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
description: "Math calculation",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Search from google",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{
|
||||||
|
query: "calculate 2 + 2",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert.equal(result.selections.length, 1);
|
||||||
|
assert.equal(result.selections.at(0)!.index, 0);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const result = await selector.select(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
description: "Math calculation",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Search from google",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{
|
||||||
|
query: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "calculate 2 + 2",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assert.equal(result.selections.length, 1);
|
||||||
|
assert.equal(result.selections.at(0)!.index, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -163,11 +163,11 @@ For questions about more specific sections, please use the vector_tool.`,
|
|||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
const originalCall = queryEngineTools[1].call.bind(queryEngineTools[1]);
|
const originalCall = queryEngineTools[1]!.call.bind(queryEngineTools[1]);
|
||||||
const mockCall = t.mock.fn(({ query }: { query: string }) => {
|
const mockCall = t.mock.fn(({ query }: { query: string }) => {
|
||||||
return originalCall({ query });
|
return originalCall({ query });
|
||||||
});
|
});
|
||||||
queryEngineTools[1].call = mockCall;
|
queryEngineTools[1]!.call = mockCall;
|
||||||
|
|
||||||
const toolMapping = SimpleToolNodeMapping.fromObjects(queryEngineTools);
|
const toolMapping = SimpleToolNodeMapping.fromObjects(queryEngineTools);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
{
|
||||||
|
"llmEventStart": [
|
||||||
|
{
|
||||||
|
"id": "PRESERVE_0",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"content": "Some choices are given below. It is provided in a numbered list (1 to 42), where each item in the list corresponds to a summary.\n---------------------\n(1) Math calculation(2) Search from google\n---------------------\nUsing only the choices above and not prior knowledge, return the choice that is most relevant to the question: 'calculate 2 + 2'\n\n\nThe output should be ONLY JSON formatted as a JSON instance.\n\nHere is an example:\n[\n {\n \"choice\": 1,\n \"reason\": \"<insert reason for choice>\"\n },\n ...\n]\n",
|
||||||
|
"role": "user"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "PRESERVE_1",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"content": "Some choices are given below. It is provided in a numbered list (1 to 42), where each item in the list corresponds to a summary.\n---------------------\n(1) Math calculation(2) Search from google\n---------------------\nUsing only the choices above and not prior knowledge, return the choice that is most relevant to the question: 'calculate 2 + 2'\n\n\nThe output should be ONLY JSON formatted as a JSON instance.\n\nHere is an example:\n[\n {\n \"choice\": 1,\n \"reason\": \"<insert reason for choice>\"\n },\n ...\n]\n",
|
||||||
|
"role": "user"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "PRESERVE_2",
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"content": "Some choices are given below. It is provided in a numbered list (1 to 42), where each item in the list corresponds to a summary.\n---------------------\n(1) Math calculation(2) Search from google\n---------------------\nUsing only the choices above and not prior knowledge, return the choice that is most relevant to the question: 'calculate 2 + 2'\n\n\nThe output should be ONLY JSON formatted as a JSON instance.\n\nHere is an example:\n[\n {\n \"choice\": 1,\n \"reason\": \"<insert reason for choice>\"\n },\n ...\n]\n",
|
||||||
|
"role": "user"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"llmEventEnd": [
|
||||||
|
{
|
||||||
|
"id": "PRESERVE_0",
|
||||||
|
"response": {
|
||||||
|
"raw": null,
|
||||||
|
"message": {
|
||||||
|
"content": "[\n {\n \"choice\": 1,\n \"reason\": \"The question 'calculate 2 + 2' is directly asking for a math calculation, which corresponds to choice 1.\"\n }\n]",
|
||||||
|
"role": "assistant",
|
||||||
|
"options": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "PRESERVE_1",
|
||||||
|
"response": {
|
||||||
|
"raw": null,
|
||||||
|
"message": {
|
||||||
|
"content": "[\n {\n \"choice\": 1,\n \"reason\": \"The question 'calculate 2 + 2' is asking for a mathematical calculation, which directly corresponds to choice 1: Math calculation.\"\n }\n]",
|
||||||
|
"role": "assistant",
|
||||||
|
"options": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "PRESERVE_2",
|
||||||
|
"response": {
|
||||||
|
"raw": null,
|
||||||
|
"message": {
|
||||||
|
"content": "[\n {\n \"choice\": 1,\n \"reason\": \"The question 'calculate 2 + 2' is asking for a mathematical calculation, which directly corresponds to choice 1: Math calculation.\"\n }\n]",
|
||||||
|
"role": "assistant",
|
||||||
|
"options": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"llmEventStream": []
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "llamaindex",
|
"name": "llamaindex",
|
||||||
"version": "0.5.22",
|
"version": "0.5.25",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -59,8 +59,6 @@
|
|||||||
"openai": "^4.57.0",
|
"openai": "^4.57.0",
|
||||||
"papaparse": "^5.4.1",
|
"papaparse": "^5.4.1",
|
||||||
"pathe": "^1.1.2",
|
"pathe": "^1.1.2",
|
||||||
"pg": "^8.12.0",
|
|
||||||
"pgvector": "^0.2.0",
|
|
||||||
"portkey-ai": "0.1.16",
|
"portkey-ai": "0.1.16",
|
||||||
"rake-modified": "^1.0.8",
|
"rake-modified": "^1.0.8",
|
||||||
"string-strip-html": "^13.4.8",
|
"string-strip-html": "^13.4.8",
|
||||||
@@ -72,11 +70,19 @@
|
|||||||
"zod": "^3.23.8"
|
"zod": "^3.23.8"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@notionhq/client": "^2.2.15"
|
"@notionhq/client": "^2.2.15",
|
||||||
|
"pg": "^8.12.0",
|
||||||
|
"pgvector": "0.2.0"
|
||||||
},
|
},
|
||||||
"peerDependenciesMeta": {
|
"peerDependenciesMeta": {
|
||||||
"@notionhq/client": {
|
"@notionhq/client": {
|
||||||
"optional": true
|
"optional": true
|
||||||
|
},
|
||||||
|
"pg": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"pgvector": {
|
||||||
|
"optional": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -85,6 +91,8 @@
|
|||||||
"@swc/core": "^1.7.22",
|
"@swc/core": "^1.7.22",
|
||||||
"concurrently": "^8.2.2",
|
"concurrently": "^8.2.2",
|
||||||
"glob": "^11.0.0",
|
"glob": "^11.0.0",
|
||||||
|
"pg": "^8.12.0",
|
||||||
|
"pgvector": "0.2.0",
|
||||||
"typescript": "^5.5.4"
|
"typescript": "^5.5.4"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export class SimpleChatHistory extends ChatHistory {
|
|||||||
messages: ChatMessage[];
|
messages: ChatMessage[];
|
||||||
private messagesBefore: number;
|
private messagesBefore: number;
|
||||||
|
|
||||||
constructor(init?: Partial<SimpleChatHistory>) {
|
constructor(init?: { messages?: ChatMessage[] | undefined }) {
|
||||||
super();
|
super();
|
||||||
this.messages = init?.messages ?? [];
|
this.messages = init?.messages ?? [];
|
||||||
this.messagesBefore = this.messages.length;
|
this.messagesBefore = this.messages.length;
|
||||||
@@ -118,7 +118,7 @@ export class SummaryChatHistory extends ChatHistory {
|
|||||||
// remove oldest message until the chat history is short enough for the context window
|
// remove oldest message until the chat history is short enough for the context window
|
||||||
messagesToSummarize.shift();
|
messagesToSummarize.shift();
|
||||||
} while (
|
} while (
|
||||||
this.tokenizer.encode(promptMessages[0].content).length >
|
this.tokenizer.encode(promptMessages[0]!.content).length >
|
||||||
this.tokensToSummarize
|
this.tokensToSummarize
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ export class SummaryChatHistory extends ChatHistory {
|
|||||||
|
|
||||||
public getLastSummary(): ChatMessage | null {
|
public getLastSummary(): ChatMessage | null {
|
||||||
const lastSummaryIndex = this.getLastSummaryIndex();
|
const lastSummaryIndex = this.getLastSummaryIndex();
|
||||||
return lastSummaryIndex ? this.messages[lastSummaryIndex] : null;
|
return lastSummaryIndex ? this.messages[lastSummaryIndex]! : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private get systemMessages() {
|
private get systemMessages() {
|
||||||
@@ -174,10 +174,10 @@ export class SummaryChatHistory extends ChatHistory {
|
|||||||
// and convert summary message so it can be send to the LLM
|
// and convert summary message so it can be send to the LLM
|
||||||
const summaryMessage: ChatMessage = transformSummary
|
const summaryMessage: ChatMessage = transformSummary
|
||||||
? {
|
? {
|
||||||
content: `Summary of the conversation so far: ${this.messages[lastSummaryIndex].content}`,
|
content: `Summary of the conversation so far: ${this.messages[lastSummaryIndex]!.content}`,
|
||||||
role: "system",
|
role: "system",
|
||||||
}
|
}
|
||||||
: this.messages[lastSummaryIndex];
|
: this.messages[lastSummaryIndex]!;
|
||||||
return [summaryMessage, ...this.messages.slice(lastSummaryIndex + 1)];
|
return [summaryMessage, ...this.messages.slice(lastSummaryIndex + 1)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ export type RetrieveParams = {
|
|||||||
export interface BaseRetriever {
|
export interface BaseRetriever {
|
||||||
retrieve(params: RetrieveParams): Promise<NodeWithScore[]>;
|
retrieve(params: RetrieveParams): Promise<NodeWithScore[]>;
|
||||||
|
|
||||||
// to be deprecated soon
|
/**
|
||||||
serviceContext?: ServiceContext;
|
* @deprecated to be deprecated soon
|
||||||
|
*/
|
||||||
|
serviceContext?: ServiceContext | undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type { BaseEmbedding } from "@llamaindex/core/embeddings";
|
import type { BaseEmbedding } from "@llamaindex/core/embeddings";
|
||||||
|
import { PromptHelper } from "@llamaindex/core/indices";
|
||||||
import type { LLM } from "@llamaindex/core/llms";
|
import type { LLM } from "@llamaindex/core/llms";
|
||||||
import {
|
import {
|
||||||
type NodeParser,
|
type NodeParser,
|
||||||
SentenceSplitter,
|
SentenceSplitter,
|
||||||
} from "@llamaindex/core/node-parser";
|
} from "@llamaindex/core/node-parser";
|
||||||
import { PromptHelper } from "./PromptHelper.js";
|
|
||||||
import { OpenAIEmbedding } from "./embeddings/OpenAIEmbedding.js";
|
import { OpenAIEmbedding } from "./embeddings/OpenAIEmbedding.js";
|
||||||
import { OpenAI } from "./llm/openai.js";
|
import { OpenAI } from "./llm/openai.js";
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
} from "@llamaindex/core/global";
|
} from "@llamaindex/core/global";
|
||||||
import { OpenAI } from "./llm/openai.js";
|
import { OpenAI } from "./llm/openai.js";
|
||||||
|
|
||||||
import { PromptHelper } from "./PromptHelper.js";
|
import { PromptHelper } from "@llamaindex/core/indices";
|
||||||
|
|
||||||
import type { BaseEmbedding } from "@llamaindex/core/embeddings";
|
import type { BaseEmbedding } from "@llamaindex/core/embeddings";
|
||||||
import type { LLM } from "@llamaindex/core/llms";
|
import type { LLM } from "@llamaindex/core/llms";
|
||||||
@@ -27,13 +27,12 @@ export type PromptConfig = {
|
|||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
prompt: PromptConfig;
|
prompt: PromptConfig;
|
||||||
llm: LLM | null;
|
|
||||||
promptHelper: PromptHelper | null;
|
promptHelper: PromptHelper | null;
|
||||||
embedModel: BaseEmbedding | null;
|
embedModel: BaseEmbedding | null;
|
||||||
nodeParser: NodeParser | null;
|
nodeParser: NodeParser | null;
|
||||||
callbackManager: CallbackManager | null;
|
callbackManager: CallbackManager | null;
|
||||||
chunkSize?: number;
|
chunkSize: number | undefined;
|
||||||
chunkOverlap?: number;
|
chunkOverlap: number | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,12 +40,10 @@ export interface Config {
|
|||||||
*/
|
*/
|
||||||
class GlobalSettings implements Config {
|
class GlobalSettings implements Config {
|
||||||
#prompt: PromptConfig = {};
|
#prompt: PromptConfig = {};
|
||||||
#llm: LLM | null = null;
|
|
||||||
#promptHelper: PromptHelper | null = null;
|
#promptHelper: PromptHelper | null = null;
|
||||||
#nodeParser: NodeParser | null = null;
|
#nodeParser: NodeParser | null = null;
|
||||||
#chunkOverlap?: number;
|
#chunkOverlap?: number;
|
||||||
|
|
||||||
#llmAsyncLocalStorage = new AsyncLocalStorage<LLM>();
|
|
||||||
#promptHelperAsyncLocalStorage = new AsyncLocalStorage<PromptHelper>();
|
#promptHelperAsyncLocalStorage = new AsyncLocalStorage<PromptHelper>();
|
||||||
#nodeParserAsyncLocalStorage = new AsyncLocalStorage<NodeParser>();
|
#nodeParserAsyncLocalStorage = new AsyncLocalStorage<NodeParser>();
|
||||||
#chunkOverlapAsyncLocalStorage = new AsyncLocalStorage<number>();
|
#chunkOverlapAsyncLocalStorage = new AsyncLocalStorage<number>();
|
||||||
@@ -62,19 +59,21 @@ class GlobalSettings implements Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get llm(): LLM {
|
get llm(): LLM {
|
||||||
if (this.#llm === null) {
|
// fixme: we might need check internal error instead of try-catch here
|
||||||
this.#llm = new OpenAI();
|
try {
|
||||||
|
CoreSettings.llm;
|
||||||
|
} catch (error) {
|
||||||
|
CoreSettings.llm = new OpenAI();
|
||||||
}
|
}
|
||||||
|
return CoreSettings.llm;
|
||||||
return this.#llmAsyncLocalStorage.getStore() ?? this.#llm;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
set llm(llm: LLM) {
|
set llm(llm: LLM) {
|
||||||
this.#llm = llm;
|
CoreSettings.llm = llm;
|
||||||
}
|
}
|
||||||
|
|
||||||
withLLM<Result>(llm: LLM, fn: () => Result): Result {
|
withLLM<Result>(llm: LLM, fn: () => Result): Result {
|
||||||
return this.#llmAsyncLocalStorage.run(llm, fn);
|
return CoreSettings.withLLM(llm, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
get promptHelper(): PromptHelper {
|
get promptHelper(): PromptHelper {
|
||||||
@@ -159,7 +158,9 @@ class GlobalSettings implements Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
set chunkOverlap(chunkOverlap: number | undefined) {
|
set chunkOverlap(chunkOverlap: number | undefined) {
|
||||||
this.#chunkOverlap = chunkOverlap;
|
if (typeof chunkOverlap === "number") {
|
||||||
|
this.#chunkOverlap = chunkOverlap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
withChunkOverlap<Result>(chunkOverlap: number, fn: () => Result): Result {
|
withChunkOverlap<Result>(chunkOverlap: number, fn: () => Result): Result {
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export function createTaskOutputStream<
|
|||||||
nextSteps: new Set(),
|
nextSteps: new Set(),
|
||||||
};
|
};
|
||||||
if (steps.length > 0) {
|
if (steps.length > 0) {
|
||||||
step.prevStep = steps[steps.length - 1];
|
step.prevStep = steps[steps.length - 1]!;
|
||||||
}
|
}
|
||||||
const taskOutputs: TaskStepOutput<
|
const taskOutputs: TaskStepOutput<
|
||||||
Model,
|
Model,
|
||||||
@@ -77,7 +77,7 @@ export function createTaskOutputStream<
|
|||||||
context.logger.log("Finished step(id, %s).", step.id);
|
context.logger.log("Finished step(id, %s).", step.id);
|
||||||
// fixme: support multi-thread when there are multiple outputs
|
// fixme: support multi-thread when there are multiple outputs
|
||||||
// todo: for now we pretend there is only one task output
|
// todo: for now we pretend there is only one task output
|
||||||
const { isLast, taskStep } = taskOutputs[0];
|
const { isLast, taskStep } = taskOutputs[0]!;
|
||||||
context = {
|
context = {
|
||||||
...taskStep.context,
|
...taskStep.context,
|
||||||
store: {
|
store: {
|
||||||
|
|||||||
@@ -89,8 +89,8 @@ function extractFinalResponse(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const thought = match[1].trim();
|
const thought = match[1]!.trim();
|
||||||
const answer = match[2].trim();
|
const answer = match[2]!.trim();
|
||||||
return [thought, answer];
|
return [thought, answer];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,9 +108,9 @@ function extractToolUse(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const thought = match[1].trim();
|
const thought = match[1]!.trim();
|
||||||
const action = match[2].trim();
|
const action = match[2]!.trim();
|
||||||
const actionInput = match[3].trim();
|
const actionInput = match[3]!.trim();
|
||||||
return [thought, action, actionInput];
|
return [thought, action, actionInput];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,14 @@ export class LLamaCloudFileService {
|
|||||||
public static async getAllProjectsWithPipelines() {
|
public static async getAllProjectsWithPipelines() {
|
||||||
initService();
|
initService();
|
||||||
try {
|
try {
|
||||||
const projects = await ProjectsService.listProjectsApiV1ProjectsGet();
|
const { data: projects } =
|
||||||
const pipelines =
|
await ProjectsService.listProjectsApiV1ProjectsGet({
|
||||||
await PipelinesService.searchPipelinesApiV1PipelinesGet();
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
const { data: pipelines } =
|
||||||
|
await PipelinesService.searchPipelinesApiV1PipelinesGet({
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
return projects.map((project) => ({
|
return projects.map((project) => ({
|
||||||
...project,
|
...project,
|
||||||
pipelines: pipelines.filter((p) => p.project_id === project.id),
|
pipelines: pipelines.filter((p) => p.project_id === project.id),
|
||||||
@@ -35,11 +40,12 @@ export class LLamaCloudFileService {
|
|||||||
customMetadata: Record<string, any> = {},
|
customMetadata: Record<string, any> = {},
|
||||||
) {
|
) {
|
||||||
initService();
|
initService();
|
||||||
const file = await FilesService.uploadFileApiV1FilesPost({
|
const { data: file } = await FilesService.uploadFileApiV1FilesPost({
|
||||||
projectId,
|
path: { project_id: projectId },
|
||||||
formData: {
|
body: {
|
||||||
upload_file: uploadFile,
|
upload_file: uploadFile,
|
||||||
},
|
},
|
||||||
|
throwOnError: true,
|
||||||
});
|
});
|
||||||
const files = [
|
const files = [
|
||||||
{
|
{
|
||||||
@@ -48,19 +54,24 @@ export class LLamaCloudFileService {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
await PipelinesService.addFilesToPipelineApiV1PipelinesPipelineIdFilesPut({
|
await PipelinesService.addFilesToPipelineApiV1PipelinesPipelineIdFilesPut({
|
||||||
pipelineId,
|
path: {
|
||||||
requestBody: files,
|
pipeline_id: pipelineId,
|
||||||
|
},
|
||||||
|
body: files,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait 2s for the file to be processed
|
// Wait 2s for the file to be processed
|
||||||
const maxAttempts = 20;
|
const maxAttempts = 20;
|
||||||
let attempt = 0;
|
let attempt = 0;
|
||||||
while (attempt < maxAttempts) {
|
while (attempt < maxAttempts) {
|
||||||
const result =
|
const { data: result } =
|
||||||
await PipelinesService.getPipelineFileStatusApiV1PipelinesPipelineIdFilesFileIdStatusGet(
|
await PipelinesService.getPipelineFileStatusApiV1PipelinesPipelineIdFilesFileIdStatusGet(
|
||||||
{
|
{
|
||||||
pipelineId,
|
path: {
|
||||||
fileId: file.id,
|
pipeline_id: pipelineId,
|
||||||
|
file_id: file.id,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if (result.status === "ERROR") {
|
if (result.status === "ERROR") {
|
||||||
@@ -83,16 +94,24 @@ export class LLamaCloudFileService {
|
|||||||
*/
|
*/
|
||||||
public static async getFileUrl(pipelineId: string, filename: string) {
|
public static async getFileUrl(pipelineId: string, filename: string) {
|
||||||
initService();
|
initService();
|
||||||
const allPipelineFiles =
|
const { data: allPipelineFiles } =
|
||||||
await PipelinesService.listPipelineFilesApiV1PipelinesPipelineIdFilesGet({
|
await PipelinesService.listPipelineFilesApiV1PipelinesPipelineIdFilesGet({
|
||||||
pipelineId,
|
path: {
|
||||||
|
pipeline_id: pipelineId,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
});
|
});
|
||||||
const file = allPipelineFiles.find((file) => file.name === filename);
|
const file = allPipelineFiles.find((file) => file.name === filename);
|
||||||
if (!file?.file_id) return null;
|
if (!file?.file_id) return null;
|
||||||
const fileContent =
|
const { data: fileContent } =
|
||||||
await FilesService.readFileContentApiV1FilesIdContentGet({
|
await FilesService.readFileContentApiV1FilesIdContentGet({
|
||||||
id: file.file_id,
|
path: {
|
||||||
projectId: file.project_id,
|
id: file.file_id,
|
||||||
|
},
|
||||||
|
query: {
|
||||||
|
project_id: file.project_id,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
});
|
});
|
||||||
return fileContent.url;
|
return fileContent.url;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,10 +38,13 @@ export class LlamaCloudIndex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const pipelineStatus =
|
const { data: pipelineStatus } =
|
||||||
await PipelinesService.getPipelineStatusApiV1PipelinesPipelineIdStatusGet(
|
await PipelinesService.getPipelineStatusApiV1PipelinesPipelineIdStatusGet(
|
||||||
{
|
{
|
||||||
pipelineId,
|
path: {
|
||||||
|
pipeline_id: pipelineId,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -90,9 +93,14 @@ export class LlamaCloudIndex {
|
|||||||
const docsToRemove = new Set<string>();
|
const docsToRemove = new Set<string>();
|
||||||
|
|
||||||
for (const doc of pendingDocs) {
|
for (const doc of pendingDocs) {
|
||||||
const { status } =
|
const {
|
||||||
|
data: { status },
|
||||||
|
} =
|
||||||
await PipelinesService.getPipelineDocumentStatusApiV1PipelinesPipelineIdDocumentsDocumentIdStatusGet(
|
await PipelinesService.getPipelineDocumentStatusApiV1PipelinesPipelineIdDocumentsDocumentIdStatusGet(
|
||||||
{ pipelineId, documentId: doc },
|
{
|
||||||
|
path: { pipeline_id: pipelineId, document_id: doc },
|
||||||
|
throwOnError: true,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (status === "NOT_STARTED" || status === "IN_PROGRESS") {
|
if (status === "NOT_STARTED" || status === "IN_PROGRESS") {
|
||||||
@@ -136,12 +144,16 @@ export class LlamaCloudIndex {
|
|||||||
name?: string,
|
name?: string,
|
||||||
projectName?: string,
|
projectName?: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const pipelines = await PipelinesService.searchPipelinesApiV1PipelinesGet({
|
const { data: pipelines } =
|
||||||
projectId: await this.getProjectId(projectName),
|
await PipelinesService.searchPipelinesApiV1PipelinesGet({
|
||||||
pipelineName: name ?? this.params.name,
|
path: {
|
||||||
});
|
project_id: await this.getProjectId(projectName),
|
||||||
|
project_name: name ?? this.params.name,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
|
||||||
return pipelines[0].id;
|
return pipelines[0]!.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getProjectId(
|
public async getProjectId(
|
||||||
@@ -177,26 +189,37 @@ export class LlamaCloudIndex {
|
|||||||
transformations: params.transformations ?? defaultTransformations,
|
transformations: params.transformations ?? defaultTransformations,
|
||||||
});
|
});
|
||||||
|
|
||||||
const project = await ProjectsService.upsertProjectApiV1ProjectsPut({
|
const { data: project } =
|
||||||
organizationId: params.organizationId,
|
await ProjectsService.upsertProjectApiV1ProjectsPut({
|
||||||
requestBody: {
|
path: {
|
||||||
name: params.projectName ?? "default",
|
organization_id: params.organizationId,
|
||||||
},
|
},
|
||||||
});
|
body: {
|
||||||
|
name: params.projectName ?? "default",
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (!project.id) {
|
if (!project.id) {
|
||||||
throw new Error("Project ID should be defined");
|
throw new Error("Project ID should be defined");
|
||||||
}
|
}
|
||||||
|
|
||||||
const pipeline = await PipelinesService.upsertPipelineApiV1PipelinesPut({
|
const { data: pipeline } =
|
||||||
projectId: project.id,
|
await PipelinesService.upsertPipelineApiV1PipelinesPut({
|
||||||
requestBody: {
|
path: {
|
||||||
name: params.name,
|
project_id: project.id,
|
||||||
configured_transformations:
|
},
|
||||||
pipelineCreateParams.configured_transformations,
|
body: pipelineCreateParams.configured_transformations
|
||||||
pipeline_type: pipelineCreateParams.pipeline_type,
|
? {
|
||||||
},
|
name: params.name,
|
||||||
});
|
configured_transformations:
|
||||||
|
pipelineCreateParams.configured_transformations,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
name: params.name,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (!pipeline.id) {
|
if (!pipeline.id) {
|
||||||
throw new Error("Pipeline ID must be defined");
|
throw new Error("Pipeline ID must be defined");
|
||||||
@@ -208,8 +231,10 @@ export class LlamaCloudIndex {
|
|||||||
|
|
||||||
await PipelinesService.upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut(
|
await PipelinesService.upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut(
|
||||||
{
|
{
|
||||||
pipelineId: pipeline.id,
|
path: {
|
||||||
requestBody: params.documents.map((doc) => ({
|
pipeline_id: pipeline.id,
|
||||||
|
},
|
||||||
|
body: params.documents.map((doc) => ({
|
||||||
metadata: doc.metadata,
|
metadata: doc.metadata,
|
||||||
text: doc.text,
|
text: doc.text,
|
||||||
excluded_embed_metadata_keys: doc.excludedEmbedMetadataKeys,
|
excluded_embed_metadata_keys: doc.excludedEmbedMetadataKeys,
|
||||||
@@ -220,10 +245,11 @@ export class LlamaCloudIndex {
|
|||||||
);
|
);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const pipelineStatus =
|
const { data: pipelineStatus } =
|
||||||
await PipelinesService.getPipelineStatusApiV1PipelinesPipelineIdStatusGet(
|
await PipelinesService.getPipelineStatusApiV1PipelinesPipelineIdStatusGet(
|
||||||
{
|
{
|
||||||
pipelineId: pipeline.id,
|
path: { pipeline_id: pipeline.id },
|
||||||
|
throwOnError: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -299,8 +325,10 @@ export class LlamaCloudIndex {
|
|||||||
|
|
||||||
await PipelinesService.createBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPost(
|
await PipelinesService.createBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPost(
|
||||||
{
|
{
|
||||||
pipelineId: pipelineId,
|
path: {
|
||||||
requestBody: [
|
pipeline_id: pipelineId,
|
||||||
|
},
|
||||||
|
body: [
|
||||||
{
|
{
|
||||||
metadata: document.metadata,
|
metadata: document.metadata,
|
||||||
text: document.text,
|
text: document.text,
|
||||||
@@ -327,8 +355,10 @@ export class LlamaCloudIndex {
|
|||||||
|
|
||||||
await PipelinesService.deletePipelineDocumentApiV1PipelinesPipelineIdDocumentsDocumentIdDelete(
|
await PipelinesService.deletePipelineDocumentApiV1PipelinesPipelineIdDocumentsDocumentIdDelete(
|
||||||
{
|
{
|
||||||
pipelineId,
|
path: {
|
||||||
documentId: document.id_,
|
pipeline_id: pipelineId,
|
||||||
|
document_id: document.id_,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -347,8 +377,10 @@ export class LlamaCloudIndex {
|
|||||||
|
|
||||||
await PipelinesService.upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut(
|
await PipelinesService.upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut(
|
||||||
{
|
{
|
||||||
pipelineId,
|
path: {
|
||||||
requestBody: [
|
pipeline_id: pipelineId,
|
||||||
|
},
|
||||||
|
body: [
|
||||||
{
|
{
|
||||||
metadata: document.metadata,
|
metadata: document.metadata,
|
||||||
text: document.text,
|
text: document.text,
|
||||||
|
|||||||
@@ -59,20 +59,27 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
|||||||
query,
|
query,
|
||||||
preFilters,
|
preFilters,
|
||||||
}: RetrieveParams): Promise<NodeWithScore[]> {
|
}: RetrieveParams): Promise<NodeWithScore[]> {
|
||||||
const pipelines = await PipelinesService.searchPipelinesApiV1PipelinesGet({
|
const { data: pipelines } =
|
||||||
projectId: await getProjectId(this.projectName, this.organizationId),
|
await PipelinesService.searchPipelinesApiV1PipelinesGet({
|
||||||
pipelineName: this.pipelineName,
|
query: {
|
||||||
});
|
project_id: await getProjectId(this.projectName, this.organizationId),
|
||||||
|
project_name: this.pipelineName,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (pipelines.length === 0 || !pipelines[0].id) {
|
if (pipelines.length === 0 || !pipelines[0]!.id) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`No pipeline found with name ${this.pipelineName} in project ${this.projectName}`,
|
`No pipeline found with name ${this.pipelineName} in project ${this.projectName}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const pipeline =
|
const { data: pipeline } =
|
||||||
await PipelinesService.getPipelineApiV1PipelinesPipelineIdGet({
|
await PipelinesService.getPipelineApiV1PipelinesPipelineIdGet({
|
||||||
pipelineId: pipelines[0].id,
|
path: {
|
||||||
|
pipeline_id: pipelines[0]!.id,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!pipeline) {
|
if (!pipeline) {
|
||||||
@@ -81,15 +88,18 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const results =
|
const { data: results } =
|
||||||
await PipelinesService.runSearchApiV1PipelinesPipelineIdRetrievePost({
|
await PipelinesService.runSearchApiV1PipelinesPipelineIdRetrievePost({
|
||||||
pipelineId: pipeline.id,
|
throwOnError: true,
|
||||||
requestBody: {
|
path: {
|
||||||
|
pipeline_id: pipeline.id,
|
||||||
|
},
|
||||||
|
body: {
|
||||||
...this.retrieveParams,
|
...this.retrieveParams,
|
||||||
query: extractText(query),
|
query: extractText(query),
|
||||||
search_filters:
|
search_filters:
|
||||||
this.retrieveParams.filters ?? (preFilters as MetadataFilters),
|
this.retrieveParams.filters ?? (preFilters as MetadataFilters),
|
||||||
dense_similarity_top_k: this.retrieveParams.similarityTopK,
|
dense_similarity_top_k: this.retrieveParams.similarityTopK!,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import type { ServiceContext } from "../ServiceContext.js";
|
import type { ServiceContext } from "../ServiceContext.js";
|
||||||
|
|
||||||
export type ClientParams = { apiKey?: string; baseUrl?: string };
|
export type ClientParams = {
|
||||||
|
apiKey?: string | undefined;
|
||||||
|
baseUrl?: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export type CloudConstructorParams = {
|
export type CloudConstructorParams = {
|
||||||
name: string;
|
name: string;
|
||||||
projectName: string;
|
projectName: string;
|
||||||
organizationId?: string;
|
organizationId?: string | undefined;
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
} & ClientParams;
|
} & ClientParams;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { OpenAPI, ProjectsService } from "@llamaindex/cloud/api";
|
import { client, ProjectsService } from "@llamaindex/cloud/api";
|
||||||
import { DEFAULT_BASE_URL } from "@llamaindex/core/global";
|
import { DEFAULT_BASE_URL } from "@llamaindex/core/global";
|
||||||
import { getEnv } from "@llamaindex/env";
|
import { getEnv } from "@llamaindex/env";
|
||||||
import type { ClientParams } from "./type.js";
|
import type { ClientParams } from "./type.js";
|
||||||
@@ -8,13 +8,26 @@ function getBaseUrl(baseUrl?: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getAppBaseUrl(): string {
|
export function getAppBaseUrl(): string {
|
||||||
return OpenAPI.BASE.replace(/api\./, "");
|
return client.getConfig().baseUrl?.replace(/api\./, "") ?? "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fixme: refactor this to init at the top level or module level
|
||||||
|
let initOnce = false;
|
||||||
export function initService({ apiKey, baseUrl }: ClientParams = {}) {
|
export function initService({ apiKey, baseUrl }: ClientParams = {}) {
|
||||||
OpenAPI.TOKEN = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
|
if (initOnce) {
|
||||||
OpenAPI.BASE = getBaseUrl(baseUrl);
|
return;
|
||||||
if (!OpenAPI.TOKEN) {
|
}
|
||||||
|
initOnce = true;
|
||||||
|
client.setConfig({
|
||||||
|
baseUrl: getBaseUrl(baseUrl),
|
||||||
|
throwOnError: true,
|
||||||
|
});
|
||||||
|
const token = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
|
||||||
|
client.interceptors.request.use((request) => {
|
||||||
|
request.headers.set("Authorization", `Bearer ${token}`);
|
||||||
|
return request;
|
||||||
|
});
|
||||||
|
if (!token) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"API Key is required for LlamaCloudIndex. Please pass the apiKey parameter",
|
"API Key is required for LlamaCloudIndex. Please pass the apiKey parameter",
|
||||||
);
|
);
|
||||||
@@ -25,10 +38,15 @@ export async function getProjectId(
|
|||||||
projectName: string,
|
projectName: string,
|
||||||
organizationId?: string,
|
organizationId?: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const projects = await ProjectsService.listProjectsApiV1ProjectsGet({
|
const { data: projects } = await ProjectsService.listProjectsApiV1ProjectsGet(
|
||||||
projectName: projectName,
|
{
|
||||||
organizationId: organizationId,
|
path: {
|
||||||
});
|
project_name: projectName,
|
||||||
|
organization_id: organizationId,
|
||||||
|
},
|
||||||
|
throwOnError: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if (projects.length === 0) {
|
if (projects.length === 0) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -40,7 +58,7 @@ export async function getProjectId(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const project = projects[0];
|
const project = projects[0]!;
|
||||||
|
|
||||||
if (!project.id) {
|
if (!project.id) {
|
||||||
throw new Error(`No project found with name ${projectName}`);
|
throw new Error(`No project found with name ${projectName}`);
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export class DeepInfraEmbedding extends BaseEmbedding {
|
|||||||
async getTextEmbedding(text: string): Promise<number[]> {
|
async getTextEmbedding(text: string): Promise<number[]> {
|
||||||
const texts = mapPrefixWithInputs(this.textPrefix, [text]);
|
const texts = mapPrefixWithInputs(this.textPrefix, [text]);
|
||||||
const embeddings = await this.getDeepInfraEmbedding(texts);
|
const embeddings = await this.getDeepInfraEmbedding(texts);
|
||||||
return embeddings[0];
|
return embeddings[0]!;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getQueryEmbedding(
|
async getQueryEmbedding(
|
||||||
@@ -97,7 +97,7 @@ export class DeepInfraEmbedding extends BaseEmbedding {
|
|||||||
if (text) {
|
if (text) {
|
||||||
const queries = mapPrefixWithInputs(this.queryPrefix, [text]);
|
const queries = mapPrefixWithInputs(this.queryPrefix, [text]);
|
||||||
const embeddings = await this.getDeepInfraEmbedding(queries);
|
const embeddings = await this.getDeepInfraEmbedding(queries);
|
||||||
return embeddings[0];
|
return embeddings[0]!;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,13 +37,13 @@ export class JinaAIEmbedding extends MultiModalEmbedding {
|
|||||||
|
|
||||||
async getTextEmbedding(text: string): Promise<number[]> {
|
async getTextEmbedding(text: string): Promise<number[]> {
|
||||||
const result = await this.getJinaEmbedding({ input: [{ text }] });
|
const result = await this.getJinaEmbedding({ input: [{ text }] });
|
||||||
return result.data[0].embedding;
|
return result.data[0]!.embedding;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getImageEmbedding(image: ImageType): Promise<number[]> {
|
async getImageEmbedding(image: ImageType): Promise<number[]> {
|
||||||
const img = await this.getImageInput(image);
|
const img = await this.getImageInput(image);
|
||||||
const result = await this.getJinaEmbedding({ input: [img] });
|
const result = await this.getJinaEmbedding({ input: [img] });
|
||||||
return result.data[0].embedding;
|
return result.data[0]!.embedding;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve multiple text embeddings in a single request
|
// Retrieve multiple text embeddings in a single request
|
||||||
@@ -81,7 +81,7 @@ export class JinaAIEmbedding extends MultiModalEmbedding {
|
|||||||
): Promise<{ bytes: string } | { url: string }> {
|
): Promise<{ bytes: string } | { url: string }> {
|
||||||
if (isLocal(image) || image instanceof Blob) {
|
if (isLocal(image) || image instanceof Blob) {
|
||||||
const base64 = await imageToDataUrl(image);
|
const base64 = await imageToDataUrl(image);
|
||||||
const bytes = base64.split(",")[1];
|
const bytes = base64.split(",")[1]!;
|
||||||
return { bytes };
|
return { bytes };
|
||||||
} else {
|
} else {
|
||||||
return { url: image.toString() };
|
return { url: image.toString() };
|
||||||
|
|||||||
@@ -105,8 +105,10 @@ export class MixedbreadAIEmbeddings extends BaseEmbedding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.embedBatchSize = params?.embedBatchSize ?? 128;
|
this.embedBatchSize = params?.embedBatchSize ?? 128;
|
||||||
this.embedInfo = params?.embedInfo;
|
if (params?.embedInfo) {
|
||||||
this.requestParams = {
|
this.embedInfo = params?.embedInfo;
|
||||||
|
}
|
||||||
|
this.requestParams = <EmbeddingsRequestWithoutInput>{
|
||||||
model: params?.model ?? "mixedbread-ai/mxbai-embed-large-v1",
|
model: params?.model ?? "mixedbread-ai/mxbai-embed-large-v1",
|
||||||
normalized: params?.normalized,
|
normalized: params?.normalized,
|
||||||
dimensions: params?.dimensions,
|
dimensions: params?.dimensions,
|
||||||
@@ -123,10 +125,16 @@ export class MixedbreadAIEmbeddings extends BaseEmbedding {
|
|||||||
"user-agent": "@mixedbread-ai/llamaindex-ts-sdk",
|
"user-agent": "@mixedbread-ai/llamaindex-ts-sdk",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
this.client = new MixedbreadAIClient({
|
this.client = new MixedbreadAIClient(
|
||||||
apiKey,
|
params?.baseUrl
|
||||||
environment: params?.baseUrl,
|
? {
|
||||||
});
|
apiKey,
|
||||||
|
environment: params?.baseUrl,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
apiKey,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -140,7 +148,7 @@ export class MixedbreadAIEmbeddings extends BaseEmbedding {
|
|||||||
* console.log(result);
|
* console.log(result);
|
||||||
*/
|
*/
|
||||||
async getTextEmbedding(text: string): Promise<number[]> {
|
async getTextEmbedding(text: string): Promise<number[]> {
|
||||||
return (await this.getTextEmbeddings([text]))[0];
|
return (await this.getTextEmbeddings([text]))[0]!;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export abstract class MultiModalEmbedding extends BaseEmbedding {
|
|||||||
_options,
|
_options,
|
||||||
);
|
);
|
||||||
for (let i = 0; i < textNodes.length; i++) {
|
for (let i = 0; i < textNodes.length; i++) {
|
||||||
textNodes[i].embedding = embeddings[i];
|
textNodes[i]!.embedding = embeddings[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageEmbeddings = await batchEmbeddings(
|
const imageEmbeddings = await batchEmbeddings(
|
||||||
@@ -49,7 +49,7 @@ export abstract class MultiModalEmbedding extends BaseEmbedding {
|
|||||||
_options,
|
_options,
|
||||||
);
|
);
|
||||||
for (let i = 0; i < imageNodes.length; i++) {
|
for (let i = 0; i < imageNodes.length; i++) {
|
||||||
imageNodes[i].embedding = imageEmbeddings[i];
|
imageNodes[i]!.embedding = imageEmbeddings[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodes;
|
return nodes;
|
||||||
|
|||||||
@@ -36,21 +36,20 @@ export class OpenAIEmbedding extends BaseEmbedding {
|
|||||||
/** embeddding model. defaults to "text-embedding-ada-002" */
|
/** embeddding model. defaults to "text-embedding-ada-002" */
|
||||||
model: string;
|
model: string;
|
||||||
/** number of dimensions of the resulting vector, for models that support choosing fewer dimensions. undefined will default to model default */
|
/** number of dimensions of the resulting vector, for models that support choosing fewer dimensions. undefined will default to model default */
|
||||||
dimensions: number | undefined;
|
dimensions?: number | undefined;
|
||||||
|
|
||||||
// OpenAI session params
|
// OpenAI session params
|
||||||
|
|
||||||
/** api key */
|
/** api key */
|
||||||
apiKey?: string = undefined;
|
apiKey?: string | undefined = undefined;
|
||||||
/** maximum number of retries, default 10 */
|
/** maximum number of retries, default 10 */
|
||||||
maxRetries: number;
|
maxRetries: number;
|
||||||
/** timeout in ms, default 60 seconds */
|
/** timeout in ms, default 60 seconds */
|
||||||
timeout?: number;
|
timeout?: number | undefined;
|
||||||
/** other session options for OpenAI */
|
/** other session options for OpenAI */
|
||||||
additionalSessionOptions?: Omit<
|
additionalSessionOptions?:
|
||||||
Partial<OpenAIClientOptions>,
|
| Omit<Partial<OpenAIClientOptions>, "apiKey" | "maxRetries" | "timeout">
|
||||||
"apiKey" | "maxRetries" | "timeout"
|
| undefined;
|
||||||
>;
|
|
||||||
|
|
||||||
/** session object */
|
/** session object */
|
||||||
session: OpenAISession;
|
session: OpenAISession;
|
||||||
@@ -119,11 +118,18 @@ export class OpenAIEmbedding extends BaseEmbedding {
|
|||||||
// TODO: ensure this for every sub class by calling it in the base class
|
// TODO: ensure this for every sub class by calling it in the base class
|
||||||
input = this.truncateMaxTokens(input);
|
input = this.truncateMaxTokens(input);
|
||||||
|
|
||||||
const { data } = await this.session.openai.embeddings.create({
|
const { data } = await this.session.openai.embeddings.create(
|
||||||
model: this.model,
|
this.dimensions
|
||||||
dimensions: this.dimensions, // only sent to OpenAI if set by user
|
? {
|
||||||
input,
|
model: this.model,
|
||||||
});
|
dimensions: this.dimensions, // only sent to OpenAI if set by user
|
||||||
|
input,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
model: this.model,
|
||||||
|
input,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
return data.map((d) => d.embedding);
|
return data.map((d) => d.embedding);
|
||||||
}
|
}
|
||||||
@@ -141,6 +147,6 @@ export class OpenAIEmbedding extends BaseEmbedding {
|
|||||||
* @param texts
|
* @param texts
|
||||||
*/
|
*/
|
||||||
async getTextEmbedding(text: string): Promise<number[]> {
|
async getTextEmbedding(text: string): Promise<number[]> {
|
||||||
return (await this.getOpenAIEmbedding([text]))[0];
|
return (await this.getOpenAIEmbedding([text]))[0]!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,16 +38,16 @@ export class ContextChatEngine extends PromptMixin implements ChatEngine {
|
|||||||
chatModel: LLM;
|
chatModel: LLM;
|
||||||
chatHistory: ChatHistory;
|
chatHistory: ChatHistory;
|
||||||
contextGenerator: ContextGenerator & PromptMixin;
|
contextGenerator: ContextGenerator & PromptMixin;
|
||||||
systemPrompt?: string;
|
systemPrompt?: string | undefined;
|
||||||
|
|
||||||
constructor(init: {
|
constructor(init: {
|
||||||
retriever: BaseRetriever;
|
retriever: BaseRetriever;
|
||||||
chatModel?: LLM;
|
chatModel?: LLM | undefined;
|
||||||
chatHistory?: ChatMessage[];
|
chatHistory?: ChatMessage[] | undefined;
|
||||||
contextSystemPrompt?: ContextSystemPrompt;
|
contextSystemPrompt?: ContextSystemPrompt | undefined;
|
||||||
nodePostprocessors?: BaseNodePostprocessor[];
|
nodePostprocessors?: BaseNodePostprocessor[] | undefined;
|
||||||
systemPrompt?: string;
|
systemPrompt?: string | undefined;
|
||||||
contextRole?: MessageType;
|
contextRole?: MessageType | undefined;
|
||||||
}) {
|
}) {
|
||||||
super();
|
super();
|
||||||
this.chatModel = init.chatModel ?? Settings.llm;
|
this.chatModel = init.chatModel ?? Settings.llm;
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ export class DefaultContextGenerator
|
|||||||
|
|
||||||
constructor(init: {
|
constructor(init: {
|
||||||
retriever: BaseRetriever;
|
retriever: BaseRetriever;
|
||||||
contextSystemPrompt?: ContextSystemPrompt;
|
contextSystemPrompt?: ContextSystemPrompt | undefined;
|
||||||
nodePostprocessors?: BaseNodePostprocessor[];
|
nodePostprocessors?: BaseNodePostprocessor[] | undefined;
|
||||||
contextRole?: MessageType;
|
contextRole?: MessageType | undefined;
|
||||||
metadataMode?: MetadataMode;
|
metadataMode?: MetadataMode | undefined;
|
||||||
}) {
|
}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
|||||||
@@ -64,9 +64,9 @@ export class RouterQueryEngine extends PromptMixin implements QueryEngine {
|
|||||||
constructor(init: {
|
constructor(init: {
|
||||||
selector: BaseSelector;
|
selector: BaseSelector;
|
||||||
queryEngineTools: RouterQueryEngineTool[];
|
queryEngineTools: RouterQueryEngineTool[];
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
summarizer?: TreeSummarize;
|
summarizer?: TreeSummarize | undefined;
|
||||||
verbose?: boolean;
|
verbose?: boolean | undefined;
|
||||||
}) {
|
}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
@@ -138,14 +138,14 @@ export class RouterQueryEngine extends PromptMixin implements QueryEngine {
|
|||||||
if (result.selections.length > 1) {
|
if (result.selections.length > 1) {
|
||||||
const responses: EngineResponse[] = [];
|
const responses: EngineResponse[] = [];
|
||||||
for (let i = 0; i < result.selections.length; i++) {
|
for (let i = 0; i < result.selections.length; i++) {
|
||||||
const engineInd = result.selections[i];
|
const engineInd = result.selections[i]!;
|
||||||
const logStr = `Selecting query engine ${engineInd.index}: ${result.selections[i].index}.`;
|
const logStr = `Selecting query engine ${engineInd.index}: ${result.selections[i]!.index}.`;
|
||||||
|
|
||||||
if (this.verbose) {
|
if (this.verbose) {
|
||||||
console.log(logStr + "\n");
|
console.log(logStr + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedQueryEngine = this.queryEngines[engineInd.index];
|
const selectedQueryEngine = this.queryEngines[engineInd.index]!;
|
||||||
responses.push(
|
responses.push(
|
||||||
await selectedQueryEngine.query({
|
await selectedQueryEngine.query({
|
||||||
query: extractText(query),
|
query: extractText(query),
|
||||||
@@ -163,15 +163,15 @@ export class RouterQueryEngine extends PromptMixin implements QueryEngine {
|
|||||||
|
|
||||||
return finalResponse;
|
return finalResponse;
|
||||||
} else {
|
} else {
|
||||||
return responses[0];
|
return responses[0]!;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let selectedQueryEngine;
|
let selectedQueryEngine;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
selectedQueryEngine = this.queryEngines[result.selections[0].index];
|
selectedQueryEngine = this.queryEngines[result.selections[0]!.index];
|
||||||
|
|
||||||
const logStr = `Selecting query engine ${result.selections[0].index}: ${result.selections[0].reason}`;
|
const logStr = `Selecting query engine ${result.selections[0]!.index}: ${result.selections[0]!.reason}`;
|
||||||
|
|
||||||
if (this.verbose) {
|
if (this.verbose) {
|
||||||
console.log(logStr + "\n");
|
console.log(logStr + "\n");
|
||||||
|
|||||||
@@ -22,16 +22,16 @@ export class FaithfulnessEvaluator
|
|||||||
extends PromptMixin
|
extends PromptMixin
|
||||||
implements BaseEvaluator
|
implements BaseEvaluator
|
||||||
{
|
{
|
||||||
private serviceContext?: ServiceContext;
|
private serviceContext?: ServiceContext | undefined;
|
||||||
private raiseError: boolean;
|
private raiseError: boolean;
|
||||||
private evalTemplate: FaithfulnessTextQAPrompt;
|
private evalTemplate: FaithfulnessTextQAPrompt;
|
||||||
private refineTemplate: FaithfulnessRefinePrompt;
|
private refineTemplate: FaithfulnessRefinePrompt;
|
||||||
|
|
||||||
constructor(params?: {
|
constructor(params?: {
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
raiseError?: boolean;
|
raiseError?: boolean | undefined;
|
||||||
faithfulnessSystemPrompt?: FaithfulnessTextQAPrompt;
|
faithfulnessSystemPrompt?: FaithfulnessTextQAPrompt | undefined;
|
||||||
faithFulnessRefinePrompt?: FaithfulnessRefinePrompt;
|
faithFulnessRefinePrompt?: FaithfulnessRefinePrompt | undefined;
|
||||||
}) {
|
}) {
|
||||||
super();
|
super();
|
||||||
this.serviceContext = params?.serviceContext;
|
this.serviceContext = params?.serviceContext;
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ import type {
|
|||||||
} from "./types.js";
|
} from "./types.js";
|
||||||
|
|
||||||
type RelevancyParams = {
|
type RelevancyParams = {
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
raiseError?: boolean;
|
raiseError?: boolean | undefined;
|
||||||
evalTemplate?: RelevancyEvalPrompt;
|
evalTemplate?: RelevancyEvalPrompt | undefined;
|
||||||
refineTemplate?: RelevancyRefinePrompt;
|
refineTemplate?: RelevancyRefinePrompt | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class RelevancyEvaluator extends PromptMixin implements BaseEvaluator {
|
export class RelevancyEvaluator extends PromptMixin implements BaseEvaluator {
|
||||||
private serviceContext?: ServiceContext;
|
private serviceContext?: ServiceContext | undefined;
|
||||||
private raiseError: boolean;
|
private raiseError: boolean;
|
||||||
|
|
||||||
private evalTemplate: RelevancyEvalPrompt;
|
private evalTemplate: RelevancyEvalPrompt;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
export const defaultEvaluationParser = (
|
export const defaultEvaluationParser = (
|
||||||
evalResponse: string,
|
evalResponse: string,
|
||||||
): [number, string] => {
|
): [number, string] => {
|
||||||
const [scoreStr, reasoningStr] = evalResponse.split("\n");
|
const [scoreStr, reasoningStr] = evalResponse.split("\n") as [string, string];
|
||||||
const score = parseFloat(scoreStr);
|
const score = parseFloat(scoreStr);
|
||||||
const reasoning = reasoningStr.trim();
|
const reasoning = reasoningStr.trim();
|
||||||
return [score, reasoning];
|
return [score, reasoning];
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ export class TitleExtractor extends BaseExtractor {
|
|||||||
|
|
||||||
return nodesToExtractTitle.map((node) => {
|
return nodesToExtractTitle.map((node) => {
|
||||||
return {
|
return {
|
||||||
documentTitle: titlesByDocument[node.sourceNode?.nodeId ?? ""],
|
documentTitle: titlesByDocument[node.sourceNode?.nodeId ?? ""]!,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,20 +52,20 @@ export abstract class BaseExtractor extends TransformComponent {
|
|||||||
const curMetadataList = await this.extract(newNodes);
|
const curMetadataList = await this.extract(newNodes);
|
||||||
|
|
||||||
for (const idx in newNodes) {
|
for (const idx in newNodes) {
|
||||||
newNodes[idx].metadata = {
|
newNodes[idx]!.metadata = {
|
||||||
...newNodes[idx].metadata,
|
...newNodes[idx]!.metadata,
|
||||||
...curMetadataList[idx],
|
...curMetadataList[idx],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const idx in newNodes) {
|
for (const idx in newNodes) {
|
||||||
if (excludedEmbedMetadataKeys) {
|
if (excludedEmbedMetadataKeys) {
|
||||||
newNodes[idx].excludedEmbedMetadataKeys.concat(
|
newNodes[idx]!.excludedEmbedMetadataKeys.concat(
|
||||||
excludedEmbedMetadataKeys,
|
excludedEmbedMetadataKeys,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (excludedLlmMetadataKeys) {
|
if (excludedLlmMetadataKeys) {
|
||||||
newNodes[idx].excludedLlmMetadataKeys.concat(excludedLlmMetadataKeys);
|
newNodes[idx]!.excludedLlmMetadataKeys.concat(excludedLlmMetadataKeys);
|
||||||
}
|
}
|
||||||
if (!this.disableTemplateRewrite) {
|
if (!this.disableTemplateRewrite) {
|
||||||
if (newNodes[idx] instanceof TextNode) {
|
if (newNodes[idx] instanceof TextNode) {
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export type {
|
|||||||
LLMToolCallEvent,
|
LLMToolCallEvent,
|
||||||
LLMToolResultEvent,
|
LLMToolResultEvent,
|
||||||
} from "@llamaindex/core/global";
|
} from "@llamaindex/core/global";
|
||||||
|
export * from "@llamaindex/core/indices";
|
||||||
export * from "@llamaindex/core/llms";
|
export * from "@llamaindex/core/llms";
|
||||||
export * from "@llamaindex/core/prompts";
|
export * from "@llamaindex/core/prompts";
|
||||||
export * from "@llamaindex/core/schema";
|
export * from "@llamaindex/core/schema";
|
||||||
@@ -62,7 +63,6 @@ export * from "./nodeParsers/index.js";
|
|||||||
export * from "./objects/index.js";
|
export * from "./objects/index.js";
|
||||||
export * from "./OutputParser.js";
|
export * from "./OutputParser.js";
|
||||||
export * from "./postprocessors/index.js";
|
export * from "./postprocessors/index.js";
|
||||||
export * from "./PromptHelper.js";
|
|
||||||
export * from "./QuestionGenerator.js";
|
export * from "./QuestionGenerator.js";
|
||||||
export * from "./Retriever.js";
|
export * from "./Retriever.js";
|
||||||
export * from "./selectors/index.js";
|
export * from "./selectors/index.js";
|
||||||
|
|||||||
@@ -17,3 +17,7 @@ export { GeminiVertexSession } from "./llm/gemini/vertex.js";
|
|||||||
// Expose AzureDynamicSessionTool for node.js runtime only
|
// Expose AzureDynamicSessionTool for node.js runtime only
|
||||||
export { JinaAIEmbedding } from "./embeddings/JinaAIEmbedding.js";
|
export { JinaAIEmbedding } from "./embeddings/JinaAIEmbedding.js";
|
||||||
export { AzureDynamicSessionTool } from "./tools/AzureDynamicSessionTool.node.js";
|
export { AzureDynamicSessionTool } from "./tools/AzureDynamicSessionTool.node.js";
|
||||||
|
|
||||||
|
// Don't export vector store modules for non-node.js runtime on top level,
|
||||||
|
// as we cannot guarantee that they will work in other environments
|
||||||
|
export * from "./vector-store.js";
|
||||||
|
|||||||
@@ -43,10 +43,10 @@ export class KeywordTable extends IndexStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface BaseIndexInit<T> {
|
export interface BaseIndexInit<T> {
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
storageContext: StorageContext;
|
storageContext: StorageContext;
|
||||||
docStore: BaseDocumentStore;
|
docStore: BaseDocumentStore;
|
||||||
indexStore?: BaseIndexStore;
|
indexStore?: BaseIndexStore | undefined;
|
||||||
indexStruct: T;
|
indexStruct: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,10 +55,10 @@ export interface BaseIndexInit<T> {
|
|||||||
* they can be retrieved for our queries.
|
* they can be retrieved for our queries.
|
||||||
*/
|
*/
|
||||||
export abstract class BaseIndex<T> {
|
export abstract class BaseIndex<T> {
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
storageContext: StorageContext;
|
storageContext: StorageContext;
|
||||||
docStore: BaseDocumentStore;
|
docStore: BaseDocumentStore;
|
||||||
indexStore?: BaseIndexStore;
|
indexStore?: BaseIndexStore | undefined;
|
||||||
indexStruct: T;
|
indexStruct: T;
|
||||||
|
|
||||||
constructor(init: BaseIndexInit<T>) {
|
constructor(init: BaseIndexInit<T>) {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import { randomUUID } from "@llamaindex/env";
|
|||||||
*/
|
*/
|
||||||
export abstract class IndexStruct {
|
export abstract class IndexStruct {
|
||||||
indexId: string;
|
indexId: string;
|
||||||
summary?: string;
|
summary?: string | undefined;
|
||||||
|
|
||||||
constructor(indexId = randomUUID(), summary = undefined) {
|
constructor(indexId = randomUUID(), summary: string | undefined = undefined) {
|
||||||
this.indexId = indexId;
|
this.indexId = indexId;
|
||||||
this.summary = summary;
|
this.summary = summary;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ abstract class BaseKeywordTableRetriever implements BaseRetriever {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sortedChunkIndices = Object.keys(chunkIndicesCount)
|
const sortedChunkIndices = Object.keys(chunkIndicesCount)
|
||||||
.sort((a, b) => chunkIndicesCount[b] - chunkIndicesCount[a])
|
.sort((a, b) => chunkIndicesCount[b]! - chunkIndicesCount[a]!)
|
||||||
.slice(0, this.numChunksPerQuery);
|
.slice(0, this.numChunksPerQuery);
|
||||||
|
|
||||||
const sortedNodes = await this.docstore.getNodes(sortedChunkIndices);
|
const sortedNodes = await this.docstore.getNodes(sortedChunkIndices);
|
||||||
@@ -175,7 +175,7 @@ export class KeywordTableIndex extends BaseIndex<KeywordTable> {
|
|||||||
if (options.indexStruct) {
|
if (options.indexStruct) {
|
||||||
indexStruct = options.indexStruct;
|
indexStruct = options.indexStruct;
|
||||||
} else if (indexStructs.length == 1) {
|
} else if (indexStructs.length == 1) {
|
||||||
indexStruct = indexStructs[0];
|
indexStruct = indexStructs[0]!;
|
||||||
} else if (indexStructs.length > 1 && options.indexId) {
|
} else if (indexStructs.length > 1 && options.indexId) {
|
||||||
indexStruct = (await indexStore.getIndexStruct(
|
indexStruct = (await indexStore.getIndexStruct(
|
||||||
options.indexId,
|
options.indexId,
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ export function simpleExtractKeywords(
|
|||||||
|
|
||||||
// Sorting tokens by frequency
|
// Sorting tokens by frequency
|
||||||
const sortedTokens: string[] = Object.keys(valueCounts).sort(
|
const sortedTokens: string[] = Object.keys(valueCounts).sort(
|
||||||
(a, b) => valueCounts[b] - valueCounts[a],
|
(a, b) => valueCounts[b]! - valueCounts[a]!,
|
||||||
);
|
);
|
||||||
|
|
||||||
const keywords: string[] = maxKeywords
|
const keywords: string[] = maxKeywords
|
||||||
|
|||||||
@@ -48,11 +48,11 @@ export enum SummaryRetrieverMode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface SummaryIndexOptions {
|
export interface SummaryIndexOptions {
|
||||||
nodes?: BaseNode[];
|
nodes?: BaseNode[] | undefined;
|
||||||
indexStruct?: IndexList;
|
indexStruct?: IndexList | undefined;
|
||||||
indexId?: string;
|
indexId?: string | undefined;
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
storageContext?: StorageContext;
|
storageContext?: StorageContext | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,7 +83,9 @@ export class SummaryIndex extends BaseIndex<IndexList> {
|
|||||||
indexStruct = options.indexStruct;
|
indexStruct = options.indexStruct;
|
||||||
} else if (indexStructs.length == 1) {
|
} else if (indexStructs.length == 1) {
|
||||||
indexStruct =
|
indexStruct =
|
||||||
indexStructs[0].type === IndexStructType.LIST ? indexStructs[0] : null;
|
indexStructs[0]!.type === IndexStructType.LIST
|
||||||
|
? indexStructs[0]!
|
||||||
|
: null;
|
||||||
} else if (indexStructs.length > 1 && options.indexId) {
|
} else if (indexStructs.length > 1 && options.indexId) {
|
||||||
indexStruct = (await indexStore.getIndexStruct(
|
indexStruct = (await indexStore.getIndexStruct(
|
||||||
options.indexId,
|
options.indexId,
|
||||||
@@ -131,8 +133,8 @@ export class SummaryIndex extends BaseIndex<IndexList> {
|
|||||||
static async fromDocuments(
|
static async fromDocuments(
|
||||||
documents: Document[],
|
documents: Document[],
|
||||||
args: {
|
args: {
|
||||||
storageContext?: StorageContext;
|
storageContext?: StorageContext | undefined;
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
} = {},
|
} = {},
|
||||||
): Promise<SummaryIndex> {
|
): Promise<SummaryIndex> {
|
||||||
let { storageContext, serviceContext } = args;
|
let { storageContext, serviceContext } = args;
|
||||||
@@ -312,7 +314,7 @@ export class SummaryIndexLLMRetriever implements BaseRetriever {
|
|||||||
choiceBatchSize: number;
|
choiceBatchSize: number;
|
||||||
formatNodeBatchFn: NodeFormatterFunction;
|
formatNodeBatchFn: NodeFormatterFunction;
|
||||||
parseChoiceSelectAnswerFn: ChoiceSelectParserFunction;
|
parseChoiceSelectAnswerFn: ChoiceSelectParserFunction;
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
|
|
||||||
// eslint-disable-next-line max-params
|
// eslint-disable-next-line max-params
|
||||||
constructor(
|
constructor(
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ export const defaultParseChoiceSelectAnswerFn: ChoiceSelectParserFunction = (
|
|||||||
return lineTokens.reduce(
|
return lineTokens.reduce(
|
||||||
(parseResult: ChoiceSelectParseResult, lineToken: string[]) => {
|
(parseResult: ChoiceSelectParseResult, lineToken: string[]) => {
|
||||||
try {
|
try {
|
||||||
const docNum = parseInt(lineToken[0].split(":")[1].trim());
|
const docNum = parseInt(lineToken[0]!.split(":")[1]!.trim());
|
||||||
const answerRelevance = parseFloat(lineToken[1].split(":")[1].trim());
|
const answerRelevance = parseFloat(lineToken[1]!.split(":")[1]!.trim());
|
||||||
if (docNum < 1 || docNum > numChoices) {
|
if (docNum < 1 || docNum > numChoices) {
|
||||||
if (raiseErr) {
|
if (raiseErr) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|||||||
@@ -29,35 +29,35 @@ import {
|
|||||||
import type { BaseNodePostprocessor } from "../../postprocessors/types.js";
|
import type { BaseNodePostprocessor } from "../../postprocessors/types.js";
|
||||||
import type { StorageContext } from "../../storage/StorageContext.js";
|
import type { StorageContext } from "../../storage/StorageContext.js";
|
||||||
import { storageContextFromDefaults } from "../../storage/StorageContext.js";
|
import { storageContextFromDefaults } from "../../storage/StorageContext.js";
|
||||||
|
import type { BaseIndexStore } from "../../storage/indexStore/types.js";
|
||||||
|
import type { BaseSynthesizer } from "../../synthesizers/types.js";
|
||||||
|
import type { QueryEngine } from "../../types.js";
|
||||||
import type {
|
import type {
|
||||||
MetadataFilters,
|
MetadataFilters,
|
||||||
VectorStore,
|
VectorStore,
|
||||||
VectorStoreByType,
|
VectorStoreByType,
|
||||||
VectorStoreQueryResult,
|
VectorStoreQueryResult,
|
||||||
} from "../../storage/index.js";
|
} from "../../vector-store/index.js";
|
||||||
import type { BaseIndexStore } from "../../storage/indexStore/types.js";
|
import { VectorStoreQueryMode } from "../../vector-store/types.js";
|
||||||
import { VectorStoreQueryMode } from "../../storage/vectorStore/types.js";
|
|
||||||
import type { BaseSynthesizer } from "../../synthesizers/types.js";
|
|
||||||
import type { QueryEngine } from "../../types.js";
|
|
||||||
import type { BaseIndexInit } from "../BaseIndex.js";
|
import type { BaseIndexInit } from "../BaseIndex.js";
|
||||||
import { BaseIndex } from "../BaseIndex.js";
|
import { BaseIndex } from "../BaseIndex.js";
|
||||||
import { IndexDict, IndexStructType } from "../json-to-index-struct.js";
|
import { IndexDict, IndexStructType } from "../json-to-index-struct.js";
|
||||||
|
|
||||||
interface IndexStructOptions {
|
interface IndexStructOptions {
|
||||||
indexStruct?: IndexDict;
|
indexStruct?: IndexDict | undefined;
|
||||||
indexId?: string;
|
indexId?: string | undefined;
|
||||||
}
|
}
|
||||||
export interface VectorIndexOptions extends IndexStructOptions {
|
export interface VectorIndexOptions extends IndexStructOptions {
|
||||||
nodes?: BaseNode[];
|
nodes?: BaseNode[] | undefined;
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
storageContext?: StorageContext;
|
storageContext?: StorageContext | undefined;
|
||||||
vectorStores?: VectorStoreByType;
|
vectorStores?: VectorStoreByType | undefined;
|
||||||
logProgress?: boolean;
|
logProgress?: boolean | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface VectorIndexConstructorProps extends BaseIndexInit<IndexDict> {
|
export interface VectorIndexConstructorProps extends BaseIndexInit<IndexDict> {
|
||||||
indexStore: BaseIndexStore;
|
indexStore: BaseIndexStore;
|
||||||
vectorStores?: VectorStoreByType;
|
vectorStores?: VectorStoreByType | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,7 +65,7 @@ export interface VectorIndexConstructorProps extends BaseIndexInit<IndexDict> {
|
|||||||
*/
|
*/
|
||||||
export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
||||||
indexStore: BaseIndexStore;
|
indexStore: BaseIndexStore;
|
||||||
embedModel?: BaseEmbedding;
|
embedModel?: BaseEmbedding | undefined;
|
||||||
vectorStores: VectorStoreByType;
|
vectorStores: VectorStoreByType;
|
||||||
|
|
||||||
private constructor(init: VectorIndexConstructorProps) {
|
private constructor(init: VectorIndexConstructorProps) {
|
||||||
@@ -137,7 +137,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
|||||||
indexStruct = options.indexStruct;
|
indexStruct = options.indexStruct;
|
||||||
} else if (indexStructs.length == 1) {
|
} else if (indexStructs.length == 1) {
|
||||||
indexStruct =
|
indexStruct =
|
||||||
indexStructs[0].type === IndexStructType.SIMPLE_DICT
|
indexStructs[0]!.type === IndexStructType.SIMPLE_DICT
|
||||||
? indexStructs[0]
|
? indexStructs[0]
|
||||||
: undefined;
|
: undefined;
|
||||||
indexStruct = indexStructs[0];
|
indexStruct = indexStructs[0];
|
||||||
@@ -164,7 +164,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
|||||||
*/
|
*/
|
||||||
async getNodeEmbeddingResults(
|
async getNodeEmbeddingResults(
|
||||||
nodes: BaseNode[],
|
nodes: BaseNode[],
|
||||||
options?: { logProgress?: boolean },
|
options?: { logProgress?: boolean | undefined },
|
||||||
): Promise<BaseNode[]> {
|
): Promise<BaseNode[]> {
|
||||||
const nodeMap = splitNodesByType(nodes);
|
const nodeMap = splitNodesByType(nodes);
|
||||||
for (const type in nodeMap) {
|
for (const type in nodeMap) {
|
||||||
@@ -187,7 +187,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
|||||||
*/
|
*/
|
||||||
async buildIndexFromNodes(
|
async buildIndexFromNodes(
|
||||||
nodes: BaseNode[],
|
nodes: BaseNode[],
|
||||||
options?: { logProgress?: boolean },
|
options?: { logProgress?: boolean | undefined },
|
||||||
) {
|
) {
|
||||||
await this.insertNodes(nodes, options);
|
await this.insertNodes(nodes, options);
|
||||||
}
|
}
|
||||||
@@ -314,13 +314,13 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
|||||||
// NOTE: if the vector store keeps text,
|
// NOTE: if the vector store keeps text,
|
||||||
// we only need to add image and index nodes
|
// we only need to add image and index nodes
|
||||||
for (let i = 0; i < nodes.length; ++i) {
|
for (let i = 0; i < nodes.length; ++i) {
|
||||||
const { type } = nodes[i];
|
const { type } = nodes[i]!;
|
||||||
if (
|
if (
|
||||||
!vectorStore.storesText ||
|
!vectorStore.storesText ||
|
||||||
type === ObjectType.INDEX ||
|
type === ObjectType.INDEX ||
|
||||||
type === ObjectType.IMAGE
|
type === ObjectType.IMAGE
|
||||||
) {
|
) {
|
||||||
const nodeWithoutEmbedding = nodes[i].clone();
|
const nodeWithoutEmbedding = nodes[i]!.clone();
|
||||||
nodeWithoutEmbedding.embedding = undefined;
|
nodeWithoutEmbedding.embedding = undefined;
|
||||||
this.indexStruct.addNode(nodeWithoutEmbedding, newIds[i]);
|
this.indexStruct.addNode(nodeWithoutEmbedding, newIds[i]);
|
||||||
await this.docStore.addDocuments([nodeWithoutEmbedding], true);
|
await this.docStore.addDocuments([nodeWithoutEmbedding], true);
|
||||||
@@ -330,7 +330,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
|||||||
|
|
||||||
async insertNodes(
|
async insertNodes(
|
||||||
nodes: BaseNode[],
|
nodes: BaseNode[],
|
||||||
options?: { logProgress?: boolean },
|
options?: { logProgress?: boolean | undefined },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!nodes || nodes.length === 0) {
|
if (!nodes || nodes.length === 0) {
|
||||||
return;
|
return;
|
||||||
@@ -384,8 +384,8 @@ type TopKMap = { [P in ModalityType]: number };
|
|||||||
|
|
||||||
export type VectorIndexRetrieverOptions = {
|
export type VectorIndexRetrieverOptions = {
|
||||||
index: VectorStoreIndex;
|
index: VectorStoreIndex;
|
||||||
similarityTopK?: number;
|
similarityTopK?: number | undefined;
|
||||||
topK?: TopKMap;
|
topK?: TopKMap | undefined;
|
||||||
filters?: MetadataFilters;
|
filters?: MetadataFilters;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -393,8 +393,8 @@ export class VectorIndexRetriever implements BaseRetriever {
|
|||||||
index: VectorStoreIndex;
|
index: VectorStoreIndex;
|
||||||
topK: TopKMap;
|
topK: TopKMap;
|
||||||
|
|
||||||
serviceContext?: ServiceContext;
|
serviceContext?: ServiceContext | undefined;
|
||||||
filters?: MetadataFilters;
|
filters?: MetadataFilters | undefined;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
index,
|
index,
|
||||||
@@ -467,7 +467,7 @@ export class VectorIndexRetriever implements BaseRetriever {
|
|||||||
const result = await vectorStore.query({
|
const result = await vectorStore.query({
|
||||||
queryEmbedding,
|
queryEmbedding,
|
||||||
mode: VectorStoreQueryMode.DEFAULT,
|
mode: VectorStoreQueryMode.DEFAULT,
|
||||||
similarityTopK: this.topK[type],
|
similarityTopK: this.topK[type]!,
|
||||||
filters: this.filters ?? filters ?? undefined,
|
filters: this.filters ?? filters ?? undefined,
|
||||||
});
|
});
|
||||||
nodes = nodes.concat(this.buildNodeListFromQueryResult(result));
|
nodes = nodes.concat(this.buildNodeListFromQueryResult(result));
|
||||||
@@ -480,11 +480,11 @@ export class VectorIndexRetriever implements BaseRetriever {
|
|||||||
const nodesWithScores: NodeWithScore[] = [];
|
const nodesWithScores: NodeWithScore[] = [];
|
||||||
for (let i = 0; i < result.ids.length; i++) {
|
for (let i = 0; i < result.ids.length; i++) {
|
||||||
const nodeFromResult = result.nodes?.[i];
|
const nodeFromResult = result.nodes?.[i];
|
||||||
if (!this.index.indexStruct.nodesDict[result.ids[i]] && nodeFromResult) {
|
if (!this.index.indexStruct.nodesDict[result.ids[i]!] && nodeFromResult) {
|
||||||
this.index.indexStruct.nodesDict[result.ids[i]] = nodeFromResult;
|
this.index.indexStruct.nodesDict[result.ids[i]!] = nodeFromResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = this.index.indexStruct.nodesDict[result.ids[i]];
|
const node = this.index.indexStruct.nodesDict[result.ids[i]!]!;
|
||||||
// XXX: Hack, if it's an image node, we reconstruct the image from the URL
|
// XXX: Hack, if it's an image node, we reconstruct the image from the URL
|
||||||
// Alternative: Store image in doc store and retrieve it here
|
// Alternative: Store image in doc store and retrieve it here
|
||||||
if (node instanceof ImageNode) {
|
if (node instanceof ImageNode) {
|
||||||
@@ -493,7 +493,7 @@ export class VectorIndexRetriever implements BaseRetriever {
|
|||||||
|
|
||||||
nodesWithScores.push({
|
nodesWithScores.push({
|
||||||
node: node,
|
node: node,
|
||||||
score: result.similarities[i],
|
score: result.similarities[i]!,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,7 @@ import {
|
|||||||
type Metadata,
|
type Metadata,
|
||||||
} from "@llamaindex/core/schema";
|
} from "@llamaindex/core/schema";
|
||||||
import type { BaseDocumentStore } from "../storage/docStore/types.js";
|
import type { BaseDocumentStore } from "../storage/docStore/types.js";
|
||||||
import type {
|
import type { VectorStore, VectorStoreByType } from "../vector-store/types.js";
|
||||||
VectorStore,
|
|
||||||
VectorStoreByType,
|
|
||||||
} from "../storage/vectorStore/types.js";
|
|
||||||
import { IngestionCache, getTransformationHash } from "./IngestionCache.js";
|
import { IngestionCache, getTransformationHash } from "./IngestionCache.js";
|
||||||
import {
|
import {
|
||||||
DocStoreStrategy,
|
DocStoreStrategy,
|
||||||
@@ -60,13 +57,13 @@ export async function runTransformations(
|
|||||||
|
|
||||||
export class IngestionPipeline {
|
export class IngestionPipeline {
|
||||||
transformations: TransformComponent[] = [];
|
transformations: TransformComponent[] = [];
|
||||||
documents?: Document[];
|
documents?: Document[] | undefined;
|
||||||
reader?: BaseReader;
|
reader?: BaseReader | undefined;
|
||||||
vectorStore?: VectorStore;
|
vectorStore?: VectorStore | undefined;
|
||||||
vectorStores?: VectorStoreByType;
|
vectorStores?: VectorStoreByType | undefined;
|
||||||
docStore?: BaseDocumentStore;
|
docStore?: BaseDocumentStore;
|
||||||
docStoreStrategy: DocStoreStrategy = DocStoreStrategy.UPSERTS;
|
docStoreStrategy: DocStoreStrategy = DocStoreStrategy.UPSERTS;
|
||||||
cache?: IngestionCache;
|
cache?: IngestionCache | undefined;
|
||||||
disableCache: boolean = false;
|
disableCache: boolean = false;
|
||||||
|
|
||||||
private _docStoreStrategy?: TransformComponent;
|
private _docStoreStrategy?: TransformComponent;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BaseNode, TransformComponent } from "@llamaindex/core/schema";
|
import { BaseNode, TransformComponent } from "@llamaindex/core/schema";
|
||||||
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
|
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
|
||||||
import type { VectorStore } from "../../storage/vectorStore/types.js";
|
import type { VectorStore } from "../../vector-store/types.js";
|
||||||
import { classify } from "./classify.js";
|
import { classify } from "./classify.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -9,7 +9,7 @@ import { classify } from "./classify.js";
|
|||||||
*/
|
*/
|
||||||
export class UpsertsAndDeleteStrategy extends TransformComponent {
|
export class UpsertsAndDeleteStrategy extends TransformComponent {
|
||||||
protected docStore: BaseDocumentStore;
|
protected docStore: BaseDocumentStore;
|
||||||
protected vectorStores?: VectorStore[];
|
protected vectorStores: VectorStore[] | undefined;
|
||||||
|
|
||||||
constructor(docStore: BaseDocumentStore, vectorStores?: VectorStore[]) {
|
constructor(docStore: BaseDocumentStore, vectorStores?: VectorStore[]) {
|
||||||
super(async (nodes: BaseNode[]): Promise<BaseNode[]> => {
|
super(async (nodes: BaseNode[]): Promise<BaseNode[]> => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BaseNode, TransformComponent } from "@llamaindex/core/schema";
|
import { BaseNode, TransformComponent } from "@llamaindex/core/schema";
|
||||||
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
|
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
|
||||||
import type { VectorStore } from "../../storage/vectorStore/types.js";
|
import type { VectorStore } from "../../vector-store/types.js";
|
||||||
import { classify } from "./classify.js";
|
import { classify } from "./classify.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8,7 +8,7 @@ import { classify } from "./classify.js";
|
|||||||
*/
|
*/
|
||||||
export class UpsertsStrategy extends TransformComponent {
|
export class UpsertsStrategy extends TransformComponent {
|
||||||
protected docStore: BaseDocumentStore;
|
protected docStore: BaseDocumentStore;
|
||||||
protected vectorStores?: VectorStore[];
|
protected vectorStores: VectorStore[] | undefined;
|
||||||
|
|
||||||
constructor(docStore: BaseDocumentStore, vectorStores?: VectorStore[]) {
|
constructor(docStore: BaseDocumentStore, vectorStores?: VectorStore[]) {
|
||||||
super(async (nodes: BaseNode[]): Promise<BaseNode[]> => {
|
super(async (nodes: BaseNode[]): Promise<BaseNode[]> => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { TransformComponent } from "@llamaindex/core/schema";
|
import { TransformComponent } from "@llamaindex/core/schema";
|
||||||
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
|
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
|
||||||
import type { VectorStore } from "../../storage/vectorStore/types.js";
|
import type { VectorStore } from "../../vector-store/types.js";
|
||||||
import { DuplicatesStrategy } from "./DuplicatesStrategy.js";
|
import { DuplicatesStrategy } from "./DuplicatesStrategy.js";
|
||||||
import { UpsertsAndDeleteStrategy } from "./UpsertsAndDeleteStrategy.js";
|
import { UpsertsAndDeleteStrategy } from "./UpsertsAndDeleteStrategy.js";
|
||||||
import { UpsertsStrategy } from "./UpsertsStrategy.js";
|
import { UpsertsStrategy } from "./UpsertsStrategy.js";
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ export function getTopKEmbeddings(
|
|||||||
const similarities: { similarity: number; id: number }[] = [];
|
const similarities: { similarity: number; id: number }[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < embeddings.length; i++) {
|
for (let i = 0; i < embeddings.length; i++) {
|
||||||
const sim = similarity(queryEmbedding, embeddings[i]);
|
const sim = similarity(queryEmbedding, embeddings[i]!);
|
||||||
if (similarityCutoff == null || sim > similarityCutoff) {
|
if (similarityCutoff == null || sim > similarityCutoff) {
|
||||||
similarities.push({ similarity: sim, id: embeddingIds[i] });
|
similarities.push({ similarity: sim, id: embeddingIds[i] });
|
||||||
}
|
}
|
||||||
@@ -78,8 +78,8 @@ export function getTopKEmbeddings(
|
|||||||
if (i >= similarities.length) {
|
if (i >= similarities.length) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
resultSimilarities.push(similarities[i].similarity);
|
resultSimilarities.push(similarities[i]!.similarity);
|
||||||
resultIds.push(similarities[i].id);
|
resultIds.push(similarities[i]!.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [resultSimilarities, resultIds];
|
return [resultSimilarities, resultIds];
|
||||||
@@ -176,7 +176,7 @@ export async function imageToString(input: ImageType): Promise<string> {
|
|||||||
export function stringToImage(input: string): ImageType {
|
export function stringToImage(input: string): ImageType {
|
||||||
if (input.startsWith("data:")) {
|
if (input.startsWith("data:")) {
|
||||||
// if the input is a base64 data URL, convert it back to a Blob
|
// if the input is a base64 data URL, convert it back to a Blob
|
||||||
const base64Data = input.split(",")[1];
|
const base64Data = input.split(",")[1]!;
|
||||||
const byteArray = Buffer.from(base64Data, "base64");
|
const byteArray = Buffer.from(base64Data, "base64");
|
||||||
return new Blob([byteArray]);
|
return new Blob([byteArray]);
|
||||||
} else if (input.startsWith("http://") || input.startsWith("https://")) {
|
} else if (input.startsWith("http://") || input.startsWith("https://")) {
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user