vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.89k stars 1.46k forks source link

Override maxToolRoundtrips per tool call #2693

Closed blechatellier closed 1 month ago

blechatellier commented 2 months ago

Feature Description

Following this Twitter discussion: https://x.com/blechatellier/status/1823688077006790917 with @lgrammel

It would be great to allow setting a tool call result (on either the server or client) while controlling whether to automatically trigger another call to the LLM.

This use case involves rendering a custom UI component on the client using the tool call result, without needing a roundtrip to the LLM. However, it should still allow multiple roundtrips for other tools.

One possible solution could be overriding maxToolRoundtrips per tool call, or passing a flag to addToolResult on the client side to prevent triggering another LLM call. A similar mechanism would be needed on the server if the tool call is running via the execute method.

Or maybe exposing triggerRequest so I can recall the LLM for some specific tools only.

blechatellier commented 2 months ago

Solved it for now by setting maxToolRoundtrips: 0 and re-submitting for some tool calls only.

interface UseToolRoundtripsParams {
  handleSubmit: (
    event?: {
      preventDefault?: () => void;
    },
    chatRequestOptions?: ChatRequestOptions,
  ) => void;
  messages: Message[];
  toolNames: string[];
}

export const useToolRoundtrips = ({
  handleSubmit,
  messages,
  toolNames,
}: UseToolRoundtripsParams) => {
  useEffect(() => {
    const lastMessage = messages[messages.length - 1];

    for (const toolInvocation of lastMessage?.toolInvocations || []) {
      if (toolInvocation.state !== 'result') {
        continue;
      }
      if (toolNames.includes(toolInvocation.toolName)) {
        handleSubmit(undefined, { allowEmptySubmit: true });
      }
    }
  }, [messages]);
};
blechatellier commented 2 months ago

Another convo to do that on the server https://github.com/vercel/ai/issues/1943