Skip to content

Commit

Permalink
only export getDocumentVectors
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic committed Feb 2, 2024
1 parent 4940cc4 commit a634430
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
7 changes: 4 additions & 3 deletions backend/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function getDocumentMetas(folder: string) {
}

// store vectorised documents for each level as array
const vectorisedDocuments = (() => {
const documentVectors = (() => {
let docs: DocumentsVector[] = [];
return {
get: () => docs,
Expand All @@ -95,6 +95,7 @@ const vectorisedDocuments = (() => {
},
};
})();
const getDocumentVectors = documentVectors.get;

// create and store the document vectors for each level
async function initDocumentVectors() {
Expand All @@ -120,7 +121,7 @@ async function initDocumentVectors() {
docVector,
});
}
vectorisedDocuments.set(docVectors);
documentVectors.set(docVectors);
console.debug(
`Initialised document vectors for each level. count=${docVectors.length}`
);
Expand All @@ -131,5 +132,5 @@ export {
getLevelDocuments,
getSandboxDocumentMetas,
initDocumentVectors,
vectorisedDocuments,
getDocumentVectors,
};
4 changes: 2 additions & 2 deletions backend/src/langchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChatOpenAI } from 'langchain/chat_models/openai';
import { OpenAI } from 'langchain/llms/openai';
import { PromptTemplate } from 'langchain/prompts';

import { vectorisedDocuments } from './document';
import { getDocumentVectors } from './document';
import { CHAT_MODELS, ChatAnswer } from './models/chat';
import { PromptEvaluationChainReply, QaChainReply } from './models/langchain';
import { LEVEL_NAMES } from './models/level';
Expand Down Expand Up @@ -39,7 +39,7 @@ function getChatModel() {

function initQAModel(level: LEVEL_NAMES, Prompt: string) {
const openAIApiKey = getOpenAIKey();
const documentVectors = vectorisedDocuments.get()[level].docVector;
const documentVectors = getDocumentVectors()[level].docVector;
// use gpt-4 if avaliable to apiKey
const modelName = getChatModel();

Expand Down
30 changes: 8 additions & 22 deletions backend/test/unit/langchain.ts/initialiseQAModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RetrievalQAChain } from 'langchain/chains';
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { PromptTemplate } from 'langchain/prompts';

import { getDocumentVectors } from '@src/document';
import { queryDocuments } from '@src/langchain';
import { LEVEL_NAMES } from '@src/models/level';
import { getOpenAIKey } from '@src/openai';
Expand Down Expand Up @@ -59,28 +60,13 @@ jest.mock('@src/openai', () => {
};
});

const mockGetVectorisedDocuments = jest.fn();
jest.mock('@src/langchain', () => {
const originalModule =
jest.requireActual<typeof import('@src/langchain')>('@src/langchain');
return {
...originalModule,
vectorisedDocuments: {
get: () => mockGetVectorisedDocuments(),
set: jest.fn(),
},
};
});
mockGetVectorisedDocuments.mockReturnValue([]);

jest.mock('@src/document', () => ({
vectorisedDocuments: {
get: jest
.fn()
.mockReturnValue([{ docVector: { asRetriever: () => 'retriever' } }]),
set: jest.fn(),
},
}));
jest.mock('@src/document');
const mockGetDocumentVectors = getDocumentVectors as unknown as jest.Mock<
() => { docVector: { asRetriever: () => string } }[]
>;
mockGetDocumentVectors.mockReturnValue([
{ docVector: { asRetriever: () => 'retriever' } },
]);

beforeEach(() => {
mockFromLLM.mockImplementation(() => mockRetrievalQAChain); // this is weird
Expand Down

0 comments on commit a634430

Please sign in to comment.