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 3 commits into
base: master
Choose a base branch
from

Conversation

ronal2do
Copy link
Contributor

  • Implemented an onFinish callback in the RAGChat class to allow users to access and modify the final output of the chat.
  • Updated the ChatOptions type to include the new onFinish property.
  • Added a new test suite to validate the functionality of the onFinish hook, ensuring it is called with the correct output after a chat interaction.

closes #96

@CahidArda
Copy link
Contributor

Hi @ronal2do,

In the current implementation, onComplete is async, therefore onFinish doesn't really modify the output.

Can you describe your use case a bit more:

  • We return the response in ragChat.chat, what would be the use case for a hook?
  • Do you need to modify the output in your use case?

@ronal2do
Copy link
Contributor Author

ronal2do commented Jan 11, 2025

@CahidArda indeed is async, I have it in a custom implementation.

application case, user wants to save on the database the response or add annotations

this is a piece of a higher level abstraction

const result = await chat(messages, {
    onFinish: async ({ response }) => {
      try {
        await saveChat({
          id,
          messages: [...coreMessages, ...response.messages],
        });
      } catch (error) {
        console.error('Failed to save chat');
      }
    },
  });

another example, but for a custom abstraction with the annotations

   const response = createDataStreamResponse({
      status: 200,
      statusText: 'OK',
      headers: {
        'Custom-Header': 'value',
      },
      async execute(dataStream) {
        // Write annotation
        dataStream.writeMessageAnnotation({ type: 'status', value: 'processing' });
        // Merge another stream
        const result = chat.chat(lastMessage, {
          experimental_activeTools: activeTools,
          onFinish: async ({ text, steps }) => {
            steps.forEach(async (step) => {
              if (step.finishReason === "tool-calls") {
                dataStream.writeMessageAnnotation({ type: 'status', value: step.toolCalls[0].toolName });
              }
            });
          },
        });
        result.mergeIntoDataStream(dataStream);
      },
      onError: error => `Custom error: ${error.message}`,
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Saving chat history in PostgreSQL
2 participants