Skip to content

Commit 5b28578

Browse files
committed
chore: upgrade core dependencies
1 parent 23b7882 commit 5b28578

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+9096
-6576
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { LLamaCloudFileService } from "llamaindex";
2+
import { NextResponse } from "next/server";
3+
4+
/**
5+
* This API is to get config from the backend envs and expose them to the frontend
6+
*/
7+
export async function GET() {
8+
if (!process.env.LLAMA_CLOUD_API_KEY) {
9+
return NextResponse.json(
10+
{
11+
error: "env variable LLAMA_CLOUD_API_KEY is required to use LlamaCloud",
12+
},
13+
{ status: 500 },
14+
);
15+
}
16+
const config = {
17+
projects: await LLamaCloudFileService.getAllProjectsWithPipelines(),
18+
pipeline: {
19+
pipeline: process.env.LLAMA_CLOUD_INDEX_NAME,
20+
project: process.env.LLAMA_CLOUD_PROJECT_NAME,
21+
},
22+
};
23+
return NextResponse.json(config, { status: 200 });
24+
}

app/api/chat/config/route.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse } from "next/server";
2+
3+
/**
4+
* This API is to get config from the backend envs and expose them to the frontend
5+
*/
6+
export async function GET() {
7+
const config = {
8+
starterQuestions: process.env.CONVERSATION_STARTERS?.trim().split("\n"),
9+
};
10+
return NextResponse.json(config, { status: 200 });
11+
}

app/api/chat/engine/chat.ts

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
import { ContextChatEngine, Settings } from "llamaindex";
1+
import { BaseChatEngine, BaseToolWithCall, LLMAgent } from "llamaindex";
2+
import fs from "node:fs/promises";
3+
import path from "node:path";
24
import { getDataSource } from "./index";
5+
import { createTools } from "./tools";
6+
import { createQueryEngineTool } from "./tools/query-engine";
37

