Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experimental package + json query engine #613

Merged
merged 12 commits into from
Mar 7, 2024
1 change: 1 addition & 0 deletions packages/core/src/agent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
ChatEngineAgentParams,
StreamingAgentChatResponse,
} from "../engines/chat/index.js";

import type { QueryEngineParamsNonStreaming } from "../types.js";

export interface AgentWorker {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export * from "./selectors/index.js";
export * from "./storage/index.js";
export * from "./synthesizers/index.js";
export * from "./tools/index.js";
export * from "./types.js";
12 changes: 12 additions & 0 deletions packages/experimental/.cjs.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "esnext"
},
"module": {
"type": "commonjs",
"ignoreDynamic": true
}
}
8 changes: 8 additions & 0 deletions packages/experimental/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "esnext"
}
}
1 change: 1 addition & 0 deletions packages/experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @llamaindex/experimental
119 changes: 119 additions & 0 deletions packages/experimental/examples/jsonQueryEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { JSONQueryEngine } from "../dist/index.js";

import { OpenAI, serviceContextFromDefaults } from "llamaindex";

const jsonValue = {
blogPosts: [
{
id: 1,
title: "First blog post",
content: "This is my first blog post",
},
{
id: 2,
title: "Second blog post",
content: "This is my second blog post",
},
],
comments: [
{
id: 1,
content: "Nice post!",
username: "jerry",
blogPostId: 1,
},
{
id: 2,
content: "Interesting thoughts",
username: "simon",
blogPostId: 2,
},
{
id: 3,
content: "Loved reading this!",
username: "simon",
blogPostId: 2,
},
],
};

const jsonSchema = {
type: "object",
properties: {
blogPosts: {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "number",
},
title: {
type: "string",
},
content: {
type: "string",
},
},
required: ["id", "title", "content"],
},
},
comments: {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "number",
},
content: {
type: "string",
},
username: {
type: "string",
},
blogPostId: {
type: "number",
},
},
required: ["id", "content", "username", "blogPostId"],
},
},
},
required: ["blogPosts", "comments"],
};

async function main() {
const llm = new OpenAI({ model: "gpt-4" });

const serviceContext = serviceContextFromDefaults({
llm,
});

const jsonQueryEngine = new JSONQueryEngine({
jsonValue,
jsonSchema,
serviceContext,
});

const rawQueryEngine = new JSONQueryEngine({
jsonValue,
jsonSchema,
serviceContext,
synthesizeResponse: false,
});

const response = await jsonQueryEngine.query({
query: "give to me the comment with id 1",
});

const rawResponse = await rawQueryEngine.query({
query: "give me all simon comments",
});

console.log({ response });

console.log({ rawResponse });
}

main();
17 changes: 17 additions & 0 deletions packages/experimental/examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "examples",
"private": true,
"version": "0.0.3",
"dependencies": {
"@llamaindex/experimental": "latest",
"llamaindex": "workspace:*"
},
"devDependencies": {
"@types/node": "^18.19.10",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"scripts": {
"lint": "eslint ."
}
}
8 changes: 8 additions & 0 deletions packages/experimental/jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@llamaindex/env",
"version": "0.0.5",
"exports": {
".": "./src/index.ts",
"./type": "./src/type.ts"
}
}
69 changes: 69 additions & 0 deletions packages/experimental/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.1",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
"exports": {
".": {
"workerd": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.polyfill.js"
},
"edge-light": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.polyfill.js"
},
"import": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/type/index.d.ts",
"default": "./dist/cjs/index.js"
}
},
"./*": {
"import": {
"types": "./dist/type/*.d.ts",
"default": "./dist/*.js"
},
"require": {
"types": "./dist/type/*.d.ts",
"default": "./dist/cjs/*.js"
}
}
},
"files": [
"dist",
"CHANGELOG.md"
],
"repository": {
"type": "git",
"url": "https://github.com/himself65/LlamaIndexTS.git",
"directory": "packages/env"
},
"scripts": {
"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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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": {
"@aws-crypto/sha256-js": "^5.2.0",
"@swc/cli": "^0.3.9",
"@swc/core": "^1.4.2",
"concurrently": "^8.2.2",
"pathe": "^1.1.2"
},
"dependencies": {
"@types/lodash": "^4.14.202",
"@types/node": "^20.11.20",
"jsonpath": "^1.1.1",
"llamaindex": "workspace:*",
"lodash": "^4.17.21"
}
}
Loading
Loading