Compare commits

..

11 Commits

Author SHA1 Message Date
github-actions[bot] a8c9c279d6 Release 0.4.6 (#981)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-26 17:22:28 -07:00
Alex Yang eece129831 feat: migrate @llamaindex/cloud package (#984) 2024-06-26 16:51:47 -07:00
Alex Yang 80e4f51a83 fix: remove check-minor-version.mjs
We no longer need that
2024-06-26 15:31:08 -07:00
Alex Yang 22ff0837c3 feat: init @llamaindex/core (#938) 2024-06-26 15:28:57 -07:00
Alex Yang 74d7e05bcb ci: continue when commit lockfile error (#982) 2024-06-26 11:06:06 -07:00
Parham Saidi 1feb23bb83 feat: added Gemini tool calling support (#973) 2024-06-26 10:49:11 -07:00
Marcus Schiesser 08c55ec258 fix: Add metadata to PDFs and use Uint8Array for readers content (#980) 2024-06-26 10:16:23 -07:00
github-actions[bot] 394e797567 Release 0.4.5 (#979)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-26 17:42:19 +07:00
Parham Saidi 6c3e5d08b8 fix: switch to correct reference for a static function (#978) 2024-06-26 17:35:22 +07:00
github-actions[bot] 6e19482814 Release 0.4.4 (#977)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-06-26 14:46:48 +07:00
Marcus Schiesser 42eb73a08f fix: IngestionPipeline not working without vectorStores (#976) 2024-06-26 14:30:58 +07:00
171 changed files with 12121 additions and 1133 deletions
-36
View File
@@ -1,36 +0,0 @@
name: Publish
on:
push:
branches:
- main
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Publish @llamaindex/env
run: npx jsr publish
working-directory: packages/env
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish @llamaindex/llamaindex
run: npx jsr publish --allow-slow-types
working-directory: packages/llamaindex
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+2
View File
@@ -58,9 +58,11 @@ jobs:
# Refs: https://github.com/changesets/changesets/issues/421
- name: Update lock file
continue-on-error: true
run: pnpm install --lockfile-only
- name: Commit lock file
continue-on-error: true
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "chore: update lock file"
+6
View File
@@ -128,6 +128,12 @@ jobs:
run: pnpm run build
- name: Copy examples
run: rsync -rv --exclude=node_modules ./examples ${{ runner.temp }}
- name: Pack @llamaindex/cloud
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/cloud
- name: Pack @llamaindex/core
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/core
- name: Pack @llamaindex/env
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/env
+3
View File
@@ -48,3 +48,6 @@ playwright/.cache/
# intellij
**/.idea
# generated API
packages/cloud/src/client
+22
View File
@@ -1,5 +1,27 @@
# docs
## 0.0.32
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.31
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.30
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.29
### Patch Changes
+6
View File
@@ -0,0 +1,6 @@
# Gemini Agent
import CodeBlock from "@theme/CodeBlock";
import CodeSourceGemini from "!raw-loader!../../../../examples/gemini/agent.ts";
<CodeBlock language="ts">{CodeSourceGemini}</CodeBlock>
+3 -1
View File
@@ -12,12 +12,14 @@ An “agent” is an automated reasoning and decision engine. It takes in a user
LlamaIndex.TS comes with a few built-in agents, but you can also create your own. The built-in agents include:
- OpenAI Agent
- Anthropic Agent
- Anthropic Agent both via Anthropic and Bedrock (in `@llamaIndex/community`)
- Gemini Agent
- ReACT Agent
## Examples
- [OpenAI Agent](../../examples/agent.mdx)
- [Gemini Agent](../../examples/agent_gemini.mdx)
## Api References
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.29",
"version": "0.0.32",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
+65
View File
@@ -0,0 +1,65 @@
import { FunctionTool, Gemini, GEMINI_MODEL, LLMAgent } from "llamaindex";
const sumNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a + b}`,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "The first number",
},
b: {
type: "number",
description: "The second number",
},
},
required: ["a", "b"],
},
},
);
const divideNumbers = FunctionTool.from(
({ a, b }: { a: number; b: number }) => `${a / b}`,
{
name: "divideNumbers",
description: "Use this function to divide two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "The dividend a to divide",
},
b: {
type: "number",
description: "The divisor b to divide by",
},
},
required: ["a", "b"],
},
},
);
async function main() {
const gemini = new Gemini({
model: GEMINI_MODEL.GEMINI_PRO,
});
const agent = new LLMAgent({
llm: gemini,
tools: [sumNumbers, divideNumbers],
});
const response = await agent.chat({
message: "How much is 5 + 5? then divide by 2",
});
console.log(response.message);
}
void main().then(() => {
console.log("Done");
});
@@ -7,7 +7,7 @@ import {
import { TextFileReader } from "llamaindex/readers/TextFileReader";
class ZipReader extends FileReader {
loadDataAsContent(fileContent: Buffer): Promise<Document<Metadata>[]> {
loadDataAsContent(fileContent: Uint8Array): Promise<Document<Metadata>[]> {
throw new Error("Implement me");
}
}
+8 -6
View File
@@ -12,10 +12,9 @@
"e2e": "turbo run e2e",
"test": "turbo run test",
"type-check": "tsc -b --diagnostics",
"release": "pnpm run check-minor-version && pnpm run build:release && changeset publish",
"release-snapshot": "pnpm run check-minor-version && pnpm run build:release && changeset publish --tag snapshot",
"check-minor-version": "node ./scripts/check-minor-version.mjs",
"new-version": "changeset version && pnpm run check-minor-version && pnpm format:write && pnpm run build:release",
"release": "pnpm run build:release && changeset publish",
"release-snapshot": "pnpm run build:release && changeset publish --tag snapshot",
"new-version": "changeset version && pnpm format:write && pnpm run build:release",
"new-snapshot": "pnpm run build:release && changeset version --snapshot"
},
"devDependencies": {
@@ -24,14 +23,14 @@
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.4",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "^1.13.4",
"eslint-config-turbo": "^2.0.5",
"eslint-plugin-react": "7.34.1",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"madge": "^7.0.0",
"prettier": "^3.3.2",
"prettier-plugin-organize-imports": "^3.2.4",
"turbo": "^1.13.4",
"turbo": "^2.0.5",
"typescript": "^5.5.2"
},
"packageManager": "pnpm@9.4.0",
@@ -40,6 +39,9 @@
"trim": "1.0.1",
"@babel/traverse": "7.23.2",
"protobufjs": "7.2.6"
},
"patchedDependencies": {
"bunchee@5.2.1": "patches/bunchee@5.2.1.patch"
}
},
"lint-staged": {
@@ -1,5 +1,30 @@
# @llamaindex/autotool-02-next-example
## 0.1.16
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
- @llamaindex/autotool@1.0.0
## 0.1.15
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
- @llamaindex/autotool@1.0.0
## 0.1.14
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
- @llamaindex/autotool@1.0.0
## 0.1.13
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool-02-next-example",
"private": true,
"version": "0.1.13",
"version": "0.1.16",
"scripts": {
"dev": "next dev",
"build": "next build",
+1 -1
View File
@@ -51,7 +51,7 @@
"unplugin": "^1.10.1"
},
"peerDependencies": {
"llamaindex": "^0.4.3",
"llamaindex": "^0.4.6",
"openai": "^4",
"typescript": "^4"
},
+18
View File
@@ -0,0 +1,18 @@
# @llamaindex/cloud
> LlamaCloud is a new generation of managed parsing, ingestion, and retrieval services, designed to bring production-grade context-augmentation to your LLM and RAG applications.
## Usage
```ts
import { OpenAPI, Service } from "@llamaindex/cloud/api";
OpenAPI.TOKEN = "YOUR_API_KEY";
OpenAPI.BASE = "https://api.cloud.llamaindex.ai/";
// ...
```
For more information, see the [API documentation](https://docs.cloud.llamaindex.ai/).
## License
MIT
+15
View File
@@ -0,0 +1,15 @@
import { defineConfig } from "@hey-api/openapi-ts";
export default defineConfig({
// you can download this file to get the latest version of the OpenAPI document
// @link https://api.cloud.llamaindex.ai/api/openapi.json
input: "./openapi.json",
output: {
path: "./src/client",
format: "prettier",
lint: "eslint",
},
types: {
enums: "javascript",
},
});
File diff suppressed because it is too large Load Diff
+40
View File
@@ -0,0 +1,40 @@
{
"name": "@llamaindex/cloud",
"version": "0.1.0",
"type": "module",
"license": "MIT",
"scripts": {
"generate": "pnpm dlx @hey-api/openapi-ts",
"build": "pnpm run generate && bunchee"
},
"files": [
"openapi.json",
"dist"
],
"exports": {
"./openapi.json": "./openapi.json",
"./api": {
"require": {
"types": "./dist/api.d.cts",
"default": "./dist/api.cjs"
},
"import": {
"types": "./dist/api.d.ts",
"default": "./dist/api.js"
},
"default": {
"types": "./dist/api.d.ts",
"default": "./dist/api.js"
}
}
},
"repository": {
"type": "git",
"url": "https://github.com/himself65/LlamaIndexTS.git",
"directory": "packages/cloud"
},
"devDependencies": {
"@hey-api/openapi-ts": "^0.48.0",
"bunchee": "^5.2.1"
}
}
+4
View File
@@ -0,0 +1,4 @@
import * as Service from "./client/services.gen";
export * from "./client";
export { Service };
+15
View File
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist/type",
"tsBuildInfoFile": "./dist/.tsbuildinfo",
"emitDeclarationOnly": true,
"moduleResolution": "Bundler",
"skipLibCheck": true,
"strict": true,
"lib": ["DOM", "ESNext"]
},
"include": ["./src"],
"exclude": ["node_modules"]
}
+22
View File
@@ -1,5 +1,27 @@
# @llamaindex/community
## 0.0.10
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.9
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.8
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.7
### Patch Changes
-8
View File
@@ -1,8 +0,0 @@
{
"name": "@llamaindex/community",
"version": "0.0.5",
"exports": {
".": "./src/index.ts",
"./type": "./src/type.ts"
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/community",
"description": "Community package for LlamaIndexTS",
"version": "0.0.7",
"version": "0.0.10",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
+67
View File
@@ -0,0 +1,67 @@
{
"name": "@llamaindex/core",
"type": "module",
"version": "0.0.1",
"description": "LlamaIndex Core Module",
"exports": {
"./decorator": {
"require": {
"types": "./dist/decorator/index.d.cts",
"default": "./dist/decorator/index.cjs"
},
"import": {
"types": "./dist/decorator/index.d.ts",
"default": "./dist/decorator/index.js"
},
"default": {
"types": "./dist/decorator/index.d.ts",
"default": "./dist/decorator/index.js"
}
},
"./global": {
"require": {
"types": "./dist/global/index.d.cts",
"default": "./dist/global/index.cjs"
},
"import": {
"types": "./dist/global/index.d.ts",
"default": "./dist/global/index.js"
},
"default": {
"types": "./dist/global/index.d.ts",
"default": "./dist/global/index.js"
}
},
"./schema": {
"require": {
"types": "./dist/schema/index.d.cts",
"default": "./dist/schema/index.cjs"
},
"import": {
"types": "./dist/schema/index.d.ts",
"default": "./dist/schema/index.js"
},
"default": {
"types": "./dist/schema/index.d.ts",
"default": "./dist/schema/index.js"
}
}
},
"scripts": {
"dev": "bunchee --watch",
"build": "bunchee"
},
"repository": {
"type": "git",
"directory": "packages/core",
"url": "https://github.com/himself65/LlamaIndexTS.git"
},
"devDependencies": {
"bunchee": "^5.2.1"
},
"dependencies": {
"@llamaindex/env": "workspace:*",
"@types/node": "^20.14.9",
"zod": "^3.23.8"
}
}
@@ -1,11 +1,11 @@
import { getEnv } from "@llamaindex/env";
import type { BaseNode } from "../../Node.js";
import { getChunkSize } from "../settings/chunk-size.js";
import { Settings } from "../global";
import type { BaseNode } from "../schema/node";
const emitOnce = false;
export function chunkSizeCheck<
This extends BaseNode,
This extends { id_: string },
Args extends any[],
Return,
>(
@@ -17,7 +17,7 @@ export function chunkSizeCheck<
) {
return function (this: This, ...args: Args) {
const content = contentGetter.call(this, ...args);
const chunkSize = getChunkSize();
const chunkSize = Settings.chunkSize;
const enableChunkSizeCheck = getEnv("ENABLE_CHUNK_SIZE_CHECK") === "true";
if (
enableChunkSizeCheck &&
@@ -25,7 +25,7 @@ export function chunkSizeCheck<
content.length > chunkSize
) {
console.warn(
`Node (${this.id_}) is larger than chunk size: ${content.length}`,
`Node (${this.id_}) is larger than chunk size: ${content.length} > ${chunkSize}`,
);
if (!emitOnce) {
console.warn(
+1
View File
@@ -0,0 +1 @@
export { Settings } from "./settings";
+17
View File
@@ -0,0 +1,17 @@
import {
getChunkSize,
setChunkSize,
withChunkSize,
} from "./settings/chunk-size";
export const Settings = {
get chunkSize(): number | undefined {
return getChunkSize();
},
set chunkSize(chunkSize: number | undefined) {
setChunkSize(chunkSize);
},
withChunkSize<Result>(chunkSize: number, fn: () => Result): Result {
return withChunkSize(chunkSize, fn);
},
};
@@ -1,14 +1,16 @@
import { AsyncLocalStorage } from "@llamaindex/env";
const chunkSizeAsyncLocalStorage = new AsyncLocalStorage<number | undefined>();
const globalChunkSize: number | null = null;
let globalChunkSize: number | null = null;
export function getChunkSize(): number | undefined {
return globalChunkSize ?? chunkSizeAsyncLocalStorage.getStore();
}
export function setChunkSize(chunkSize: number | undefined) {
chunkSizeAsyncLocalStorage.enterWith(chunkSize);
if (chunkSize !== undefined) {
globalChunkSize = chunkSize;
}
}
export function withChunkSize<Result>(
+1
View File
@@ -0,0 +1 @@
export * from "./schema";
+2
View File
@@ -0,0 +1,2 @@
export * from "./node";
export * from "./zod";
+452
View File
@@ -0,0 +1,452 @@
import { createSHA256, path, randomUUID } from "@llamaindex/env";
import { chunkSizeCheck, lazyInitHash } from "../decorator";
export enum NodeRelationship {
SOURCE = "SOURCE",
PREVIOUS = "PREVIOUS",
NEXT = "NEXT",
PARENT = "PARENT",
CHILD = "CHILD",
}
export enum ObjectType {
TEXT = "TEXT",
IMAGE = "IMAGE",
INDEX = "INDEX",
DOCUMENT = "DOCUMENT",
IMAGE_DOCUMENT = "IMAGE_DOCUMENT",
}
export enum MetadataMode {
ALL = "ALL",
EMBED = "EMBED",
LLM = "LLM",
NONE = "NONE",
}
export type Metadata = Record<string, any>;
export interface RelatedNodeInfo<T extends Metadata = Metadata> {
nodeId: string;
nodeType?: ObjectType;
metadata: T;
hash?: string;
}
export type RelatedNodeType<T extends Metadata = Metadata> =
| RelatedNodeInfo<T>
| RelatedNodeInfo<T>[];
export type BaseNodeParams<T extends Metadata = Metadata> = {
id_?: string;
metadata?: T;
excludedEmbedMetadataKeys?: string[];
excludedLlmMetadataKeys?: string[];
relationships?: Partial<Record<NodeRelationship, RelatedNodeType<T>>>;
hash?: string;
embedding?: number[];
};
/**
* Generic abstract class for retrievable nodes
*/
export abstract class BaseNode<T extends Metadata = Metadata> {
/**
* The unique ID of the Node/Document. The trailing underscore is here
* to avoid collisions with the id keyword in Python.
*
* Set to a UUID by default.
*/
id_: string;
embedding?: number[];
// Metadata fields
metadata: T;
excludedEmbedMetadataKeys: string[];
excludedLlmMetadataKeys: string[];
relationships: Partial<Record<NodeRelationship, RelatedNodeType<T>>>;
@lazyInitHash
accessor hash: string = "";
protected constructor(init?: BaseNodeParams<T>) {
const {
id_,
metadata,
excludedEmbedMetadataKeys,
excludedLlmMetadataKeys,
relationships,
hash,
embedding,
} = init || {};
this.id_ = id_ ?? randomUUID();
this.metadata = metadata ?? ({} as T);
this.excludedEmbedMetadataKeys = excludedEmbedMetadataKeys ?? [];
this.excludedLlmMetadataKeys = excludedLlmMetadataKeys ?? [];
this.relationships = relationships ?? {};
this.embedding = embedding;
}
abstract get type(): ObjectType;
abstract getContent(metadataMode: MetadataMode): string;
abstract getMetadataStr(metadataMode: MetadataMode): string;
// todo: set value as a generic type
abstract setContent(value: unknown): void;
get sourceNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.SOURCE];
if (Array.isArray(relationship)) {
throw new Error("Source object must be a single RelatedNodeInfo object");
}
return relationship;
}
get prevNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.PREVIOUS];
if (Array.isArray(relationship)) {
throw new Error(
"Previous object must be a single RelatedNodeInfo object",
);
}
return relationship;
}
get nextNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.NEXT];
if (Array.isArray(relationship)) {
throw new Error("Next object must be a single RelatedNodeInfo object");
}
return relationship;
}
get parentNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.PARENT];
if (Array.isArray(relationship)) {
throw new Error("Parent object must be a single RelatedNodeInfo object");
}
return relationship;
}
get childNodes(): RelatedNodeInfo<T>[] | undefined {
const relationship = this.relationships[NodeRelationship.CHILD];
if (!Array.isArray(relationship)) {
throw new Error(
"Child object must be a an array of RelatedNodeInfo objects",
);
}
return relationship;
}
abstract generateHash(): string;
getEmbedding(): number[] {
if (this.embedding === undefined) {
throw new Error("Embedding not set");
}
return this.embedding;
}
asRelatedNodeInfo(): RelatedNodeInfo<T> {
return {
nodeId: this.id_,
metadata: this.metadata,
hash: this.hash,
};
}
/**
* Called by built in JSON.stringify (see https://javascript.info/json)
* Properties are read-only as they are not deep-cloned (not necessary for stringification).
* @see toMutableJSON - use to return a mutable JSON instead
*/
toJSON(): Record<string, any> {
return {
...this,
type: this.type,
// hash is an accessor property, so it's not included in the rest operator
hash: this.hash,
};
}
clone(): BaseNode {
return jsonToNode(this.toMutableJSON()) as BaseNode;
}
/**
* Converts the object to a JSON representation.
* Properties can be safely modified as a deep clone of the properties are created.
* @return {Record<string, any>} - The JSON representation of the object.
*/
toMutableJSON(): Record<string, any> {
return structuredClone(this.toJSON());
}
}
export type TextNodeParams<T extends Metadata = Metadata> =
BaseNodeParams<T> & {
text?: string;
textTemplate?: string;
startCharIdx?: number;
endCharIdx?: number;
metadataSeparator?: string;
};
/**
* TextNode is the default node type for text. Most common node type in LlamaIndex.TS
*/
export class TextNode<T extends Metadata = Metadata> extends BaseNode<T> {
text: string;
textTemplate: string;
startCharIdx?: number;
endCharIdx?: number;
// textTemplate: NOTE write your own formatter if needed
// metadataTemplate: NOTE write your own formatter if needed
metadataSeparator: string;
constructor(init: TextNodeParams<T> = {}) {
super(init);
const { text, textTemplate, startCharIdx, endCharIdx, metadataSeparator } =
init;
this.text = text ?? "";
this.textTemplate = textTemplate ?? "";
if (startCharIdx) {
this.startCharIdx = startCharIdx;
}
if (endCharIdx) {
this.endCharIdx = endCharIdx;
}
this.metadataSeparator = metadataSeparator ?? "\n";
}
/**
* Generate a hash of the text node.
* The ID is not part of the hash as it can change independent of content.
* @returns
*/
generateHash() {
const hashFunction = createSHA256();
hashFunction.update(`type=${this.type}`);
hashFunction.update(
`startCharIdx=${this.startCharIdx} endCharIdx=${this.endCharIdx}`,
);
hashFunction.update(this.getContent(MetadataMode.ALL));
return hashFunction.digest();
}
get type() {
return ObjectType.TEXT;
}
@chunkSizeCheck
getContent(metadataMode: MetadataMode = MetadataMode.NONE): string {
const metadataStr = this.getMetadataStr(metadataMode).trim();
return `${metadataStr}\n\n${this.text}`.trim();
}
getMetadataStr(metadataMode: MetadataMode): string {
if (metadataMode === MetadataMode.NONE) {
return "";
}
const usableMetadataKeys = new Set(Object.keys(this.metadata).sort());
if (metadataMode === MetadataMode.LLM) {
for (const key of this.excludedLlmMetadataKeys) {
usableMetadataKeys.delete(key);
}
} else if (metadataMode === MetadataMode.EMBED) {
for (const key of this.excludedEmbedMetadataKeys) {
usableMetadataKeys.delete(key);
}
}
return [...usableMetadataKeys]
.map((key) => `${key}: ${this.metadata[key]}`)
.join(this.metadataSeparator);
}
setContent(value: string) {
this.text = value;
this.hash = this.generateHash();
}
getNodeInfo() {
return { start: this.startCharIdx, end: this.endCharIdx };
}
getText() {
return this.getContent(MetadataMode.NONE);
}
}
export type IndexNodeParams<T extends Metadata = Metadata> =
TextNodeParams<T> & {
indexId: string;
};
export class IndexNode<T extends Metadata = Metadata> extends TextNode<T> {
indexId: string;
constructor(init?: IndexNodeParams<T>) {
super(init);
const { indexId } = init || {};
this.indexId = indexId ?? "";
}
get type() {
return ObjectType.INDEX;
}
}
/**
* A document is just a special text node with a docId.
*/
export class Document<T extends Metadata = Metadata> extends TextNode<T> {
constructor(init?: TextNodeParams<T>) {
super(init);
}
get type() {
return ObjectType.DOCUMENT;
}
}
export function jsonToNode(json: any, type?: ObjectType) {
if (!json.type && !type) {
throw new Error("Node type not found");
}
const nodeType = type || json.type;
switch (nodeType) {
case ObjectType.TEXT:
return new TextNode(json);
case ObjectType.INDEX:
return new IndexNode(json);
case ObjectType.DOCUMENT:
return new Document(json);
case ObjectType.IMAGE_DOCUMENT:
return new ImageDocument(json);
default:
throw new Error(`Invalid node type: ${nodeType}`);
}
}
export type ImageType = string | Blob | URL;
export type ImageNodeParams<T extends Metadata = Metadata> =
TextNodeParams<T> & {
image: ImageType;
};
export class ImageNode<T extends Metadata = Metadata> extends TextNode<T> {
image: ImageType; // image as blob
constructor(init: ImageNodeParams<T>) {
super(init);
const { image } = init;
this.image = image;
}
get type() {
return ObjectType.IMAGE;
}
getUrl(): URL {
// id_ stores the relative path, convert it to the URL of the file
const absPath = path.resolve(this.id_);
return new URL(`file://${absPath}`);
}
// Calculates the image part of the hash
private generateImageHash() {
const hashFunction = createSHA256();
if (this.image instanceof Blob) {
// TODO: ideally we should use the blob's content to calculate the hash:
// hashFunction.update(new Uint8Array(await this.image.arrayBuffer()));
// as this is async, we're using the node's ID for the time being
hashFunction.update(this.id_);
} else if (this.image instanceof URL) {
hashFunction.update(this.image.toString());
} else if (typeof this.image === "string") {
hashFunction.update(this.image);
} else {
throw new Error(
`Unknown image type: ${typeof this.image}. Can't calculate hash`,
);
}
return hashFunction.digest();
}
generateHash() {
const hashFunction = createSHA256();
// calculates hash based on hash of both components (image and text)
hashFunction.update(super.generateHash());
hashFunction.update(this.generateImageHash());
return hashFunction.digest();
}
}
export class ImageDocument<T extends Metadata = Metadata> extends ImageNode<T> {
constructor(init: ImageNodeParams<T>) {
super(init);
}
get type() {
return ObjectType.IMAGE_DOCUMENT;
}
}
/**
* A node with a similarity score
*/
export interface NodeWithScore<T extends Metadata = Metadata> {
node: BaseNode<T>;
score?: number;
}
export enum ModalityType {
TEXT = "TEXT",
IMAGE = "IMAGE",
}
type NodesByType = {
[P in ModalityType]?: BaseNode[];
};
export function splitNodesByType(nodes: BaseNode[]): NodesByType {
const result: NodesByType = {};
for (const node of nodes) {
let type: ModalityType;
if (node instanceof ImageNode) {
type = ModalityType.IMAGE;
} else if (node instanceof TextNode) {
type = ModalityType.TEXT;
} else {
throw new Error(`Unknown node type: ${node.type}`);
}
if (type in result) {
result[type]?.push(node);
} else {
result[type] = [node];
}
}
return result;
}
+16
View File
@@ -0,0 +1,16 @@
import { z } from "zod";
export const toolMetadataSchema = z.object({
description: z.string(),
name: z.string(),
parameters: z.record(z.any()),
});
export const baseToolSchema = z.object({
call: z.optional(z.function()),
metadata: toolMetadataSchema,
});
export const baseToolWithCallSchema = baseToolSchema.extend({
call: z.function(),
});
+25
View File
@@ -0,0 +1,25 @@
import { Settings } from "@llamaindex/core/global";
import { TextNode } from "@llamaindex/core/schema";
import { env } from "process";
import { afterEach, describe, expect, test, vi } from "vitest";
describe("chunkSizeCheck", () => {
afterEach(() => {
Settings.chunkSize = undefined;
env.ENABLE_CHUNK_SIZE_CHECK = undefined;
});
test("should warn when content is larger than chunk size", () => {
env.ENABLE_CHUNK_SIZE_CHECK = "true";
let message = "";
const consoleMock = vi
.spyOn(console, "warn")
.mockImplementation((msg) => (message += msg + "\n"));
Settings.chunkSize = 0;
const node = new TextNode();
expect(message).toEqual("");
node.setContent("a".repeat(1024));
expect(message).toContain("is larger than chunk size");
});
});
+12
View File
@@ -0,0 +1,12 @@
{
"name": "@llamaindex/core-tests",
"private": true,
"type": "module",
"scripts": {
"test": "vitest"
},
"devDependencies": {
"@llamaindex/core": "workspace:*",
"vitest": "^1.6.0"
}
}
@@ -1,4 +1,4 @@
import { Document, TextNode } from "llamaindex/Node";
import { Document, TextNode } from "@llamaindex/core/schema";
import { beforeEach, describe, expect, test } from "vitest";
describe("Document", () => {
@@ -50,7 +50,6 @@ describe("TextNode", () => {
`
{
"embedding": undefined,
"endCharIdx": undefined,
"excludedEmbedMetadataKeys": [],
"excludedLlmMetadataKeys": [],
"hash": "Z6SWgFPlalaeblMGQGw0KS3qKgmZdEWXKfzEp/K+QN0=",
@@ -62,7 +61,6 @@ describe("TextNode", () => {
"metadataSeparator": "
",
"relationships": {},
"startCharIdx": undefined,
"text": "Hello World",
"textTemplate": "",
"type": "TEXT",
+18
View File
@@ -0,0 +1,18 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib",
"module": "node16",
"moduleResolution": "node16",
"target": "ESNext"
},
"include": ["./**/*.ts"],
"references": [
{
"path": "../tsconfig.json"
},
{
"path": "../../env/tsconfig.json"
}
]
}
+15
View File
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist/type",
"tsBuildInfoFile": "./dist/.tsbuildinfo",
"emitDeclarationOnly": true,
"moduleResolution": "Bundler",
"skipLibCheck": true,
"strict": true,
"types": ["node"]
},
"include": ["./src"],
"exclude": ["node_modules"]
}
-10
View File
@@ -1,10 +0,0 @@
{
"name": "@llamaindex/env",
"version": "0.1.6",
"exports": {
".": "./src/index.edge-light.ts"
},
"publish": {
"include": ["LICENSE", "README.md", "src/**/*", "jsr.json"]
}
}
+22
View File
@@ -1,5 +1,27 @@
# @llamaindex/experimental
## 0.0.41
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.40
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.39
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.38
### Patch Changes
-8
View File
@@ -1,8 +0,0 @@
{
"name": "@llamaindex/experimental",
"version": "0.0.5",
"exports": {
".": "./src/index.ts",
"./type": "./src/type.ts"
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.38",
"version": "0.0.41",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
+19
View File
@@ -1,5 +1,24 @@
# llamaindex
## 0.4.6
### Patch Changes
- 1feb23b: feat: Gemini tool calling for agent support
- 08c55ec: Add metadata to PDFs and use Uint8Array for readers content
## 0.4.5
### Patch Changes
- 6c3e5d0: fix: switch to correct reference for a static function
## 0.4.4
### Patch Changes
- 42eb73a: Fix IngestionPipeline not working without vectorStores
## 0.4.3
### Patch Changes
@@ -1,5 +1,27 @@
# @llamaindex/cloudflare-worker-agent-test
## 0.0.25
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.24
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.23
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.22
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloudflare-worker-agent-test",
"version": "0.0.22",
"version": "0.0.25",
"type": "module",
"private": true,
"scripts": {
@@ -1,5 +1,27 @@
# @llamaindex/next-agent-test
## 0.1.25
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.1.24
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.1.23
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.1.22
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-agent-test",
"version": "0.1.22",
"version": "0.1.25",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,27 @@
# test-edge-runtime
## 0.1.24
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.1.23
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.1.22
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.1.21
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/nextjs-edge-runtime-test",
"version": "0.1.21",
"version": "0.1.24",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,27 @@
# @llamaindex/next-node-runtime
## 0.0.6
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.5
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.4
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.3
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-node-runtime-test",
"version": "0.0.3",
"version": "0.0.6",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,27 @@
# @llamaindex/waku-query-engine-test
## 0.0.25
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.24
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.23
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.22
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/waku-query-engine-test",
"version": "0.0.22",
"version": "0.0.25",
"type": "module",
"private": true,
"scripts": {
-11
View File
@@ -1,11 +0,0 @@
{
"name": "@llamaindex/llamaindex",
"version": "0.4.3",
"exports": "./src/index.ts",
"imports": {
"@llamaindex/env": "jsr:@llamaindex/env@0.1.6"
},
"publish": {
"include": ["LICENSE", "README.md", "src/**/*", "jsr.json"]
}
}
+6 -4
View File
@@ -1,6 +1,6 @@
{
"name": "llamaindex",
"version": "0.4.3",
"version": "0.4.6",
"license": "MIT",
"type": "module",
"keywords": [
@@ -27,7 +27,8 @@
"@google/generative-ai": "^0.12.0",
"@grpc/grpc-js": "^1.10.8",
"@huggingface/inference": "^2.7.0",
"@llamaindex/cloud": "0.0.7",
"@llamaindex/cloud": "workspace:*",
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*",
"@mistralai/mistralai": "^0.4.0",
"@pinecone-database/pinecone": "^2.2.2",
@@ -60,7 +61,8 @@
"tiktoken": "^1.0.15",
"unpdf": "^0.10.1",
"wikipedia": "^2.1.2",
"wink-nlp": "^2.3.0"
"wink-nlp": "^2.3.0",
"zod": "^3.23.8"
},
"peerDependencies": {
"@notionhq/client": "^2.2.15"
@@ -148,7 +150,7 @@
"repository": {
"type": "git",
"url": "https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/core"
"directory": "packages/llamaindex"
},
"scripts": {
"lint": "eslint .",
+2 -2
View File
@@ -1,4 +1,4 @@
import type { NodeWithScore } from "./Node.js";
import type { NodeWithScore } from "@llamaindex/core/schema";
import type {
ChatMessage,
ChatResponse,
@@ -64,7 +64,7 @@ export class EngineResponse implements ChatResponse, ChatResponseChunk {
sourceNodes?: NodeWithScore[],
): EngineResponse {
return new EngineResponse(
this.toChatResponse(chunk.delta, chunk.raw),
EngineResponse.toChatResponse(chunk.delta, chunk.raw),
true,
sourceNodes,
);
+5 -447
View File
@@ -1,448 +1,6 @@
import { createSHA256, path, randomUUID } from "@llamaindex/env";
import { chunkSizeCheck, lazyInitHash } from "./internal/decorator/node.js";
console.warn(
'Package "llamaindex/Node" is deprecated, and will be removed in the next major release.',
);
console.warn("Please import from the @llamaindex/core/schema package instead.");
export enum NodeRelationship {
SOURCE = "SOURCE",
PREVIOUS = "PREVIOUS",
NEXT = "NEXT",
PARENT = "PARENT",
CHILD = "CHILD",
}
export enum ObjectType {
TEXT = "TEXT",
IMAGE = "IMAGE",
INDEX = "INDEX",
DOCUMENT = "DOCUMENT",
IMAGE_DOCUMENT = "IMAGE_DOCUMENT",
}
export enum MetadataMode {
ALL = "ALL",
EMBED = "EMBED",
LLM = "LLM",
NONE = "NONE",
}
export type Metadata = Record<string, any>;
export interface RelatedNodeInfo<T extends Metadata = Metadata> {
nodeId: string;
nodeType?: ObjectType;
metadata: T;
hash?: string;
}
export type RelatedNodeType<T extends Metadata = Metadata> =
| RelatedNodeInfo<T>
| RelatedNodeInfo<T>[];
export type BaseNodeParams<T extends Metadata = Metadata> = {
id_?: string;
metadata?: T;
excludedEmbedMetadataKeys?: string[];
excludedLlmMetadataKeys?: string[];
relationships?: Partial<Record<NodeRelationship, RelatedNodeType<T>>>;
hash?: string;
embedding?: number[];
};
/**
* Generic abstract class for retrievable nodes
*/
export abstract class BaseNode<T extends Metadata = Metadata> {
/**
* The unique ID of the Node/Document. The trailing underscore is here
* to avoid collisions with the id keyword in Python.
*
* Set to a UUID by default.
*/
id_: string;
embedding?: number[];
// Metadata fields
metadata: T;
excludedEmbedMetadataKeys: string[];
excludedLlmMetadataKeys: string[];
relationships: Partial<Record<NodeRelationship, RelatedNodeType<T>>>;
@lazyInitHash
accessor hash: string = "";
protected constructor(init?: BaseNodeParams<T>) {
const {
id_,
metadata,
excludedEmbedMetadataKeys,
excludedLlmMetadataKeys,
relationships,
hash,
embedding,
} = init || {};
this.id_ = id_ ?? randomUUID();
this.metadata = metadata ?? ({} as T);
this.excludedEmbedMetadataKeys = excludedEmbedMetadataKeys ?? [];
this.excludedLlmMetadataKeys = excludedLlmMetadataKeys ?? [];
this.relationships = relationships ?? {};
this.embedding = embedding;
}
abstract get type(): ObjectType;
abstract getContent(metadataMode: MetadataMode): string;
abstract getMetadataStr(metadataMode: MetadataMode): string;
// todo: set value as a generic type
abstract setContent(value: unknown): void;
get sourceNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.SOURCE];
if (Array.isArray(relationship)) {
throw new Error("Source object must be a single RelatedNodeInfo object");
}
return relationship;
}
get prevNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.PREVIOUS];
if (Array.isArray(relationship)) {
throw new Error(
"Previous object must be a single RelatedNodeInfo object",
);
}
return relationship;
}
get nextNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.NEXT];
if (Array.isArray(relationship)) {
throw new Error("Next object must be a single RelatedNodeInfo object");
}
return relationship;
}
get parentNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.PARENT];
if (Array.isArray(relationship)) {
throw new Error("Parent object must be a single RelatedNodeInfo object");
}
return relationship;
}
get childNodes(): RelatedNodeInfo<T>[] | undefined {
const relationship = this.relationships[NodeRelationship.CHILD];
if (!Array.isArray(relationship)) {
throw new Error(
"Child object must be a an array of RelatedNodeInfo objects",
);
}
return relationship;
}
abstract generateHash(): string;
getEmbedding(): number[] {
if (this.embedding === undefined) {
throw new Error("Embedding not set");
}
return this.embedding;
}
asRelatedNodeInfo(): RelatedNodeInfo<T> {
return {
nodeId: this.id_,
metadata: this.metadata,
hash: this.hash,
};
}
/**
* Called by built in JSON.stringify (see https://javascript.info/json)
* Properties are read-only as they are not deep-cloned (not necessary for stringification).
* @see toMutableJSON - use to return a mutable JSON instead
*/
toJSON(): Record<string, any> {
return {
...this,
type: this.type,
// hash is an accessor property, so it's not included in the rest operator
hash: this.hash,
};
}
clone(): BaseNode {
return jsonToNode(this.toMutableJSON()) as BaseNode;
}
/**
* Converts the object to a JSON representation.
* Properties can be safely modified as a deep clone of the properties are created.
* @return {Record<string, any>} - The JSON representation of the object.
*/
toMutableJSON(): Record<string, any> {
return structuredClone(this.toJSON());
}
}
export type TextNodeParams<T extends Metadata = Metadata> =
BaseNodeParams<T> & {
text?: string;
textTemplate?: string;
startCharIdx?: number;
endCharIdx?: number;
metadataSeparator?: string;
};
/**
* TextNode is the default node type for text. Most common node type in LlamaIndex.TS
*/
export class TextNode<T extends Metadata = Metadata> extends BaseNode<T> {
text: string;
textTemplate: string;
startCharIdx?: number;
endCharIdx?: number;
// textTemplate: NOTE write your own formatter if needed
// metadataTemplate: NOTE write your own formatter if needed
metadataSeparator: string;
constructor(init: TextNodeParams<T> = {}) {
super(init);
const { text, textTemplate, startCharIdx, endCharIdx, metadataSeparator } =
init;
this.text = text ?? "";
this.textTemplate = textTemplate ?? "";
if (startCharIdx) {
this.startCharIdx = startCharIdx;
}
this.endCharIdx = endCharIdx;
this.metadataSeparator = metadataSeparator ?? "\n";
}
/**
* Generate a hash of the text node.
* The ID is not part of the hash as it can change independent of content.
* @returns
*/
generateHash() {
const hashFunction = createSHA256();
hashFunction.update(`type=${this.type}`);
hashFunction.update(
`startCharIdx=${this.startCharIdx} endCharIdx=${this.endCharIdx}`,
);
hashFunction.update(this.getContent(MetadataMode.ALL));
return hashFunction.digest();
}
get type() {
return ObjectType.TEXT;
}
@chunkSizeCheck
getContent(metadataMode: MetadataMode = MetadataMode.NONE): string {
const metadataStr = this.getMetadataStr(metadataMode).trim();
return `${metadataStr}\n\n${this.text}`.trim();
}
getMetadataStr(metadataMode: MetadataMode): string {
if (metadataMode === MetadataMode.NONE) {
return "";
}
const usableMetadataKeys = new Set(Object.keys(this.metadata).sort());
if (metadataMode === MetadataMode.LLM) {
for (const key of this.excludedLlmMetadataKeys) {
usableMetadataKeys.delete(key);
}
} else if (metadataMode === MetadataMode.EMBED) {
for (const key of this.excludedEmbedMetadataKeys) {
usableMetadataKeys.delete(key);
}
}
return [...usableMetadataKeys]
.map((key) => `${key}: ${this.metadata[key]}`)
.join(this.metadataSeparator);
}
setContent(value: string) {
this.text = value;
this.hash = this.generateHash();
}
getNodeInfo() {
return { start: this.startCharIdx, end: this.endCharIdx };
}
getText() {
return this.getContent(MetadataMode.NONE);
}
}
export type IndexNodeParams<T extends Metadata = Metadata> =
TextNodeParams<T> & {
indexId: string;
};
export class IndexNode<T extends Metadata = Metadata> extends TextNode<T> {
indexId: string;
constructor(init?: IndexNodeParams<T>) {
super(init);
const { indexId } = init || {};
this.indexId = indexId ?? "";
}
get type() {
return ObjectType.INDEX;
}
}
/**
* A document is just a special text node with a docId.
*/
export class Document<T extends Metadata = Metadata> extends TextNode<T> {
constructor(init?: TextNodeParams<T>) {
super(init);
}
get type() {
return ObjectType.DOCUMENT;
}
}
export function jsonToNode(json: any, type?: ObjectType) {
if (!json.type && !type) {
throw new Error("Node type not found");
}
const nodeType = type || json.type;
switch (nodeType) {
case ObjectType.TEXT:
return new TextNode(json);
case ObjectType.INDEX:
return new IndexNode(json);
case ObjectType.DOCUMENT:
return new Document(json);
case ObjectType.IMAGE_DOCUMENT:
return new ImageDocument(json);
default:
throw new Error(`Invalid node type: ${nodeType}`);
}
}
export type ImageType = string | Blob | URL;
export type ImageNodeParams<T extends Metadata = Metadata> =
TextNodeParams<T> & {
image: ImageType;
};
export class ImageNode<T extends Metadata = Metadata> extends TextNode<T> {
image: ImageType; // image as blob
constructor(init: ImageNodeParams<T>) {
super(init);
const { image } = init;
this.image = image;
}
get type() {
return ObjectType.IMAGE;
}
getUrl(): URL {
// id_ stores the relative path, convert it to the URL of the file
const absPath = path.resolve(this.id_);
return new URL(`file://${absPath}`);
}
// Calculates the image part of the hash
private generateImageHash() {
const hashFunction = createSHA256();
if (this.image instanceof Blob) {
// TODO: ideally we should use the blob's content to calculate the hash:
// hashFunction.update(new Uint8Array(await this.image.arrayBuffer()));
// as this is async, we're using the node's ID for the time being
hashFunction.update(this.id_);
} else if (this.image instanceof URL) {
hashFunction.update(this.image.toString());
} else if (typeof this.image === "string") {
hashFunction.update(this.image);
} else {
throw new Error(
`Unknown image type: ${typeof this.image}. Can't calculate hash`,
);
}
return hashFunction.digest();
}
generateHash() {
const hashFunction = createSHA256();
// calculates hash based on hash of both components (image and text)
hashFunction.update(super.generateHash());
hashFunction.update(this.generateImageHash());
return hashFunction.digest();
}
}
export class ImageDocument<T extends Metadata = Metadata> extends ImageNode<T> {
constructor(init: ImageNodeParams<T>) {
super(init);
}
get type() {
return ObjectType.IMAGE_DOCUMENT;
}
}
/**
* A node with a similarity score
*/
export interface NodeWithScore<T extends Metadata = Metadata> {
node: BaseNode<T>;
score?: number;
}
export enum ModalityType {
TEXT = "TEXT",
IMAGE = "IMAGE",
}
type NodesByType = {
[P in ModalityType]?: BaseNode[];
};
export function splitNodesByType(nodes: BaseNode[]): NodesByType {
const result: NodesByType = {};
for (const node of nodes) {
let type: ModalityType;
if (node instanceof ImageNode) {
type = ModalityType.IMAGE;
} else if (node instanceof TextNode) {
type = ModalityType.TEXT;
} else {
throw new Error(`Unknown node type: ${node.type}`);
}
if (type in result) {
result[type]?.push(node);
} else {
result[type] = [node];
}
}
return result;
}
export * from "@llamaindex/core/schema";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { NodeWithScore } from "./Node.js";
import type { NodeWithScore } from "@llamaindex/core/schema";
import type { ServiceContext } from "./ServiceContext.js";
import type { MessageContent } from "./index.edge.js";
+4 -8
View File
@@ -1,3 +1,4 @@
import { Settings as CoreSettings } from "@llamaindex/core/global";
import { CallbackManager } from "./callbacks/CallbackManager.js";
import { OpenAI } from "./llm/openai.js";
@@ -17,11 +18,6 @@ import {
setEmbeddedModel,
withEmbeddedModel,
} from "./internal/settings/EmbedModel.js";
import {
getChunkSize,
setChunkSize,
withChunkSize,
} from "./internal/settings/chunk-size.js";
import type { LLM } from "./llm/types.js";
import type { NodeParser } from "./nodeParsers/types.js";
@@ -148,15 +144,15 @@ class GlobalSettings implements Config {
}
set chunkSize(chunkSize: number | undefined) {
setChunkSize(chunkSize);
CoreSettings.chunkSize = chunkSize;
}
get chunkSize(): number | undefined {
return getChunkSize();
return CoreSettings.chunkSize;
}
withChunkSize<Result>(chunkSize: number, fn: () => Result): Result {
return withChunkSize(chunkSize, fn);
return CoreSettings.withChunkSize(chunkSize, fn);
}
get chunkOverlap(): number | undefined {
+16 -6
View File
@@ -12,6 +12,7 @@ import { consoleLogger, emptyLogger } from "../internal/logger.js";
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
import { isAsyncIterable } from "../internal/utils.js";
import type { ChatMessage, LLM, MessageContent } from "../llm/index.js";
import { ObjectRetriever } from "../objects/index.js";
import type { BaseToolWithCall, ToolOutput } from "../types.js";
import type {
AgentTaskContext,
@@ -126,12 +127,21 @@ export type AgentParamsBase<
>
? AdditionalMessageOptions
: never,
> = {
llm?: AI;
chatHistory?: ChatMessage<AdditionalMessageOptions>[];
systemPrompt?: MessageContent;
verbose?: boolean;
};
> =
| {
llm?: AI;
chatHistory?: ChatMessage<AdditionalMessageOptions>[];
systemPrompt?: MessageContent;
verbose?: boolean;
tools: BaseToolWithCall[];
}
| {
llm?: AI;
chatHistory?: ChatMessage<AdditionalMessageOptions>[];
systemPrompt?: MessageContent;
verbose?: boolean;
toolRetriever: ObjectRetriever<BaseToolWithCall>;
};
/**
* Worker will schedule tasks and handle the task execution
+2
View File
@@ -3,6 +3,7 @@ import { ObjectRetriever } from "../objects/index.js";
import { Settings } from "../Settings.js";
import type { BaseToolWithCall } from "../types.js";
import { AgentRunner, AgentWorker, type AgentParamsBase } from "./base.js";
import { validateAgentParams } from "./utils.js";
type LLMParamsBase = AgentParamsBase<LLM>;
@@ -22,6 +23,7 @@ export class LLMAgentWorker extends AgentWorker<LLM> {
export class LLMAgent extends AgentRunner<LLM> {
constructor(params: LLMAgentParams) {
validateAgentParams(params);
const llm = params.llm ?? (Settings.llm ? (Settings.llm as LLM) : null);
if (!llm)
throw new Error(
+5 -23
View File
@@ -11,35 +11,18 @@ import {
type LLM,
} from "../llm/index.js";
import { extractText } from "../llm/utils.js";
import { ObjectRetriever } from "../objects/index.js";
import { Settings } from "../Settings.js";
import type {
BaseTool,
BaseToolWithCall,
JSONObject,
JSONValue,
} from "../types.js";
import type { BaseTool, JSONObject, JSONValue } from "../types.js";
import { AgentRunner, AgentWorker, type AgentParamsBase } from "./base.js";
import type { TaskHandler } from "./types.js";
import {
callTool,
consumeAsyncIterable,
createReadableStream,
validateAgentParams,
} from "./utils.js";
type ReACTAgentParamsBase = AgentParamsBase<LLM>;
type ReACTAgentParamsWithTools = ReACTAgentParamsBase & {
tools: BaseToolWithCall[];
};
type ReACTAgentParamsWithToolRetriever = ReACTAgentParamsBase & {
toolRetriever: ObjectRetriever<BaseToolWithCall>;
};
export type ReACTAgentParams =
| ReACTAgentParamsWithTools
| ReACTAgentParamsWithToolRetriever;
export type ReACTAgentParams = AgentParamsBase<LLM>;
type BaseReason = {
type: unknown;
@@ -342,9 +325,8 @@ export class ReACTAgentWorker extends AgentWorker<LLM, ReACTAgentStore> {
}
export class ReActAgent extends AgentRunner<LLM, ReACTAgentStore> {
constructor(
params: ReACTAgentParamsWithTools | ReACTAgentParamsWithToolRetriever,
) {
constructor(params: ReACTAgentParams) {
validateAgentParams(params);
super({
llm: params.llm ?? Settings.llm,
chatHistory: params.chatHistory ?? [],
+22
View File
@@ -1,4 +1,6 @@
import { baseToolWithCallSchema } from "@llamaindex/core/schema";
import { ReadableStream } from "@llamaindex/env";
import { z } from "zod";
import type { Logger } from "../internal/logger.js";
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
import {
@@ -17,6 +19,7 @@ import type {
ToolCallLLMMessageOptions,
} from "../llm/index.js";
import type { BaseTool, JSONObject, JSONValue, ToolOutput } from "../types.js";
import type { AgentParamsBase } from "./base.js";
import type { TaskHandler } from "./types.js";
type StepToolsResponseParams<Model extends LLM> = {
@@ -61,6 +64,7 @@ export async function stepToolsStreaming<Model extends LLM>({
// check if first chunk has tool calls, if so, this is a function call
// otherwise, it's a regular message
const hasToolCall = !!(value.options && "toolCall" in value.options);
enqueueOutput({
taskStep: step,
output: finalStream,
@@ -78,6 +82,14 @@ export async function stepToolsStreaming<Model extends LLM>({
});
}
}
// If there are toolCalls but they didn't get read into the stream, used for Gemini
if (!toolCalls.size && value.options && "toolCall" in value.options) {
value.options.toolCall.forEach((toolCall) => {
toolCalls.set(toolCall.id, toolCall);
});
}
step.context.store.messages = [
...step.context.store.messages,
{
@@ -288,3 +300,13 @@ export function createReadableStream<T>(
},
});
}
export function validateAgentParams<AI extends LLM>(
params: AgentParamsBase<AI>,
) {
if ("tools" in params) {
z.array(baseToolWithCallSchema).parse(params.tools);
} else {
// todo: check `params.toolRetriever` when migrate to @llamaindex/core
}
}
@@ -1,6 +1,6 @@
import type { Anthropic } from "@anthropic-ai/sdk";
import type { NodeWithScore } from "@llamaindex/core/schema";
import { CustomEvent } from "@llamaindex/env";
import type { NodeWithScore } from "../Node.js";
import type { AgentEndEvent, AgentStartEvent } from "../agent/types.js";
import {
EventCaller,
@@ -1,4 +1,4 @@
import type { Document } from "../Node.js";
import type { Document } from "@llamaindex/core/schema";
import type { BaseRetriever } from "../Retriever.js";
import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
import type { TransformComponent } from "../ingestion/types.js";
@@ -8,9 +8,10 @@ import type { QueryEngine } from "../types.js";
import type { CloudRetrieveParams } from "./LlamaCloudRetriever.js";
import { LlamaCloudRetriever } from "./LlamaCloudRetriever.js";
import { getPipelineCreate } from "./config.js";
import type { CloudConstructorParams } from "./types.js";
import { getAppBaseUrl, getClient } from "./utils.js";
import type { CloudConstructorParams } from "./constants.js";
import { getAppBaseUrl, initService } from "./utils.js";
import { Service } from "@llamaindex/cloud/api";
import { getEnv } from "@llamaindex/env";
import { OpenAIEmbedding } from "../embeddings/OpenAIEmbedding.js";
import { SimpleNodeParser } from "../nodeParsers/SimpleNodeParser.js";
@@ -20,6 +21,7 @@ export class LlamaCloudIndex {
constructor(params: CloudConstructorParams) {
this.params = params;
initService(this.params);
}
private async waitForPipelineIngestion(
@@ -31,18 +33,15 @@ export class LlamaCloudIndex {
this.params.projectName,
);
const client = await getClient({
...this.params,
baseUrl: this.params.baseUrl,
});
if (verbose) {
console.log("Waiting for pipeline ingestion: ");
}
while (true) {
const pipelineStatus =
await client.pipelines.getPipelineStatus(pipelineId);
await Service.getPipelineStatusApiV1PipelinesPipelineIdStatusGet({
pipelineId,
});
if (pipelineStatus.status === "SUCCESS") {
if (verbose) {
@@ -79,7 +78,7 @@ export class LlamaCloudIndex {
this.params.projectName,
);
const client = await getClient({
const client = await initService({
...this.params,
baseUrl: this.params.baseUrl,
});
@@ -94,10 +93,10 @@ export class LlamaCloudIndex {
const docsToRemove = new Set<string>();
for (const doc of pendingDocs) {
const { status } = await client.pipelines.getPipelineDocumentStatus(
pipelineId,
doc,
);
const { status } =
await Service.getPipelineDocumentStatusApiV1PipelinesPipelineIdDocumentsDocumentIdStatusGet(
{ pipelineId, documentId: doc },
);
if (status === "NOT_STARTED" || status === "IN_PROGRESS") {
continue;
@@ -140,12 +139,7 @@ export class LlamaCloudIndex {
name: string,
projectName: string,
): Promise<string> {
const client = await getClient({
...this.params,
baseUrl: this.params.baseUrl,
});
const pipelines = await client.pipelines.searchPipelines({
const pipelines = await Service.searchPipelinesApiV1PipelinesGet({
projectName,
pipelineName: name,
});
@@ -169,7 +163,7 @@ export class LlamaCloudIndex {
const appUrl = getAppBaseUrl(params.baseUrl);
const client = await getClient({ ...params, baseUrl: appUrl });
const client = await initService({ ...params, baseUrl: appUrl });
const pipelineCreateParams = await getPipelineCreate({
pipelineName: params.name,
@@ -178,21 +172,23 @@ export class LlamaCloudIndex {
transformations: params.transformations ?? defaultTransformations,
});
const project = await client.projects.upsertProject({
name: params.projectName ?? "default",
const project = await Service.upsertProjectApiV1ProjectsPut({
requestBody: {
name: params.projectName ?? "default",
},
});
if (!project.id) {
throw new Error("Project ID should be defined");
}
const pipeline = await client.pipelines.upsertPipeline({
const pipeline = await Service.upsertPipelineApiV1PipelinesPut({
projectId: project.id,
body: {
requestBody: {
name: params.name,
configuredTransformations:
pipelineCreateParams.configuredTransformations,
pipelineType: pipelineCreateParams.pipelineType,
configured_transformations:
pipelineCreateParams.configured_transformations,
pipeline_type: pipelineCreateParams.pipeline_type,
},
});
@@ -204,21 +200,24 @@ export class LlamaCloudIndex {
console.log(`Created pipeline ${pipeline.id} with name ${params.name}`);
}
await client.pipelines.upsertBatchPipelineDocuments(
pipeline.id,
params.documents.map((doc) => ({
metadata: doc.metadata,
text: doc.text,
excludedEmbedMetadataKeys: doc.excludedLlmMetadataKeys,
excludedLlmMetadataKeys: doc.excludedEmbedMetadataKeys,
id: doc.id_,
})),
await Service.upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut(
{
pipelineId: pipeline.id,
requestBody: params.documents.map((doc) => ({
metadata: doc.metadata,
text: doc.text,
excluded_embed_metadata_keys: doc.excludedEmbedMetadataKeys,
excluded_llm_metadata_keys: doc.excludedEmbedMetadataKeys,
id: doc.id_,
})),
},
);
while (true) {
const pipelineStatus = await client.pipelines.getPipelineStatus(
pipeline.id,
);
const pipelineStatus =
await Service.getPipelineStatusApiV1PipelinesPipelineIdStatusGet({
pipelineId: pipeline.id,
});
if (pipelineStatus.status === "SUCCESS") {
console.info(
@@ -283,7 +282,7 @@ export class LlamaCloudIndex {
async insert(document: Document) {
const appUrl = getAppBaseUrl(this.params.baseUrl);
const client = await getClient({ ...this.params, baseUrl: appUrl });
const client = await initService({ ...this.params, baseUrl: appUrl });
const pipelineId = await this.getPipelineId(
this.params.name,
@@ -294,15 +293,20 @@ export class LlamaCloudIndex {
throw new Error("We couldn't find the pipeline ID for the given name");
}
await client.pipelines.createBatchPipelineDocuments(pipelineId, [
await Service.createBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPost(
{
metadata: document.metadata,
text: document.text,
excludedEmbedMetadataKeys: document.excludedLlmMetadataKeys,
excludedLlmMetadataKeys: document.excludedEmbedMetadataKeys,
id: document.id_,
pipelineId: pipelineId,
requestBody: [
{
metadata: document.metadata,
text: document.text,
excluded_embed_metadata_keys: document.excludedLlmMetadataKeys,
excluded_llm_metadata_keys: document.excludedEmbedMetadataKeys,
id: document.id_,
},
],
},
]);
);
await this.waitForDocumentIngestion([document.id_]);
}
@@ -310,7 +314,7 @@ export class LlamaCloudIndex {
async delete(document: Document) {
const appUrl = getAppBaseUrl(this.params.baseUrl);
const client = await getClient({ ...this.params, baseUrl: appUrl });
const client = await initService({ ...this.params, baseUrl: appUrl });
const pipelineId = await this.getPipelineId(
this.params.name,
@@ -321,7 +325,12 @@ export class LlamaCloudIndex {
throw new Error("We couldn't find the pipeline ID for the given name");
}
await client.pipelines.deletePipelineDocument(pipelineId, document.id_);
await Service.deletePipelineDocumentApiV1PipelinesPipelineIdDocumentsDocumentIdDelete(
{
pipelineId,
documentId: document.id_,
},
);
await this.waitForPipelineIngestion();
}
@@ -329,7 +338,7 @@ export class LlamaCloudIndex {
async refreshDoc(document: Document) {
const appUrl = getAppBaseUrl(this.params.baseUrl);
const client = await getClient({ ...this.params, baseUrl: appUrl });
const client = await initService({ ...this.params, baseUrl: appUrl });
const pipelineId = await this.getPipelineId(
this.params.name,
@@ -340,15 +349,20 @@ export class LlamaCloudIndex {
throw new Error("We couldn't find the pipeline ID for the given name");
}
await client.pipelines.upsertBatchPipelineDocuments(pipelineId, [
await Service.upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut(
{
metadata: document.metadata,
text: document.text,
excludedEmbedMetadataKeys: document.excludedLlmMetadataKeys,
excludedLlmMetadataKeys: document.excludedEmbedMetadataKeys,
id: document.id_,
pipelineId,
requestBody: [
{
metadata: document.metadata,
text: document.text,
excluded_embed_metadata_keys: document.excludedLlmMetadataKeys,
excluded_llm_metadata_keys: document.excludedEmbedMetadataKeys,
id: document.id_,
},
],
},
]);
);
await this.waitForDocumentIngestion([document.id_]);
}
@@ -1,30 +1,33 @@
import type { LlamaCloudApi, LlamaCloudApiClient } from "@llamaindex/cloud";
import type { NodeWithScore } from "../Node.js";
import { ObjectType, jsonToNode } from "../Node.js";
import {
type MetadataFilters,
type RetrievalParams,
Service,
type TextNodeWithScore,
} from "@llamaindex/cloud/api";
import type { NodeWithScore } from "@llamaindex/core/schema";
import { jsonToNode, ObjectType } from "@llamaindex/core/schema";
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
import { wrapEventCaller } from "../internal/context/EventCaller.js";
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
import { extractText } from "../llm/utils.js";
import type { ClientParams, CloudConstructorParams } from "./types.js";
import { DEFAULT_PROJECT_NAME } from "./types.js";
import { getClient } from "./utils.js";
import type { ClientParams, CloudConstructorParams } from "./constants.js";
import { DEFAULT_PROJECT_NAME } from "./constants.js";
import { initService } from "./utils.js";
export type CloudRetrieveParams = Omit<
LlamaCloudApi.RetrievalParams,
RetrievalParams,
"query" | "searchFilters" | "className" | "denseSimilarityTopK"
> & { similarityTopK?: number };
export class LlamaCloudRetriever implements BaseRetriever {
client?: LlamaCloudApiClient;
clientParams: ClientParams;
retrieveParams: CloudRetrieveParams;
projectName: string = DEFAULT_PROJECT_NAME;
pipelineName: string;
private resultNodesToNodeWithScore(
nodes: LlamaCloudApi.TextNodeWithScore[],
nodes: TextNodeWithScore[],
): NodeWithScore[] {
return nodes.map((node: LlamaCloudApi.TextNodeWithScore) => {
return nodes.map((node: TextNodeWithScore) => {
return {
// Currently LlamaCloud only supports text nodes
node: jsonToNode(node.node, ObjectType.TEXT),
@@ -35,6 +38,7 @@ export class LlamaCloudRetriever implements BaseRetriever {
constructor(params: CloudConstructorParams & CloudRetrieveParams) {
this.clientParams = { apiKey: params.apiKey, baseUrl: params.baseUrl };
initService(this.clientParams);
this.retrieveParams = params;
this.pipelineName = params.name;
if (params.projectName) {
@@ -42,21 +46,12 @@ export class LlamaCloudRetriever implements BaseRetriever {
}
}
private async getClient(): Promise<LlamaCloudApiClient> {
if (!this.client) {
this.client = await getClient(this.clientParams);
}
return this.client;
}
@wrapEventCaller
async retrieve({
query,
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
const client = await this.getClient();
const pipelines = await client?.pipelines.searchPipelines({
const pipelines = await Service.searchPipelinesApiV1PipelinesGet({
projectName: this.projectName,
pipelineName: this.pipelineName,
});
@@ -67,7 +62,9 @@ export class LlamaCloudRetriever implements BaseRetriever {
);
}
const pipeline = await client?.pipelines.getPipeline(pipelines[0].id);
const pipeline = await Service.getPipelineApiV1PipelinesPipelineIdGet({
pipelineId: pipelines[0].id,
});
if (!pipeline) {
throw new Error(
@@ -75,19 +72,17 @@ export class LlamaCloudRetriever implements BaseRetriever {
);
}
const results = await client?.pipelines.runSearch(pipeline.id, {
...this.retrieveParams,
query: extractText(query),
searchFilters: preFilters as LlamaCloudApi.MetadataFilters,
});
const results = await Service.runSearchApiV1PipelinesPipelineIdRetrievePost(
{
pipelineId: pipeline.id,
requestBody: {
...this.retrieveParams,
query: extractText(query),
search_filters: preFilters as MetadataFilters,
},
},
);
const nodes = this.resultNodesToNodeWithScore(results.retrievalNodes);
getCallbackManager().dispatchEvent("retrieve", {
query,
nodes,
});
return nodes;
return this.resultNodesToNodeWithScore(results.retrieval_nodes);
}
}
+13 -11
View File
@@ -1,24 +1,27 @@
import { LlamaCloudApi } from "@llamaindex/cloud";
import { BaseNode } from "../Node.js";
import type {
ConfiguredTransformationItem,
PipelineCreate,
PipelineType,
} from "@llamaindex/cloud/api";
import { BaseNode } from "@llamaindex/core/schema";
import { OpenAIEmbedding } from "../embeddings/OpenAIEmbedding.js";
import type { TransformComponent } from "../ingestion/types.js";
import { SimpleNodeParser } from "../nodeParsers/SimpleNodeParser.js";
export type GetPipelineCreateParams = {
pipelineName: string;
pipelineType: any; // TODO: PlatformApi.PipelineType is not exported
pipelineType: PipelineType;
transformations?: TransformComponent[];
inputNodes?: BaseNode[];
};
function getTransformationConfig(
transformation: TransformComponent,
): LlamaCloudApi.ConfiguredTransformationItem {
): ConfiguredTransformationItem {
if (transformation instanceof SimpleNodeParser) {
return {
configurableTransformationType: "SENTENCE_AWARE_NODE_PARSER",
configurable_transformation_type: "SENTENCE_AWARE_NODE_PARSER",
component: {
// TODO: API doesnt accept camelCase
chunk_size: transformation.textSplitter.chunkSize, // TODO: set to public in SentenceSplitter
chunk_overlap: transformation.textSplitter.chunkOverlap, // TODO: set to public in SentenceSplitter
include_metadata: transformation.includeMetadata,
@@ -28,9 +31,8 @@ function getTransformationConfig(
}
if (transformation instanceof OpenAIEmbedding) {
return {
configurableTransformationType: "OPENAI_EMBEDDING",
configurable_transformation_type: "OPENAI_EMBEDDING",
component: {
// TODO: API doesnt accept camelCase
model: transformation.model,
api_key: transformation.apiKey,
embed_batch_size: transformation.embedBatchSize,
@@ -43,12 +45,12 @@ function getTransformationConfig(
export async function getPipelineCreate(
params: GetPipelineCreateParams,
): Promise<LlamaCloudApi.PipelineCreate> {
): Promise<PipelineCreate> {
const { pipelineName, pipelineType, transformations = [] } = params;
return {
name: pipelineName,
configuredTransformations: transformations.map(getTransformationConfig),
pipelineType: pipelineType,
configured_transformations: transformations.map(getTransformationConfig),
pipeline_type: pipelineType,
};
}
+1 -1
View File
@@ -1,6 +1,6 @@
export type { CloudConstructorParams } from "./constants.js";
export { LlamaCloudIndex } from "./LlamaCloudIndex.js";
export {
LlamaCloudRetriever,
type CloudRetrieveParams,
} from "./LlamaCloudRetriever.js";
export type { CloudConstructorParams } from "./types.js";
+9 -18
View File
@@ -1,7 +1,7 @@
import type { LlamaCloudApiClient } from "@llamaindex/cloud";
import { OpenAPI, Service } from "@llamaindex/cloud/api";
import { getEnv } from "@llamaindex/env";
import type { ClientParams } from "./types.js";
import { DEFAULT_BASE_URL } from "./types.js";
import type { ClientParams } from "./constants.js";
import { DEFAULT_BASE_URL } from "./constants.js";
function getBaseUrl(baseUrl?: string): string {
return baseUrl ?? getEnv("LLAMA_CLOUD_BASE_URL") ?? DEFAULT_BASE_URL;
@@ -11,26 +11,17 @@ export function getAppBaseUrl(baseUrl?: string): string {
return getBaseUrl(baseUrl).replace(/api\./, "");
}
export async function getClient({
export function initService({
apiKey,
baseUrl,
}: ClientParams = {}): Promise<LlamaCloudApiClient> {
const { LlamaCloudApiClient } = await import("@llamaindex/cloud");
// Get the environment variables or use defaults
baseUrl = getBaseUrl(baseUrl);
apiKey = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
if (!apiKey) {
}: ClientParams = {}): typeof Service {
OpenAPI.TOKEN = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
OpenAPI.BASE = getBaseUrl(baseUrl);
if (!OpenAPI.TOKEN) {
throw new Error(
"API Key is required for LlamaCloudIndex. Please pass the apiKey parameter",
);
}
const client = new LlamaCloudApiClient({
token: apiKey,
environment: baseUrl,
});
return client;
return Service;
}
@@ -1,5 +1,5 @@
import type { ImageType } from "@llamaindex/core/schema";
import _ from "lodash";
import type { ImageType } from "../Node.js";
import { lazyLoadTransformers } from "../internal/deps/transformers.js";
import { MultiModalEmbedding } from "./MultiModalEmbedding.js";
// only import type, to avoid bundling error
@@ -1,4 +1,4 @@
import type { ImageType } from "../Node.js";
import type { ImageType } from "@llamaindex/core/schema";
import { MultiModalEmbedding } from "./MultiModalEmbedding.js";
/**
@@ -5,7 +5,7 @@ import {
splitNodesByType,
type BaseNode,
type ImageType,
} from "../Node.js";
} from "@llamaindex/core/schema";
import type { MessageContentDetail } from "../llm/types.js";
import { extractImage, extractSingleText } from "../llm/utils.js";
import { BaseEmbedding, batchEmbeddings } from "./types.js";
+2 -2
View File
@@ -1,6 +1,6 @@
import type { BaseNode } from "@llamaindex/core/schema";
import { MetadataMode } from "@llamaindex/core/schema";
import { type Tokenizers } from "@llamaindex/env";
import type { BaseNode } from "../Node.js";
import { MetadataMode } from "../Node.js";
import type { TransformComponent } from "../ingestion/types.js";
import type { MessageContentDetail } from "../llm/types.js";
import { extractSingleText } from "../llm/utils.js";
+1 -1
View File
@@ -1,7 +1,7 @@
import type { ImageType } from "@llamaindex/core/schema";
import { fs } from "@llamaindex/env";
import _ from "lodash";
import { filetypemime } from "magic-bytes.js";
import type { ImageType } from "../Node.js";
import { DEFAULT_SIMILARITY_TOP_K } from "../constants.js";
import type { VectorStoreQueryMode } from "../storage/vectorStore/types.js";
@@ -1,4 +1,4 @@
import { type NodeWithScore } from "../../Node.js";
import { type NodeWithScore } from "@llamaindex/core/schema";
import type { ContextSystemPrompt } from "../../Prompt.js";
import { defaultContextSystemPrompt } from "../../Prompt.js";
import type { BaseRetriever } from "../../Retriever.js";
@@ -1,6 +1,6 @@
import type { NodeWithScore } from "@llamaindex/core/schema";
import type { ChatHistory } from "../../ChatHistory.js";
import type { EngineResponse } from "../../EngineResponse.js";
import type { NodeWithScore } from "../../Node.js";
import type { ChatMessage } from "../../llm/index.js";
import type { MessageContent } from "../../llm/types.js";
@@ -1,5 +1,5 @@
import type { NodeWithScore } from "@llamaindex/core/schema";
import type { EngineResponse } from "../../EngineResponse.js";
import type { NodeWithScore } from "../../Node.js";
import type { BaseRetriever } from "../../Retriever.js";
import { wrapEventCaller } from "../../internal/context/EventCaller.js";
import type { BaseNodePostprocessor } from "../../postprocessors/index.js";
@@ -1,5 +1,5 @@
import type { NodeWithScore } from "@llamaindex/core/schema";
import { EngineResponse } from "../../EngineResponse.js";
import type { NodeWithScore } from "../../Node.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { llmFromSettingsOrContext } from "../../Settings.js";
import { PromptMixin } from "../../prompts/index.js";
@@ -1,6 +1,6 @@
import type { NodeWithScore } from "@llamaindex/core/schema";
import { TextNode } from "@llamaindex/core/schema";
import type { EngineResponse } from "../../EngineResponse.js";
import type { NodeWithScore } from "../../Node.js";
import { TextNode } from "../../Node.js";
import { LLMQuestionGenerator } from "../../QuestionGenerator.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { PromptMixin } from "../../prompts/Mixin.js";
@@ -1,4 +1,4 @@
import { MetadataMode } from "../Node.js";
import { MetadataMode } from "@llamaindex/core/schema";
import type { ServiceContext } from "../ServiceContext.js";
import { llmFromSettingsOrContext } from "../Settings.js";
import type { ChatMessage, LLM } from "../llm/types.js";
@@ -1,4 +1,4 @@
import { Document, MetadataMode } from "../Node.js";
import { Document, MetadataMode } from "@llamaindex/core/schema";
import type { ServiceContext } from "../ServiceContext.js";
import { SummaryIndex } from "../indices/summary/index.js";
import { PromptMixin } from "../prompts/Mixin.js";
@@ -1,4 +1,4 @@
import { Document, MetadataMode } from "../Node.js";
import { Document, MetadataMode } from "@llamaindex/core/schema";
import type { ServiceContext } from "../ServiceContext.js";
import { SummaryIndex } from "../indices/summary/index.js";
import { PromptMixin } from "../prompts/Mixin.js";
@@ -1,5 +1,5 @@
import type { BaseNode } from "../Node.js";
import { MetadataMode, TextNode } from "../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import { MetadataMode, TextNode } from "@llamaindex/core/schema";
import type { LLM } from "../llm/index.js";
import { OpenAI } from "../llm/index.js";
import {
+2 -2
View File
@@ -1,5 +1,5 @@
import type { BaseNode } from "../Node.js";
import { MetadataMode, TextNode } from "../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import { MetadataMode, TextNode } from "@llamaindex/core/schema";
import type { TransformComponent } from "../ingestion/types.js";
import { defaultNodeTextTemplate } from "./prompts.js";
+2 -1
View File
@@ -1,6 +1,8 @@
export * from "@llamaindex/core/schema";
export * from "./agent/index.js";
export * from "./callbacks/CallbackManager.js";
export * from "./ChatHistory.js";
export * from "./cloud/index.js";
export * from "./constants.js";
export * from "./embeddings/index.js";
export * from "./EngineResponse.js";
@@ -11,7 +13,6 @@ export * from "./extractors/index.js";
export * from "./indices/index.js";
export * from "./ingestion/index.js";
export * from "./llm/index.js";
export * from "./Node.js";
export * from "./nodeParsers/index.js";
export * from "./objects/index.js";
export * from "./OutputParser.js";
-2
View File
@@ -13,5 +13,3 @@ export {
export { type VertexGeminiSessionOptions } from "./llm/gemini/types.js";
export { GeminiVertexSession } from "./llm/gemini/vertex.js";
// Fern only supports node.js runtime
export * from "./cloud/index.js";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { BaseNode, Document } from "../Node.js";
import type { BaseNode, Document } from "@llamaindex/core/schema";
import type { BaseRetriever } from "../Retriever.js";
import type { ServiceContext } from "../ServiceContext.js";
import { nodeParserFromSettingsOrContext } from "../Settings.js";
@@ -1,5 +1,5 @@
import type { BaseNode } from "../Node.js";
import { jsonToNode } from "../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import { jsonToNode } from "@llamaindex/core/schema";
import { IndexStruct } from "./IndexStruct.js";
export enum IndexStructType {
@@ -1,5 +1,9 @@
import type { BaseNode, Document, NodeWithScore } from "../../Node.js";
import { MetadataMode } from "../../Node.js";
import type {
BaseNode,
Document,
NodeWithScore,
} from "@llamaindex/core/schema";
import { MetadataMode } from "@llamaindex/core/schema";
import type {
KeywordExtractPrompt,
QueryKeywordExtractPrompt,
@@ -1,5 +1,9 @@
import type {
BaseNode,
Document,
NodeWithScore,
} from "@llamaindex/core/schema";
import _ from "lodash";
import type { BaseNode, Document, NodeWithScore } from "../../Node.js";
import type { ChoiceSelectPrompt } from "../../Prompt.js";
import { defaultChoiceSelectPrompt } from "../../Prompt.js";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
@@ -1,6 +1,6 @@
import type { BaseNode } from "@llamaindex/core/schema";
import { MetadataMode } from "@llamaindex/core/schema";
import _ from "lodash";
import type { BaseNode } from "../../Node.js";
import { MetadataMode } from "../../Node.js";
export type NodeFormatterFunction = (summaryNodes: BaseNode[]) => string;
export const defaultFormatNodeBatchFn: NodeFormatterFunction = (
@@ -6,7 +6,7 @@ import {
type BaseNode,
type Document,
type NodeWithScore,
} from "../../Node.js";
} from "@llamaindex/core/schema";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { nodeParserFromSettingsOrContext } from "../../Settings.js";
@@ -1,6 +1,6 @@
import type { BaseNode } from "@llamaindex/core/schema";
import { MetadataMode } from "@llamaindex/core/schema";
import { createSHA256 } from "@llamaindex/env";
import type { BaseNode } from "../Node.js";
import { MetadataMode } from "../Node.js";
import { docToJson, jsonToDoc } from "../storage/docStore/utils.js";
import { SimpleKVStore } from "../storage/kvStore/SimpleKVStore.js";
import type { BaseKVStore } from "../storage/kvStore/types.js";
@@ -4,7 +4,7 @@ import {
type BaseNode,
type Document,
type Metadata,
} from "../Node.js";
} from "@llamaindex/core/schema";
import type { BaseReader } from "../readers/type.js";
import type { BaseDocumentStore } from "../storage/docStore/types.js";
import type {
@@ -77,13 +77,15 @@ export class IngestionPipeline {
if (!this.docStore) {
this.docStoreStrategy = DocStoreStrategy.NONE;
}
this.vectorStores = this.vectorStores ?? {
[ModalityType.TEXT]: this.vectorStore,
};
this.vectorStores =
this.vectorStores ??
(this.vectorStore
? { [ModalityType.TEXT]: this.vectorStore }
: undefined);
this._docStoreStrategy = createDocStoreStrategy(
this.docStoreStrategy,
this.docStore,
Object.values(this.vectorStores),
this.vectorStores ? Object.values(this.vectorStores) : undefined,
);
if (!this.disableCache) {
this.cache = new IngestionCache();
@@ -1,4 +1,4 @@
import type { BaseNode } from "../../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
import type { TransformComponent } from "../types.js";
@@ -1,4 +1,4 @@
import type { BaseNode } from "../../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
import type { VectorStore } from "../../storage/vectorStore/types.js";
import type { TransformComponent } from "../types.js";
@@ -1,4 +1,4 @@
import type { BaseNode } from "../../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
import type { VectorStore } from "../../storage/vectorStore/types.js";
import type { TransformComponent } from "../types.js";
@@ -1,4 +1,4 @@
import type { BaseNode } from "../../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
export async function classify(docStore: BaseDocumentStore, nodes: BaseNode[]) {
+1 -1
View File
@@ -1,4 +1,4 @@
import type { BaseNode } from "../Node.js";
import type { BaseNode } from "@llamaindex/core/schema";
export interface TransformComponent {
transform(nodes: BaseNode[], options?: any): Promise<BaseNode[]>;
+60 -8
View File
@@ -2,17 +2,20 @@ import {
GoogleGenerativeAI,
GenerativeModel as GoogleGenerativeModel,
type EnhancedGenerateContentResponse,
type FunctionCall,
type ModelParams as GoogleModelParams,
type GenerateContentStreamResult as GoogleStreamGenerateContentResult,
} from "@google/generative-ai";
import { getEnv } from "@llamaindex/env";
import { getEnv, randomUUID } from "@llamaindex/env";
import { ToolCallLLM } from "../base.js";
import type {
CompletionResponse,
LLMCompletionParamsNonStreaming,
LLMCompletionParamsStreaming,
LLMMetadata,
ToolCall,
ToolCallLLMMessageOptions,
} from "../types.js";
import { streamConverter, wrapLLMEvent } from "../utils.js";
import {
@@ -29,7 +32,12 @@ import {
type GoogleGeminiSessionOptions,
type IGeminiSession,
} from "./types.js";
import { GeminiHelper, getChatContext, getPartsText } from "./utils.js";
import {
GeminiHelper,
getChatContext,
getPartsText,
mapBaseToolToGeminiFunctionDeclaration,
} from "./utils.js";
export const GEMINI_MODEL_INFO_MAP: Record<GEMINI_MODEL, GeminiModelInfo> = {
[GEMINI_MODEL.GEMINI_PRO]: { contextWindow: 30720 },
@@ -86,13 +94,33 @@ export class GeminiSession implements IGeminiSession {
return response.text();
}
getToolsFromResponse(
response: EnhancedGenerateContentResponse,
): ToolCall[] | undefined {
return response.functionCalls()?.map(
(call: FunctionCall) =>
({
name: call.name,
input: call.args,
id: randomUUID(),
}) as ToolCall,
);
}
async *getChatStream(
result: GoogleStreamGenerateContentResult,
): GeminiChatStreamResponse {
yield* streamConverter(result.stream, (response) => ({
delta: this.getResponseText(response),
raw: response,
}));
yield* streamConverter(result.stream, (response) => {
const tools = this.getToolsFromResponse(response);
const options: ToolCallLLMMessageOptions = tools?.length
? { toolCall: tools }
: {};
return {
delta: this.getResponseText(response),
raw: response,
options,
};
});
}
getCompletionStream(
@@ -188,10 +216,22 @@ export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
const client = this.session.getGenerativeModel(this.metadata);
const chat = client.startChat({
history: context.history,
tools: params.tools && [
{
functionDeclarations: params.tools.map(
mapBaseToolToGeminiFunctionDeclaration,
),
},
],
});
const { response } = await chat.sendMessage(context.message);
const topCandidate = response.candidates![0];
const tools = this.session.getToolsFromResponse(response);
const options: ToolCallLLMMessageOptions = tools?.length
? { toolCall: tools }
: {};
return {
raw: response,
message: {
@@ -199,6 +239,7 @@ export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
role: GeminiHelper.ROLES_FROM_GEMINI[
topCandidate.content.role as GeminiMessageRole
],
options,
},
};
}
@@ -210,6 +251,13 @@ export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
const client = this.session.getGenerativeModel(this.metadata);
const chat = client.startChat({
history: context.history,
tools: params.tools && [
{
functionDeclarations: params.tools.map(
mapBaseToolToGeminiFunctionDeclaration,
),
},
],
});
const result = await chat.sendMessageStream(context.message);
yield* this.session.getChatStream(result);
@@ -241,13 +289,17 @@ export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
if (stream) {
const result = await client.generateContentStream(
getPartsText(GeminiHelper.messageContentToGeminiParts(prompt)),
getPartsText(
GeminiHelper.messageContentToGeminiParts({ content: prompt }),
),
);
return this.session.getCompletionStream(result);
}
const result = await client.generateContent(
getPartsText(GeminiHelper.messageContentToGeminiParts(prompt)),
getPartsText(
GeminiHelper.messageContentToGeminiParts({ content: prompt }),
),
);
return {
text: this.session.getResponseText(result.response),

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