Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: abstracting the creation of a vector store #47

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
72 changes: 72 additions & 0 deletions src/common/model/createMemoryStore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { CreateMemoryStore } from './createMemoryStore';
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';

describe('CreateMemoryStore function', () => {
const initialFiles = [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this file in the testFiles and then read it here. Makes the code a lot nicer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

{
pageContent: '**LOGAF Level 1 - src/test/cases/.cache/faee919bf4f6a5b85a44b1a8eacc0ca24223d6c4033a2b4c52bc79bb8e1bc1bb.ts**\n' +
'\n' +
'The code exposes a secret key which is a serious security issue. Never log sensitive information like API keys, passwords, or secrets. Consider using environment variables to store such sensitive information. For example:\n' +
'\n' +
'```typescript\n' +
'const secretKey = process.env.SECRET_KEY;\n' +
'\n' +
'function exposeSecret() {\n' +
' console.log(`The secret key is: ${secretKey}`);\n' +
'}\n' +
'\n' +
'exposeSecret();\n' +
'```\n' +
'\n' +
'In this case, the secret key is stored in an environment variable named `SECRET_KEY`. Remember to add `SECRET_KEY` to your `.env` file and never commit the `.env` file to the repository.\n' +
'\n' +
'🔑❌🔒\n',
metadata: {
source: '/Users/sebo/Desktop/aleios/code-review-gpt/src/test/cases/snapshots/exposed-secret.md'
}
},
{
pageContent: '**LOGAF Level 1 - src/test/cases/.cache/5519e4e1b45143a504ec259a5d911dea930372c19b3f56b51afab53f55339b56.ts**\n' +
'\n' +
'The function `nestedLoops` has too many nested loops which can lead to performance issues and is hard to read and maintain. Consider refactoring the code to reduce the number of nested loops. If the logic of the code allows, you could use recursion or divide the task into smaller functions. Here is an example of how you could refactor this code using recursion:\n' +
'\n' +
'```\n' +
'function recursiveLoop(depth, maxDepth, maxCount) {\n' +
' if (depth === maxDepth) {\n' +
' console.log(...arguments);\n' +
' } else {\n' +
' for (let i = 0; i < maxCount; i++) {\n' +
' recursiveLoop(i, depth + 1, maxDepth, maxCount);\n' +
' }\n' +
' }\n' +
'}\n' +
'\n' +
'recursiveLoop(0, 10, 10);\n' +
'```\n' +
'\n' +
'This code does the same thing as the original code but is much easier to read and maintain.\n' +
'\n' +
'🔄🐌🔧\n',
metadata: {
source: '/Users/sebo/Desktop/aleios/code-review-gpt/src/test/cases/snapshots/too-many-nested-loops.md'
}
},
];
it('Checks that the CreateMemoryStore function returns a MemoryVectorStore object', () => {
const result = CreateMemoryStore(initialFiles);

expect(result).toBeInstanceOf(Promise<MemoryVectorStore>);
});

it('Checks if the function provides the required functionality', async () => {
const result = await CreateMemoryStore(initialFiles);

const expectedResult = await MemoryVectorStore.fromDocuments(
initialFiles,
new OpenAIEmbeddings(),
{}
);
expect(result).toEqual(expectedResult);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also can you check that a simularitySearchWithScore returns a number. Thats the most used method on this class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

});
});
13 changes: 13 additions & 0 deletions src/common/model/createMemoryStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Document } from 'langchain/dist/document';
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';

export const CreateMemoryStore = async (initialFiles: Document<Record<string, any>>[]): Promise<MemoryVectorStore> => {
const embeddingModel = new OpenAIEmbeddings();

return await MemoryVectorStore.fromDocuments(
initialFiles,
embeddingModel,
{}
);
}
6 changes: 2 additions & 4 deletions src/review/prompt/makeSlimmedFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getChangedLines } from "./fileLines/getChangedLines";
import { getLanguageOfFile } from "./getLanguageOfFile";
import { slimmedContextPrompt } from "./prompts";
import { ReviewFile } from "./types";
import { CreateMemoryStore } from '../../common/model/createMemoryStore';

export const makeSlimmedFile = async (
file: ReviewFile,
Expand Down Expand Up @@ -40,10 +41,7 @@ export const makeSlimmedFile = async (
const doc = await splitter.createDocuments([changedLines]);

// Generate memory store with the whole file
const fileEmbeddings = await MemoryVectorStore.fromDocuments(
doc,
new OpenAIEmbeddings()
);
const fileEmbeddings = await CreateMemoryStore(doc);

// Make a similarity search between the embeddings of the whole file
// and the embeddings of the changed lines.
Expand Down
8 changes: 3 additions & 5 deletions src/test/load/loadSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { CreateMemoryStore } from '../../common/model/createMemoryStore';

/**
* Load a snapshot for a test from a file.
Expand All @@ -28,9 +29,6 @@ export const loadSnapshots = async (shapshotsDir: string) => {
return loadSnapshot(path.join(shapshotsDir, snapshotFile));
})
);
return MemoryVectorStore.fromDocuments(
snapshots.flat(),
new OpenAIEmbeddings(),
{}
);

return await CreateMemoryStore(snapshots.flat());
};