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

Feature Request: Add onStepFinish Support to useCompletion Hook in Vercel AI SDK #3897

Open
MustafaK99 opened this issue Nov 26, 2024 · 2 comments
Labels
ai/ui enhancement New feature or request

Comments

@MustafaK99
Copy link

Feature Description

Summary

I am requesting a new feature for the useCompletion hook in the Vercel AI SDK to support an onStepFinish callback, similar to the one available in the backend generateText function. This enhancement would enable developers to handle intermediate steps and results directly within the hook, providing a more seamless and interactive development experience.

Current Behavior

Currently, backend generateText has an onStepFinish callback as follows:

const result = await generateText({

  model: yourModel,

  maxSteps: 10,

  onStepFinish({ text, toolCalls, toolResults, finishReason, usage }) {
    // your own logic, e.g., for saving or using step

  },

  // ...

});

However, corresponding frontend hooks do not provide access to this functionality. For example:

const { complete, completion, isLoading } = useCompletion({
  api: '/api/completion',
});

Developers cannot currently access intermediate step data such as text, toolCalls, toolResults, finishReason, or usage within the hook.

Use Cases

Proposed Behavior

Add an onStepFinish option to the frontend hook, allowing developers to define a callback function that receives the same parameters as the backend implementation. The enhanced hook might look like this:

const { complete, completion, isLoading } = useCompletion({

  api: '/api/completion',

  onStepFinish: ({ text, toolCalls, toolResults, finishReason, usage }) => {

    // Custom logic for handling intermediate steps
    console.log('Step finished:', text);

 
 },

});

Implementation Details

Add an onStepFinish parameter to the frontend hook.

Pass the onStepFinish function to the underlying API route and ensure it triggers for each intermediate step.

Expose the same data (text, toolCalls, toolResults, finishReason, usage) as in the backend implementation.

Example Use Case

Consider a application where intermediate responses are shown to users before the final completion is ready. With the onStepFinish feature, the frontend hook could be used to:

Display partial responses as they are generated.

Log usage metrics or tool calls for analytics.

Update the UI dynamically without waiting for the final result.

Additional context

would be cool to have seen onFinish within the useChat

@MustafaK99 MustafaK99 added the enhancement New feature or request label Nov 26, 2024
@lgrammel
Copy link
Collaborator

Have you tried using useChat with streamText? while it does not support what you need, i'm wondering why you choose useCompletion and generateTExt?

@lgrammel lgrammel added the ai/ui label Nov 26, 2024
@MustafaK99
Copy link
Author

have a command+k like feature similar to cursor because it wasn't really a chat I went for useCompletion works well for most of my use cases such as translation, autocomplete but I wanted to add some tool calls for one of the APIs and then log the steps to the front-end. I might just stream it with the data option for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/ui enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants