Compare commits

..

24 Commits

Author SHA1 Message Date
Emanuel Ferreira fe715066fb cr 2024-02-08 08:41:23 -03:00
Emanuel Ferreira bd13d400b8 Merge branch 'feat/use-batching-vectstoreindex' of github.com:run-llama/LlamaIndexTS into feat/use-batching-vectstoreindex 2024-02-08 08:23:23 -03:00
Emanuel Ferreira f22e2cd144 feat: batch openai embedding 2024-02-08 08:22:06 -03:00
Alex Yang 5b07ade7dd fix: type 2024-02-07 15:32:22 -06:00
Alex Yang b0366dd7f7 Create tame-ways-applaud.md 2024-02-07 15:10:55 -06:00
Marcus Schiesser 2f5a11ccd4 feat: use batching in vector store index 2024-02-07 15:15:40 +07:00
Marcus Schiesser eeb90d7991 fix(cl): add link to configure search tool 2024-02-07 14:07:57 +07:00
Marcus Schiesser 7b7329bd18 feat(cl): Added latest turbo models for GPT-3.5 and GPT 4 2024-02-07 12:46:19 +07:00
Alex Yang b3acbb06f4 docs: update CONTRIBUTING.md (#516) 2024-02-07 12:05:29 +07:00
Marcus Schiesser 7db7562841 fix(cl): just retrieve top-k 3 for context to prevent token exceed 2024-02-07 10:59:31 +07:00
yisding 0e75b124c3 minor update 2024-02-06 12:24:06 -08:00
yisding d79a0b76f3 update packages 2024-02-06 11:55:38 -08:00
yisding c3eb4933fb RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.10

[skip ci]
2024-02-06 11:50:39 -08:00
yisding e3a956aedd pnpm install 2024-02-06 11:48:12 -08:00
yisding e562e479dc Merge branch 'main' of github.com:run-llama/LlamaIndexTS 2024-02-06 11:39:07 -08:00
Alex Yang 1900e019e3 build: fix build errors (#521) 2024-02-06 12:54:08 -06:00
Emanuel Ferreira 317f140822 fix: revert embed batch temporarily (#520) 2024-02-06 12:01:48 -03:00
Emanuel Ferreira cd829474d6 feat(queryEngineTool): add query engine tool to agents (#509) 2024-02-06 11:11:26 -03:00
Emanuel Ferreira b6c1500570 feat(embedding): add batch embed size (#407) 2024-02-06 10:19:14 -03:00
Huu Le (Lee) d06a85bd34 feat: Add support for llamahub tools (#517)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-02-06 17:34:03 +07:00
Ian Sinnott 6b9a2feac5 Consistent Document IDs in NotionReader.ts (#519) 2024-02-06 15:52:29 +07:00
Mike Fortman bd08004afe Update Astra DB Vectorstore to support namespaces (#485)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-02-06 11:31:08 +07:00
yisding 36f2903eb3 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.9

[skip ci]
2024-02-02 11:26:00 -08:00
yisding 09464e6da7 docs(changeset): add OpenAIAgent (thanks @EmanuelCampos) 2024-02-02 11:23:03 -08:00
69 changed files with 2254 additions and 1574 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"create-llama": patch
---
feat(create-llama) add option to select tools (google_search, wikipedia)
+5
View File
@@ -0,0 +1,5 @@
---
"create-llama": patch
---
Added latest turbo models for GPT-3.5 and GPT 4
-5
View File
@@ -1,5 +0,0 @@
---
"llamaindex": patch
---
fix: update `VectorIndexRetriever` constructor parameters' type.
+5
View File
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---
feat: use batching in vector store index
+2 -2
View File
@@ -4,6 +4,6 @@
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers-contrib/features/turborepo-npm:1": {},
"ghcr.io/devcontainers-contrib/features/typescript:2": {},
"ghcr.io/devcontainers-contrib/features/pnpm:2": {},
},
"ghcr.io/devcontainers-contrib/features/pnpm:2": {}
}
}
+7 -1
View File
@@ -49,8 +49,14 @@ jobs:
- name: Build create-llama
run: pnpm run build
working-directory: ./packages/create-llama
- name: Pack
run: pnpm pack --pack-destination ./output
working-directory: ./packages/create-llama
- name: Extract Pack
run: tar -xvzf ./output/*.tgz -C ./output
working-directory: ./packages/create-llama
- name: Run Playwright tests
run: pnpm run e2e
run: pnpm exec playwright test
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
working-directory: ./packages/create-llama
+6
View File
@@ -38,6 +38,12 @@ jobs:
- name: Run Circular Dependency Check
run: pnpm run circular-check
working-directory: ./packages/core
- uses: actions/upload-artifact@v3
if: failure()
with:
name: typecheck-build-dist
path: ./packages/core/dist
if-no-files-found: error
typecheck-examples:
runs-on: ubuntu-latest
+12
View File
@@ -78,3 +78,15 @@ pnpm start
That should start a webserver which will serve the docs on https://localhost:3000
Any changes you make should be reflected in the browser. If you need to regenerate the API docs and find that your TSDoc isn't getting the updates, feel free to remove apps/docs/api. It will automatically regenerate itself when you run pnpm start again.
## Publishing
To publish a new version of the library, run
```shell
pnpm new-llamaindex
pnpm new-create-llama
pnpm release
git push # push to the main branch
git push --tags
```
@@ -0,0 +1,128 @@
# OpenAI Agent + QueryEngineTool
QueryEngineTool is a tool that allows you to query a vector index. In this example, we will create a vector index from a set of documents and then create a QueryEngineTool from the vector index. We will then create an OpenAIAgent with the QueryEngineTool and chat with the agent.
## Setup
First, you need to install the `llamaindex` package. You can do this by running the following command in your terminal:
```bash
pnpm i llamaindex
```
Then you can import the necessary classes and functions.
```ts
import {
OpenAIAgent,
SimpleDirectoryReader,
VectorStoreIndex,
QueryEngineTool,
} from "llamaindex";
```
## Create a vector index
Now we can create a vector index from a set of documents.
```ts
// Load the documents
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: "node_modules/llamaindex/examples/",
});
// Create a vector index from the documents
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
```
## Create a QueryEngineTool
Now we can create a QueryEngineTool from the vector index.
```ts
// Create a query engine from the vector index
const abramovQueryEngine = vectorIndex.asQueryEngine();
// Create a QueryEngineTool with the query engine
const queryEngineTool = new QueryEngineTool({
queryEngine: abramovQueryEngine,
metadata: {
name: "abramov_query_engine",
description: "A query engine for the Abramov documents",
},
});
```
## Create an OpenAIAgent
```ts
// Create an OpenAIAgent with the query engine tool tools
const agent = new OpenAIAgent({
tools: [queryEngineTool],
verbose: true,
});
```
## Chat with the agent
Now we can chat with the agent.
```ts
const response = await agent.chat({
message: "What was his salary?",
});
console.log(String(response));
```
## Full code
```ts
import {
OpenAIAgent,
SimpleDirectoryReader,
VectorStoreIndex,
QueryEngineTool,
} from "llamaindex";
async function main() {
// Load the documents
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: "node_modules/llamaindex/examples/",
});
// Create a vector index from the documents
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
// Create a query engine from the vector index
const abramovQueryEngine = vectorIndex.asQueryEngine();
// Create a QueryEngineTool with the query engine
const queryEngineTool = new QueryEngineTool({
queryEngine: abramovQueryEngine,
metadata: {
name: "abramov_query_engine",
description: "A query engine for the Abramov documents",
},
});
// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [queryEngineTool],
verbose: true,
});
// Chat with the agent
const response = await agent.chat({
message: "What was his salary?",
});
// Print the response
console.log(String(response));
}
main().then(() => {
console.log("Done");
});
```
+2 -2
View File
@@ -6,6 +6,6 @@
"composite": true,
"incremental": true,
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo",
},
"tsBuildInfoFile": "./lib/.tsbuildinfo"
}
}
+46
View File
@@ -0,0 +1,46 @@
import {
OpenAIAgent,
QueryEngineTool,
SimpleDirectoryReader,
VectorStoreIndex,
} from "llamaindex";
async function main() {
// Load the documents
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: "node_modules/llamaindex/examples/",
});
// Create a vector index from the documents
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
// Create a query engine from the vector index
const abramovQueryEngine = vectorIndex.asQueryEngine();
// Create a QueryEngineTool with the query engine
const queryEngineTool = new QueryEngineTool({
queryEngine: abramovQueryEngine,
metadata: {
name: "abramov_query_engine",
description: "A query engine for the Abramov documents",
},
});
// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [queryEngineTool],
verbose: true,
});
// Chat with the agent
const response = await agent.chat({
message: "What was his salary?",
});
// Print the response
console.log(String(response));
}
main().then(() => {
console.log("Done");
});
+11 -2
View File
@@ -14,18 +14,27 @@ Here are two sample scripts which work well with the sample data in the Astra Po
- `ASTRA_DB_APPLICATION_TOKEN`: The generated app token for your Astra database
- `ASTRA_DB_ENDPOINT`: The API endpoint for your Astra database
- `ASTRA_DB_NAMESPACE`: (Optional) The namespace where your collection is stored defaults to `default_keyspace`
- `OPENAI_API_KEY`: Your OpenAI key
2. `cd` Into the `examples` directory
3. run `npm i`
## Load the data
## Example load and query
Loads and queries a simple vectorstore with some documents about Astra DB
run `ts-node astradb/example`
## Movie Reviews Example
### Load the data
This sample loads the same dataset of movie reviews as the Astra Portal sample dataset. (Feel free to load the data in your the Astra Data Explorer to compare)
run `ts-node astradb/load`
## Use RAG to Query the data
### Use RAG to Query the data
Check out your data in the Astra Data Explorer and change the sample query as you see fit.
+58
View File
@@ -0,0 +1,58 @@
import {
AstraDBVectorStore,
Document,
storageContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";
const collectionName = "test_collection";
async function main() {
try {
const docs = [
new Document({
text: "AstraDB is built on Apache Cassandra",
metadata: {
id: 123,
foo: "bar",
},
}),
new Document({
text: "AstraDB is a NoSQL DB",
metadata: {
id: 456,
foo: "baz",
},
}),
new Document({
text: "AstraDB supports vector search",
metadata: {
id: 789,
foo: "qux",
},
}),
];
const astraVS = new AstraDBVectorStore();
await astraVS.create(collectionName, {
vector: { dimension: 1536, metric: "cosine" },
});
await astraVS.connect(collectionName);
const ctx = await storageContextFromDefaults({ vectorStore: astraVS });
const index = await VectorStoreIndex.fromDocuments(docs, {
storageContext: ctx,
});
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
query: "Describe AstraDB.",
});
console.log(response.toString());
} catch (e) {
console.error(e);
}
}
main();
+2 -2
View File
@@ -10,9 +10,9 @@ const collectionName = "movie_reviews";
async function main() {
try {
const reader = new PapaCSVReader(false);
const docs = await reader.loadData("../data/movie_reviews.csv");
const docs = await reader.loadData("./data/movie_reviews.csv");
const astraVS = new AstraDBVectorStore();
const astraVS = new AstraDBVectorStore({ contentKey: "reviewtext" });
await astraVS.create(collectionName, {
vector: { dimension: 1536, metric: "cosine" },
});
+2 -2
View File
@@ -8,7 +8,7 @@ const collectionName = "movie_reviews";
async function main() {
try {
const astraVS = new AstraDBVectorStore();
const astraVS = new AstraDBVectorStore({ contentKey: "reviewtext" });
await astraVS.connect(collectionName);
const ctx = serviceContextFromDefaults();
@@ -19,7 +19,7 @@ async function main() {
const queryEngine = await index.asQueryEngine({ retriever });
const results = await queryEngine.query({
query: "What is the best reviewed movie?",
query: 'How was "La Sapienza" reviewed?',
});
console.log(results.response);
+17 -10
View File
@@ -1,4 +1,9 @@
import { Document, SubQuestionQueryEngine, VectorStoreIndex } from "llamaindex";
import {
Document,
QueryEngineTool,
SubQuestionQueryEngine,
VectorStoreIndex,
} from "llamaindex";
import essay from "./essay";
@@ -6,16 +11,18 @@ import essay from "./essay";
const document = new Document({ text: essay, id_: essay });
const index = await VectorStoreIndex.fromDocuments([document]);
const queryEngine = SubQuestionQueryEngine.fromDefaults({
queryEngineTools: [
{
queryEngine: index.asQueryEngine(),
metadata: {
name: "pg_essay",
description: "Paul Graham essay on What I Worked On",
},
const queryEngineTools = [
new QueryEngineTool({
queryEngine: index.asQueryEngine(),
metadata: {
name: "pg_essay",
description: "Paul Graham essay on What I Worked On",
},
],
}),
];
const queryEngine = SubQuestionQueryEngine.fromDefaults({
queryEngineTools,
});
const response = await queryEngine.query({
+4 -4
View File
@@ -10,13 +10,13 @@
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"incremental": true,
"composite": true,
"composite": true
},
"ts-node": {
"files": true,
"compilerOptions": {
"module": "commonjs",
},
"module": "commonjs"
}
},
"include": ["./**/*.ts"],
"include": ["./**/*.ts"]
}
+7 -7
View File
@@ -18,20 +18,20 @@
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@turbo/gen": "^1.11.3",
"@types/jest": "^29.5.11",
"@turbo/gen": "^1.12.2",
"@types/jest": "^29.5.12",
"eslint": "^8.56.0",
"eslint-config-custom": "workspace:*",
"husky": "^9.0.6",
"husky": "^9.0.10",
"jest": "^29.7.0",
"lint-staged": "^15.2.0",
"prettier": "^3.2.4",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"ts-jest": "^29.1.2",
"turbo": "^1.11.3",
"turbo": "^1.12.2",
"typescript": "^5.3.3"
},
"packageManager": "pnpm@8.14.3+sha256.2d0363bb6c314daa67087ef07743eea1ba2e2d360c835e8fec6b5575e4ed9484",
"packageManager": "pnpm@8.15.1",
"pnpm": {
"overrides": {
"trim": "1.0.1",
+14
View File
@@ -1,5 +1,19 @@
# llamaindex
## 0.1.10
### Patch Changes
- b6c1500: feat(embedBatchSize): add batching for embeddings
- 6cc3a36: fix: update `VectorIndexRetriever` constructor parameters' type.
- cd82947: feat(queryEngineTool): add query engine tool to agents
## 0.1.9
### Patch Changes
- 09464e6: add OpenAIAgent (thanks @EmanuelCampos)
## 0.1.8
### Patch Changes
+14 -19
View File
@@ -1,26 +1,26 @@
{
"name": "llamaindex",
"private": true,
"version": "0.1.8",
"version": "0.1.10",
"license": "MIT",
"dependencies": {
"@anthropic-ai/sdk": "^0.12.4",
"@anthropic-ai/sdk": "^0.13.0",
"@datastax/astra-db-ts": "^0.1.4",
"@mistralai/mistralai": "^0.0.10",
"@notionhq/client": "^2.2.14",
"@pinecone-database/pinecone": "^1.1.3",
"@qdrant/js-client-rest": "^1.7.0",
"@xenova/transformers": "^2.14.1",
"assemblyai": "^4.2.1",
"@xenova/transformers": "^2.15.0",
"assemblyai": "^4.2.2",
"chromadb": "~1.7.3",
"file-type": "^18.7.0",
"js-tiktoken": "^1.0.8",
"js-tiktoken": "^1.0.10",
"lodash": "^4.17.21",
"mammoth": "^1.6.0",
"md-utils-ts": "^2.0.0",
"mongodb": "^6.3.0",
"notion-md-crawler": "^0.0.2",
"openai": "^4.26.0",
"openai": "^4.26.1",
"papaparse": "^5.4.1",
"pathe": "^1.1.2",
"pdf2json": "^3.0.5",
@@ -29,18 +29,18 @@
"portkey-ai": "^0.1.16",
"rake-modified": "^1.0.8",
"replicate": "^0.25.2",
"string-strip-html": "^13.4.5",
"string-strip-html": "^13.4.6",
"wink-nlp": "^1.14.3"
},
"devDependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@types/edit-json-file": "^1.7.3",
"@types/jest": "^29.5.11",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.202",
"@types/node": "^18.19.10",
"@types/node": "^18.19.14",
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.0",
"bunchee": "^4.4.3",
"bunchee": "^4.4.6",
"edit-json-file": "^1.8.0",
"madge": "^6.1.0",
"typescript": "^5.3.3"
@@ -118,11 +118,6 @@
"import": "./dist/Response.mjs",
"require": "./dist/Response.js"
},
"./Retriever": {
"types": "./dist/Retriever.d.mts",
"import": "./dist/Retriever.mjs",
"require": "./dist/Retriever.js"
},
"./ServiceContext": {
"types": "./dist/ServiceContext.d.mts",
"import": "./dist/ServiceContext.mjs",
@@ -133,10 +128,10 @@
"import": "./dist/TextSplitter.mjs",
"require": "./dist/TextSplitter.js"
},
"./Tool": {
"types": "./dist/Tool.d.mts",
"import": "./dist/Tool.mjs",
"require": "./dist/Tool.js"
"./tools": {
"types": "./dist/tools.d.mts",
"import": "./dist/tools.mjs",
"require": "./dist/tools.js"
},
"./readers/AssemblyAIReader": {
"types": "./dist/readers/AssemblyAIReader.d.mts",
+8 -1
View File
@@ -266,7 +266,14 @@ export class AgentRunner extends BaseAgentRunner {
let resultOutput;
while (true) {
const curStepOutput = await this._runStep(task.taskId);
const curStepOutput = await this._runStep(
task.taskId,
undefined,
ChatResponseMode.WAIT,
{
toolChoice,
},
);
if (curStepOutput.isLast) {
resultOutput = curStepOutput;
@@ -1,3 +1,4 @@
import type { Anthropic } from "@anthropic-ai/sdk";
import { NodeWithScore } from "../Node";
/*
@@ -39,14 +40,7 @@ export interface DefaultStreamToken {
//OpenAI stream token schema is the default.
//Note: Anthropic and Replicate also use similar token schemas.
export type OpenAIStreamToken = DefaultStreamToken;
export type AnthropicStreamToken = {
completion: string;
model: string;
stop_reason: string | undefined;
stop?: boolean | undefined;
log_id?: string;
};
export type AnthropicStreamToken = Anthropic.Completion;
//
//Callback Responses
//
@@ -36,7 +36,7 @@ export class HuggingFaceEmbedding extends BaseEmbedding {
return this.extractor;
}
async getTextEmbedding(text: string): Promise<number[]> {
override async getTextEmbedding(text: string): Promise<number[]> {
const extractor = await this.getExtractor();
const output = await extractor(text, { pooling: "mean", normalize: true });
return Array.from(output.data);
@@ -59,7 +59,9 @@ export class OpenAIEmbedding extends BaseEmbedding {
this.model = init?.model ?? "text-embedding-ada-002";
this.dimensions = init?.dimensions; // if no dimensions provided, will be undefined/not sent to OpenAI
this.embedBatchSize = init?.embedBatchSize ?? 10;
this.maxRetries = init?.maxRetries ?? 10;
this.timeout = init?.timeout ?? 60 * 1000; // Default is 60 seconds
this.additionalSessionOptions = init?.additionalSessionOptions;
@@ -100,21 +102,43 @@ export class OpenAIEmbedding extends BaseEmbedding {
}
}
private async getOpenAIEmbedding(input: string) {
/**
* Get embeddings for a batch of texts
* @param texts
* @param options
*/
private async getOpenAIEmbedding(input: string[]): Promise<number[][]> {
const { data } = await this.session.openai.embeddings.create({
model: this.model,
dimensions: this.dimensions, // only sent to OpenAI if set by user
input,
});
return data[0].embedding;
return data.map((d) => d.embedding);
}
/**
* Get embeddings for a batch of texts
* @param texts
*/
async getTextEmbeddings(texts: string[]): Promise<number[][]> {
return await this.getOpenAIEmbedding(texts);
}
/**
* Get embeddings for a single text
* @param texts
*/
async getTextEmbedding(text: string): Promise<number[]> {
return this.getOpenAIEmbedding(text);
return (await this.getOpenAIEmbedding([text]))[0];
}
/**
* Get embeddings for a query
* @param texts
* @param options
*/
async getQueryEmbedding(query: string): Promise<number[]> {
return this.getOpenAIEmbedding(query);
return (await this.getOpenAIEmbedding([query]))[0];
}
}
+63 -5
View File
@@ -2,7 +2,11 @@ import { BaseNode, MetadataMode } from "../Node";
import { TransformComponent } from "../ingestion";
import { SimilarityType, similarity } from "./utils";
const DEFAULT_EMBED_BATCH_SIZE = 10;
export abstract class BaseEmbedding implements TransformComponent {
embedBatchSize = DEFAULT_EMBED_BATCH_SIZE;
similarity(
embedding1: number[],
embedding2: number[],
@@ -14,12 +18,66 @@ export abstract class BaseEmbedding implements TransformComponent {
abstract getTextEmbedding(text: string): Promise<number[]>;
abstract getQueryEmbedding(query: string): Promise<number[]>;
async transform(nodes: BaseNode[], _options?: any): Promise<BaseNode[]> {
for (const node of nodes) {
node.embedding = await this.getTextEmbedding(
node.getContent(MetadataMode.EMBED),
);
/**
* Optionally override this method to retrieve multiple embeddings in a single request
* @param texts
*/
async getTextEmbeddings(texts: string[]): Promise<Array<number[]>> {
const embeddings: number[][] = [];
for (const text of texts) {
const embedding = await this.getTextEmbedding(text);
embeddings.push(embedding);
}
return embeddings;
}
/**
* Get embeddings for a batch of texts
* @param texts
* @param options
*/
async getTextEmbeddingsBatch(
texts: string[],
options?: {
logProgress?: boolean;
},
): Promise<Array<number[]>> {
const resultEmbeddings: Array<number[]> = [];
const chunkSize = this.embedBatchSize;
const queue: string[] = texts;
const curBatch: string[] = [];
for (let i = 0; i < queue.length; i++) {
curBatch.push(queue[i]);
if (i == queue.length - 1 || curBatch.length == chunkSize) {
const embeddings = await this.getTextEmbeddings(curBatch);
resultEmbeddings.push(...embeddings);
if (options?.logProgress) {
console.log(`getting embedding progress: ${i} / ${queue.length}`);
}
curBatch.length = 0;
}
}
return resultEmbeddings;
}
async transform(nodes: BaseNode[], _options?: any): Promise<BaseNode[]> {
const texts = nodes.map((node) => node.getContent(MetadataMode.EMBED));
const embeddings = await this.getTextEmbeddingsBatch(texts);
for (let i = 0; i < nodes.length; i++) {
nodes[i].embedding = embeddings[i];
}
return nodes;
}
}
@@ -14,9 +14,9 @@ import {
} from "../../synthesizers";
import {
BaseQueryEngine,
BaseTool,
QueryEngineParamsNonStreaming,
QueryEngineParamsStreaming,
QueryEngineTool,
ToolMetadata,
} from "../../types";
import { BaseQuestionGenerator, SubQuestion } from "./types";
@@ -27,28 +27,23 @@ import { BaseQuestionGenerator, SubQuestion } from "./types";
export class SubQuestionQueryEngine implements BaseQueryEngine {
responseSynthesizer: BaseSynthesizer;
questionGen: BaseQuestionGenerator;
queryEngines: Record<string, BaseQueryEngine>;
queryEngines: BaseTool[];
metadatas: ToolMetadata[];
constructor(init: {
questionGen: BaseQuestionGenerator;
responseSynthesizer: BaseSynthesizer;
queryEngineTools: QueryEngineTool[];
queryEngineTools: BaseTool[];
}) {
this.questionGen = init.questionGen;
this.responseSynthesizer =
init.responseSynthesizer ?? new ResponseSynthesizer();
this.queryEngines = init.queryEngineTools.reduce<
Record<string, BaseQueryEngine>
>((acc, tool) => {
acc[tool.metadata.name] = tool.queryEngine;
return acc;
}, {});
this.queryEngines = init.queryEngineTools;
this.metadatas = init.queryEngineTools.map((tool) => tool.metadata);
}
static fromDefaults(init: {
queryEngineTools: QueryEngineTool[];
queryEngineTools: BaseTool[];
questionGen?: BaseQuestionGenerator;
responseSynthesizer?: BaseSynthesizer;
serviceContext?: ServiceContext;
@@ -122,13 +117,24 @@ export class SubQuestionQueryEngine implements BaseQueryEngine {
): Promise<NodeWithScore | null> {
try {
const question = subQ.subQuestion;
const queryEngine = this.queryEngines[subQ.toolName];
const response = await queryEngine.query({
const queryEngine = this.queryEngines.find(
(tool) => tool.metadata.name === subQ.toolName,
);
if (!queryEngine) {
return null;
}
const responseText = await queryEngine?.call?.({
query: question,
parentEvent,
});
const responseText = response.response;
if (!responseText) {
return null;
}
const nodeText = `Sub question: ${question}\nResponse: ${responseText}`;
const node = new TextNode({ text: nodeText });
return { node, score: 0 };
@@ -166,20 +166,14 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
nodes: BaseNode[],
options?: { logProgress?: boolean },
): Promise<BaseNode[]> {
const nodesWithEmbeddings: BaseNode[] = [];
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i];
if (options?.logProgress) {
console.log(`Getting embedding for node ${i + 1}/${nodes.length}`);
}
node.embedding = await this.embedModel.getTextEmbedding(
node.getContent(MetadataMode.EMBED),
);
nodesWithEmbeddings.push(node);
}
return nodesWithEmbeddings;
const texts = nodes.map((node) => node.getContent(MetadataMode.EMBED));
const embeddings = await this.embedModel.getTextEmbeddingsBatch(texts, {
logProgress: options?.logProgress,
});
return nodes.map((node, i) => {
node.embedding = embeddings[i];
return node;
});
}
/**
+5 -1
View File
@@ -42,7 +42,11 @@ export class NotionReader implements BaseReader {
toDocuments(pages: Pages): Document[] {
return Object.values(pages).map((page) => {
const text = pageToString(page);
return new Document({ text, metadata: page.metadata });
return new Document({
id_: page.metadata.id, // Use the Notion-provided UUID for the document
text,
metadata: page.metadata,
});
});
}
@@ -1,8 +1,9 @@
import { AstraDB } from "@datastax/astra-db-ts";
import { Collection } from "@datastax/astra-db-ts/dist/collections";
import { CreateCollectionOptions } from "@datastax/astra-db-ts/dist/collections/options";
import { BaseNode, Document, MetadataMode } from "../../Node";
import { BaseNode, MetadataMode } from "../../Node";
import { VectorStore, VectorStoreQuery, VectorStoreQueryResult } from "./types";
import { metadataDictToNode, nodeToMetadata } from "./utils";
const MAX_INSERT_BATCH_SIZE = 20;
@@ -12,7 +13,7 @@ export class AstraDBVectorStore implements VectorStore {
astraDBClient: AstraDB;
idKey: string;
contentKey: string | undefined; // if undefined the entirety of the node aside from the id and embedding will be stored as content
contentKey: string;
metadataKey: string;
private collection: Collection | undefined;
@@ -22,6 +23,7 @@ export class AstraDBVectorStore implements VectorStore {
params?: {
token: string;
endpoint: string;
namespace: string;
};
},
) {
@@ -40,11 +42,15 @@ export class AstraDBVectorStore implements VectorStore {
if (!endpoint) {
throw new Error("Must specify ASTRA_DB_ENDPOINT via env variable.");
}
this.astraDBClient = new AstraDB(token, endpoint);
const namespace =
init?.params?.namespace ??
process.env.ASTRA_DB_NAMESPACE ??
"default_keyspace";
this.astraDBClient = new AstraDB(token, endpoint, namespace);
}
this.idKey = init?.idKey ?? "_id";
this.contentKey = init?.contentKey;
this.contentKey = init?.contentKey ?? "content";
this.metadataKey = init?.metadataKey ?? "metadata";
}
@@ -102,12 +108,20 @@ export class AstraDBVectorStore implements VectorStore {
if (!nodes || nodes.length === 0) {
return [];
}
const dataToInsert = nodes.map((node) => {
const metadata = nodeToMetadata(
node,
true,
this.contentKey,
this.flatMetadata,
);
return {
_id: node.id_,
$vector: node.getEmbedding(),
content: node.getContent(MetadataMode.ALL),
metadata: node.metadata,
[this.idKey]: node.id_,
[this.contentKey]: node.getContent(MetadataMode.NONE),
[this.metadataKey]: metadata,
};
});
@@ -122,11 +136,10 @@ export class AstraDBVectorStore implements VectorStore {
for (const batch of batchData) {
console.debug(`Inserting batch of size ${batch.length}`);
const result = await collection.insertMany(batch);
await collection.insertMany(batch);
}
return dataToInsert.map((node) => node._id);
return dataToInsert.map((node) => node?.[this.idKey] as string);
}
/**
@@ -185,27 +198,24 @@ export class AstraDBVectorStore implements VectorStore {
const similarities: number[] = [];
await cursor.forEach(async (row: Record<string, any>) => {
const id = row[this.idKey];
const embedding = row.$vector;
const similarity = row.$similarity;
const metadata = row[this.metadataKey];
const {
$vector: embedding,
$similarity: similarity,
[this.idKey]: id,
[this.contentKey]: content,
[this.metadataKey]: metadata = {},
...rest
} = row;
// Remove fields from content
delete row[this.idKey];
delete row.$similarity;
delete row.$vector;
delete row[this.metadataKey];
const content = this.contentKey
? row[this.contentKey]
: JSON.stringify(row);
const node = new Document({
id_: id,
text: content,
metadata: metadata ?? {},
embedding: embedding,
const node = metadataDictToNode(metadata, {
fallback: {
id,
text: content,
metadata,
...rest,
},
});
node.setContent(content);
ids.push(id);
similarities.push(similarity);
+19 -4
View File
@@ -36,7 +36,16 @@ export function nodeToMetadata(
return metadata;
}
export function metadataDictToNode(metadata: Metadata): BaseNode {
type MetadataDictToNodeOptions = {
// If the metadata doesn't contain node content, use this object as a fallback, for usage see
// AstraDBVectorStore.ts
fallback: Record<string, any>;
};
export function metadataDictToNode(
metadata: Metadata,
options?: MetadataDictToNodeOptions,
): BaseNode {
const {
_node_content: nodeContent,
_node_type: nodeType,
@@ -45,11 +54,17 @@ export function metadataDictToNode(metadata: Metadata): BaseNode {
ref_doc_id,
...rest
} = metadata;
let nodeObj;
if (!nodeContent) {
throw new Error("Node content not found in metadata.");
if (options?.fallback) {
nodeObj = options?.fallback;
} else {
throw new Error("Node content not found in metadata.");
}
} else {
nodeObj = JSON.parse(nodeContent);
nodeObj.metadata = rest;
}
const nodeObj = JSON.parse(nodeContent);
nodeObj.metadata = rest;
// Note: we're using the name of the class stored in `_node_type`
// and not the type attribute to reconstruct
+38 -1
View File
@@ -1,4 +1,12 @@
import { similarity, SimilarityType } from "../embeddings";
import { OpenAIEmbedding, similarity, SimilarityType } from "../embeddings";
import { mockEmbeddingModel } from "./utility/mockOpenAI";
// Mock the OpenAI getOpenAISession function during testing
jest.mock("../llm/open_ai", () => {
return {
getOpenAISession: jest.fn().mockImplementation(() => null),
};
});
describe("similarity", () => {
test("throws error on mismatched lengths", () => {
@@ -42,3 +50,32 @@ describe("similarity", () => {
);
});
});
describe("[OpenAIEmbedding]", () => {
let embedModel: OpenAIEmbedding;
beforeAll(() => {
let openAIEmbedding = new OpenAIEmbedding();
mockEmbeddingModel(openAIEmbedding);
embedModel = openAIEmbedding;
});
test("getTextEmbedding", async () => {
const embedding = await embedModel.getTextEmbedding("hello");
expect(embedding.length).toEqual(6);
});
test("getTextEmbeddings", async () => {
const texts = ["hello", "world"];
const embeddings = await embedModel.getTextEmbeddings(texts);
expect(embeddings.length).toEqual(1);
});
test("getTextEmbeddingsBatch", async () => {
const texts = ["hello", "world"];
const embeddings = await embedModel.getTextEmbeddingsBatch(texts);
expect(embeddings.length).toEqual(1);
});
});
@@ -90,6 +90,11 @@ export function mockEmbeddingModel(embedModel: OpenAIEmbedding) {
resolve([1, 0, 0, 0, 0, 0]);
});
});
jest.spyOn(embedModel, "getTextEmbeddings").mockImplementation(async (x) => {
return new Promise((resolve) => {
resolve([[1, 0, 0, 0, 0, 0]]);
});
});
jest.spyOn(embedModel, "getQueryEmbedding").mockImplementation(async (x) => {
return new Promise((resolve) => {
resolve([0, 1, 0, 0, 0, 0]);
@@ -0,0 +1,54 @@
import { BaseQueryEngine, BaseTool, ToolMetadata } from "../types";
export type QueryEngineToolParams = {
queryEngine: BaseQueryEngine;
metadata: ToolMetadata;
};
type QueryEngineCallParams = {
query: string;
};
const DEFAULT_NAME = "query_engine_tool";
const DEFAULT_DESCRIPTION =
"Useful for running a natural language query against a knowledge base and get back a natural language response.";
const DEFAULT_PARAMETERS = {
type: "object",
properties: {
query: {
type: "string",
description: "The query to search for",
},
},
required: ["query"],
};
export class QueryEngineTool implements BaseTool {
private queryEngine: BaseQueryEngine;
metadata: ToolMetadata;
constructor({ queryEngine, metadata }: QueryEngineToolParams) {
this.queryEngine = queryEngine;
this.metadata = {
name: metadata?.name ?? DEFAULT_NAME,
description: metadata?.description ?? DEFAULT_DESCRIPTION,
parameters: metadata?.parameters ?? DEFAULT_PARAMETERS,
};
}
async call(...args: QueryEngineCallParams[]): Promise<any> {
let queryStr: string;
if (args && args.length > 0) {
queryStr = String(args[0].query);
} else {
throw new Error(
"Cannot call query engine without specifying `input` parameter.",
);
}
const response = await this.queryEngine.query({ query: queryStr });
return response.response;
}
}
+1
View File
@@ -1,2 +1,3 @@
export * from "./QueryEngineTool";
export * from "./functionTool";
export * from "./types";
-7
View File
@@ -40,13 +40,6 @@ export interface BaseTool {
metadata: ToolMetadata;
}
/**
* A Tool that uses a QueryEngine.
*/
export interface QueryEngineTool extends BaseTool {
queryEngine: BaseQueryEngine;
}
/**
* An OutputParser is used to extract structured data from the raw output of the LLM.
*/
+2 -2
View File
@@ -12,8 +12,8 @@
"strict": true,
"lib": ["es2015", "dom"],
"target": "ES2015",
"resolveJsonModule": true,
"resolveJsonModule": true
},
"include": ["./src"],
"exclude": ["node_modules"],
"exclude": ["node_modules"]
}
+15 -1
View File
@@ -1,6 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import path from "path";
import { green } from "picocolors";
import { green, yellow } from "picocolors";
import { tryGitInit } from "./helpers/git";
import { isFolderEmpty } from "./helpers/is-folder-empty";
import { getOnline } from "./helpers/is-online";
@@ -12,6 +12,7 @@ import terminalLink from "terminal-link";
import type { InstallTemplateArgs } from "./helpers";
import { installTemplate } from "./helpers";
import { templatesDir } from "./helpers/dir";
import { toolsRequireConfig } from "./helpers/tools";
export type InstallAppArgs = Omit<
InstallTemplateArgs,
@@ -38,6 +39,7 @@ export async function createApp({
externalPort,
postInstallAction,
dataSource,
tools,
}: InstallAppArgs): Promise<void> {
const root = path.resolve(appPath);
@@ -82,6 +84,7 @@ export async function createApp({
externalPort,
postInstallAction,
dataSource,
tools,
};
if (frontend) {
@@ -114,6 +117,17 @@ export async function createApp({
console.log();
}
if (toolsRequireConfig(tools)) {
console.log(
yellow(
`You have selected tools that require configuration. Please configure them in the ${terminalLink(
"tools_config.json",
`file://${root}/tools_config.json`,
)} file.`,
),
);
}
console.log("");
console.log(`${green("Success!")} Created ${appName} at ${appPath}`);
console.log(
+2 -2
View File
@@ -10,7 +10,7 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"tsBuildInfoFile": "./lib/.tsbuildinfo"
},
"include": ["./**/*.ts"],
"include": ["./**/*.ts"]
}
+13 -2
View File
@@ -74,6 +74,15 @@ export async function runCreateLlama(
externalPort: number,
postInstallAction: TemplatePostInstallAction,
): Promise<CreateLlamaResult> {
const createLlama = path.join(
__dirname,
"..",
"output",
"package",
"dist",
"index.js",
);
const name = [
templateType,
templateFramework,
@@ -82,8 +91,8 @@ export async function runCreateLlama(
appType,
].join("-");
const command = [
"npx",
"create-llama",
"node",
createLlama,
name,
"--template",
templateType,
@@ -108,6 +117,8 @@ export async function runCreateLlama(
externalPort,
"--post-install-action",
postInstallAction,
"--tools",
"none",
].join(" ");
console.log(`running command '${command}' in ${cwd}`);
let appProcess = exec(command, {
+30 -3
View File
@@ -6,6 +6,7 @@ import terminalLink from "terminal-link";
import { copy } from "./copy";
import { templatesDir } from "./dir";
import { isPoetryAvailable, tryPoetryInstall } from "./poetry";
import { getToolConfig } from "./tools";
import { InstallTemplateArgs, TemplateVectorDB } from "./types";
interface Dependency {
@@ -128,6 +129,7 @@ export const installPythonTemplate = async ({
engine,
vectorDb,
dataSource,
tools,
postInstallAction,
}: Pick<
InstallTemplateArgs,
@@ -137,6 +139,7 @@ export const installPythonTemplate = async ({
| "engine"
| "vectorDb"
| "dataSource"
| "tools"
| "postInstallAction"
>) => {
console.log("\nInitializing Python project with template:", template, "\n");
@@ -162,20 +165,44 @@ export const installPythonTemplate = async ({
});
if (engine === "context") {
const enginePath = path.join(root, "app", "engine");
const compPath = path.join(templatesDir, "components");
let vectorDbDirName = vectorDb ?? "none";
const vectorDbDirName = vectorDb ?? "none";
const VectorDBPath = path.join(
compPath,
"vectordbs",
"python",
vectorDbDirName,
);
const enginePath = path.join(root, "app", "engine");
await copy("**", path.join(root, "app", "engine"), {
await copy("**", enginePath, {
parents: true,
cwd: VectorDBPath,
});
// Copy engine code
if (tools !== undefined && tools.length > 0) {
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "engines", "python", "agent"),
});
// Write tools_config.json
const configContent: Record<string, any> = {};
tools.forEach((tool) => {
configContent[tool] = getToolConfig(tool) ?? {};
});
const configFilePath = path.join(root, "tools_config.json");
await fs.writeFile(
configFilePath,
JSON.stringify(configContent, null, 2),
);
} else {
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "engines", "python", "chat"),
});
}
const dataSourceType = dataSource?.type;
if (dataSourceType !== undefined && dataSourceType !== "none") {
let loaderPath =
+33
View File
@@ -0,0 +1,33 @@
export type Tool = {
display: string;
name: string;
config?: Record<string, any>;
};
export const supportedTools: Tool[] = [
{
display: "Google Search (configuration required after installation)",
name: "google_search",
config: {
engine:
"Your search engine id, see https://developers.google.com/custom-search/v1/overview#prerequisites",
key: "Your search api key",
num: 2,
},
},
{
display: "Wikipedia",
name: "wikipedia",
},
];
export const getToolConfig = (name: string) => {
return supportedTools.find((tool) => tool.name === name)?.config;
};
export const toolsRequireConfig = (tools?: string[]): boolean => {
if (tools) {
return tools.some((tool) => getToolConfig(tool));
}
return false;
};
+1
View File
@@ -41,4 +41,5 @@ export interface InstallTemplateArgs {
vectorDb?: TemplateVectorDB;
externalPort?: number;
postInstallAction?: TemplatePostInstallAction;
tools?: string[];
}
+28
View File
@@ -11,6 +11,7 @@ import { createApp } from "./create-app";
import { getPkgManager } from "./helpers/get-pkg-manager";
import { isFolderEmpty } from "./helpers/is-folder-empty";
import { runApp } from "./helpers/run-app";
import { supportedTools } from "./helpers/tools";
import { validateNpmName } from "./helpers/validate-pkg";
import packageJson from "./package.json";
import { QuestionArgs, askQuestions, onPromptState } from "./questions";
@@ -146,6 +147,13 @@ const program = new Commander.Command(packageJson.name)
`
Select which vector database you would like to use, such as 'none', 'pg' or 'mongo'. The default option is not to use a vector database and use the local filesystem instead ('none').
`,
)
.option(
"--tools <tools>",
`
Specify the tools you want to use by providing a comma-separated list. For example, 'google_search,wikipedia'. Use 'none' to not using any tools.
`,
)
.allowUnknownOption()
@@ -156,6 +164,25 @@ if (process.argv.includes("--no-frontend")) {
if (process.argv.includes("--no-eslint")) {
program.eslint = false;
}
if (process.argv.includes("--tools")) {
if (program.tools === "none") {
program.tools = [];
} else {
program.tools = program.tools.split(",");
// Check if tools are available
const toolsName = supportedTools.map((tool) => tool.name);
program.tools.forEach((tool: string) => {
if (!toolsName.includes(tool)) {
console.error(
`Error: Tool '${tool}' is not supported. Supported tools are: ${toolsName.join(
", ",
)}`,
);
process.exit(1);
}
});
}
}
const packageManager = !!program.useNpm
? "npm"
@@ -256,6 +283,7 @@ async function run(): Promise<void> {
externalPort: program.externalPort,
postInstallAction: program.postInstallAction,
dataSource: program.dataSource,
tools: program.tools,
});
conf.set("preferences", preferences);
+1 -1
View File
@@ -24,7 +24,7 @@
"dev": "ncc build ./index.ts -w -o dist/",
"build": "npm run clean && ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
"lint": "eslint . --ignore-pattern dist",
"e2e": "pnpm pack --pack-destination ./output && npm i -g ./output/create-llama-*.tgz && playwright test",
"e2e": "playwright test",
"prepublishOnly": "cd ../../ && pnpm run build:release"
},
"devDependencies": {
+34 -3
View File
@@ -10,6 +10,7 @@ import { COMMUNITY_OWNER, COMMUNITY_REPO } from "./helpers/constant";
import { templatesDir } from "./helpers/dir";
import { getAvailableLlamapackOptions } from "./helpers/llama-pack";
import { getRepoRootFolders } from "./helpers/repo";
import { supportedTools, toolsRequireConfig } from "./helpers/tools";
export type QuestionArgs = Omit<
InstallAppArgs,
@@ -70,6 +71,7 @@ const defaults: QuestionArgs = {
type: "none",
config: {},
},
tools: [],
};
const handlers = {
@@ -214,7 +216,12 @@ export const askQuestions = async (
const hasOpenAiKey = program.openAiKey || process.env["OPENAI_API_KEY"];
const hasVectorDb = program.vectorDb && program.vectorDb !== "none";
if (!hasVectorDb && hasOpenAiKey) {
// Can run the app if all tools do not require configuration
if (
!hasVectorDb &&
hasOpenAiKey &&
!toolsRequireConfig(program.tools)
) {
actionChoices.push({
title:
"Generate code, install dependencies, and run the app (~2 min)",
@@ -412,9 +419,9 @@ export const askQuestions = async (
name: "model",
message: "Which model would you like to use?",
choices: [
{ title: "gpt-3.5-turbo", value: "gpt-3.5-turbo" },
{ title: "gpt-3.5-turbo", value: "gpt-3.5-turbo-0125" },
{ title: "gpt-4-turbo-preview", value: "gpt-4-turbo-preview" },
{ title: "gpt-4", value: "gpt-4" },
{ title: "gpt-4-1106-preview", value: "gpt-4-1106-preview" },
{
title: "gpt-4-vision-preview",
value: "gpt-4-vision-preview",
@@ -563,6 +570,30 @@ export const askQuestions = async (
}
}
if (
!program.tools &&
program.framework === "fastapi" &&
program.engine === "context"
) {
if (ciInfo.isCI) {
program.tools = getPrefOrDefault("tools");
} else {
const toolChoices = supportedTools.map((tool) => ({
title: tool.display,
value: tool.name,
}));
const { tools } = await prompts({
type: "multiselect",
name: "tools",
message:
"Would you like to build an agent using tools? If so, select the tools here, otherwise just press enter",
choices: toolChoices,
});
program.tools = tools;
preferences.tools = tools;
}
}
if (!program.openAiKey) {
const { key } = await prompts(
{
@@ -0,0 +1,50 @@
import os
from typing import Any, Optional
from llama_index.llms import LLM
from llama_index.agent import AgentRunner
from app.engine.tools import ToolFactory
from app.engine.index import get_index
from llama_index.agent import ReActAgent
from llama_index.tools.query_engine import QueryEngineTool
def create_agent_from_llm(
llm: Optional[LLM] = None,
**kwargs: Any,
) -> AgentRunner:
from llama_index.agent import OpenAIAgent, ReActAgent
from llama_index.llms.openai import OpenAI
from llama_index.llms.openai_utils import is_function_calling_model
if isinstance(llm, OpenAI) and is_function_calling_model(llm.model):
return OpenAIAgent.from_tools(
llm=llm,
**kwargs,
)
else:
return ReActAgent.from_tools(
llm=llm,
**kwargs,
)
def get_chat_engine():
tools = []
# Add query tool
index = get_index()
llm = index.service_context.llm
query_engine = index.as_query_engine(similarity_top_k=3)
query_engine_tool = QueryEngineTool.from_defaults(query_engine=query_engine)
tools.append(query_engine_tool)
# Add additional tools
tools += ToolFactory.from_env()
return create_agent_from_llm(
llm=llm,
tools=tools,
verbose=True,
)
@@ -0,0 +1,33 @@
import json
import importlib
from llama_index.tools.tool_spec.base import BaseToolSpec
from llama_index.tools.function_tool import FunctionTool
class ToolFactory:
@staticmethod
def create_tool(tool_name: str, **kwargs) -> list[FunctionTool]:
try:
module_name = f"llama_hub.tools.{tool_name}.base"
module = importlib.import_module(module_name)
tool_cls_name = tool_name.title().replace("_", "") + "ToolSpec"
tool_class = getattr(module, tool_cls_name)
tool_spec: BaseToolSpec = tool_class(**kwargs)
return tool_spec.to_tool_list()
except (ImportError, AttributeError) as e:
raise ValueError(f"Unsupported tool: {tool_name}") from e
except TypeError as e:
raise ValueError(
f"Could not create tool: {tool_name}. With config: {kwargs}"
) from e
@staticmethod
def from_env() -> list[FunctionTool]:
tools = []
with open("tools_config.json", "r") as f:
tool_configs = json.load(f)
for name, config in tool_configs.items():
tools += ToolFactory.create_tool(name, **config)
return tools
@@ -0,0 +1,7 @@
from app.engine.index import get_index
def get_chat_engine():
return get_index().as_chat_engine(
similarity_top_k=3, chat_mode="condense_plus_context"
)
@@ -9,7 +9,7 @@ from llama_index.vector_stores import MongoDBAtlasVectorSearch
from app.engine.context import create_service_context
def get_chat_engine():
def get_index():
service_context = create_service_context()
logger = logging.getLogger("uvicorn")
logger.info("Connecting to index from MongoDB...")
@@ -20,4 +20,4 @@ def get_chat_engine():
)
index = VectorStoreIndex.from_vector_store(store, service_context)
logger.info("Finished connecting to index from MongoDB.")
return index.as_chat_engine(similarity_top_k=5, chat_mode="condense_plus_context")
return index
@@ -1,15 +1,15 @@
import logging
import os
from app.engine.constants import STORAGE_DIR
from app.engine.context import create_service_context
from llama_index import (
StorageContext,
load_index_from_storage,
)
from app.engine.constants import STORAGE_DIR
from app.engine.context import create_service_context
def get_chat_engine():
def get_index():
service_context = create_service_context()
# check if storage already exists
if not os.path.exists(STORAGE_DIR):
@@ -22,4 +22,4 @@ def get_chat_engine():
storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
index = load_index_from_storage(storage_context, service_context=service_context)
logger.info(f"Finished loading index from {STORAGE_DIR}")
return index.as_chat_engine(similarity_top_k=5, chat_mode="condense_plus_context")
return index
@@ -6,11 +6,11 @@ from app.engine.context import create_service_context
from app.engine.utils import init_pg_vector_store_from_env
def get_chat_engine():
def get_index():
service_context = create_service_context()
logger = logging.getLogger("uvicorn")
logger.info("Connecting to index from PGVector...")
store = init_pg_vector_store_from_env()
index = VectorStoreIndex.from_vector_store(store, service_context)
logger.info("Finished connecting to index from PGVector.")
return index.as_chat_engine(similarity_top_k=5, chat_mode="condense_plus_context")
return index
@@ -29,7 +29,7 @@ async function getDataSource(llm: LLM) {
export async function createChatEngine(llm: LLM) {
const index = await getDataSource(llm);
const retriever = index.asRetriever({ similarityTopK: 5 });
const retriever = index.asRetriever({ similarityTopK: 3 });
return new ContextChatEngine({
chatModel: llm,
retriever,
@@ -35,7 +35,7 @@ async function getDataSource(llm: LLM) {
export async function createChatEngine(llm: LLM) {
const index = await getDataSource(llm);
const retriever = index.asRetriever();
retriever.similarityTopK = 5;
retriever.similarityTopK = 3;
return new ContextChatEngine({
chatModel: llm,
@@ -31,7 +31,7 @@ async function getDataSource(llm: LLM) {
export async function createChatEngine(llm: LLM) {
const index = await getDataSource(llm);
const retriever = index.asRetriever({ similarityTopK: 5 });
const retriever = index.asRetriever({ similarityTopK: 3 });
return new ContextChatEngine({
chatModel: llm,
retriever,
@@ -5,6 +5,6 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
},
"moduleResolution": "node"
}
}
@@ -5,7 +5,7 @@ from llama_index.chat_engine.types import BaseChatEngine
from llama_index.llms.base import ChatMessage
from llama_index.llms.types import MessageRole
from pydantic import BaseModel
from app.engine.index import get_chat_engine
from app.engine import get_chat_engine
chat_router = r = APIRouter()
@@ -0,0 +1,7 @@
from llama_index.chat_engine import SimpleChatEngine
from app.context import create_base_context
def get_chat_engine():
return SimpleChatEngine.from_defaults(service_context=create_base_context())
@@ -1,7 +0,0 @@
from llama_index.chat_engine import SimpleChatEngine
from app.context import create_base_context
def get_chat_engine():
return SimpleChatEngine.from_defaults(service_context=create_base_context())
@@ -13,6 +13,8 @@ llama-index = "^0.9.19"
pypdf = "^3.17.0"
python-dotenv = "^1.0.0"
docx2txt = "^0.8"
llama-hub = "^0.0.77"
wikipedia = "^1.4.0"
[build-system]
requires = ["poetry-core"]
@@ -5,6 +5,6 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
},
"moduleResolution": "node"
}
}
@@ -3,7 +3,7 @@ from typing import List
from fastapi.responses import StreamingResponse
from llama_index.chat_engine.types import BaseChatEngine
from app.engine.index import get_chat_engine
from app.engine import get_chat_engine
from fastapi import APIRouter, Depends, HTTPException, Request, status
from llama_index.llms.base import ChatMessage
from llama_index.llms.types import MessageRole
@@ -0,0 +1,7 @@
from llama_index.chat_engine import SimpleChatEngine
from app.context import create_base_context
def get_chat_engine():
return SimpleChatEngine.from_defaults(service_context=create_base_context())
@@ -1,7 +0,0 @@
from llama_index.chat_engine import SimpleChatEngine
from app.context import create_base_context
def get_chat_engine():
return SimpleChatEngine.from_defaults(service_context=create_base_context())
@@ -13,6 +13,8 @@ llama-index = "^0.9.19"
pypdf = "^3.17.0"
python-dotenv = "^1.0.0"
docx2txt = "^0.8"
llama-hub = "^0.0.77"
wikipedia = "^1.4.0"
[build-system]
requires = ["poetry-core"]
@@ -15,14 +15,14 @@
"incremental": true,
"plugins": [
{
"name": "next",
},
"name": "next"
}
],
"paths": {
"@/*": ["./*"],
"@/*": ["./*"]
},
"forceConsistentCasingInFileNames": true,
"forceConsistentCasingInFileNames": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"],
"exclude": ["node_modules"]
}
+3 -3
View File
@@ -10,14 +10,14 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"tsBuildInfoFile": "./lib/.tsbuildinfo"
},
"include": [
"create-app.ts",
"index.ts",
"./helpers",
"questions.ts",
"package.json",
"package.json"
],
"exclude": ["dist"],
"exclude": ["dist"]
}
+1
View File
@@ -14,6 +14,7 @@ module.exports = {
"ASTRA_DB_APPLICATION_TOKEN",
"ASTRA_DB_ENDPOINT",
"ASTRA_DB_NAMESPACE",
"AZURE_OPENAI_KEY",
"AZURE_OPENAI_ENDPOINT",
+1315 -1368
View File
File diff suppressed because it is too large Load Diff
+8 -8
View File
@@ -10,24 +10,24 @@
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"incremental": true,
"composite": true,
"composite": true
},
"files": [],
"references": [
{
"path": "./apps/docs/tsconfig.json",
"path": "./apps/docs/tsconfig.json"
},
{
"path": "./packages/core",
"path": "./packages/core"
},
{
"path": "./packages/create-llama",
"path": "./packages/create-llama"
},
{
"path": "./packages/create-llama/e2e",
"path": "./packages/create-llama/e2e"
},
{
"path": "./examples",
},
],
"path": "./examples"
}
]
}