按語義相似性搜索向量存儲文檔

透過語義相似性和批次操作搜尋向量儲存文件

Initiate a search operation with a query text and receive most semantically similar responses from the vector store.

Path parameters
vector-store-idstringRequired

The ID of the vector store.

Query parameters
qstringRequired

The search query text.

nintegerOptional

Number of chunks to return.

tnumberOptional

Matching threshold.

prev_chunksintegerOptional

Number of chunks before the matched chunk to include.

next_chunksintegerOptional

Number of chunks after the matched chunk to include.

Header parameters
x-api-keystringRequired

The API key for authentication.

Responses
200
Search results retrieved successfully.
application/json
get
GET /api/v1/vector-stores/{vector-store-id}/documents/search HTTP/1.1
Host: api.rememberizer.ai
x-api-key: text
Accept: */*
200

Search results retrieved successfully.

{
  "vector_store": {
    "id": "text",
    "name": "text"
  },
  "matched_chunks": [
    {
      "document": {
        "id": 1,
        "name": "text",
        "type": "text",
        "size": 1,
        "indexed_on": "2025-06-18T19:39:21.188Z",
        "vector_store": "text",
        "created": "2025-06-18T19:39:21.188Z",
        "modified": "2025-06-18T19:39:21.188Z"
      },
      "matched_content": "text",
      "distance": 1
    }
  ]
}

示例請求

curl -X GET \
  "https://api.rememberizer.ai/api/v1/vector-stores/vs_abc123/documents/search?q=如何%20將%20我們的%20產品%20與%20第三方%20系統%20整合&n=5&prev_chunks=1&next_chunks=1" \
  -H "x-api-key: YOUR_API_KEY"

YOUR_API_KEY 替換為您的實際向量存儲 API 金鑰,並將 vs_abc123 替換為您的向量存儲 ID。

路徑參數

參數
類型
描述

vector-store-id

字串

必填。 要搜索的向量存儲的 ID。

查詢參數

參數
類型
描述

q

字串

必填。 搜尋查詢文字。

n

整數

要返回的結果數量。預設:10。

t

數字

匹配閾值。預設:0.7。

prev_chunks

整數

要包含的匹配塊之前的塊數。預設:0。

next_chunks

整數

要包含的匹配塊之後的塊數。預設:0。

回應格式

{
  "vector_store": {
    "id": "vs_abc123",
    "name": "產品文檔"
  },
  "matched_chunks": [
    {
      "document": {
        "id": 1234,
        "name": "整合指南.pdf",
        "type": "application/pdf",
        "size": 250000,
        "indexed_on": "2023-06-15T10:30:00Z",
        "vector_store": "vs_abc123",
        "created": "2023-06-15T10:15:00Z",
        "modified": "2023-06-15T10:30:00Z"
      },
      "matched_content": "我們的產品提供多種第三方系統的整合選項。主要方法是通過我們的 RESTful API,該 API 支持 OAuth2 認證。此外,您還可以使用我們提供的 SDK,支持 Python、JavaScript 和 Java。",
      "distance": 0.123
    },
    // ... 更多匹配的片段
  ]
}

認證

此端點需要使用 x-api-key 標頭中的 API 金鑰進行認證。

錯誤回應

狀態碼
描述

400

錯誤的請求 - 缺少必要的參數或格式無效

401

未授權 - API 金鑰無效或缺失

404

找不到 - 向量儲存未找到

500

內部伺服器錯誤

搜尋優化技巧

上下文窗口

使用 prev_chunksnext_chunks 參數來控制每個匹配包含多少上下文:

  • 將兩者設置為 0 以獲得沒有上下文的精確匹配

  • 將兩者設置為 1-2 以獲得具有最小上下文的匹配

  • 將兩者設置為 3-5 以獲得具有相當上下文的匹配

匹配閾值

t 參數控制匹配的過濾嚴格程度:

  • 較高的值(例如,0.9)僅返回非常接近的匹配

  • 較低的值(例如,0.5)返回更多具有更大多樣性的匹配

  • 默認值(0.7)提供了一種平衡的方法

批次操作

對於高吞吐量的應用,Rememberizer 支援在向量儲存上進行高效的批次操作。這些方法在處理多個搜尋查詢時優化性能。

批次搜尋實作

import requests
import time
import concurrent.futures

def batch_search_vector_store(vector_store_id, queries, num_results=5, batch_size=10):
    """
    對向量儲存庫執行批次搜尋
    
    參數:
        vector_store_id: 要搜尋的向量儲存庫的 ID
        queries: 搜尋查詢字串的列表
        num_results: 每個查詢的結果數量
        batch_size: 平行請求的數量
        
    回傳:
        搜尋結果的列表
    """
    headers = {
        "x-api-key": "YOUR_API_KEY"
    }
    
    results = []
    
    # 以批次處理以避免過載 API
    for i in range(0, len(queries), batch_size):
        batch_queries = queries[i:i+batch_size]
        
        with concurrent.futures.ThreadPoolExecutor(max_workers=batch_size) as executor:
            futures = []
            
            for query in batch_queries:
                params = {
                    "q": query,
                    "n": num_results,
                    "prev_chunks": 1,
                    "next_chunks": 1
                }
                
                # 提交請求到執行緒池
                future = executor.submit(
                    requests.get,
                    f"https://api.rememberizer.ai/api/v1/vector-stores/{vector_store_id}/documents/search",
                    headers=headers,
                    params=params
                )
                futures.append(future)
            
            # 收集所有未來的結果
            for future in futures:
                response = future.result()
                if response.status_code == 200:
                    results.append(response.json())
                else:
                    results.append({"error": f"失敗,狀態碼: {response.status_code}"})
        
        # 在批次之間添加延遲以避免速率限制
        if i + batch_size < len(queries):
            time.sleep(1)
    
    return results

# 範例用法
queries = [
    "與 REST API 的整合",
    "身份驗證協議",
    "如何部署到生產環境",
    "性能優化技術",
    "錯誤處理最佳實踐"
]

search_results = batch_search_vector_store("vs_abc123", queries, num_results=3, batch_size=5)

批次操作的性能優化

在實施向量存儲搜索的批次操作時,考慮以下最佳實踐:

  1. 最佳批次大小:對於大多數應用程序,並行處理 5-10 個查詢在吞吐量和資源使用之間提供了良好的平衡。

  2. 速率限制意識:在批次之間包含延遲機制(通常為 1-2 秒),以避免觸及 API 速率限制。

  3. 錯誤處理:為可能在批次中失敗的單個查詢實施健壯的錯誤處理。

  4. 連接管理:對於高流量應用程序,實施連接池以減少開銷。

  5. 超時配置:為每個請求設置適當的超時,以防止長時間運行的查詢阻塞整個批次。

  6. 結果處理:考慮在結果可用時異步處理結果,而不是等待所有結果。

  7. 監控:跟踪性能指標,如平均響應時間和成功率,以識別優化機會。

對於查詢量非常大的生產應用程序,考慮實施隊列系統和工作進程以有效管理大型批次。

此端點允許您使用語義相似性搜索您的向量存儲。它返回與您的查詢在概念上相關的文檔,即使它們不包含確切的關鍵字。這使得它對自然語言查詢和問題回答特別強大。

Last updated