Skip to main content
POST
/
api
/
chat
Query chat
curl --request POST \
  --url https://api.example.com/api/chat \
  --header 'Content-Type: application/json' \
  --data '
{
  "query": "<string>"
}
'
{
  "response": "<string>"
}

Overview

The chat endpoint allows you to ask natural language questions about your uploaded documents. It uses a RAG (Retrieval-Augmented Generation) system to find relevant information from your documents and generate accurate, context-aware responses.

Authentication

This endpoint requires authentication using Laravel Sanctum. Include your API token in the request.
The chat endpoint only searches through documents that belong to the authenticated user.

How RAG works

The RAG system processes your query through the following steps:
  1. Query embedding - Your question is converted into a vector embedding using the same embedding model used for document chunks
  2. Vector search - MongoDB’s vector search finds the 3 most relevant document chunks (from 100 candidates) that match your query
  3. Context retrieval - The content from matching chunks is assembled into context, separated by delimiters
  4. LLM generation - Your question and the retrieved context are sent to an LLM (via OpenRouter) to generate a natural language answer
The system uses MongoDB Atlas Vector Search with cosine similarity to find the most relevant document chunks.

Request

Body parameters

query
string
required
The question you want to ask about your documents. Maximum 1000 characters.

Response

response
string
The AI-generated answer based on your documents. This is a natural language response that synthesizes information from the retrieved document chunks.

Error responses

The endpoint handles several error scenarios gracefully:
If no relevant documents are found, you’ll receive: "I couldn't find any relevant information in your documents to answer that."

Validation errors

Status code: 422
{
  "message": "The query field is required. (and 1 more error)",
  "errors": {
    "query": [
      "The query field is required."
    ]
  }
}

Authentication errors

Status code: 401
{
  "message": "Unauthenticated."
}

AI service errors

If the embedding service fails or the LLM API encounters an error, you’ll receive a descriptive error message in the response field:
  • "I'm sorry, I couldn't process your request at the moment." - Embedding generation failed
  • "Error communicating with AI service." - LLM API request failed
  • "An unexpected error occurred." - Unexpected exception during processing

Examples

curl -X POST https://api.filebright.com/api/chat \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "query": "What are the main features of the product?"
  }'

Response example

{
  "response": "Based on your documents, the main features of the product include: advanced document processing with OCR capabilities, real-time collaboration tools, automated workflow management, and comprehensive analytics dashboards. The product also offers integration with popular cloud storage services and supports over 50 file formats."
}

Vector search configuration

The RAG system uses MongoDB Atlas Vector Search with the following configuration:
  • Index name: vector_index
  • Vector field: embedding
  • Number of candidates: 100
  • Results returned: 3 most relevant chunks
  • Filter: User ID (ensures you only search your own documents)
The system retrieves the top 3 most relevant chunks from your documents. These chunks are concatenated with \n\n---\n\n delimiters to provide context to the LLM.

Build docs developers (and LLMs) love