Skip to content

Commit

Permalink
Added tests for endpoint with RAG and chat history
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-kural committed Jul 9, 2024
1 parent 4fddd6c commit 501e60f
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/tests/endpoint-rag.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getChatEndpointRunner,
} from "../endpoints/endpoints";
import { setupGenkit } from "../genkit/genkit";
import { InMemoryChatHistoryStore } from "../history/in-memory-chat-history-store";
import { getDataRetriever } from "../rag/data-retrievers/data-retrievers";

/**
Expand All @@ -22,6 +23,7 @@ describe("Test - Endpoint RAG Tests", () => {
// Set to true to run the test
const Tests = {
test_rag_works: true,
test_rag_works_with_history: true,
};

// default test timeout
Expand All @@ -34,6 +36,7 @@ describe("Test - Endpoint RAG Tests", () => {
// define chat endpoint
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-rag",
topic: "store inventory",
enableRAG: true,
retriever: await getDataRetriever({
dataType: "csv",
Expand Down Expand Up @@ -76,4 +79,93 @@ describe("Test - Endpoint RAG Tests", () => {
},
defaultTimeout
);

if (Tests.test_rag_works_with_history)
test(
"Test RAG works (w/ Chat History)",
async () => {
// define chat endpoint
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-rag",
topic: "store inventory",
enableRAG: true,
retriever: await getDataRetriever({
dataType: "csv",
filePath: "src/tests/test-data/inventory-data.csv",
generateEmbeddings: true,
}),
enableChatHistory: true,
chatHistoryStore: new InMemoryChatHistoryStore(),
});
try {
// send test query
const response = await runEndpoint(endpoint, {
query: "What is the price of Seagate ST1000DX002?",
});

// check response is valid and does not contain error
expect(response).toBeDefined();

// if error is present, throw error
if (
response !== "string" &&
Object.keys(response).includes("error")
) {
throw new Error(`${JSON.stringify(response)}`);
}
expect(response).toHaveProperty("response");
expect(response).toHaveProperty("chatId");

if (typeof response !== "string" && "chatId" in response) {
const chatId = response.chatId;

// response should not be empty
expect(response.response.length).toBeGreaterThan(0);

// confirm response accuracy
// should contain 68.06
expect(response.response).toContain("68.06");

const secondResponse = await runEndpoint(endpoint, {
query: "How many of these do we have in stock?",
chatId,
});

expect(secondResponse).toBeDefined();

// if error is present, throw error
if (
secondResponse !== "string" &&
Object.keys(secondResponse).includes("error")
) {
throw new Error(`${JSON.stringify(secondResponse)}`);
}

expect(secondResponse).toHaveProperty("response");
expect(secondResponse).toHaveProperty("chatId");

if (
typeof secondResponse !== "string" &&
"chatId" in secondResponse
) {
// response should not be empty
expect(response.response.length).toBeGreaterThan(0);
// chat ID should be the same
expect(secondResponse.chatId).toEqual(chatId);
} else {
throw new Error(
`error in second response. Invalid response object. Response: ${JSON.stringify(secondResponse)}`
);
}
} else {
throw new Error(
`Response field invalid. Response: ${JSON.stringify(response)}`
);
}
} catch (error) {
throw new Error(`Error in test. Error: ${error}`);
}
},
defaultTimeout
);
});

0 comments on commit 501e60f

Please sign in to comment.