alRetrieval
Ingest documents and query them with natural-language semantic retrieval. Supports PDF, DOCX, PPTX, TXT, and Markdown files.
- Path
- /v1/alRetrieval
- Key scope
- alretrieval
Supported file formats
PDF, DOCX, PPTX, TXT, MD
PDFs are processed via OCR; DOCX, PPTX, TXT, and MD have text extracted directly — no OCR cost
Scope Required: All alRetrieval endpoints require an API key with the alretrieval scope. Pass it as a Bearer token in the Authorization header.
POSTGenerate an answer
Retrieves passages the same way as retrieve, then writes an answer grounded in them, in the language of the question, with inline citations: ((مسار: …)) for sections, ((صفحة N)) for pages. Use the structured sources array in your interface. Costs 2 requests (retrieval + generation). The API keeps no conversation state: send earlier turns in history.
POST /v1/alRetrieval/collections/{collection_id}/answer
Parameters:
| Parameter | Type | Description |
|---|---|---|
query | string | required — 1 to 4,000 characters. |
document_ids | string[] | optional — as in retrieve. |
history | object[] | optional — earlier turns, oldest first: {"role": "user" | "assistant", "content"}. |
stream | boolean | optional — stream the answer as server-sent events. |
Response (200 OK):
{
"answer": "الإجازة السنوية مدتها ثلاثون يوماً ((صفحة 1)).",
"pipeline": "basic",
"document_ids": ["3146366e04ff"],
"sources": [ { "document_id": "3146366e04ff", "document_title": "Leave policy", "text": "...", "score": 1.0,
"page_start": 1, "page_end": 1, "pages": [1], "section": null, "section_paths": [], "is_direct_hit": true } ],
"rephrased_query": null,
"search_terms": null,
"strategy": "fast"
}
rephrased_query and search_terms are set only when the first attempt found strong passages but no answer, so the question was retried once, rephrased; otherwise both are null. The stream's sources event carries them too.
strategy is "fast" or "agent". When the research agent answered (a structural question such as the last article of a law), steps lists what it read: {"index", "tool", "input", "result", "message", "elapsed_ms"}.
Streaming:
With "stream": true the response is text/event-stream: one sources event, many delta events, then done. An error event replaces done if generation fails mid-stream. Lines starting with : are keep-alive comments; ignore them. When the research agent answers, status events ({"message": "…"}, in the language of the question, ready to show while it works) and step events (the steps entries) come before sources.
event: sources
data: {"pipeline": "deep_tree", "document_ids": ["3146366e04ff"], "sources": [ ... ]}
event: delta
data: {"text": "الإجازة السنوية مدتها "}
event: delta
data: {"text": "ثلاثون يوماً ..."}
event: done
data: {"finish_reason": "stop"}
document_ids.
Code Examples:
import json, requests
headers = {"Authorization": "Bearer YOUR_API_KEY"}
url = "https://alapi.deep.sa/v1/alRetrieval/collections/COLLECTION_ID/answer"
answer = requests.post(url, json={"query": "كم مدة الإجازة السنوية؟"}, headers=headers).json()
print(answer["answer"])
# Streaming
with requests.post(url, json={"query": "وماذا بعد عشر سنوات؟", "stream": True,
"history": [{"role": "user", "content": "كم مدة الإجازة السنوية؟"},
{"role": "assistant", "content": answer["answer"]}]},
headers=headers, stream=True) as res:
event = None
for line in res.iter_lines(decode_unicode=True):
if line.startswith("event: "):
event = line[7:]
elif line.startswith("data: ") and event == "delta":
print(json.loads(line[6:])["text"], end="", flush=True)
Ready to Get Started?
Generate an API key from your dashboard and start building