OpenAI 搜索向量存储接口

2026-11-10 18:20 更新

搜索向量存储

post /vector_stores/{vector_store_id}/search

根据查询和文件属性筛选条件,在向量存储中搜索相关块。

路径参数

  • vector_store_id: string:此请求涉及的向量存储 ID。

请求体参数

媒体类型为 application/json。

  • query: string or array of string

用于搜索的查询字符串。

  • string

  • array of string

  • filters: optional ComparisonFilter or CompoundFilter

基于文件属性应用的筛选条件。

  • ComparisonFilter object { key, type, value }

筛选条件,通过指定的比较操作,将给定属性键与某个值进行比较。

  • key: string

要与值比较的键。

  • type: "eq" or "ne" or "gt" or 5 more(默认 "eq")

指定比较运算符:eq、ne、gt、gte、lt、lte、in、nin。

  • eq:等于;ne:不等于;gt:大于;gte:大于或等于;lt:小于;lte:小于或等于;in:包含于;nin:不包含于。

  • "eq"

  • "ne"

  • "gt"

  • "gte"

  • "lt"

  • "lte"

  • "in"

  • "nin"

  • value: string or number or boolean or array of string or number

与属性键比较的值,支持字符串、数字或布尔类型。

  • string

  • number

  • boolean

  • array of string or number

  • string

  • number

  • CompoundFilter object { filters, type }

使用and或or组合多个筛选条件。

  • filters: array of ComparisonFilter or CompoundFilter

要组合的筛选条件数组,条目可为ComparisonFilter或CompoundFilter。

  • ComparisonFilter object { key, type, value }

筛选条件,通过指定的比较操作,将给定属性键与某个值进行比较。

  • CompoundFilter object { filters, type }

使用and或or组合多个筛选条件。

  • type: "and" or "or"

操作类型,可选and或or。

  • "and"

  • "or"

  • max_num_results: optional integer(默认 10,范围 1 至 50)

返回结果的最大数量,必须在1到50之间,包含两端。

  • ranking_options: optional object { ranker, score_threshold }

搜索排序选项。

  • ranker: optional "none" or "auto" or "default-2024-11-15"(默认 "auto")

启用重排;设为none可禁用重排,有助于降低延迟。

  • "none"

  • "auto"

  • "default-2024-11-15"

  • score_threshold: optional number(默认 0,范围 0 至 1)

  • rewrite_query: optional boolean(默认 false)

是否为向量搜索改写自然语言查询。

返回值

  • data: array of object { attributes, content, file_id, 2 more }

搜索结果项列表。

  • attributes: map[string or number or boolean] or null

可附加到对象的一组16个键值对,可用于以结构化格式保存额外信息,并通过API或控制台查询对象。键为最长64个字符的字符串;值可以是最长512个字符的字符串、布尔值或数字。

  • string(最长 512 个字符)

  • number

  • boolean

  • content: array of object { text, type }

来自文件的内容块。

  • text: string

搜索返回的文本内容。

  • type: "text"

内容类型。

  • "text"

  • file_id: string

向量存储文件的ID。

  • filename: string

向量存储文件的名称。

  • score: number(范围 0 至 1)

结果的相似度分数。

  • has_more: boolean

是否还有更多结果可获取。

  • next_page: string or null

下一页的令牌(若有)。

  • object: "vector_store.search_results.page"

对象类型,固定为vector_store.search_results.page。

  • "vector_store.search_results.page"

  • search_query: array of string

示例

## [中文] 发送POST请求;将示例API凭证及资源标识替换为应用使用的值。
curl https://api.openai.com/v1/vector_stores/$VECTOR_STORE_ID/search \
    -H 'Content-Type: application/json' \
    -H 'OpenAI-Beta: assistants=v2' \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d '{
          "query": "string"
        }'

响应

中文说明:以下为原文配置、命令或事件示例,字段名、枚举、标识符及数值保留。

{
  "data": [
    {
      "attributes": {
        "foo": "string"
      },
      "content": [
        {
          "text": "text",
          "type": "text"
        }
      ],
      "file_id": "file_id",
      "filename": "filename",
      "score": 0
    }
  ],
  "has_more": true,
  "next_page": "next_page",
  "object": "vector_store.search_results.page",
  "search_query": [
    "string"
  ]
}

示例

代码内文字译释:

  • 退货政策是什么?

## [中文] 发送POST请求;将示例API凭证及资源标识替换为应用使用的值。
curl -X POST \
https://api.openai.com/v1/vector_stores/vs_abc123/search \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "What is the return policy?", "filters": {...}}'

响应

中文说明:以下为原文配置、命令或事件示例,字段名、枚举、标识符及数值保留。

代码内文字译释:

  • 退货政策是什么?
  • 约翰·多伊。
  • 相关内容块。
  • 简·史密斯。
  • 来自向量存储的示例文本内容。

{
  "object": "vector_store.search_results.page",
  "search_query": ["What is the return policy?"],
  "data": [
    {
      "file_id": "file_123",
      "filename": "document.pdf",
      "score": 0.95,
      "attributes": {
        "author": "John Doe",
        "date": "2023-01-01"
      },
      "content": [
        {
          "type": "text",
          "text": "Relevant chunk"
        }
      ]
    },
    {
      "file_id": "file_456",
      "filename": "notes.txt",
      "score": 0.89,
      "attributes": {
        "author": "Jane Smith",
        "date": "2023-01-02"
      },
      "content": [
        {
          "type": "text",
          "text": "Sample text content from the vector store."
        }
      ]
    }
  ],
  "has_more": false,
  "next_page": null
}
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号