4-
export async function createChatEngine() {
5-
const index = await getDataSource();
6-
if (!index) {
7-
throw new Error(
8-
`StorageContext is empty - call 'npm run generate' to generate the storage first`,
9-
);
8+
export async function createChatEngine(documentIds?: string[], params?: any) {
9+
const tools: BaseToolWithCall[] = [];
10+
11+
// Add a query engine tool if we have a data source
12+
// Delete this code if you don't have a data source
13+
const index = await getDataSource(params);
14+
if (index) {
15+
tools.push(createQueryEngineTool(index, { documentIds }));
1016
}
11-
const retriever = index.asRetriever();
12-
retriever.similarityTopK = process.env.TOP_K
13-
? parseInt(process.env.TOP_K)
14-
: 3;
1517

16-
return new ContextChatEngine({
17-
chatModel: Settings.llm,
18-
retriever,
18+
const configFile = path.join("config", "tools.json");
19+
let toolConfig: any;
20+
try {
21+
// add tools from config file if it exists
22+
toolConfig = JSON.parse(await fs.readFile(configFile, "utf8"));
23+
} catch (e) {
24+
console.info(`Could not read ${configFile} file. Using no tools.`);
25+
}
26+
if (toolConfig) {
27+
tools.push(...(await createTools(toolConfig)));
28+
}
29+
30+
const agent = new LLMAgent({
31+
tools,
1932
systemPrompt: process.env.SYSTEM_PROMPT,
20-
});
33+
}) as unknown as BaseChatEngine;
34+
35+
return agent;
2136
}

app/api/chat/engine/generate.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as dotenv from "dotenv";
55

66
import { getDocuments } from "./loader";
77
import { initSettings } from "./settings";
8-
import { STORAGE_CACHE_DIR } from "./shared";
98

109
// Load environment variables from local .env file
1110
dotenv.config();
@@ -20,11 +19,16 @@ async function getRuntime(func: any) {
2019
async function generateDatasource() {
2120
console.log(`Generating storage context...`);
2221
// Split documents, create embeddings and store them in the storage context
22+
const persistDir = process.env.STORAGE_CACHE_DIR;
23+
if (!persistDir) {
24+
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
25+
}
2326
const ms = await getRuntime(async () => {
2427
const storageContext = await storageContextFromDefaults({
25-
persistDir: STORAGE_CACHE_DIR,
28+
persistDir,
2629
});
2730
const documents = await getDocuments();
31+
2832
await VectorStoreIndex.fromDocuments(documents, {
2933
storageContext,
3034
});

app/api/chat/engine/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { SimpleDocumentStore, VectorStoreIndex } from "llamaindex";
22
import { storageContextFromDefaults } from "llamaindex/storage/StorageContext";
3-
import { STORAGE_CACHE_DIR } from "./shared";
43

5-
export async function getDataSource() {
4+
export async function getDataSource(params?: any) {
5+
const persistDir = process.env.STORAGE_CACHE_DIR;
6+
if (!persistDir) {
7+
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
8+
}
69
const storageContext = await storageContextFromDefaults({
7-
persistDir: `${STORAGE_CACHE_DIR}`,
10+
persistDir,
811
});
912

1013
const numberOfDocs = Object.keys(

app/api/chat/engine/loader.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
import { SimpleDirectoryReader } from "llamaindex";
1+
import {
2+
FILE_EXT_TO_READER,
3+
SimpleDirectoryReader,
4+
} from "@llamaindex/readers/directory";
25

36
export const DATA_DIR = "./data";
47

8+
export function getExtractors() {
9+
return FILE_EXT_TO_READER;
10+
}
11+
512
export async function getDocuments() {
6-
return await new SimpleDirectoryReader().loadData({
13+
const documents = await new SimpleDirectoryReader().loadData({
714
directoryPath: DATA_DIR,
815
});
16+
// Set private=false to mark the document as public (required for filtering)
17+
for (const document of documents) {
18+
document.metadata = {
19+
...document.metadata,
20+
private: "false",
21+
};
22+
}
23+
return documents;
924
}

app/api/chat/engine/provider.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { OpenAI, OpenAIEmbedding } from "@llamaindex/openai";
2+
import { Settings } from "llamaindex";
3+
import {
4+
DefaultAzureCredential,
5+
getBearerTokenProvider,
6+
} from "@azure/identity";
7+
8+
const AZURE_COGNITIVE_SERVICES_SCOPE =
9+
"https://cognitiveservices.azure.com/.default";
10+
11+
export function setupProvider() {
12+
const credential = new DefaultAzureCredential();
13+
const azureADTokenProvider = getBearerTokenProvider(
14+
credential,
15+
AZURE_COGNITIVE_SERVICES_SCOPE,
16+
);
17+
18+
const azure = {
19+
azureADTokenProvider,
20+
deployment: process.env.AZURE_DEPLOYMENT_NAME ?? "gpt-35-turbo",
21+
};
22+
23+
// configure LLM model
24+
Settings.llm = new OpenAI({
25+
azure,
26+
}) as any;
27+
28+
// configure embedding model
29+
azure.deployment = process.env.EMBEDDING_MODEL as string;
30+
Settings.embedModel = new OpenAIEmbedding({
31+
azure,
32+
model: process.env.EMBEDDING_MODEL,
33+
dimensions: process.env.EMBEDDING_DIM
34+
? parseInt(process.env.EMBEDDING_DIM)
35+
: undefined,
36+
});
37+
}

app/api/chat/engine/queryFilter.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { MetadataFilter, MetadataFilters } from "llamaindex";
2+
3+
export function generateFilters(documentIds: string[]): MetadataFilters {
4+
// filter all documents have the private metadata key set to true
5+
const publicDocumentsFilter: MetadataFilter = {
6+
key: "private",
7+
value: "true",
8+
operator: "!=",
9+
};
10+
11+
// if no documentIds are provided, only retrieve information from public documents
12+
if (!documentIds.length) return { filters: [publicDocumentsFilter] };
13+
14+
const privateDocumentsFilter: MetadataFilter = {
15+
key: "doc_id",
16+
value: documentIds,
17+
operator: "in",
18+
};
19+
20+
// if documentIds are provided, retrieve information from public and private documents
21+
return {
22+
filters: [publicDocumentsFilter, privateDocumentsFilter],
23+
condition: "or",
24+
};
25+
}

app/api/chat/engine/settings.ts

+6-94
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,18 @@
1-
import { OpenAI, OpenAIEmbedding, Settings } from "llamaindex";
2-
3-
import {
4-
DefaultAzureCredential,
5-
getBearerTokenProvider,
6-
} from "@azure/identity";
7-
import { OllamaEmbedding } from "llamaindex/embeddings/OllamaEmbedding";
8-
import { Ollama } from "llamaindex/llm/ollama";
1+
import { Settings } from "llamaindex";
2+
import { setupProvider } from "./provider";
93

104
const CHUNK_SIZE = 512;
115
const CHUNK_OVERLAP = 20;
12-
const AZURE_COGNITIVE_SERVICES_SCOPE =
13-
"https://cognitiveservices.azure.com/.default";
146

157
export const initSettings = async () => {
168
console.log(`Using '${process.env.MODEL_PROVIDER}' model provider`);
179

18-
// if provider is OpenAI, MODEL must be set
19-
if (process.env.MODEL_PROVIDER === 'openai' && process.env.OPENAI_API_TYPE !== 'AzureOpenAI' && !process.env.MODEL) {
20-
throw new Error("'MODEL' env variable must be set.");
21-
}
22-
23-
// if provider is Azure OpenAI, AZURE_DEPLOYMENT_NAME must be set
24-
if (process.env.MODEL_PROVIDER === 'openai' && process.env.OPENAI_API_TYPE === 'AzureOpenAI' && !process.env.AZURE_DEPLOYMENT_NAME) {
25-
throw new Error("'AZURE_DEPLOYMENT_NAME' env variables must be set.");
10+
if (!process.env.MODEL || !process.env.EMBEDDING_MODEL) {
11+
throw new Error("'MODEL' and 'EMBEDDING_MODEL' env variables must be set.");
2612
}
2713

28-
if (!process.env.EMBEDDING_MODEL) {
29-
throw new Error("'EMBEDDING_MODEL' env variable must be set.");
30-
}
31-
32-
switch (process.env.MODEL_PROVIDER) {
33-
case "ollama":
34-
initOllama();
35-
break;
36-
case "openai":
37-
if (process.env.OPENAI_API_TYPE === "AzureOpenAI") {
38-
await initAzureOpenAI();
39-
} else {
40-
initOpenAI();
41-
}
42-
break;
43-
default:
44-
throw new Error(
45-
`Model provider '${process.env.MODEL_PROVIDER}' not supported.`,
46-
);
47-
}
4814
Settings.chunkSize = CHUNK_SIZE;
4915
Settings.chunkOverlap = CHUNK_OVERLAP;
50-
};
51-
52-
function initOpenAI() {
53-
Settings.llm = new OpenAI({
54-
model: process.env.MODEL ?? "gpt-3.5-turbo",
55-
maxTokens: 512,
56-
});
57-
Settings.embedModel = new OpenAIEmbedding({
58-
model: process.env.EMBEDDING_MODEL,
59-
dimensions: process.env.EMBEDDING_DIM
60-
? parseInt(process.env.EMBEDDING_DIM)
61-
: undefined,
62-
});
63-
}
64-
65-
async function initAzureOpenAI() {
66-
const credential = new DefaultAzureCredential();
67-
const azureADTokenProvider = getBearerTokenProvider(
68-
credential,
69-
AZURE_COGNITIVE_SERVICES_SCOPE,
70-
);
71-
72-
const azure = {
73-
azureADTokenProvider,
74-
deployment: process.env.AZURE_OPENAI_DEPLOYMENT ?? "gpt-35-turbo",
75-
};
76-
77-
// configure LLM model
78-
Settings.llm = new OpenAI({
79-
azure,
80-
}) as any;
81-
82-
// configure embedding model
83-
azure.deployment = process.env.EMBEDDING_MODEL as string;
84-
Settings.embedModel = new OpenAIEmbedding({
85-
azure,
86-
model: process.env.EMBEDDING_MODEL,
87-
dimensions: process.env.EMBEDDING_DIM
88-
? parseInt(process.env.EMBEDDING_DIM)
89-
: undefined,
90-
});
91-
}
92-
93-
function initOllama() {
94-
const config = {
95-
host: process.env.OLLAMA_BASE_URL ?? "http://127.0.0.1:11434",
96-
};
97-
Settings.llm = new Ollama({
98-
model: process.env.MODEL ?? "",
99-
config,
100-
});
101-
Settings.embedModel = new OllamaEmbedding({
102-
model: process.env.EMBEDDING_MODEL ?? "",
103-
config,
104-
});
105-
}
10616

17+
setupProvider();
18+
};

app/api/chat/engine/shared.ts

-1
This file was deleted.

0 commit comments

Comments
 (0)