mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-09 11:25:43 -04:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 83b2f0b0af | |||
| 1a6abb38bc | |||
| 6bc5bddb59 | |||
| e6d6576b2f | |||
| bf25ff6104 | |||
| 32ad0992cf | |||
| af650343d9 | |||
| f6f4ca44bd |
+4
-1
@@ -1,3 +1,5 @@
|
||||
const { join } = require("node:path");
|
||||
|
||||
module.exports = {
|
||||
root: true,
|
||||
extends: [
|
||||
@@ -6,7 +8,7 @@ module.exports = {
|
||||
"plugin:@typescript-eslint/recommended-type-checked-only",
|
||||
],
|
||||
parserOptions: {
|
||||
project: true,
|
||||
project: join(__dirname, "tsconfig.eslint.json"),
|
||||
__tsconfigRootDir: __dirname,
|
||||
},
|
||||
settings: {
|
||||
@@ -23,6 +25,7 @@ module.exports = {
|
||||
ignoreIIFE: true,
|
||||
},
|
||||
],
|
||||
"no-debugger": "error",
|
||||
"@typescript-eslint/await-thenable": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"@typescript-eslint/ban-types": "off",
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# docs
|
||||
|
||||
## 0.0.25
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.0.24
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -21,7 +21,7 @@ export OPENAI_API_KEY=your-api-key
|
||||
Import the required modules:
|
||||
|
||||
```ts
|
||||
import { CorrectnessEvaluator, OpenAI, Settings } from "llamaindex";
|
||||
import { CorrectnessEvaluator, OpenAI, Settings, Response } from "llamaindex";
|
||||
```
|
||||
|
||||
Let's setup gpt-4 for better results:
|
||||
@@ -45,7 +45,7 @@ const evaluator = new CorrectnessEvaluator();
|
||||
|
||||
const result = await evaluator.evaluateResponse({
|
||||
query,
|
||||
response,
|
||||
response: new Response(response),
|
||||
});
|
||||
|
||||
console.log(
|
||||
|
||||
@@ -21,7 +21,13 @@ export OPENAI_API_KEY=your-api-key
|
||||
Import the required modules:
|
||||
|
||||
```ts
|
||||
import { RelevancyEvaluator, OpenAI, Settings } from "llamaindex";
|
||||
import {
|
||||
RelevancyEvaluator,
|
||||
OpenAI,
|
||||
Settings,
|
||||
Document,
|
||||
VectorStoreIndex,
|
||||
} from "llamaindex";
|
||||
```
|
||||
|
||||
Let's setup gpt-4 for better results:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "docs",
|
||||
"version": "0.0.24",
|
||||
"version": "0.0.25",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"docusaurus": "docusaurus",
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { FunctionTool, OpenAI, ToolCallOptions } from "llamaindex";
|
||||
|
||||
(async () => {
|
||||
// The tool call will generate a partial JSON for `gpt-4-turbo`
|
||||
// See thread: https://community.openai.com/t/gpt-4o-doesnt-consistently-respect-json-schema-on-tool-use/751125/7
|
||||
|
||||
const models = ["gpt-4o", "gpt-4-turbo"];
|
||||
for (const model of models) {
|
||||
const validJSON = await callLLM({ model });
|
||||
console.log(
|
||||
`LLM call resulting in large tool input with '${model}': LLM generates ${validJSON ? "valid" : "invalid"} JSON.`,
|
||||
);
|
||||
}
|
||||
})();
|
||||
|
||||
async function callLLM(init: Partial<OpenAI>) {
|
||||
const csvData =
|
||||
"Country,Average Height (cm)\nNetherlands,156\nDenmark,158\nNorway,160";
|
||||
|
||||
const userQuestion = "Describe data in this csv";
|
||||
|
||||
// fake code interpreter tool
|
||||
const interpreterTool = FunctionTool.from(
|
||||
({ code }: { code: string }) => code,
|
||||
{
|
||||
name: "interpreter",
|
||||
description:
|
||||
"Execute python code in a Jupyter notebook cell and return any result, stdout, stderr, display_data, and error.",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
code: {
|
||||
type: "string",
|
||||
description: "The python code to execute in a single cell.",
|
||||
},
|
||||
},
|
||||
required: ["code"],
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const systemPrompt =
|
||||
"You are a Python interpreter.\n- You are given tasks to complete and you run python code to solve them.\n- The python code runs in a Jupyter notebook. Every time you call $(interpreter) tool, the python code is executed in a separate cell. It's okay to make multiple calls to $(interpreter).\n- Display visualizations using matplotlib or any other visualization library directly in the notebook. Shouldn't save the visualizations to a file, just return the base64 encoded data.\n- You can install any pip package (if it exists) if you need to but the usual packages for data analysis are already preinstalled.\n- You can run any python code you want in a secure environment.";
|
||||
|
||||
const llm = new OpenAI(init);
|
||||
const response = await llm.chat({
|
||||
tools: [interpreterTool],
|
||||
messages: [
|
||||
{ role: "system", content: systemPrompt },
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: userQuestion,
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
text: `Use data from following CSV raw contents:\n${csvData}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const options = response.message?.options as ToolCallOptions;
|
||||
const input = options.toolCall[0].input as string;
|
||||
try {
|
||||
JSON.parse(input);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -1,5 +1,15 @@
|
||||
# @llamaindex/autotool-02-next-example
|
||||
|
||||
## 0.1.9
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
- @llamaindex/autotool@0.0.1
|
||||
|
||||
## 0.1.8
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/autotool-02-next-example",
|
||||
"private": true,
|
||||
"version": "0.1.8",
|
||||
"version": "0.1.9",
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
"unplugin": "^1.10.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"llamaindex": "^0.3.16",
|
||||
"llamaindex": "^0.3.17",
|
||||
"openai": "^4",
|
||||
"typescript": "^4"
|
||||
},
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript"
|
||||
},
|
||||
"target": "esnext"
|
||||
},
|
||||
"module": {
|
||||
"type": "commonjs",
|
||||
"ignoreDynamic": true
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript"
|
||||
},
|
||||
"target": "esnext"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,14 @@
|
||||
# @llamaindex/community
|
||||
|
||||
## 0.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.0.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/community",
|
||||
"description": "Community package for LlamaIndexTS",
|
||||
"version": "0.0.2",
|
||||
"version": "0.0.3",
|
||||
"type": "module",
|
||||
"types": "dist/type/index.d.ts",
|
||||
"main": "dist/cjs/index.js",
|
||||
@@ -13,23 +13,24 @@
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/type/index.d.ts",
|
||||
"default": "./dist/cjs/index.js"
|
||||
"default": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"./*": {
|
||||
"./llm/bedrock": {
|
||||
"import": {
|
||||
"types": "./dist/type/*.d.ts",
|
||||
"default": "./dist/*.js"
|
||||
"types": "./dist/type/llm/bedrock.d.ts",
|
||||
"default": "./dist/llm/bedrock/base.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/type/*.d.ts",
|
||||
"default": "./dist/cjs/*.js"
|
||||
"types": "./dist/type/llm/bedrock.d.ts",
|
||||
"default": "./dist/llm/bedrock/base.cjs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"CHANGELOG.md"
|
||||
"CHANGELOG.md",
|
||||
"!**/*.tsbuildinfo"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -37,23 +38,20 @@
|
||||
"directory": "packages/community"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"build": "rm -rf ./dist && pnpm run build:esm && pnpm run build:cjs && pnpm run build:type",
|
||||
"build:esm": "swc src -d dist --strip-leading-paths --config-file ../../.swcrc",
|
||||
"build:cjs": "swc src -d dist/cjs --strip-leading-paths --config-file ../../.cjs.swcrc",
|
||||
"build": "rm -rf ./dist && pnpm run build:code && pnpm run build:type",
|
||||
"build:code": "tsup",
|
||||
"build:type": "tsc -p tsconfig.json",
|
||||
"postbuild": "node -e \"require('fs').writeFileSync('./dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))\"",
|
||||
"dev": "concurrently \"pnpm run build:esm --watch\" \"pnpm run build:cjs --watch\" \"pnpm run build:type --watch\""
|
||||
},
|
||||
"devDependencies": {
|
||||
"@swc/cli": "^0.3.12",
|
||||
"@swc/core": "^1.5.5",
|
||||
"concurrently": "^8.2.2",
|
||||
"pathe": "^1.1.2"
|
||||
"tsup": "^8.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-bedrock-runtime": "^3.582.0",
|
||||
"@types/node": "^20.12.11",
|
||||
"@types/node": "^20.14.2",
|
||||
"llamaindex": "workspace:*"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
"outDir": "./dist/type",
|
||||
"tsBuildInfoFile": "./dist/.tsbuildinfo",
|
||||
"emitDeclarationOnly": true,
|
||||
"module": "node16",
|
||||
"moduleResolution": "node16",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["./src"],
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/script/type",
|
||||
"tsBuildInfoFile": "./dist/script/.tsbuildinfo",
|
||||
"emitDeclarationOnly": true
|
||||
},
|
||||
"include": ["./tsup.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { defineConfig } from "tsup";
|
||||
|
||||
export default defineConfig([
|
||||
{
|
||||
entry: ["src/index.ts", "src/llm/bedrock/base.ts"],
|
||||
format: ["cjs", "esm"],
|
||||
sourcemap: true,
|
||||
},
|
||||
]);
|
||||
@@ -1,5 +1,13 @@
|
||||
# llamaindex
|
||||
|
||||
## 0.3.17
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6bc5bdd: feat: add cache disabling, fast mode, do not unroll columns mode and custom page seperator to LlamaParseReader
|
||||
- bf25ff6: fix: polyfill for cloudflare worker
|
||||
- e6d6576: chore: use `unpdf`
|
||||
|
||||
## 0.3.16
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# @llamaindex/core-e2e
|
||||
|
||||
## 0.0.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- bf25ff6: fix: polyfill for cloudflare worker
|
||||
|
||||
## 0.0.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
# @llamaindex/cloudflare-worker-agent-test
|
||||
|
||||
## 0.0.18
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- bf25ff6: fix: polyfill for cloudflare worker
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.0.17
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/cloudflare-worker-agent-test",
|
||||
"version": "0.0.17",
|
||||
"version": "0.0.18",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
@@ -12,13 +12,13 @@
|
||||
"cf-typegen": "wrangler types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/vitest-pool-workers": "^0.2.6",
|
||||
"@cloudflare/workers-types": "^4.20240502.0",
|
||||
"@cloudflare/vitest-pool-workers": "^0.4.3",
|
||||
"@cloudflare/workers-types": "^4.20240605.0",
|
||||
"@vitest/runner": "1.3.0",
|
||||
"@vitest/snapshot": "1.3.0",
|
||||
"typescript": "^5.4.5",
|
||||
"vitest": "1.3.0",
|
||||
"wrangler": "^3.53.1"
|
||||
"wrangler": "^3.60.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"llamaindex": "workspace:*"
|
||||
|
||||
@@ -10,10 +10,12 @@ export default {
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [],
|
||||
});
|
||||
console.log(1);
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: "Hello? What is the weather today?",
|
||||
});
|
||||
console.log(2);
|
||||
const textEncoder = new TextEncoder();
|
||||
const response = responseStream.pipeThrough<Uint8Array>(
|
||||
// @ts-expect-error: see https://github.com/cloudflare/workerd/issues/2067
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# @llamaindex/next-agent-test
|
||||
|
||||
## 0.1.18
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.1.17
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/next-agent-test",
|
||||
"version": "0.1.17",
|
||||
"version": "0.1.18",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# test-edge-runtime
|
||||
|
||||
## 0.1.17
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.1.16
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/nextjs-edge-runtime-test",
|
||||
"version": "0.1.16",
|
||||
"version": "0.1.17",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# @llamaindex/waku-query-engine-test
|
||||
|
||||
## 0.0.18
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.0.17
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/waku-query-engine-test",
|
||||
"version": "0.0.17",
|
||||
"version": "0.0.18",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/core-e2e",
|
||||
"private": true,
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.7",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"e2e": "node --import tsx --import ./mock-register.js --test ./node/*.e2e.ts",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/core",
|
||||
"version": "0.3.16",
|
||||
"version": "0.3.17",
|
||||
"exports": "./src/index.ts",
|
||||
"imports": {
|
||||
"@llamaindex/env": "jsr:@llamaindex/env@0.1.3"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "llamaindex",
|
||||
"version": "0.3.16",
|
||||
"version": "0.3.17",
|
||||
"expectedMinorVersion": "3",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
@@ -52,18 +52,23 @@
|
||||
"openai": "^4.48.1",
|
||||
"papaparse": "^5.4.1",
|
||||
"pathe": "^1.1.2",
|
||||
"pdf2json": "3.1.3",
|
||||
"pg": "^8.12.0",
|
||||
"pgvector": "^0.1.8",
|
||||
"portkey-ai": "^0.1.16",
|
||||
"rake-modified": "^1.0.8",
|
||||
"string-strip-html": "^13.4.8",
|
||||
"unpdf": "^0.10.1",
|
||||
"wikipedia": "^2.1.2",
|
||||
"wink-nlp": "^2.3.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@notionhq/client": "^2.2.15"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@notionhq/client": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@notionhq/client": "^2.2.15",
|
||||
"@swc/cli": "^0.3.12",
|
||||
@@ -136,7 +141,8 @@
|
||||
"files": [
|
||||
"dist",
|
||||
"CHANGELOG.md",
|
||||
"examples"
|
||||
"examples",
|
||||
"!**/*.tsbuildinfo"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -103,7 +103,6 @@ export class OpenAIAgent extends AgentRunner<OpenAI> {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debugger;
|
||||
const responseChunkStream = new ReadableStream<
|
||||
ChatResponseChunk<ToolCallLLMMessageOptions>
|
||||
>({
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { ImageType } from "../Node.js";
|
||||
import { MultiModalEmbedding } from "./MultiModalEmbedding.js";
|
||||
|
||||
/**
|
||||
* Cloudflare worker doesn't support image embeddings for now
|
||||
*/
|
||||
export class CloudflareWorkerMultiModalEmbedding extends MultiModalEmbedding {
|
||||
getImageEmbedding(images: ImageType): Promise<number[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
getTextEmbedding(text: string): Promise<number[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export async function getImageEmbedModel() {
|
||||
if (globalThis.navigator?.userAgent === "Cloudflare-Workers") {
|
||||
return (await import("../../embeddings/CloudflareWorkerEmbedding.js"))
|
||||
.CloudflareWorkerMultiModalEmbedding;
|
||||
} else {
|
||||
return (await import("../../embeddings/ClipEmbedding.js")).ClipEmbedding;
|
||||
}
|
||||
}
|
||||
@@ -110,22 +110,30 @@ export class LlamaParseReader extends FileReader {
|
||||
apiKey: string;
|
||||
// The base URL of the Llama Parsing API.
|
||||
baseUrl: string = "https://api.cloud.llamaindex.ai/api/parsing";
|
||||
// The maximum timeout in seconds to wait for the parsing to finish.
|
||||
maxTimeout = 2000;
|
||||
// The interval in seconds to check if the parsing is done.
|
||||
checkInterval = 1;
|
||||
// Whether to print the progress of the parsing.
|
||||
verbose = true;
|
||||
// The result type for the parser.
|
||||
resultType: ResultType = "text";
|
||||
// The interval in seconds to check if the parsing is done.
|
||||
checkInterval = 1;
|
||||
// The maximum timeout in seconds to wait for the parsing to finish.
|
||||
maxTimeout = 2000;
|
||||
// Whether to print the progress of the parsing.
|
||||
verbose = true;
|
||||
// The language of the text to parse.
|
||||
language: Language = "en";
|
||||
// The parsing instruction for the parser.
|
||||
parsingInstruction: string = "";
|
||||
// If set to true, the parser will ignore diagonal text (when the text rotation in degrees modulo 90 is not 0).
|
||||
skipDiagonalText: boolean = false;
|
||||
// If set to true, the cache will be ignored and the document re-processes. All document are kept in cache for 48hours after the job was completed to avoid processing the same document twice.
|
||||
invalidateCache: boolean = false;
|
||||
// The parsing instruction for the parser. Backend default is an empty string.
|
||||
parsingInstruction?: string;
|
||||
// Wether to ignore diagonal text (when the text rotation in degrees is not 0, 90, 180 or 270, so not a horizontal or vertical text). Backend default is false.
|
||||
skipDiagonalText?: boolean;
|
||||
// Wheter to ignore the cache and re-process the document. All documents are kept in cache for 48hours after the job was completed to avoid processing the same document twice. Backend default is false.
|
||||
invalidateCache?: boolean;
|
||||
// Wether the document should not be cached in the first place. Backend default is false.
|
||||
doNotCache?: boolean;
|
||||
// Wether to use a faster mode to extract text from documents. This mode will skip OCR of images, and table/heading reconstruction. Note: Non-compatible with gpt4oMode. Backend default is false.
|
||||
fastMode?: boolean;
|
||||
// Wether to keep column in the text according to document layout. Reduce reconstruction accuracy, and LLM's/embedings performances in most cases.
|
||||
doNotUnrollColumns?: boolean;
|
||||
// The page separator to use to split the text. Default is None, which means the parser will use the default separator '\\n---\\n'.
|
||||
pageSeperator?: string;
|
||||
// Whether to use gpt-4o to extract text from documents.
|
||||
gpt4oMode: boolean = false;
|
||||
// The API key for the GPT-4o API. Optional, lowers the cost of parsing. Can be set as an env variable: LLAMA_CLOUD_GPT4O_API_KEY.
|
||||
@@ -162,14 +170,26 @@ export class LlamaParseReader extends FileReader {
|
||||
|
||||
const body = new FormData();
|
||||
body.set("file", new Blob([data], { type: mimeType }));
|
||||
body.append("language", this.language);
|
||||
body.append("parsing_instruction", this.parsingInstruction);
|
||||
body.append("skip_diagonal_text", this.skipDiagonalText.toString());
|
||||
body.append("invalidate_cache", this.invalidateCache.toString());
|
||||
body.append("gpt4o_mode", this.gpt4oMode.toString());
|
||||
if (this.gpt4oMode && this.gpt4oApiKey) {
|
||||
body.append("gpt4o_api_key", this.gpt4oApiKey);
|
||||
}
|
||||
|
||||
const LlamaParseBodyParams = {
|
||||
language: this.language,
|
||||
parsing_instruction: this.parsingInstruction,
|
||||
skip_diagonal_text: this.skipDiagonalText?.toString(),
|
||||
invalidate_cache: this.invalidateCache?.toString(),
|
||||
do_not_cache: this.doNotCache?.toString(),
|
||||
fast_mode: this.fastMode?.toString(),
|
||||
do_not_unroll_columns: this.doNotUnrollColumns?.toString(),
|
||||
page_seperator: this.pageSeperator,
|
||||
gpt4o_mode: this.gpt4oMode?.toString(),
|
||||
gpt4o_api_key: this.gpt4oApiKey,
|
||||
};
|
||||
|
||||
// Appends body with any defined LlamaParseBodyParams
|
||||
Object.entries(LlamaParseBodyParams).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
body.append(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
const headers = {
|
||||
Authorization: `Bearer ${this.apiKey}`,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { fs } from "@llamaindex/env";
|
||||
import { Document } from "../Node.js";
|
||||
import { FileReader } from "./type.js";
|
||||
|
||||
@@ -5,29 +6,30 @@ import { FileReader } from "./type.js";
|
||||
* Read the text of a PDF
|
||||
*/
|
||||
export class PDFReader extends FileReader {
|
||||
async loadDataAsContent(fileContent: Buffer): Promise<Document[]> {
|
||||
const pages = await readPDF(fileContent);
|
||||
return pages.map((text, page) => {
|
||||
async loadData(file: string): Promise<Document[]> {
|
||||
const content = await fs.readFile(file);
|
||||
return this.loadDataAsContent(new Uint8Array(content.buffer));
|
||||
}
|
||||
|
||||
async loadDataAsContent(content: Uint8Array): Promise<Document[]> {
|
||||
const { totalPages, text } = await readPDF(content);
|
||||
return text.map((text, page) => {
|
||||
const metadata = {
|
||||
page_number: page + 1,
|
||||
total_pages: totalPages,
|
||||
};
|
||||
return new Document({ text, metadata });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function readPDF(data: Buffer): Promise<string[]> {
|
||||
const parser = await import("pdf2json").then(
|
||||
({ default: Pdfparser }) => new Pdfparser(null, true),
|
||||
);
|
||||
const text = await new Promise<string>((resolve, reject) => {
|
||||
parser.on("pdfParser_dataError", (error) => {
|
||||
reject(error);
|
||||
});
|
||||
parser.on("pdfParser_dataReady", () => {
|
||||
resolve((parser as any).getRawTextContent() as string);
|
||||
});
|
||||
parser.parseBuffer(data);
|
||||
});
|
||||
return text.split(/----------------Page \(\d+\) Break----------------/g);
|
||||
async function readPDF(data: Uint8Array): Promise<{
|
||||
totalPages: number;
|
||||
text: string[];
|
||||
}> {
|
||||
const { extractText } = await import("unpdf");
|
||||
return (await extractText(data)) as {
|
||||
totalPages: number;
|
||||
text: string[];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { path } from "@llamaindex/env";
|
||||
import { ModalityType, ObjectType } from "../Node.js";
|
||||
import { ClipEmbedding } from "../embeddings/ClipEmbedding.js";
|
||||
import { getImageEmbedModel } from "../internal/settings/image-embed-model.js";
|
||||
import {
|
||||
DEFAULT_IMAGE_VECTOR_NAMESPACE,
|
||||
DEFAULT_NAMESPACE,
|
||||
@@ -44,7 +44,7 @@ export async function storageContextFromDefaults({
|
||||
}
|
||||
if (storeImages && !(ModalityType.IMAGE in vectorStores)) {
|
||||
vectorStores[ModalityType.IMAGE] = new SimpleVectorStore({
|
||||
embedModel: new ClipEmbedding(),
|
||||
embedModel: new (await getImageEmbedModel())(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -60,7 +60,7 @@ export async function storageContextFromDefaults({
|
||||
if (storeImages && !(ObjectType.IMAGE in vectorStores)) {
|
||||
vectorStores[ModalityType.IMAGE] = await SimpleVectorStore.fromPersistDir(
|
||||
path.join(persistDir, DEFAULT_IMAGE_VECTOR_NAMESPACE),
|
||||
new ClipEmbedding(),
|
||||
new (await getImageEmbedModel())(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
# @llamaindex/core-test
|
||||
|
||||
## 0.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- e6d6576: chore: use `unpdf`
|
||||
|
||||
## 0.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/core-test",
|
||||
"private": true,
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.4",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "vitest run"
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
Sample PDF
|
||||
|
||||
This is a simple PDF file. Fun fun fun.
|
||||
|
||||
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus facilisis odio sed mi.
|
||||
|
||||
Curabitur suscipit. Nullam vel nisi. Etiam semper ipsum ut lectus. Proin aliquam, erat eget
|
||||
|
||||
pharetra commodo, eros mi condimentum quam, sed commodo justo quam ut velit.
|
||||
|
||||
Integer
|
||||
|
||||
a
|
||||
|
||||
erat.
|
||||
|
||||
Cras
|
||||
|
||||
laoreet
|
||||
|
||||
ligula
|
||||
|
||||
cursus
|
||||
|
||||
enim.
|
||||
|
||||
Aenean
|
||||
|
||||
scelerisque
|
||||
|
||||
velit
|
||||
|
||||
et
|
||||
|
||||
tellus.
|
||||
|
||||
Vestibulum dictum aliquet sem. Nulla facilisi. Vestibulum accumsan ante vitae elit. Nulla
|
||||
|
||||
erat dolor, blandit in, rutrum quis, semper pulvinar, enim. Nullam varius congue risus.
|
||||
|
||||
Vivamus sollicitudin, metus ut interdum eleifend, nisi tellus pellentesque elit, tristique
|
||||
|
||||
accumsan eros quam et risus. Suspendisse libero odio, mattis sit amet, aliquet eget,
|
||||
|
||||
hendrerit vel, nulla. Sed vitae augue. Aliquam erat volutpat. Aliquam feugiat vulputate nisl.
|
||||
|
||||
Suspendisse quis nulla pretium ante pretium mollis. Proin velit ligula, sagittis at, egestas a,
|
||||
|
||||
pulvinar quis, nisl.
|
||||
|
||||
Pellentesque sit amet lectus. Praesent pulvinar, nunc quis iaculis sagittis, justo quam
|
||||
|
||||
lobortis tortor, sed vestibulum dui metus venenatis est. Nunc cursus ligula. Nulla facilisi.
|
||||
|
||||
Phasellus ullamcorper consectetuer ante. Duis tincidunt, urna id condimentum luctus, nibh
|
||||
|
||||
ante vulputate sapien, id sagittis massa orci ut enim. Pellentesque vestibulum convallis
|
||||
|
||||
sem. Nulla consequat quam ut nisl. Nullam est. Curabitur tincidunt dapibus lorem. Proin
|
||||
|
||||
velit turpis, scelerisque sit amet, iaculis nec, rhoncus ac, ipsum. Phasellus lorem arcu,
|
||||
|
||||
feugiat eu, gravida eu, consequat molestie, ipsum. Nullam vel est ut ipsum volutpat
|
||||
|
||||
feugiat. Aenean pellentesque.
|
||||
|
||||
In mauris. Pellentesque dui nisi, iaculis eu, rhoncus in, venenatis ac, ante. Ut odio justo,
|
||||
|
||||
scelerisque vel, facilisis non, commodo a, pede. Cras nec massa sit amet tortor volutpat
|
||||
|
||||
varius. Donec lacinia, neque a luctus aliquet, pede massa imperdiet ante, at varius lorem
|
||||
|
||||
pede sed sapien. Fusce erat nibh, aliquet in, eleifend eget, commodo eget, erat. Fusce
|
||||
|
||||
consectetuer. Cras risus tortor, porttitor nec, tristique sed, convallis semper, eros. Fusce
|
||||
|
||||
vulputate ipsum a mauris. Phasellus mollis. Curabitur sed urna. Aliquam nec sapien non
|
||||
|
||||
nibh pulvinar convallis. Vivamus facilisis augue quis quam. Proin cursus aliquet metus.
|
||||
|
||||
Suspendisse lacinia. Nulla at tellus ac turpis eleifend scelerisque. Maecenas a pede vitae
|
||||
|
||||
enim commodo interdum. Donec odio. Sed sollicitudin dui vitae justo.
|
||||
|
||||
Morbi elit nunc, facilisis a, mollis a, molestie at, lectus. Suspendisse eget mauris eu tellus
|
||||
|
||||
molestie cursus. Duis ut magna at justo dignissim condimentum. Cum sociis natoque
|
||||
|
||||
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus varius. Ut sit
|
||||
|
||||
amet diam suscipit mauris ornare aliquam. Sed varius. Duis arcu. Etiam tristique massa
|
||||
|
||||
eget dui. Phasellus congue. Aenean est erat, tincidunt eget, venenatis quis, commodo at,
|
||||
|
||||
quam.
|
||||
@@ -0,0 +1,27 @@
|
||||
import { PDFReader } from "llamaindex";
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
describe("pdf reader", () => {
|
||||
const reader = new PDFReader();
|
||||
test("basic.pdf", async () => {
|
||||
const documents = await reader.loadData("../../../examples/data/basic.pdf");
|
||||
expect(documents.length).toBe(1);
|
||||
expect(documents[0].metadata).toEqual({
|
||||
page_number: 1,
|
||||
total_pages: 1,
|
||||
});
|
||||
await expect(documents[0].text).toMatchFileSnapshot(
|
||||
"./.snap/basic.pdf.snap",
|
||||
);
|
||||
});
|
||||
test("brk-2022.pdf", async () => {
|
||||
const documents = await reader.loadData(
|
||||
"../../../examples/data/brk-2022.pdf",
|
||||
);
|
||||
expect(documents.length).toBe(140);
|
||||
});
|
||||
test("manga.pdf", async () => {
|
||||
const documents = await reader.loadData("../../../examples/data/manga.pdf");
|
||||
expect(documents.length).toBe(4);
|
||||
});
|
||||
});
|
||||
Vendored
+2
-1
@@ -48,7 +48,8 @@
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"CHANGELOG.md"
|
||||
"CHANGELOG.md",
|
||||
"!**/*.tsbuildinfo"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# @llamaindex/experimental
|
||||
|
||||
## 0.0.34
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6bc5bdd]
|
||||
- Updated dependencies [bf25ff6]
|
||||
- Updated dependencies [e6d6576]
|
||||
- llamaindex@0.3.17
|
||||
|
||||
## 0.0.33
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/experimental",
|
||||
"description": "Experimental package for LlamaIndexTS",
|
||||
"version": "0.0.33",
|
||||
"version": "0.0.34",
|
||||
"type": "module",
|
||||
"types": "dist/type/index.d.ts",
|
||||
"main": "dist/cjs/index.js",
|
||||
@@ -37,7 +37,8 @@
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"CHANGELOG.md"
|
||||
"CHANGELOG.md",
|
||||
"!**/*.tsbuildinfo"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -42,7 +42,8 @@
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"CHANGELOG.md"
|
||||
"CHANGELOG.md",
|
||||
"!**/*.tsbuildinfo"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
Generated
+1349
-923
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"allowJs": true
|
||||
},
|
||||
"include": ["."],
|
||||
"exclude": ["**/node_modules", "**/dist", "**/lib"]
|
||||
}
|
||||
@@ -20,6 +20,12 @@
|
||||
{
|
||||
"path": "./apps/docs/tsconfig.json"
|
||||
},
|
||||
{
|
||||
"path": "./packages/community/tsconfig.json"
|
||||
},
|
||||
{
|
||||
"path": "./packages/community/tsconfig.script.json"
|
||||
},
|
||||
{
|
||||
"path": "./packages/core/tsconfig.json"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user