Skip to content

Commit

Permalink
added await in examples so loaders can complete
Browse files Browse the repository at this point in the history
  • Loading branch information
adhityan committed May 11, 2024
1 parent 2fd0335 commit 4d5f874
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 36 deletions.
6 changes: 3 additions & 3 deletions examples/chroma/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const llmApplication = await new RAGApplicationBuilder()
.setVectorDb(new ChromaDb({ url: 'http://localhost:8000' }))
.build();

llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));
await llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
await llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
await llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));

console.log((await llmApplication.query('Who founded Tesla?')).result);
// The founder of Tesla is Elon Musk. He co-founded the company with JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003. Elon Musk is also the CEO of SpaceX and Neuralink.
Expand Down
6 changes: 3 additions & 3 deletions examples/cohere/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const llmApplication = await new RAGApplicationBuilder()
.setVectorDb(new HNSWDb())
.build();

llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));
await llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
await llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
await llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));

console.log((await llmApplication.query('Who founded Tesla?')).result);
// The founder of Tesla is Elon Musk. He co-founded the company with JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003. Elon Musk is also the CEO of SpaceX and Neuralink.
Expand Down
25 changes: 17 additions & 8 deletions examples/ollama/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { RAGApplicationBuilder, WebLoader, YoutubeLoader, SitemapLoader, Ollama, AdaEmbeddings } from '../../../src/index.js';
import {
RAGApplicationBuilder,
WebLoader,
YoutubeLoader,
SitemapLoader,
Ollama,
AdaEmbeddings,
} from '../../../src/index.js';
import { HNSWDb } from '../../../src/vectorDb/hnswlib-db.js';

const modelName = process.argv[2] || 'llama3';

const llmApplication = await new RAGApplicationBuilder()
.setEmbeddingModel(new AdaEmbeddings())
.setModel(new Ollama({
modelName: modelName,
baseUrl: 'http://localhost:11434'
}))
.setModel(
new Ollama({
modelName: modelName,
baseUrl: 'http://localhost:11434',
}),
)
.setSearchResultCount(30)
.setVectorDb(new HNSWDb())
.build();

llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));
await llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
await llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
await llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));

console.log((await llmApplication.query('Who founded Tesla?')).result);
// The founder of Tesla is Elon Musk. He co-founded the company with JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003. Elon Musk is also the CEO of SpaceX and Neuralink.
Expand Down
6 changes: 3 additions & 3 deletions examples/pinecone/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const llmApplication = await new RAGApplicationBuilder()
)
.build();

llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));
await llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
await llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
await llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));

console.log((await llmApplication.query('Who founded Tesla?')).result);
// The founder of Tesla is Elon Musk. He co-founded the company with JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003. Elon Musk is also the CEO of SpaceX and Neuralink.
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"private": true,
"scripts": {
"start": "tsc && node dist/examples/simple/src/index.js"
"start": "tsc && DEBUG=embedjs:* node dist/examples/simple/src/index.js"
},
"author": "",
"license": "ISC",
Expand All @@ -13,6 +13,6 @@
"ts-node": "^10.9.2"
},
"devDependencies": {
"@types/node": "^20.11.24"
"@types/node": "^20.12.11"
}
}
6 changes: 3 additions & 3 deletions examples/simple/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { HNSWDb } from '../../../src/vectorDb/hnswlib-db.js';

const llmApplication = await new RAGApplicationBuilder().setSearchResultCount(30).setVectorDb(new HNSWDb()).build();

llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));
await llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
await llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
await llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));

console.log((await llmApplication.query('Who founded Tesla?')).result);
// The founder of Tesla is Elon Musk. He co-founded the company with JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003. Elon Musk is also the CEO of SpaceX and Neuralink.
Expand Down
19 changes: 10 additions & 9 deletions examples/vertexai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ import 'dotenv/config';

import { RAGApplicationBuilder, WebLoader, YoutubeLoader, SitemapLoader } from '../../../src/index.js';
import { HNSWDb } from '../../../src/vectorDb/hnswlib-db.js';
import { VertexAI,GeckoEmbedding } from '../../../src/index.js';
import { VertexAI, GeckoEmbedding } from '../../../src/index.js';

const llmApplication = await new RAGApplicationBuilder()
.setModel(new VertexAI({ modelName: 'gemini-1.5-pro-preview-0409'}))
.setModel(new VertexAI({ modelName: 'gemini-1.5-pro-preview-0409' }))
.setEmbeddingModel(new GeckoEmbedding())
.setSearchResultCount(30)
.setVectorDb(new HNSWDb()).build();
.setVectorDb(new HNSWDb())
.build();

llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));
await llmApplication.addLoader(new YoutubeLoader({ videoIdOrUrl: 'pQiT2U5E9tI' }));
await llmApplication.addLoader(new SitemapLoader({ url: 'https://tesla-info.com/sitemap.xml' }));
await llmApplication.addLoader(new WebLoader({ url: 'https://en.wikipedia.org/wiki/Tesla,_Inc.' }));

let question = 'Who founded Tesla?';
console.log("[QUESTION]", question);
console.log('[QUESTION]', question);
console.log((await llmApplication.query(question)).result);

question = 'Tell me about the history of Tesla?';
console.log("[QUESTION]", question);
console.log('[QUESTION]', question);
console.log((await llmApplication.query(question)).result);

question = 'What cars does Tesla have';
console.log("[QUESTION]", question);
console.log('[QUESTION]', question);
console.log((await llmApplication.query(question)).result);
7 changes: 2 additions & 5 deletions src/vectorDb/hnswlib-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ export class HNSWDb implements BaseDb {
private docCount: number;
private docMap: Map<number, { pageContent: string; metadata: Metadata<Record<string, string | number | boolean>> }>;

constructor() {
this.docCount = 0;
this.docMap = new Map();
}

async init({ dimensions }: { dimensions: number }) {
this.index = await new HNSWLib.HierarchicalNSW('cosine', dimensions);
this.index.initIndex(0);
this.docMap = new Map();
this.docCount = 0;
}

async insertChunks(chunks: EmbeddedChunk[]): Promise<number> {
Expand Down

0 comments on commit 4d5f874

Please sign in to comment.