Contextual Retrieval
Exposes two Go functions that prepend a short LLM-generated context to each document chunk before retrieval, improving search accuracy.
What this file does
Exposes two Go functions that prepend a short LLM-generated context to each document chunk before retrieval, improving search accuracy.
When to use it
- Adding context to chunks before embedding for RAG pipelines
- Improving retrieval quality on long documents
- Using Ollama as the LLM backend for contextual enrichment
- Customising the prompt template for chunk context generation
Assumes this stack
Contextual Retrieval
!!! info "📦 content package"
!!! note "Inspired by: Introducing Contextual Retrieval"
CreateChunkContext
CreateChunkContext generates a succinct context for a given chunk within the whole document content.
This context is intended to improve search retrieval of the chunk.
Parameters:
- wholeDocumentContent: The entire content of the document as a string.
- chunk: The specific chunk of the document for which context is to be generated.
- ollamaUrl: The URL for the Ollama service.
- contextualModel: The model used for generating the context.
- options: Additional options for the LLM (Language Model).
Returns:
- A string containing the succinct context for the chunk.
- An error if the context generation fails.
CreateChunkContextuse a default template for the prompt. If you want to use a custom template, you can useCreateChunkContextWithPromptTemplate.
CreateChunkContextWithPromptTemplate
CreateChunkContextWithPromptTemplate generates a contextual response based on a given prompt template and document content.
It interpolates the template with the provided document and chunk content, then uses an LLM to generate a response.
Parameters:
- promptTpl: A string template for the prompt.
- wholeDocumentContent: The content of the entire document.
- chunk: A Chunk struct containing a portion of the document content.
- ollamaUrl: The URL of the LLM service.
- contextualModel: The model to be used for generating the response.
- options: Options for the LLM query.
Returns:
- A string containing the generated response.
- An error if the process fails at any step.
Template example
promptTemplateForContext := `
<chunk>
{{.chunkContent}}
</chunk>
Generate a brief context of the above chunk to situate this chunk within the below document.
<document>
{{.wholeDocument}}
</document>
`
!!! note 👀 you will find a complete example in:
- [examples/59-jean-luc-picard-contextual-retrieval](https://github.com/parakeet-nest/parakeet/tree/main/examples/59-jean-luc-picard-contextual-retrieval)
What's inside
2 exported functions, 1 default prompt template, 1 custom-prompt variant, 1 code example link
Change this for your project
- Replace
ollamaUrlwith your own Ollama service endpoint - Replace
contextualModelwith the model name you intend to use - Replace the example prompt template in
promptTemplateForContextif you need different wording
Where it goes
A prompt collection. Copy the individual prompts you need rather than the whole file.
Related Documents
starProject
Lists 120+ starred open-source projects across AI, DevOps, Flutter, and web development for discovery and inspiration.
midjourney-expert
Serves as a reference for Midjourney V7/Niji 7 prompting, covering parameters, reference systems, editing tools, and moderation workarounds.
🎣 Hooks & Headlines Generator
Generates 20, 30 headline variations across 10 proven formulas, with psychological reasoning and platform-specific best practices.
Daily Agent Tasks Framework
Gives you a daily structure for assigning strategic work to Claude and development work to Codex, organized by project priority and current sprint.