Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

refactor: camelCase names #22

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ const { App } = require("embedchain");

//Run the app commands inside an async function only
async function testApp() {
const naval_chat_bot = await App();
const navalChatBot = await App();

// Embed Online Resources
await naval_chat_bot.add("web_page", "https://nav.al/feedback");
await naval_chat_bot.add("web_page", "https://nav.al/agi");
await naval_chat_bot.add(
await navalChatBot.add("web_page", "https://nav.al/feedback");
await navalChatBot.add("web_page", "https://nav.al/agi");
await navalChatBot.add(
"pdf_file",
"https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf"
);

// Embed Local Resources
await naval_chat_bot.add_local("qna_pair", [
await navalChatBot.addLocal("qna_pair", [
"Who is Naval Ravikant?",
"Naval Ravikant is an Indian-American entrepreneur and investor.",
]);

const result = await naval_chat_bot.query(
const result = await navalChatBot.query(
"What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?"
);
console.log(result);
Expand Down Expand Up @@ -100,23 +100,23 @@ dotenv.config();
const { App } = require("embedchain");

async function testApp() {
const naval_chat_bot = await App();
const navalChatBot = await App();

// Embed Online Resources
await naval_chat_bot.add("web_page", "https://nav.al/feedback");
await naval_chat_bot.add("web_page", "https://nav.al/agi");
await naval_chat_bot.add(
await navalChatBot.add("web_page", "https://nav.al/feedback");
await navalChatBot.add("web_page", "https://nav.al/agi");
await navalChatBot.add(
"pdf_file",
"https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf"
);

// Embed Local Resources
await naval_chat_bot.add_local("qna_pair", [
await navalChatBot.addLocal("qna_pair", [
"Who is Naval Ravikant?",
"Naval Ravikant is an Indian-American entrepreneur and investor.",
]);

const result = await naval_chat_bot.query(
const result = await navalChatBot.query(
"What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?"
);
console.log(result);
Expand Down Expand Up @@ -161,7 +161,7 @@ await app.add("web_page", "a_valid_web_page_url");
To supply your own QnA pair, use the data_type as `qna_pair` and enter a tuple. Eg:

```javascript
await app.add_local("qna_pair", ["Question", "Answer"]);
await app.addLocal("qna_pair", ["Question", "Answer"]);
```

### More Formats coming soon
Expand Down
4 changes: 2 additions & 2 deletions embedchain/__tests__/readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jest.mock('../embedchain', () => {
EmbedChainApp: jest.fn().mockImplementation(() => {
return {
add: mockAdd,
add_local: mockAddLocal,
addLocal: mockAddLocal,
query: mockQuery,
};
}),
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Test App', () => {
);

// Embed Local Resources
await navalChatBot.add_local('qna_pair', [
await navalChatBot.addLocal('qna_pair', [
'Who is Naval Ravikant?',
'Naval Ravikant is an Indian-American entrepreneur and investor.',
]);
Expand Down
2 changes: 1 addition & 1 deletion embedchain/chunkers/BaseChunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BaseChunker {
this.textSplitter = textSplitter;
}

async create_chunks(loader: BaseLoader, url: Input): Promise<ChunkResult> {
async createChunks(loader: BaseLoader, url: Input): Promise<ChunkResult> {
const documents: ChunkResult['documents'] = [];
const ids: ChunkResult['ids'] = [];
const datas: LoaderResult = await loader.loadData(url);
Expand Down
76 changes: 36 additions & 40 deletions embedchain/embedchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ const configuration = new Configuration({
const openai = new OpenAIApi(configuration);

class EmbedChain {
db_client: any;
dbClient: any;

// TODO: Definitely assign
collection!: Collection;

user_asks: [DataType, Input][] = [];
userAsks: [DataType, Input][] = [];

init_app: Promise<void>;
initApp: Promise<void>;

constructor(db: BaseVectorDB | null = null) {
if (!db) {
this.init_app = this.setup_chroma();
this.initApp = this.setupChroma();
} else {
this.init_app = this.setup_other(db);
this.initApp = this.setupOther(db);
}
}

async setup_chroma(): Promise<void> {
async setupChroma(): Promise<void> {
const db = new ChromaDB();
await db.init_db;
this.db_client = db.client;
await db.initDb;
this.dbClient = db.client;
if (db.collection) {
this.collection = db.collection;
} else {
Expand All @@ -55,52 +55,48 @@ class EmbedChain {
}
}

async setup_other(db: BaseVectorDB): Promise<void> {
await db.init_db;
async setupOther(db: BaseVectorDB): Promise<void> {
await db.initDb;
// TODO: Figure out how we can initialize an unknown database.
// this.db_client = db.client;
// this.dbClient = db.client;
// this.collection = db.collection;
this.user_asks = [];
this.userAsks = [];
}

static getLoader(data_type: DataType) {
static getLoader(dataType: DataType) {
const loaders: { [t in DataType]: BaseLoader } = {
pdf_file: new PdfFileLoader(),
web_page: new WebPageLoader(),
qna_pair: new LocalQnaPairLoader(),
};
return loaders[data_type];
return loaders[dataType];
}

static getChunker(data_type: DataType) {
static getChunker(dataType: DataType) {
const chunkers: { [t in DataType]: BaseChunker } = {
pdf_file: new PdfFileChunker(),
web_page: new WebPageChunker(),
qna_pair: new QnaPairChunker(),
};
return chunkers[data_type];
return chunkers[dataType];
}

public async add(data_type: DataType, url: RemoteInput) {
const loader = EmbedChain.getLoader(data_type);
const chunker = EmbedChain.getChunker(data_type);
this.user_asks.push([data_type, url]);
await this.load_and_embed(loader, chunker, url);
public async add(dataType: DataType, url: RemoteInput) {
const loader = EmbedChain.getLoader(dataType);
const chunker = EmbedChain.getChunker(dataType);
this.userAsks.push([dataType, url]);
await this.loadAndEmbed(loader, chunker, url);
}

public async add_local(data_type: DataType, content: LocalInput) {
const loader = EmbedChain.getLoader(data_type);
const chunker = EmbedChain.getChunker(data_type);
this.user_asks.push([data_type, content]);
await this.load_and_embed(loader, chunker, content);
public async addLocal(dataType: DataType, content: LocalInput) {
const loader = EmbedChain.getLoader(dataType);
const chunker = EmbedChain.getChunker(dataType);
this.userAsks.push([dataType, content]);
await this.loadAndEmbed(loader, chunker, content);
}

protected async load_and_embed(
loader: any,
chunker: BaseChunker,
src: Input
) {
const embeddingsData = await chunker.create_chunks(loader, src);
protected async loadAndEmbed(loader: any, chunker: BaseChunker, src: Input) {
const embeddingsData = await chunker.createChunks(loader, src);
let { documents, ids, metadatas } = embeddingsData;

const existingDocs = await this.collection.get({ ids });
Expand Down Expand Up @@ -159,18 +155,18 @@ class EmbedChain {
);
}

protected async retrieveFromDatabase(input_query: string) {
protected async retrieveFromDatabase(inputQuery: string) {
const result = await this.collection.query({
nResults: 1,
queryTexts: [input_query],
queryTexts: [inputQuery],
});
const resultFormatted = await EmbedChain.formatResult(result);
const content = resultFormatted[0][0].pageContent;
return content;
}

static generatePrompt(input_query: string, context: any) {
const prompt = `Use the following pieces of context to answer the query at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n${context}\nQuery: ${input_query}\nHelpful Answer:`;
static generatePrompt(inputQuery: string, context: any) {
const prompt = `Use the following pieces of context to answer the query at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n${context}\nQuery: ${inputQuery}\nHelpful Answer:`;
return prompt;
}

Expand All @@ -179,9 +175,9 @@ class EmbedChain {
return answer;
}

public async query(input_query: string) {
const context = await this.retrieveFromDatabase(input_query);
const prompt = EmbedChain.generatePrompt(input_query, context);
public async query(inputQuery: string) {
const context = await this.retrieveFromDatabase(inputQuery);
const prompt = EmbedChain.generatePrompt(inputQuery, context);
const answer = await EmbedChain.getAnswerFromLlm(prompt);
return answer;
}
Expand All @@ -190,7 +186,7 @@ class EmbedChain {
class EmbedChainApp extends EmbedChain {
// The EmbedChain app.
// Has two functions: add and query.
// adds(data_type, url): adds the data from the given URL to the vector db.
// adds(dataType, url): adds the data from the given URL to the vector db.
// query(query): finds answer to the given query using vector database and LLM.
}

Expand Down
2 changes: 1 addition & 1 deletion embedchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { EmbedChainApp } from './embedchain';

export const App = async () => {
const app = new EmbedChainApp();
await app.init_app;
await app.initApp;
return app;
};
6 changes: 3 additions & 3 deletions embedchain/vectordb/BaseVectorDb.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class BaseVectorDB {
init_db: Promise<void>;
initDb: Promise<void>;

constructor() {
this.init_db = this.getClientAndCollection();
this.initDb = this.getClientAndCollection();
}

// eslint-disable-next-line class-methods-use-this
protected async getClientAndCollection(): Promise<void> {
throw new Error('get_client_and_collection() method is not implemented');
throw new Error('getClientAndCollection() method is not implemented');
}
}

Expand Down