Sphere RAG API

A powerful API for embedding generation and retrieval-augmented generation

Getting Started

The Sphere RAG API provides endpoints for embedding generation and retrieval-augmented generation operations. This documentation will help you understand how to use each endpoint effectively.

The base URL for all API requests is: https://api.getsphere.ai

Our API consists of 5 main endpoints:

For interactive API documentation with a testing interface, visit our Swagger UI docs.

Authentication

All API requests require authentication using an API key. You can obtain your API key by contacting our team.

To authenticate your requests, include your API key in the X-API-Key header:

X-API-Key: your_api_key_here

API Key Details

Your API key determines:

  • Usage quotas and rate limits
  • Pricing tier and billing
  • Access to specific features and content collections

Contact us at support@getsphere.ai for API key management and updates.

API Endpoints

Retrieve Content Chunks

POST /chunks

Search and retrieve relevant content chunks without generating an answer.

Request Parameters

Parameter Type Required Description
query string Yes User query to find relevant content chunks
top_k integer No Number of most relevant chunks to retrieve (default: 5, max: 20)
max_cpm float No Maximum cost per mille (thousand tokens) to filter content
matching_threshold float No Similarity threshold for chunks (0-1 or 0-100)

Example Request

{
  "query": "What are the key factors in climate change?",
  "top_k": 5,
  "matching_threshold": 0.75
}

Example Response

{
  "chunks": [
    {
      "chunk_id": "789012",
      "price": 0.01,
      "publisher": {"id": "pub-456", "name": "Environmental Science Institute"},
      "publication": {
        "id": "journal-789",
        "title": "Climate Research Journal",
        "issn": "5678-9012"
      },
      "media_type": "text/plain",
      "content": "Carbon dioxide emissions are a primary driver of global climate change...",
      "distance": 0.12,
      "timestamp": "2023-02-18T10:15:00Z",
      "created_at": "2023-02-19T08:30:45Z",
      "related_chunks": []
    }
  ],
  "timing": {
    "embedding_seconds": 0.25,
    "postgres_search_seconds": 0.15,
    "retrieval_seconds": 0.40,
    "total_seconds": 0.40
  }
}

Generate Answer

POST /generate

Query content and generate an answer based on retrieved chunks.

Request Parameters

Parameter Type Required Description
query string Yes User query or question to answer
top_k integer No Number of most relevant chunks to retrieve (default: 5, max: 20)
max_cpm float No Maximum cost per mille (thousand tokens) to filter content
matching_threshold float No Similarity threshold for chunks (0-1 or 0-100)

Example Request

{
  "query": "What are the benefits of exercise on mental health?",
  "top_k": 5,
  "matching_threshold": 0.75
}

Example Response

{
  "answer": "Regular exercise has been shown to have significant positive effects on mental health...",
  "chunks": [
    {
      "chunk_id": "123456",
      "price": 0.01,
      "publisher": {"id": "pub-123", "name": "Health Research Institute"},
      "publication": {
        "id": "journal-456",
        "title": "Journal of Mental Health",
        "issn": "1234-5678"
      },
      "media_type": "text/plain",
      "content": "Studies have shown that regular exercise can reduce symptoms of depression and anxiety...",
      "distance": 0.15,
      "timestamp": "2023-01-15T14:30:00Z",
      "created_at": "2023-01-16T09:45:22Z",
      "related_chunks": []
    }
  ],
  "timing": {
    "embedding_seconds": 0.25,
    "postgres_search_seconds": 0.15,
    "retrieval_seconds": 0.40,
    "generation_seconds": 1.20,
    "total_seconds": 1.60
  }
}

Generate Embeddings

POST /embeddings

Generates a vector embedding for the provided text content.

Request Parameters

Parameter Type Description
content string Text content to generate embeddings for

Example Request

{
  "content": "This is a sample text for embedding generation"
}

Example Response

{
  "embedding": [0.023, -0.045, 0.011, 0.087, -0.193, ...],
  "dimension": 1024
}

Batch Embeddings

POST /batch_embeddings

Generate embeddings for multiple text contents in a single request.

Request Parameters

Parameter Type Description
contents array of strings List of text contents to generate embeddings for

Example Request

{
  "contents": [
    "First text to embed",
    "Second text to embed",
    "Third text to embed"
  ]
}

Example Response

{
  "embeddings": [
    [0.023, -0.045, 0.011, ...],
    [0.031, -0.052, 0.018, ...],
    [0.019, -0.038, 0.009, ...]
  ],
  "processing_time_ms": 150.25,
  "avg_per_item_ms": 50.08
}

Health Check

GET /health

Check if the API service is operational.

Example Response

{
  "status": "healthy",
  "service": "sphere_rag"
}

Code Examples

Python

import requests
import json

API_URL = "https://api.getsphere.ai"
API_KEY = "your_api_key_here"
HEADERS = {
    "Content-Type": "application/json",
    "X-API-Key": API_KEY
}

# Health check
def health_check():
    response = requests.get(f"{API_URL}/health")
    return response.json()

# Generate single embedding
def get_embedding(text):
    response = requests.post(
        f"{API_URL}/embeddings",
        headers=HEADERS,
        json={"content": text}
    )
    return response.json()

# Generate batch embeddings
def get_batch_embeddings(texts):
    response = requests.post(
        f"{API_URL}/batch_embeddings",
        headers=HEADERS,
        json={"contents": texts}
    )
    return response.json()

# Get relevant chunks
def get_chunks(query, top_k=5):
    response = requests.post(
        f"{API_URL}/chunks",
        headers=HEADERS,
        json={"query": query, "top_k": top_k}
    )
    return response.json()

# Generate answer from content
def get_answer(query, top_k=5):
    response = requests.post(
        f"{API_URL}/generate",
        headers=HEADERS,
        json={"query": query, "top_k": top_k}
    )
    return response.json()

# Example usage
health = health_check()
embedding = get_embedding("What is machine learning?")
batch_embeddings = get_batch_embeddings(["Text 1", "Text 2", "Text 3"])
chunks = get_chunks("How does climate change affect biodiversity?")
answer = get_answer("What are the benefits of exercise?")

JavaScript

const API_URL = 'https://api.getsphere.ai';
const API_KEY = 'your_api_key_here';
const HEADERS = {
  'Content-Type': 'application/json',
  'X-API-Key': API_KEY
};

// Health check
async function healthCheck() {
  const response = await fetch(`${API_URL}/health`);
  return response.json();
}

// Generate single embedding
async function getEmbedding(text) {
  const response = await fetch(`${API_URL}/embeddings`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({ content: text })
  });
  return response.json();
}

// Generate batch embeddings
async function getBatchEmbeddings(texts) {
  const response = await fetch(`${API_URL}/batch_embeddings`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({ contents: texts })
  });
  return response.json();
}

// Get relevant chunks
async function getChunks(query, topK = 5) {
  const response = await fetch(`${API_URL}/chunks`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({ query, top_k: topK })
  });
  return response.json();
}

// Generate answer from content
async function getAnswer(query, topK = 5) {
  const response = await fetch(`${API_URL}/generate`, {
    method: 'POST',
    headers: HEADERS,
    body: JSON.stringify({ query, top_k: topK })
  });
  return response.json();
}

// Example usage
healthCheck().then(health => console.log(health));
getEmbedding("What is machine learning?").then(embedding => console.log(embedding));
getBatchEmbeddings(["Text 1", "Text 2", "Text 3"]).then(embeddings => console.log(embeddings));
getChunks("How does climate change affect biodiversity?").then(chunks => console.log(chunks));
getAnswer("What are the benefits of exercise?").then(answer => console.log(answer));