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

feat(chat): add onFinish hook to RAGChat for final response handling #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 56 additions & 0 deletions src/rag-chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,59 @@ describe("RAGChat - context filtering", () => {
{ timeout: 30_000 }
);
});

describe("RAGChat with onFinish hook", () => {
const namespace = "result-metadata";
const vector = new Index({
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
url: process.env.UPSTASH_VECTOR_REST_URL!,
});

const ragChat = new RAGChat({
vector,
namespace,
streaming: true,
model: upstash("meta-llama/Meta-Llama-3-8B-Instruct"),
});

afterAll(async () => {
await vector.reset({ namespace });
await vector.deleteNamespace(namespace);
});

test(
"should call onFinish callback with correct output",
async () => {
// Set up test data
await ragChat.context.add({
type: "text",
data: "Tokyo is the Capital of Japan.",
options: { namespace, metadata: { unit: "Samurai" } },
});
await ragChat.context.add({
type: "text",
data: "Shakuhachi is a traditional wind instrument",
options: { namespace, metadata: { unit: "Shakuhachi" } },
});
await awaitUntilIndexed(vector);

// Create a spy for onFinish callback
let onFinishCalled = false;
let capturedOutput = "";

const result = await ragChat.chat<{ unit: string }>("Where is the capital of Japan?", {
namespace,
onFinish: ({ output }) => {
onFinishCalled = true;
capturedOutput = output;
},
});

expect(onFinishCalled).toBe(true);
expect(capturedOutput).toBe(result.output);
expect(result.output.toLowerCase()).toContain("tokyo");
expect(result.metadata).toEqual([{ unit: "Samurai" }, { unit: "Shakuhachi" }]);
},
{ timeout: 30_000 }
);
});
4 changes: 4 additions & 0 deletions src/rag-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export class RAGChat {
if (!optionsWithDefault.disableHistory) {
await this.addAssistantMessageToHistory(output, optionsWithDefault);
}
if (optionsWithDefault.onFinish) {
optionsWithDefault.onFinish({ output });
}
},
},
this.debug
Expand Down Expand Up @@ -292,6 +295,7 @@ export class RAGChat {
? DEFAULT_PROMPT_WITHOUT_RAG
: (options?.promptFn ?? this.config.prompt),
contextFilter: options?.contextFilter ?? undefined,
onFinish: options?.onFinish,
};
}
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export type ChatOptions = {
* https://upstash.com/docs/vector/features/filtering#metadata-filtering
*/
contextFilter?: string;

/**
* Hook to access the final response and modify as you wish.
*/
onFinish?: ({ output }: { output: string }) => void;
} & CommonChatAndRAGOptions;

export type PrepareChatResult = { data: string; id: string; metadata: unknown }[];
Expand Down