Skip to content

Commit

Permalink
updating rag api
Browse files Browse the repository at this point in the history
  • Loading branch information
domsteil committed Mar 8, 2024
1 parent 81ed925 commit 021c520
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions src/backend/services/rag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,40 @@ const serviceContext = serviceContextFromDefaults({
chunkSize: 4096,
});

// Create Vector Store Index
const indexCreation = async () => {
var index = await VectorStoreIndex.fromDocuments(documentsContractsMetadata, {
serviceContext: serviceContext,
});

async function indexCreation() {
var index = await VectorStoreIndex.fromDocuments(documentsContractsMetadata, { serviceContext });
console.log('Index:', index);

return index;
};

const index = await indexCreation();
}

// Load Contract ABIs
const loadContractABIs = async (): Promise<any> => {
async function loadContractABIs() {
try {
console.log('Retrieving Contract ABIs...');

const index = await indexCreation();
const retriever = index.asRetriever();

console.log('Retriever:', retriever);

const retrievedContracts = await retriever.retrieve(NLQ);

// Format the Retrieved Contracts based on the results
const formattedContracts = retrievedContracts.map((contract) => {
return `The Contract: ${contract.node.text}\nThe Contract's ABI:\n${contract.node.metadata.abis}`;
return `The Contract: ${contract.node.metadata}\nThe Contract's ABI:\n${contract.node.metadata.abis}`;
});

console.log('Formatted Contracts:', formattedContracts);

// Create In Memory Vector Store
return createInMemoryVectorStore(formattedContracts);

} catch (error) {
console.error('Error loading contracts:', error);
throw error;
}
};
}

// Create In Memory Vector Store
const createInMemoryVectorStore = async (contracts: string[]): Promise<any> => {
console.log('Creating In Memory Vector Store...');

// Metadata
const metadata = [];
const metadata = [] as any;

// Embeddings
const embeddings = new OllamaEmbeddings({ model: 'llama2:7b' });
Expand All @@ -141,11 +130,17 @@ const createInMemoryVectorStore = async (contracts: string[]): Promise<any> => {

console.log('Loading Contract ABIs...');

// Load Contract ABIs
const abiInMemoryVectorStore = await loadContractABIs();
export async function contractsRetreival() {

// Load Contract ABIs
const abiInMemoryVectorStore = await loadContractABIs();

// Create ABI Retriever
const contractAbiRetriever = await abiInMemoryVectorStore.asRetriever({ k: TOP_K_ABIS });

// Create ABI Retriever
export const contractAbiRetriever = await abiInMemoryVectorStore.asRetriever({ k: TOP_K_ABIS });
return contractAbiRetriever;

}

// Load Metamask Examples
const loadMetamaskExamples = async (): Promise<any> => {
Expand Down Expand Up @@ -173,10 +168,16 @@ const createFaissStoreFromExamples = async (examples: any[]): Promise<any> => {
return FaissStore.fromDocuments(examples, embeddings);
};

// Metamask Examples
const metamaskExamplesInMemoryVectorStore = await loadMetamaskExamples();
export async function metamaskExamplesRetrieval() {

// Retrieval Engine
export const metamaskExamplesRetriever = metamaskExamplesInMemoryVectorStore.asRetriever({
k: TOP_K_EXAMPLES,
});
// Metamask Examples
const metamaskExamplesInMemoryVectorStore = await loadMetamaskExamples();

// Retrieval Engine
const metamaskExamplesRetriever = metamaskExamplesInMemoryVectorStore.asRetriever({
k: TOP_K_EXAMPLES,
});

return metamaskExamplesRetriever;

};

0 comments on commit 021c520

Please sign in to comment.