API ReferenceRetrieval

Retrieval

Search a knowledge base to retrieve relevant document chunks. This endpoint supports semantic (vector), keyword, and hybrid search modes. Use it to build custom RAG pipelines or to inspect what context would be retrieved for a given query.

Endpoint

POST https://api.tensoras.ai/v1/knowledge-bases/{kb_id}/retrieve

Authentication

Authorization: Bearer tns_your_key_here

Request Body

ParameterTypeRequiredDefaultDescription
kb_idstringYesThe knowledge base ID (path parameter).
querystringYesThe search query text.
top_kintegerNo10The maximum number of results to return. Max: 100.
search_typestringNo"hybrid"The search strategy. One of "hybrid", "vector", "keyword".
filtersobjectNoMetadata filters to narrow results. See filters below.
include_metadatabooleanNotrueWhether to include document metadata in the results.
min_scorenumberNo0.0Minimum relevance score threshold. Results below this score are excluded.

Search Types

TypeDescription
hybridCombines vector similarity search with keyword matching for the best overall relevance. This is the recommended default.
vectorPure semantic search using embedding similarity. Best for finding conceptually related content even when exact keywords differ.
keywordTraditional keyword-based search using BM25 scoring. Best when you need exact term matching.

Filters

Use metadata filters to narrow down search results. Filters are applied before scoring.

{
  "filters": {
    "document_name": "user-guide.pdf",
    "source_type": "file_upload",
    "metadata": {
      "department": "engineering",
      "version": {"$gte": 2}
    }
  }
}
Filter FieldTypeDescription
document_namestringFilter by exact document name.
document_idstringFilter by document ID.
source_typestringFilter by data source type.
metadataobjectFilter by custom metadata fields. Supports exact match and operators: $eq, $ne, $gt, $gte, $lt, $lte, $in.

Response Body

{
  "object": "list",
  "results": [
    {
      "chunk_id": "chunk_abc123",
      "text": "To configure SSL certificates, navigate to Settings > Security > SSL/TLS. Upload your certificate file and private key...",
      "score": 0.9234,
      "metadata": {
        "department": "engineering",
        "version": 3
      },
      "document_id": "doc_xyz789",
      "document_name": "admin-guide.pdf"
    },
    {
      "chunk_id": "chunk_def456",
      "text": "SSL certificates can be renewed automatically by enabling auto-renewal in the dashboard...",
      "score": 0.8756,
      "metadata": {
        "department": "engineering",
        "version": 3
      },
      "document_id": "doc_xyz789",
      "document_name": "admin-guide.pdf"
    }
  ],
  "query": "How do I configure SSL certificates?",
  "search_type": "hybrid",
  "knowledge_base_id": "kb_abc123"
}
FieldTypeDescription
objectstringAlways "list".
resultsarrayThe retrieved chunks, sorted by relevance score in descending order.
results[].chunk_idstringThe unique identifier of the chunk.
results[].textstringThe text content of the chunk.
results[].scorenumberThe relevance score between 0 and 1. Higher is more relevant.
results[].metadataobjectDocument metadata, included when include_metadata is true.
results[].document_idstringThe ID of the source document.
results[].document_namestringThe name of the source document.
querystringThe original query text.
search_typestringThe search type that was used.
knowledge_base_idstringThe knowledge base that was searched.

Examples

Basic Retrieval

curl

curl https://api.tensoras.ai/v1/knowledge-bases/kb_abc123/retrieve \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer tns_your_key_here" \
  -d '{
    "query": "How do I configure SSL certificates?",
    "top_k": 5,
    "search_type": "hybrid"
  }'

Python

import requests
 
response = requests.post(
    "https://api.tensoras.ai/v1/knowledge-bases/kb_abc123/retrieve",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer tns_your_key_here",
    },
    json={
        "query": "How do I configure SSL certificates?",
        "top_k": 5,
        "search_type": "hybrid",
    },
)
 
results = response.json()["results"]
for result in results:
    print(f"[{result['score']:.4f}] {result['document_name']}")
    print(f"  {result['text'][:100]}...")
    print()

Node.js

const response = await fetch(
  "https://api.tensoras.ai/v1/knowledge-bases/kb_abc123/retrieve",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer tns_your_key_here",
    },
    body: JSON.stringify({
      query: "How do I configure SSL certificates?",
      top_k: 5,
      search_type: "hybrid",
    }),
  }
);
 
const { results } = await response.json();
for (const result of results) {
  console.log(`[${result.score.toFixed(4)}] ${result.document_name}`);
  console.log(`  ${result.text.slice(0, 100)}...`);
  console.log();
}

Retrieval with Filters

curl

curl https://api.tensoras.ai/v1/knowledge-bases/kb_abc123/retrieve \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer tns_your_key_here" \
  -d '{
    "query": "deployment best practices",
    "top_k": 10,
    "search_type": "vector",
    "filters": {
      "metadata": {
        "department": "engineering",
        "version": {"$gte": 2}
      }
    },
    "min_score": 0.5
  }'

Python

import requests
 
response = requests.post(
    "https://api.tensoras.ai/v1/knowledge-bases/kb_abc123/retrieve",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer tns_your_key_here",
    },
    json={
        "query": "deployment best practices",
        "top_k": 10,
        "search_type": "vector",
        "filters": {
            "metadata": {
                "department": "engineering",
                "version": {"$gte": 2},
            }
        },
        "min_score": 0.5,
    },
)
 
results = response.json()["results"]
print(f"Found {len(results)} results above threshold")
for result in results:
    print(f"  [{result['score']:.4f}] {result['document_name']}: {result['text'][:80]}...")

Node.js

const response = await fetch(
  "https://api.tensoras.ai/v1/knowledge-bases/kb_abc123/retrieve",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer tns_your_key_here",
    },
    body: JSON.stringify({
      query: "deployment best practices",
      top_k: 10,
      search_type: "vector",
      filters: {
        metadata: {
          department: "engineering",
          version: { $gte: 2 },
        },
      },
      min_score: 0.5,
    }),
  }
);
 
const { results } = await response.json();
console.log(`Found ${results.length} results above threshold`);
for (const result of results) {
  console.log(
    `  [${result.score.toFixed(4)}] ${result.document_name}: ${result.text.slice(0, 80)}...`
  );
}

Full RAG Pipeline

Combine retrieval with chat completions for a complete RAG implementation.

Python

import requests
from openai import OpenAI
 
API_BASE = "https://api.tensoras.ai/v1"
API_KEY = "tns_your_key_here"
 
# Step 1: Retrieve relevant context
retrieval_response = requests.post(
    f"{API_BASE}/knowledge-bases/kb_abc123/retrieve",
    headers={
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}",
    },
    json={
        "query": "What is the refund policy?",
        "top_k": 5,
        "search_type": "hybrid",
    },
)
 
chunks = retrieval_response.json()["results"]
context = "\n\n---\n\n".join([chunk["text"] for chunk in chunks])
 
# Step 2: Generate a response using the retrieved context
client = OpenAI(base_url=API_BASE, api_key=API_KEY)
 
response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {
            "role": "system",
            "content": f"Answer the user's question based on the following context. If the answer is not in the context, say so.\n\nContext:\n{context}",
        },
        {"role": "user", "content": "What is the refund policy?"},
    ],
    temperature=0.3,
)
 
print(response.choices[0].message.content)

Error Handling

{
  "error": {
    "message": "Knowledge base 'kb_abc123' not found",
    "type": "not_found_error",
    "param": "kb_id",
    "code": "knowledge_base_not_found"
  }
}