Compare commits

...

12 Commits

Author SHA1 Message Date
yisding 3bab23172a changeset 2023-11-23 10:53:30 -08:00
yisding 18c132d494 Merge pull request #228 from run-llama/ms/create-llama-fixes
Several fixes for improving compatibility with Next.JS
2023-11-23 10:50:13 -08:00
Marcus Schiesser d072353e08 fix: copy pdf-parse test doc for npm build 2023-11-23 20:58:43 +07:00
Marcus Schiesser c8bbc101cc feat: remove AssemblyAIReader as it's not working with Next.JS 2023-11-23 18:23:24 +07:00
Marcus Schiesser b93f748998 fix: don't resolve mongodb for next.js 2023-11-23 18:20:15 +07:00
Marcus Schiesser ecb100448a fix: remove forceConsistentCasingInFileNames warning 2023-11-23 18:19:29 +07:00
Marcus Schiesser c749c856b5 fix: add missing clsx package 2023-11-23 18:18:35 +07:00
Marcus Schiesser 0baf278972 fix: transformers.js not working with nextjs 2023-11-23 16:46:18 +07:00
Marcus Schiesser ae7780266a fix: curl test for express (streaming) 2023-11-23 15:56:36 +07:00
Marcus Schiesser 587960aebe fix: use dotenv for npm run generate, use .env for NextJS, fix package versions for pnpm 2023-11-23 15:55:47 +07:00
Marcus Schiesser 4e1b6784f7 fix: pdfparse not working with in ESM version 2023-11-23 14:22:29 +07:00
yisding 8b381f2640 LITS 0.0.36 2023-11-21 22:33:14 -08:00
25 changed files with 154 additions and 329 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---
Fixed errors (#225 and #226) Thanks @marcusschiesser
+13
View File
@@ -0,0 +1,13 @@
# mongodb-llamaindexts
## 0.0.2
### Patch Changes
- Updated dependencies
- Updated dependencies
- Updated dependencies
- Updated dependencies
- Updated dependencies
- Updated dependencies
- llamaindex@0.0.36
+1 -1
View File
@@ -1,5 +1,5 @@
{
"version": "0.0.1",
"version": "0.0.2",
"private": true,
"name": "mongodb-llamaindexts",
"dependencies": {
+12
View File
@@ -1,5 +1,17 @@
# simple
## 0.0.34
### Patch Changes
- Updated dependencies
- Updated dependencies
- Updated dependencies
- Updated dependencies
- Updated dependencies
- Updated dependencies
- llamaindex@0.0.36
## 0.0.33
### Patch Changes
-60
View File
@@ -1,60 +0,0 @@
import { program } from "commander";
import { AudioTranscriptReader, CreateTranscriptParameters } from "llamaindex";
import { stdin as input, stdout as output } from "node:process";
// readline/promises is still experimental so not in @types/node yet
// @ts-ignore
import readline from "node:readline/promises";
import { VectorStoreIndex } from "../../packages/core/src/indices";
program
.option(
"-a, --audio-url [string]",
"URL or path of the audio file to transcribe",
)
.option("-i, --transcript-id [string]", "ID of the AssemblyAI transcript")
.action(async (options) => {
if (!process.env.ASSEMBLYAI_API_KEY) {
console.log("No ASSEMBLYAI_API_KEY found in environment variables.");
return;
}
const reader = new AudioTranscriptReader();
let params: CreateTranscriptParameters | string;
console.log(options);
if (options.audioUrl) {
params = {
audio_url: options.audioUrl,
};
} else if (options.transcriptId) {
params = options.transcriptId;
} else {
console.log(
"You must provide either an --audio-url or a --transcript-id",
);
return;
}
const documents = await reader.loadData(params);
console.log(documents);
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments(documents);
// Create query engine
const queryEngine = index.asQueryEngine();
const rl = readline.createInterface({ input, output });
while (true) {
const query = await rl.question("Ask a question: ");
if (!query) {
break;
}
const response = await queryEngine.query(query);
console.log(response.toString());
}
});
program.parse();
+1 -1
View File
@@ -1,5 +1,5 @@
{
"version": "0.0.33",
"version": "0.0.34",
"private": true,
"name": "simple",
"dependencies": {
-60
View File
@@ -1,60 +0,0 @@
import { program } from "commander";
import { AudioTranscriptReader, CreateTranscriptParameters } from "llamaindex";
import { stdin as input, stdout as output } from "node:process";
// readline/promises is still experimental so not in @types/node yet
// @ts-ignore
import readline from "node:readline/promises";
import { VectorStoreIndex } from "../../packages/core/src/indices";
program
.option(
"-a, --audio-url [string]",
"URL or path of the audio file to transcribe",
)
.option("-i, --transcript-id [string]", "ID of the AssemblyAI transcript")
.action(async (options) => {
if (!process.env.ASSEMBLYAI_API_KEY) {
console.log("No ASSEMBLYAI_API_KEY found in environment variables.");
return;
}
const reader = new AudioTranscriptReader();
let params: CreateTranscriptParameters | string;
console.log(options);
if (options.audioUrl) {
params = {
audio_url: options.audioUrl,
};
} else if (options.transcriptId) {
params = options.transcriptId;
} else {
console.log(
"You must provide either an --audio-url or a --transcript-id",
);
return;
}
const documents = await reader.loadData(params);
console.log(documents);
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments(documents);
// Create query engine
const queryEngine = index.asQueryEngine();
const rl = readline.createInterface({ input, output });
while (true) {
const query = await rl.question("Ask a question: ");
if (!query) {
break;
}
const response = await queryEngine.query(query);
console.log(response.toString());
}
});
program.parse();
+11
View File
@@ -1,5 +1,16 @@
# llamaindex
## 0.0.36
### Patch Changes
- Support for Claude 2.1
- Add AssemblyAI integration (thanks @Swimburger)
- Use cryptoJS (thanks @marcusschiesser)
- Add PGVectorStore (thanks @mtutty)
- Add CLIP embeddings (thanks @marcusschiesser)
- Add MongoDB support (thanks @marcusschiesser)
## 0.0.35
### Patch Changes
+1 -9
View File
@@ -1,12 +1,11 @@
{
"name": "llamaindex",
"version": "0.0.35",
"version": "0.0.36",
"license": "MIT",
"dependencies": {
"@anthropic-ai/sdk": "^0.9.1",
"@notionhq/client": "^2.2.13",
"@xenova/transformers": "^2.8.0",
"assemblyai": "^3.0.1",
"crypto-js": "^4.2.0",
"js-tiktoken": "^1.0.8",
"lodash": "^4.17.21",
@@ -50,12 +49,5 @@
"test": "jest",
"build": "tsup src/index.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts --format esm,cjs --dts --watch"
},
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
}
}
-1
View File
@@ -19,7 +19,6 @@ export * from "./constants";
export * from "./embeddings";
export * from "./indices";
export * from "./llm/LLM";
export * from "./readers/AssemblyAI";
export * from "./readers/CSVReader";
export * from "./readers/HTMLReader";
export * from "./readers/MarkdownReader";
-148
View File
@@ -1,148 +0,0 @@
import {
AssemblyAI,
BaseServiceParams,
CreateTranscriptParameters,
SubtitleFormat,
TranscriptParagraph,
TranscriptSentence,
} from "assemblyai";
import { Document } from "../Node";
import { BaseReader } from "./base";
type AssemblyAIOptions = Partial<BaseServiceParams>;
/**
* Base class for AssemblyAI Readers.
*/
abstract class AssemblyAIReader implements BaseReader {
protected client: AssemblyAI;
/**
* Creates a new AssemblyAI Reader.
* @param assemblyAIOptions The options to configure the AssemblyAI Reader.
* Configure the `assemblyAIOptions.apiKey` with your AssemblyAI API key, or configure it as the `ASSEMBLYAI_API_KEY` environment variable.
*/
constructor(assemblyAIOptions?: AssemblyAIOptions) {
let options = assemblyAIOptions;
if (!options) {
options = {};
}
if (!options.apiKey) {
options.apiKey = process.env.ASSEMBLYAI_API_KEY;
}
if (!options.apiKey) {
throw new Error("No AssemblyAI API key provided. Pass an `apiKey` option, or configure the `ASSEMBLYAI_API_KEY` environment variable.");
}
this.client = new AssemblyAI(options as BaseServiceParams);
}
abstract loadData(...args: any[]): Promise<Document[]>;
protected async getOrCreateTranscript(params: CreateTranscriptParameters | string) {
if (typeof params === "string") {
return await this.client.transcripts.get(params);
}
else {
return await this.client.transcripts.create(params);
}
}
protected async getTranscriptId(params: CreateTranscriptParameters | string) {
if (typeof params === "string") {
return params;
}
else {
return (await this.client.transcripts.create(params)).id;
}
}
}
/**
* Creates and reads the transcript as a document using AssemblyAI.
*/
class AudioTranscriptReader extends AssemblyAIReader {
/**
* Creates or gets a transcript and loads the transcript as a document using AssemblyAI.
* @param params The parameters to create or get the transcript.
* @returns A promise that resolves to a single document containing the transcript text.
*/
async loadData(params: CreateTranscriptParameters | string): Promise<Document[]> {
const transcript = await this.getOrCreateTranscript(params);
return [
new Document({ text: transcript.text || undefined }),
];
}
}
/**
* Creates a transcript and returns a document for each paragraph.
*/
class AudioTranscriptParagraphsReader extends AssemblyAIReader {
/**
* Creates or gets a transcript, and returns a document for each paragraph.
* @param params The parameters to create or get the transcript.
* @returns A promise that resolves to an array of documents, each containing a paragraph of the transcript.
*/
async loadData(params: CreateTranscriptParameters | string): Promise<Document[]> {
let transcriptId = await this.getTranscriptId(params);
const paragraphsResponse = await this.client.transcripts.paragraphs(
transcriptId
);
return paragraphsResponse.paragraphs.map((p: TranscriptParagraph) =>
new Document({ text: p.text }),
);
}
}
/**
* Creates a transcript and returns a document for each sentence.
*/
class AudioTranscriptSentencesReader extends AssemblyAIReader {
/**
* Creates or gets a transcript, and returns a document for each sentence.
* @param params The parameters to create or get the transcript.
* @returns A promise that resolves to an array of documents, each containing a sentence of the transcript.
*/
async loadData(params: CreateTranscriptParameters | string): Promise<Document[]> {
let transcriptId = await this.getTranscriptId(params);
const sentencesResponse = await this.client.transcripts.sentences(
transcriptId
);
return sentencesResponse.sentences.map((p: TranscriptSentence) =>
new Document({ text: p.text }),
);
}
}
/**
* Creates a transcript and reads subtitles for the transcript as `srt` or `vtt` format.
*/
class AudioSubtitlesReader extends AssemblyAIReader {
/**
* Creates or gets a transcript and reads subtitles for the transcript as `srt` or `vtt` format.
* @param params The parameters to create or get the transcript.
* @param subtitleFormat The format of the subtitles, either `srt` or `vtt`.
* @returns A promise that resolves a document containing the subtitles as the page content.
*/
async loadData(
params: CreateTranscriptParameters | string,
subtitleFormat: SubtitleFormat = 'srt'
): Promise<Document[]> {
let transcriptId = await this.getTranscriptId(params);
const subtitles = await this.client.transcripts.subtitles(transcriptId, subtitleFormat);
return [new Document({ text: subtitles })];
}
}
export {
AudioTranscriptReader,
AudioTranscriptParagraphsReader,
AudioTranscriptSentencesReader,
AudioSubtitlesReader,
}
export type {
AssemblyAIOptions,
CreateTranscriptParameters,
SubtitleFormat
}
@@ -5,6 +5,8 @@ import {
VectorStoreIndex,
} from "llamaindex";
import * as dotenv from "dotenv";
import {
CHUNK_OVERLAP,
CHUNK_SIZE,
@@ -12,6 +14,9 @@ import {
STORAGE_DIR,
} from "./constants.mjs";
// Load environment variables from local .env file
dotenv.config();
async function getRuntime(func) {
const start = Date.now();
await func();
+15 -16
View File
@@ -14,26 +14,14 @@ import {
TemplateFramework,
} from "./types";
const envFileNameMap: Record<TemplateFramework, string> = {
nextjs: ".env.local",
express: ".env",
fastapi: ".env",
};
const createEnvLocalFile = async (
root: string,
framework: TemplateFramework,
openAIKey?: string,
) => {
const createEnvLocalFile = async (root: string, openAIKey?: string) => {
if (openAIKey) {
const envFileName = envFileNameMap[framework];
if (!envFileName) return;
const envFileName = ".env";
await fs.writeFile(
path.join(root, envFileName),
`OPENAI_API_KEY=${openAIKey}\n`,
);
console.log(`Created '${envFileName}' file containing OPENAI_API_KEY`);
process.env["OPENAI_API_KEY"] = openAIKey;
}
};
@@ -42,7 +30,16 @@ const copyTestData = async (
framework: TemplateFramework,
packageManager?: PackageManager,
engine?: TemplateEngine,
openAIKey?: string,
) => {
if (framework === "nextjs") {
// XXX: This is a hack to make the build for nextjs work with pdf-parse
// pdf-parse needs './test/data/05-versions-space.pdf' to exist - can be removed when pdf-parse is removed
const srcFile = path.join(__dirname, "components", "data", "101.pdf");
const destPath = path.join(root, "test", "data");
await fs.mkdir(destPath, { recursive: true });
await fs.copyFile(srcFile, path.join(destPath, "05-versions-space.pdf"));
}
if (engine === "context" || framework === "fastapi") {
const srcPath = path.join(__dirname, "components", "data");
const destPath = path.join(root, "data");
@@ -54,7 +51,7 @@ const copyTestData = async (
}
if (packageManager && engine === "context") {
if (process.env["OPENAI_API_KEY"]) {
if (openAIKey || process.env["OPENAI_API_KEY"]) {
console.log(
`\nRunning ${cyan(
`${packageManager} run generate`,
@@ -226,6 +223,7 @@ const installTSTemplate = async ({
"tailwind-merge": "^2",
"@radix-ui/react-slot": "^1",
"class-variance-authority": "^0.7",
clsx: "^1.2.1",
"lucide-react": "^0.291",
remark: "^14.0.3",
"remark-code-import": "^1.2.0",
@@ -313,7 +311,7 @@ export const installTemplate = async (
// This is a backend, so we need to copy the test data and create the env file.
// Copy the environment file to the target directory.
await createEnvLocalFile(props.root, props.framework, props.openAIKey);
await createEnvLocalFile(props.root, props.openAIKey);
// Copy test pdf file
await copyTestData(
@@ -321,6 +319,7 @@ export const installTemplate = async (
props.framework,
props.packageManager,
props.engine,
props.openAIKey,
);
}
};
@@ -1,5 +1,15 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: {
serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: {
@@ -2,6 +2,16 @@
const nextConfig = {
output: "export",
images: { unoptimized: true },
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: {
serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: {
@@ -9,6 +9,7 @@
},
"dependencies": {
"llamaindex": "0.0.31",
"dotenv": "^16.3.1",
"nanoid": "^5",
"next": "^13",
"react": "^18",
@@ -18,11 +19,11 @@
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10",
"autoprefixer": "^10.1",
"eslint": "^8",
"eslint-config-next": "^13",
"postcss": "^8",
"tailwindcss": "^3",
"tailwindcss": "^3.3",
"typescript": "^5"
}
}
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
@@ -19,9 +23,19 @@
}
],
"paths": {
"@/*": ["./*"]
}
"@/*": [
"./*"
]
},
"forceConsistentCasingInFileNames": true,
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
@@ -18,7 +18,7 @@ Then call the express API endpoint `/api/chat` to see the result:
```
curl --location 'localhost:8000/api/chat' \
--header 'Content-Type: application/json' \
--header 'Content-Type: text/plain' \
--data '{ "messages": [{ "role": "user", "content": "Hello" }] }'
```
@@ -9,7 +9,7 @@
"dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\""
},
"dependencies": {
"ai": "^2",
"ai": "^2.2.5",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4",
@@ -25,4 +25,4 @@
"tsup": "^7",
"typescript": "^5"
}
}
}
@@ -1,5 +1,15 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: {
serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: {
@@ -2,6 +2,16 @@
const nextConfig = {
output: "export",
images: { unoptimized: true },
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: {
serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: {
@@ -8,8 +8,9 @@
"lint": "next lint"
},
"dependencies": {
"ai": "^2",
"ai": "^2.2.5",
"llamaindex": "0.0.31",
"dotenv": "^16.3.1",
"next": "^13",
"react": "^18",
"react-dom": "^18"
@@ -18,11 +19,11 @@
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10",
"autoprefixer": "^10.1",
"eslint": "^8",
"eslint-config-next": "^13",
"postcss": "^8",
"tailwindcss": "^3",
"tailwindcss": "^3.3",
"typescript": "^5"
}
}
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
@@ -19,9 +23,19 @@
}
],
"paths": {
"@/*": ["./*"]
}
"@/*": [
"./*"
]
},
"forceConsistentCasingInFileNames": true,
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
-1
View File
@@ -9,7 +9,6 @@ module.exports = {
"OPENAI_API_KEY",
"REPLICATE_API_TOKEN",
"ANTHROPIC_API_KEY",
"ASSEMBLYAI_API_KEY",
"AZURE_OPENAI_KEY",
"AZURE_OPENAI_ENDPOINT",
-12
View File
@@ -156,9 +156,6 @@ importers:
'@xenova/transformers':
specifier: ^2.8.0
version: 2.8.0
assemblyai:
specifier: ^3.0.1
version: 3.1.1
crypto-js:
specifier: ^4.2.0
version: 4.2.0
@@ -5203,15 +5200,6 @@ packages:
safer-buffer: 2.1.2
dev: true
/assemblyai@3.1.1:
resolution: {integrity: sha512-rEhLnIZU7TupOkdzN9WjxhVU5bxMVofkNslzTArsuilaqOusPUmh6I7SGcJCGwKKE8qEweBaoY4DZ03cxv4KoA==}
dependencies:
ws: 8.14.2
transitivePeerDependencies:
- bufferutil
- utf-8-validate
dev: false
/assert@2.1.0:
resolution: {integrity: sha512-eLHpSK/Y4nhMJ07gDaAzoX/XAKS8PSaojml3M0DM4JpV1LAi5JOJ/p6H/XWrl8L+DzVEvVCW1z3vWAaB9oTsQw==}
dependencies: