ai icon indicating copy to clipboard operation
ai copied to clipboard

feat (provider/openai): support file search tool

Open matthewmichel opened this issue 8 months ago • 9 comments

Extending the openai-tools to support the "file_search" tool call documented by OpenAI here: https://platform.openai.com/docs/guides/tools-file-search?lang=javascript

Note: originally set this up as "file_search_preview", but opted for "file_search" because OpenAI does not have this tool tagged as a preview feature and the tool name in the OpenAI docs is "file_search".

matthewmichel avatar Mar 12 '25 03:03 matthewmichel

Nice! without parsing their special outputs how will this work in practice?

lgrammel avatar Mar 12 '25 13:03 lgrammel

When I tested it to see what would happen when adding in a vector_store_id it returned a response as expected through the "text" value with references to the vector store files (indicating to me that it worked to perform the file search).

The "text" value (returned by the new OpenAI Responses API) should work as expected when using the file_search tool. I'll work on adding some handlers for the new "annotations" value that is returned by the Responses API for better clarity.

matthewmichel avatar Mar 12 '25 15:03 matthewmichel

@lgrammel I should have checked before responding. The "annotations" property is already available on the OpenAIResponsesAssistantMessage. This should be enough to pass through those annotations through the SDK response.

export type OpenAIResponsesAssistantMessage = {
  role: 'assistant';
  content: Array<{ 
    type: 'output_text'; 
    text: string; 
    annotations?: Array<{
      type: 'file_citation';
      index: number;
      file_id: string;
      filename: string;
    } | {
      type: 'url_citation';
      start_index: number;
      end_index: number;
      url: string;
      title: string;
    }>;
  }>;
};

This matches the OpenAI docs for the file search tool: https://platform.openai.com/docs/guides/tools-file-search

EDIT: I didn't realize I actually made this change 🤦‍♂️ The update to this type has been pushed to this PR.

matthewmichel avatar Mar 12 '25 15:03 matthewmichel

Ok made some adjustments to ensure the annotations information is being returned properly through the sdk. Tested this locally and got a nice sources value when trying out with the file_search tool.

sources: [
    {
      sourceType: 'file',
      id: 'PSvUjMxv2hlJ4vuj',
      fileId: 'file-IhP983o4sPmYkgGOtVdureJO',
      filename: 'Artificial Intelligence (AI) Policy.txt'
    }
  ]

I've also updated the next-openai examples to account for either url or file source annotations.

matthewmichel avatar Mar 12 '25 17:03 matthewmichel

Any chance we could get this release as experimental_alpha and then migrate to one which supports all providers? I would love to migrate a lot of Assistants to the new responses api but I'm blocked by the file_search limitation

cosbgn avatar Mar 13 '25 09:03 cosbgn

Waiting for this file_search adapter,

A game changer for ALL 🙂‍↔️

MwSpaceLLC avatar Mar 13 '25 13:03 MwSpaceLLC

This PR will close #5188

IKatsuba avatar Mar 20 '25 12:03 IKatsuba

Is there any update on this @lgrammel ?

nsenthilkumar avatar Mar 25 '25 20:03 nsenthilkumar

Hey, @lgrammel any update ?

shrix1 avatar Apr 15 '25 09:04 shrix1

@lgrammel sorry for the possible spam, but any plans to merge this soon? Any change needed to the PR?

kvnwolf avatar May 06 '25 07:05 kvnwolf

Looking forward to this be merged 💪

andresgutgon avatar May 21 '25 08:05 andresgutgon

@lgrammel any updates on this?

ruidpm avatar May 22 '25 13:05 ruidpm

seems like blocked, I really need this tool, I could use assistant to move this into openai could work for some of you, but it doesn't work for me as I am trying to use assistant ui cloud which doesn't support assistant.

lagorsse avatar May 23 '25 23:05 lagorsse

@lgrammel do you need any help on this? It seems the responses api is not compatible with APIs from Anthropic/xAI especially these custom constructs like file_search, web_search in the responses API?

https://apimagic.ai/blog/switching-assistant-responses-api

We can either use the existing tools parameter available on the ai-sdk methods or add it to the providerOptions like below:

const providerOptions = {
  openai: {
    tools: [
      {
        type: 'file_search',
        vector_store_ids: ['vs_1234567890'], 
        max_num_results: 20,
      },
    ],
    tool_choice: { type: 'tool', tool_name: 'file_search' },
  },
};

Jaikant avatar Jun 07 '25 07:06 Jaikant

Any news?

ssg-chris avatar Jun 10 '25 09:06 ssg-chris

This functionality is available in ai sdk 5 beta.

lgrammel avatar Jun 30 '25 11:06 lgrammel