Core Concepts
Retrieve context
Get the most relevant chunks from an engine and give them to the model you already use.
Sometimes your own model should write the answer, because you've tuned its prompt, picked its provider, or need it to follow your app's format. lynk.retrieve runs the search and ranking without writing an answer, and returns the chunks for your model to use.
Retrieve context when...
- You run your own model and prompt.
- You want to combine Docklee's context with other context in one prompt.
- You need the source text itself, not a summary.
Retrieve chunks
This example retrieves the top chunks for one question:
from docklee import Lynk
lynk = Lynk(engine_id="your-engine-id")
chunks = lynk.retrieve_sync("What is the refund window?", top_k=5)
for chunk in chunks:
print(chunk.chunk_id, chunk.content)The chunks come back best match first, from vector and OKF sources alike:
{
"chunks": [
{
"chunk_id": "c2a8f5e1-7b39-4d6c-a014-8e3f2b9d61c7",
"content": "Customers can request a refund within 30 days of purchase.",
"metadata": {"policy": "refunds"}
},
{
"chunk_id": "7e3b9a04-1f6c-4d25-8b97-2a5e0c8d1f63",
"content": "After 30 days, purchases can be exchanged for store credit.",
"metadata": {"policy": "refunds"}
}
]
}retrieve options
| Option | Python | TypeScript | Default | Description |
|---|---|---|---|---|
| Question | question | first argument | none | The question, in plain words. |
| Engine | engine_id | engineId | DOCKLEE_ENGINE_ID | The engine to search for this call. |
| Result count | top_k | topK | 8 | How many chunks to return, from 1 to 100. |
Your plan can cap top_k lower than the range above.
The chunk
| Python | TypeScript | Description |
|---|---|---|
chunk_id | chunkId | The chunk's ID. |
content | content | The chunk's text. |
metadata | metadata | The metadata stored with the chunk, such as what you passed to capture. |
Build a prompt
Number the chunks so your model can cite them back to you:
chunks = lynk.retrieve_sync(question, top_k=5)
context = "\n\n".join(f"[{i}] {chunk.content}" for i, chunk in enumerate(chunks, 1))
prompt = (
"Answer from the sources below. Cite them by number. "
"If they don't contain the answer, say so.\n\n"
f"{context}\n\nQuestion: {question}"
)Retrieve from an engine and recall from a memory space in the same turn. Put both in one prompt, and your model can answer from your content and remember the user at the same time.
Next steps
Your model has the context. See how the two engine sources work together next.