From 26e6251399cdfa5199e0a157f70e6b9b99774a55 Mon Sep 17 00:00:00 2001 From: thucpn Date: Fri, 29 Nov 2024 16:20:17 +0700 Subject: [PATCH] refactor: @llamaindex/vercel --- examples/package.json | 4 +- examples/vercel/llamacloud.ts | 4 +- examples/vercel/vector-store.ts | 4 +- packages/providers/vercel/package.json | 50 +++++++++++++++++++ packages/providers/vercel/src/index.ts | 1 + .../providers/vercel/src}/tool.ts | 10 ++-- packages/providers/vercel/tsconfig.json | 19 +++++++ pnpm-lock.yaml | 22 ++++++-- tsconfig.json | 3 ++ 9 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 packages/providers/vercel/package.json create mode 100644 packages/providers/vercel/src/index.ts rename {examples/vercel => packages/providers/vercel/src}/tool.ts (74%) create mode 100644 packages/providers/vercel/tsconfig.json diff --git a/examples/package.json b/examples/package.json index 799a6a9e1a..6b11abdc71 100644 --- a/examples/package.json +++ b/examples/package.json @@ -11,6 +11,7 @@ "@llamaindex/core": "^0.4.10", "@llamaindex/readers": "^1.0.11", "@llamaindex/workflow": "^0.0.6", + "@llamaindex/vercel": "^0.0.1", "@notionhq/client": "^2.2.15", "@pinecone-database/pinecone": "^4.0.0", "@vercel/postgres": "^0.10.0", @@ -23,8 +24,7 @@ "llamaindex": "^0.8.13", "mongodb": "^6.7.0", "pathe": "^1.1.2", - "postgres": "^3.4.4", - "zod": "^3.23.8" + "postgres": "^3.4.4" }, "devDependencies": { "@types/node": "^22.9.0", diff --git a/examples/vercel/llamacloud.ts b/examples/vercel/llamacloud.ts index 485801f6c1..5a0fcd7654 100644 --- a/examples/vercel/llamacloud.ts +++ b/examples/vercel/llamacloud.ts @@ -1,8 +1,8 @@ import { openai } from "@ai-sdk/openai"; +import { llamaindex } from "@llamaindex/vercel"; import { streamText } from "ai"; import { Document, LlamaCloudIndex } from "llamaindex"; import fs from "node:fs/promises"; -import { llamaindex } from "./tool"; async function main() { const path = "node_modules/llamaindex/examples/abramov.txt"; @@ -17,7 +17,7 @@ async function main() { }); const queryTool = await llamaindex({ index, - description: "Search through Abramov's essay", // optional description + description: "Search through the documents", // optional description }); console.log("Successfully created index and queryTool"); diff --git a/examples/vercel/vector-store.ts b/examples/vercel/vector-store.ts index c626dd8e65..ce2d1f1322 100644 --- a/examples/vercel/vector-store.ts +++ b/examples/vercel/vector-store.ts @@ -1,8 +1,8 @@ import { openai } from "@ai-sdk/openai"; +import { llamaindex } from "@llamaindex/vercel"; import { streamText } from "ai"; import { Document, VectorStoreIndex } from "llamaindex"; import fs from "node:fs/promises"; -import { llamaindex } from "./tool"; async function main() { const path = "node_modules/llamaindex/examples/abramov.txt"; @@ -12,7 +12,7 @@ async function main() { const index = await VectorStoreIndex.fromDocuments([document]); const queryTool = await llamaindex({ index, - description: "Search through Abramov's essay", // optional description + description: "Search through the documents", // optional description }); console.log("Successfully created index and queryTool"); diff --git a/packages/providers/vercel/package.json b/packages/providers/vercel/package.json new file mode 100644 index 0000000000..484b16b632 --- /dev/null +++ b/packages/providers/vercel/package.json @@ -0,0 +1,50 @@ +{ + "name": "@llamaindex/vercel", + "description": "Vercel Adapter for LlamaIndex", + "version": "0.0.1", + "type": "module", + "main": "./dist/index.cjs", + "module": "./dist/index.js", + "exports": { + ".": { + "edge-light": { + "types": "./dist/index.edge-light.d.ts", + "default": "./dist/index.edge-light.js" + }, + "workerd": { + "types": "./dist/index.edge-light.d.ts", + "default": "./dist/index.edge-light.js" + }, + "require": { + "types": "./dist/index.d.cts", + "default": "./dist/index.cjs" + }, + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + } + }, + "files": [ + "dist" + ], + "repository": { + "type": "git", + "url": "https://github.com/run-llama/LlamaIndexTS.git", + "directory": "packages/providers/vercel" + }, + "scripts": { + "build": "bunchee", + "dev": "bunchee --watch" + }, + "devDependencies": { + "bunchee": "5.6.1" + }, + "dependencies": { + "@llamaindex/core": "workspace:*", + "zod": "^3.23.8" + }, + "peerDependencies": { + "ai": "^4.0.0" + } +} diff --git a/packages/providers/vercel/src/index.ts b/packages/providers/vercel/src/index.ts new file mode 100644 index 0000000000..5484738837 --- /dev/null +++ b/packages/providers/vercel/src/index.ts @@ -0,0 +1 @@ +export { llamaindex } from "./tool"; diff --git a/examples/vercel/tool.ts b/packages/providers/vercel/src/tool.ts similarity index 74% rename from examples/vercel/tool.ts rename to packages/providers/vercel/src/tool.ts index e5fef5d339..a0bc960de2 100644 --- a/examples/vercel/tool.ts +++ b/packages/providers/vercel/src/tool.ts @@ -1,12 +1,16 @@ -import { CoreTool } from "ai"; -import { LlamaCloudIndex, VectorStoreIndex } from "llamaindex"; +import type { BaseQueryEngine } from "@llamaindex/core/query-engine"; +import type { CoreTool } from "ai"; import { z } from "zod"; +interface DatasourceIndex { + asQueryEngine: () => BaseQueryEngine; +} + export async function llamaindex({ index, description, }: { - index: VectorStoreIndex | LlamaCloudIndex; + index: DatasourceIndex; description?: string; }): Promise { const queryEngine = index.asQueryEngine(); diff --git a/packages/providers/vercel/tsconfig.json b/packages/providers/vercel/tsconfig.json new file mode 100644 index 0000000000..3fad47fb71 --- /dev/null +++ b/packages/providers/vercel/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "bundler", + "outDir": "./lib", + "tsBuildInfoFile": "./lib/.tsbuildinfo" + }, + "include": ["./src"], + "references": [ + { + "path": "../../core/tsconfig.json" + }, + { + "path": "../../env/tsconfig.json" + } + ] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 504f4dfa2c..0a3cfc25fc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -608,6 +608,9 @@ importers: '@llamaindex/readers': specifier: ^1.0.11 version: link:../packages/readers + '@llamaindex/vercel': + specifier: ^0.0.1 + version: link:../packages/providers/vercel '@llamaindex/workflow': specifier: ^0.0.6 version: link:../packages/workflow @@ -650,9 +653,6 @@ importers: postgres: specifier: ^3.4.4 version: 3.4.5 - zod: - specifier: ^3.23.8 - version: 3.23.8 devDependencies: '@types/node': specifier: ^22.9.0 @@ -1375,6 +1375,22 @@ importers: specifier: 5.6.1 version: 5.6.1(typescript@5.6.3) + packages/providers/vercel: + dependencies: + '@llamaindex/core': + specifier: workspace:* + version: link:../../core + ai: + specifier: ^4.0.0 + version: 4.0.6(react@19.0.0-rc-bf7e210c-20241017)(zod@3.23.8) + zod: + specifier: ^3.23.8 + version: 3.23.8 + devDependencies: + bunchee: + specifier: 5.6.1 + version: 5.6.1(typescript@5.6.3) + packages/providers/vllm: dependencies: '@llamaindex/openai': diff --git a/tsconfig.json b/tsconfig.json index f8ea549d3c..ee6c508cab 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -56,6 +56,9 @@ { "path": "./packages/providers/vllm/tsconfig.json" }, + { + "path": "./packages/providers/vercel/tsconfig.json" + }, { "path": "./packages/cloud/tsconfig.json" },