Skip to content

Commit

Permalink
#4 refactored code to fix issue with close-ended chat + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-kural committed Jul 8, 2024
1 parent c5198f9 commit fcd7360
Show file tree
Hide file tree
Showing 10 changed files with 6,849 additions and 4,045 deletions.
10,709 changes: 6,739 additions & 3,970 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions src/agents/chat-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,17 @@ export class ChatAgent implements ChatAgentInterface {
* @returns Returns the formatted input.
* @throws Throws an error if the agent type is invalid.
*/
private static getFormattedInput(
agentType?: ChatAgentType,
query?: string,
topic?: string,
context?: string
) {
private static getFormattedInput({
agentType,
query,
context,
topic,
}: {
agentType?: ChatAgentType;
query?: string;
topic?: string;
context?: string;
}) {
switch (agentType) {
case "open-ended":
return {
Expand Down Expand Up @@ -288,7 +293,7 @@ export class ChatAgent implements ChatAgentInterface {
// if undefined, will use model defined in the dotprompt
model: model,
config: modelConfig,
input: ChatAgent.getFormattedInput(agentType, query, context, topic),
input: ChatAgent.getFormattedInput({ agentType, query, context, topic }),
tools: tools,
});
// return the response
Expand Down
30 changes: 0 additions & 30 deletions src/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/test.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/tests/api-keys.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe("Test - Endpoint API Keys Tests", () => {
status: "active",
endpoints: "all", // allow access to all endpoints
});
// define chat flow
const flow = defineChatEndpoint({
// define chat endpoint
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-api-key-required",
enableAuth: true,
apiKeyStore,
Expand All @@ -53,7 +53,7 @@ describe("Test - Endpoint API Keys Tests", () => {

try {
// send test query without API key
response = await runEndpoint(flow, {
response = await runEndpoint(endpoint, {
query: "How can you help? In one sentence.",
uid: "test-user",
});
Expand Down Expand Up @@ -86,16 +86,16 @@ describe("Test - Endpoint API Keys Tests", () => {
status: "active",
endpoints: "all", // allow access to all endpoints
});
// define chat flow
const flow = defineChatEndpoint({
// define chat endpoint
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-api-key-auth-working",
enableAuth: true,
apiKeyStore,
});
try {
// send test query with API key
const response = await runEndpoint(
flow,
endpoint,
{
query: "How can you help? In one sentence.",
uid: testUID,
Expand Down
10 changes: 5 additions & 5 deletions src/tests/cache.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ describe("Test - Endpoint Cache Tests", () => {
const cacheStore = new InMemoryCacheStore({
cacheQueryAfterThreshold: 2, // cache response after same query is received twice
});
// define chat flow
const flow = defineChatEndpoint({
// define chat endpoint
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-cache",
enableCache: true,
cacheStore: cacheStore,
});

try {
// send query the first time
await runEndpoint(flow, {
await runEndpoint(endpoint, {
query: "Answer in one sentence: what is Firebase?",
});

// send query the second time
await runEndpoint(flow, {
await runEndpoint(endpoint, {
query: "Answer in one sentence: what is Firebase?",
});

// send query the third time
const response = await runEndpoint(flow, {
const response = await runEndpoint(endpoint, {
query: "Answer in one sentence: what is Firebase?",
});

Expand Down
14 changes: 7 additions & 7 deletions src/tests/chat-history.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ describe("Test - Endpoint Chat History Tests", () => {
test(
"Sending invalid Chat ID when chat history is disabled",
async () => {
const flow = defineChatEndpoint({
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-chat-id-history-disabled",
enableChatHistory: false,
});
const response = await runEndpoint(flow, {
const response = await runEndpoint(endpoint, {
query: "How can you help? In one sentence.",
chatId: "test-chat-id",
});
Expand All @@ -59,12 +59,12 @@ describe("Test - Endpoint Chat History Tests", () => {
test(
"Sending invalid Chat ID when chat history is enabled",
async () => {
const flow = defineChatEndpoint({
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-chat-id-history-enabled",
enableChatHistory: true,
chatHistoryStore: new InMemoryChatHistoryStore(),
});
const response = await runEndpoint(flow, {
const response = await runEndpoint(endpoint, {
query: "How can you help? In one sentence.",
chatId: "test-chat-id",
});
Expand All @@ -78,12 +78,12 @@ describe("Test - Endpoint Chat History Tests", () => {
test(
"Test chat history works",
async () => {
const flow = defineChatEndpoint({
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-chat-history",
enableChatHistory: true,
chatHistoryStore: new InMemoryChatHistoryStore(),
});
const response = await runEndpoint(flow, {
const response = await runEndpoint(endpoint, {
query: "What is Firebase? In one sentence.",
});
expect(response).toBeDefined();
Expand All @@ -95,7 +95,7 @@ describe("Test - Endpoint Chat History Tests", () => {
// response should not be empty
expect(response.response.length).toBeGreaterThan(0);

const secondResponse = await runEndpoint(flow, {
const secondResponse = await runEndpoint(endpoint, {
query: "Can I use it for authentication? In one sentence.",
chatId,
});
Expand Down
21 changes: 7 additions & 14 deletions src/tests/endpoint-basic.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,27 @@ describe("Test - Chat Endpoint Core Funtionality Tests", () => {
// Tests to be performed
// Set to true to run the test
const Tests = {
define_chat_flow: true,
define_chat_endpoint: true,
confirm_response_generation: true,
sending_invalid_chat_id_when_chat_history_is_disabled: true,
sending_invalid_chat_id_when_chat_history_is_enabled: true,
test_chat_history_works: true,
test_cache_works: true,
test_api_key_is_required: true,
test_api_key_auth_working: true,
test_rag_works: true,
};

// default test timeout
const defaultTimeout = 10000; // 10 secondss

if (Tests.define_chat_flow)
test("Define chat flow", () => {
const flow = defineChatEndpoint({ endpoint: "test-chat" });
expect(flow).toBeDefined();
if (Tests.define_chat_endpoint)
test("Define chat endpoint", () => {
const endpoint = defineChatEndpoint({ endpoint: "test-chat" });
expect(endpoint).toBeDefined();
});

if (Tests.confirm_response_generation)
test(
"Confirm response generation",
async () => {
const flow = defineChatEndpoint({
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-response",
});
const response = await runEndpoint(flow, {
const response = await runEndpoint(endpoint, {
query: "How can you help? In one sentence.",
});
expect(response).toBeDefined();
Expand Down
70 changes: 70 additions & 0 deletions src/tests/endpoint-close-ended.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
defineChatEndpoint,
getChatEndpointRunner,
} from "../endpoints/endpoints";
import { setupGenkit } from "../genkit/genkit";

/**
* Test suite for Close-ended Chat Endpoint.
*
* Some tests include the use of LLM model, defining a chat agent, defining API key store, defining chat history store, and defining cache store.
*/
describe("Test - Close-ended Chat Endpoint Tests", () => {
// Initialize endpoint runner
const runEndpoint = getChatEndpointRunner();

beforeAll(() => {
setupGenkit();
});

// Tests to be performed
// Set to true to run the test
const Tests = {
define_close_ended_chat: true,
confirm_response_generation: true,
};

// default test timeout
const defaultTimeout = 10000; // 10 secondss

if (Tests.define_close_ended_chat)
test("Define chat endpoint", () => {
const endpoint = defineChatEndpoint({
endpoint: "test-chat-close",
agentType: "close-ended",
topic: "Firebase",
});
expect(endpoint).toBeDefined();
});

if (Tests.confirm_response_generation)
test(
"Confirm response generation",
async () => {
const endpoint = defineChatEndpoint({
endpoint: "test-chat-close-response",
agentType: "close-ended",
topic: "Firebase",
});
const response = await runEndpoint(endpoint, {
query: "How can you help? In one sentence.",
});
expect(response).toBeDefined();
if (typeof response === "string") {
// should not be empty
expect(response.length).toBeGreaterThan(0);
} else {
expect(response).toHaveProperty("response");
if ("response" in response) {
// should not be empty
expect(response.response.length).toBeGreaterThan(0);
} else {
throw new Error(
`error in response generation. Response: ${JSON.stringify(response)}`
);
}
}
},
defaultTimeout
);
});
6 changes: 3 additions & 3 deletions src/tests/endpoint-rag.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ describe("Test - Endpoint RAG Tests", () => {
test(
"Test RAG works (w/ Data Retriever, Embedding Generation, RAG-enabled Endpoint)",
async () => {
// define chat flow
const flow = defineChatEndpoint({
// define chat endpoint
const endpoint = defineChatEndpoint({
endpoint: "test-chat-open-rag",
enableRAG: true,
retriever: await getDataRetriever({
Expand All @@ -43,7 +43,7 @@ describe("Test - Endpoint RAG Tests", () => {
});
try {
// send test query
const response = await runEndpoint(flow, {
const response = await runEndpoint(endpoint, {
query: "What is the price of Seagate ST1000DX002?",
});

Expand Down

0 comments on commit fcd7360

Please sign in to comment.