Skip to content

Commit

Permalink
fix to pass bug finder test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Liu committed Dec 7, 2023
1 parent 88d19ce commit 05733f5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 88 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ module.exports = {
},
"rules": {
},
"globals": {
"generateRelatedWords": true,
"scoreContent": true,
"rankContents": true,
"getTopContent": true,
"getEmbeddings": true,
"combinedScore": true
},
'ignorePatterns': ["**/node_modules/**"]
}
90 changes: 45 additions & 45 deletions content_ranker/content_ranker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const OpenAI = require('langchain/llms/openai').OpenAI;
const PromptTemplate = require('langchain/prompts').PromptTemplate;
const OpenAIEmbeddings=require('langchain/embeddings/openai').OpenAIEmbeddings;
const OpenAIEmbeddings = require('langchain/embeddings/openai').OpenAIEmbeddings;
const math = require('mathjs');

/* Create instance for embeddings */
Expand All @@ -17,15 +17,15 @@ const llm = new OpenAI({
* @return {Promise<Array>} A promise that resolves to an array of embeddings.
*/
async function getEmbeddings(text) {
try {
const response = await embeddings.embedQuery(text);
return response;
} catch (error) {
console.error('Error fetching embeddings, using fallback:', error);
// Fallback: return null
return null;
}
try {
const response = await embeddings.embedQuery(text);
return response;
} catch (error) {
console.error('Error fetching embeddings, using fallback:', error);
// Fallback: return null
return null;
}

}

/**
Expand All @@ -46,27 +46,27 @@ function cosineSimilarity(vecA, vecB) {
* @return {Promise<number>} A promise that resolves to the combined score.
*/
async function combinedScore(content, preferencesVector, generatedWords) {
try {
if (!preferencesVector) {
return fallbackScore(generatedWords, content) * 100;
}
const contentVector = await getEmbeddings(content);
const cosineScore = cosineSimilarity(preferencesVector, contentVector);
const originalScore = scoreContent(generatedWords, content);
return (cosineScore * 0.5 + originalScore * 0.5) * 100;
} catch (error) {
console.error('Error calculating combined score:', error);
throw error;
try {
if (!preferencesVector) {
return fallbackScore(generatedWords, content) * 100;
}
const contentVector = await getEmbeddings(content);
const cosineScore = cosineSimilarity(preferencesVector, contentVector);
const originalScore = scoreContent(generatedWords, content);
return (cosineScore * 0.5 + originalScore * 0.5) * 100;
} catch (error) {
console.error('Error calculating combined score:', error);
throw error;
}

}

function fallbackScore(generatedWords, content) {
let score = 0;
generatedWords.forEach(word => {
score += countWordOccurrences(word, content);
});
return score;
let score = 0;
generatedWords.forEach(word => {
score += countWordOccurrences(word, content);
});
return score;
}
/**
* Generates related words based on given preferences.
Expand All @@ -75,24 +75,24 @@ function fallbackScore(generatedWords, content) {
*/
async function generateRelatedWords(preferences) {

try {
const prompt = PromptTemplate.fromTemplate("I want to ranking the content by the user preferences, so I will give you 3 preferences words from a user and you generate 10 related words for each preference word and i will count them in the content to do the count.Return the generated 30 words without the original words without any other text and comma-separated. Here are the preferences words:{preference}");
const formattedPrompt = await prompt.format({preference: preferences});
const llmResult = await llm.predict(formattedPrompt);
const generatedWords = llmResult.split(',').map(word => word.trim());
console.log("generatedWords", generatedWords);
return {
originalWords: preferences.split(',').map(word => word.trim()),
generatedWords: generatedWords,
};
} catch (error) {
console.error('Error generating related words, using fallback:', error);
// Fallback: return original words as generated words
return {
originalWords: preferences.split(',').map(word => word.trim()),
generatedWords: preferences.split(',').map(word => word.trim()),
};
}
try {
const prompt = PromptTemplate.fromTemplate("I want to ranking the content by the user preferences, so I will give you 3 preferences words from a user and you generate 10 related words for each preference word and i will count them in the content to do the count.Return the generated 30 words without the original words without any other text and comma-separated. Here are the preferences words:{preference}");
const formattedPrompt = await prompt.format({ preference: preferences });
const llmResult = await llm.predict(formattedPrompt);
const generatedWords = llmResult.split(',').map(word => word.trim());
console.log("generatedWords", generatedWords);
return {
originalWords: preferences.split(',').map(word => word.trim()),
generatedWords: generatedWords,
};
} catch (error) {
console.error('Error generating related words, using fallback:', error);
// Fallback: return original words as generated words
return {
originalWords: preferences.split(',').map(word => word.trim()),
generatedWords: preferences.split(',').map(word => word.trim()),
};
}

}
/**
Expand Down Expand Up @@ -157,7 +157,7 @@ async function rankContents(contents, preferencesVector, generatedWords) {
*/
async function getTopContent(preferences, contents) {
console.log('Generating related words based on client preferences...');
const {generatedWords} = await generateRelatedWords(preferences);
const { generatedWords } = await generateRelatedWords(preferences);
console.log('Vectorizing preferences...');
const preferencesVector = await getEmbeddings(preferences);
console.log('Done');
Expand Down
84 changes: 41 additions & 43 deletions test/content_ranker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ function mockApiFailure() {

describe("Testing functions from content_ranker.js", () => {
// Mocking OpenAIEmbeddings and OpenAI methods

mockApiSuccess()
const {
generateRelatedWords,
scoreContent,
rankContents,
getTopContent,
getEmbeddings,
combinedScore
Expand All @@ -48,43 +46,43 @@ describe("Testing functions from content_ranker.js", () => {
});


test('countWordOccurrences function', () => {
const count = countWordOccurrences('love', 'I love coding and love testing');
expect(count).toBe(2);
});

test('getEmbeddings function', async () => {
const embeddings = await getEmbeddings('example text');
expect(embeddings).toHaveLength(1536); // Checking if the embeddings array length is 1536
});

test('cosineSimilarity function', () => {
const vecA = [1, 0, 0];
const vecB = [0, 1, 0];
const similarity = cosineSimilarity(vecA, vecB);
expect(similarity).toBeCloseTo(0);
});


test("combinedScore function", async () => {
// No need to redefine the mock here since it's already defined at the top
const content = "This is a test content.";
const preferencesVector = new Array(1536).fill(0.2);
const generatedWords = ["test"];

const score = await combinedScore(content, preferencesVector, generatedWords);
expect(score).toBeDefined();
});
test("getTopContent function", async () => {
const preferences = "love, joy";
const contents = [
"Content with love and happiness",
"Content with joy and excitement",
"Neutral content without specific preferences"];
let topContent = await getTopContent(preferences, contents);
expect(topContent).toContain("love");

});
test('countWordOccurrences function', () => {
const count = countWordOccurrences('love', 'I love coding and love testing');
expect(count).toBe(2);
});

test('getEmbeddings function', async () => {
const embeddings = await getEmbeddings('example text');
expect(embeddings).toHaveLength(1536); // Checking if the embeddings array length is 1536
});

test('cosineSimilarity function', () => {
const vecA = [1, 0, 0];
const vecB = [0, 1, 0];
const similarity = cosineSimilarity(vecA, vecB);
expect(similarity).toBeCloseTo(0);
});


test("combinedScore function", async () => {
// No need to redefine the mock here since it's already defined at the top
const content = "This is a test content.";
const preferencesVector = new Array(1536).fill(0.2);
const generatedWords = ["test"];

const score = await combinedScore(content, preferencesVector, generatedWords);
expect(score).toBeDefined();
});
test("getTopContent function", async () => {
const preferences = "love, joy";
const contents = [
"Content with love and happiness",
"Content with joy and excitement",
"Neutral content without specific preferences"];
let topContent = await getTopContent(preferences, contents);
expect(topContent).toContain("love");

});
});

describe("Tests with simulated API failures", () => {
Expand All @@ -105,8 +103,8 @@ describe("Tests with simulated API failures", () => {
afterEach(() => {
jest.clearAllMocks();
});


// generateRelatedWords function handling API failure
test("generateRelatedWords function handles API failure", async () => {
const result = await generateRelatedWords("love, happiness, joy");
Expand Down Expand Up @@ -134,6 +132,6 @@ describe("Tests with simulated API failures", () => {
expect(ranked).toHaveLength(contents.length);
expect(ranked[0].content).toContain("joy and love"); // Assuming "love" has the highest occurrence
});


});

0 comments on commit 05733f5

Please sign in to comment.