ll-24-25
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # openai-file-search File search =========== Allow models to search your files for relevant information before generating a response. Overview -------- File search is a tool available in the [Responses API](/docs/api-reference/responses). It enables models to retrieve information in a knowledge base of previously uploaded files through semantic and keyword search. By creating vector stores and uploading files to them, you can augment the models' inherent knowledge by giving them access to these knowledge bases or `vector_stores`. To learn more about how vector stores and semantic search work, refer to our [retrieval guide](/docs/guides/retrieval). This is a hosted tool managed by OpenAI, meaning you don't have to implement code on your end to handle its execution. When the model decides to use it, it will automatically call the tool, retrieve information from your files, and return an output. How to use ---------- Prior to using file search with the Responses API, you need to have set up a knowledge base in a vector store and uploaded files to it. Create a vector store and upload a file Follow these steps to create a vector store and upload a file to it. You can use [this example file](https://cdn.openai.com/API/docs/deep_research_blog.pdf) or upload your own. #### Upload the file to the File API Upload a file ```python import requests from io import BytesIO from openai import OpenAI client = OpenAI() def create_file(client, file_path): if file_path.startswith("http://") or file_path.startswith("https://"): # Download the file content from the URL response = requests.get(file_path) file_content = BytesIO(response.content) file_name = file_path.split("/")[-1] file_tuple = (file_name, file_content) result = client.files.create( file=file_tuple, purpose="assistants" ) else: # Handle local file path with open(file_path, "rb") as file_content: result = client.files.create( file=file_content, purpose="assistants" ) print(result.id) return result.id # Replace with your own file path or URL file_id = create_file(client, "https://cdn.openai.com/API/docs/deep_research_blog.pdf") ``` ```javascript import fs from "fs"; import OpenAI from "openai"; const openai = new OpenAI(); async function createFile(filePath) { let result; if (filePath.startsWith("http://") || filePath.startsWith("https://")) { // Download the file content from the URL const res = await fetch(filePath); const buffer = await res.arrayBuffer(); const urlParts = filePath.split("/"); const fileName = urlParts[urlParts.length - 1]; const file = new File([buffer], fileName); result = await openai.files.create({ file: file, purpose: "assistants", }); } else { // Handle local file path const fileContent = fs.createReadStream(filePath); result = await openai.files.create({ file: fileContent, purpose: "assistants", }); } return result.id; } // Replace with your own file path or URL const fileId = await createFile( "https://cdn.openai.com/API/docs/deep_research_blog.pdf" ); console.log(fileId); ``` #### Create a vector store Create a vector store ```python vector_store = client.vector_stores.create( name="knowledge_base" ) print(vector_store.id) ``` ```javascript const vectorStore = await openai.vectorStores.create({ name: "knowledge_base", }); console.log(vectorStore.id); ``` #### Add the file to the vector store Add a file to a vector store ```python client.vector_stores.files.create( vector_store_id=vector_store.id, file_id=file_id ) print(result) ``` ```javascript await openai.vectorStores.files.create( vectorStore.id, { file_id: fileId, } }); ``` #### Check status Run this code until the file is ready to be used (i.e., when the status is `completed`). Check status ```python result = client.vector_stores.files.list( vector_store_id=vector_store.id ) print(result) ``` ```javascript const result = await openai.vectorStores.files.list({ vector_store_id: vectorStore.id, }); console.log(result); ``` Once your knowledge base is set up, you can include the `file_search` tool in the list of tools available to the model, along with the list of vector stores in which to search. At the moment, you can search in only one vector store at a time, so you can include only one vector store ID when calling the file search tool. File search tool ```python from openai import OpenAI client = OpenAI() response = client.responses.create( model="gpt-4o-mini", input="What is deep research by OpenAI?", tools=[{ "type": "file_search", "vector_store_ids": ["<vector_store_id>"] }] ) print(response) ``` ```javascript import OpenAI from "openai"; const openai = new OpenAI(); const response = await openai.responses.create({ model: "gpt-4o-mini", input: "What is deep research by OpenAI?", tools: [{ type: "file_search", vector_store_ids: ["<vector_store_id>"], }], }); console.log(response); ``` When this tool is called by the model, you will receive a response with multiple outputs: 1. A `file_search_call` output item, which contains the id of the file search call. 2. A `message` output item, which contains the response from the model, along with the file citations. File search response ```json { "output": [ { "type": "file_search_call", "id": "fs_67c09ccea8c48191ade9367e3ba71515", "status": "completed", "queries": ["What is deep research?"], "search_results": null }, { "id": "msg_67c09cd3091c819185af2be5d13d87de", "type": "message", "role": "assistant", "content": [ { "type": "output_text", "text": "Deep research is a sophisticated capability that allows for extensive inquiry and synthesis of information across various domains. It is designed to conduct multi-step research tasks, gather data from multiple online sources, and provide comprehensive reports similar to what a research analyst would produce. This functionality is particularly useful in fields requiring detailed and accurate information...", "annotations": [ { "type": "file_citation", "index": 992, "file_id": "file-2dtbBZdjtDKS8eqWxqbgDi", "filename": "deep_research_blog.pdf" }, { "type": "file_citation", "index": 992, "file_id": "file-2dtbBZdjtDKS8eqWxqbgDi", "filename": "deep_research_blog.pdf" }, { "type": "file_citation", "index": 1176, "file_id": "file-2dtbBZdjtDKS8eqWxqbgDi", "filename": "deep_research_blog.pdf" }, { "type": "file_citation", "index": 1176, "file_id": "file-2dtbBZdjtDKS8eqWxqbgDi", "filename": "deep_research_blog.pdf" } ] } ] } ] } ``` Retrieval customization ----------------------- ### Limiting the number of results Using the file search tool with the Responses API, you can customize the number of results you want to retrieve from the vector stores. This can help reduce both token usage and latency, but may come at the cost of reduced answer quality. Limit the number of results ```python response = client.responses.create( model="gpt-4o-mini", input="What is deep research by OpenAI?", tools=[{ "type": "file_search", "vector_store_ids": ["<vector_store_id>"], "max_num_results": 2 }] ) print(response) ``` ```javascript const response = await openai.responses.create({ model: "gpt-4o-mini", input: "What is deep research by OpenAI?", tools: [{ type: "file_search", vector_store_ids: ["<vector_store_id>"], max_num_results: 2, }], }); console.log(response); ``` ### Include search results in the response While you can see annotations (references to files) in the output text, the file search call will not return search results by default. To include search results in the response, you can use the `include` parameter when creating the response. Include search results ```python response = client.responses.create( model="gpt-4o-mini", input="What is deep research by OpenAI?", tools=[{ "type": "file_search", "vector_store_ids": ["<vector_store_id>"] }], include=["file_search_call.results"] ) print(response) ``` ```javascript const response = await openai.responses.create({ model: "gpt-4o-mini", input: "What is deep research by OpenAI?", tools: [{ type: "file_search", vector_store_ids: ["<vector_store_id>"], }], include: ["file_search_call.results"], }); console.log(response); ``` ### Metadata filtering You can filter the search results based on the metadata of the files. For more details, refer to our [retrieval guide](/docs/guides/retrieval), which covers: * How to [set attributes on vector store files](/docs/guides/retrieval#attributes) * How to [define filters](/docs/guides/retrieval#attribute-filtering) Metadata filtering ```python response = client.responses.create( model="gpt-4o-mini", input="What is deep research by OpenAI?", tools=[{ "type": "file_search", "vector_store_ids": ["<vector_store_id>"], "filters": { "type": "eq", "key": "type", "value": "blog" } }] ) print(response) ``` ```javascript const response = await openai.responses.create({ model: "gpt-4o-mini", input: "What is deep research by OpenAI?", tools: [{ type: "file_search", vector_store_ids: ["<vector_store_id>"], filters: { type: "eq", key: "type", value: "blog" } }] }); console.log(response); ``` Supported files --------------- _For `text/` MIME types, the encoding must be one of `utf-8`, `utf-16`, or `ascii`._ |File format|MIME type| |---|---| |.c|text/x-c| |.cpp|text/x-c++| |.cs|text/x-csharp| |.css|text/css| |.doc|application/msword| |.docx|application/vnd.openxmlformats-officedocument.wordprocessingml.document| |.go|text/x-golang| |.html|text/html| |.java|text/x-java| |.js|text/javascript| |.json|application/json| |.md|text/markdown| |.pdf|application/pdf| |.php|text/x-php| |.pptx|application/vnd.openxmlformats-officedocument.presentationml.presentation| |.py|text/x-python| |.py|text/x-script.python| |.rb|text/x-ruby| |.sh|application/x-sh| |.tex|text/x-tex| |.ts|application/typescript| |.txt|text/plain| Limitations ----------- Below are some usage limitations on file search that implementors should be aware of. * Projects are limited to a total size of 100GB for all Files * Vector stores are limited to a total of 10k files * Individual files can be a max of 512MB (roughly 5M tokens per file)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully