Progressive context loading with three-layer information model
OpenViking uses a three-layer information model (L0/L1/L2) to balance retrieval efficiency and content completeness. This design enables progressive detail loading, significantly reducing token consumption while maintaining context quality.
# Hierarchical Retriever OverviewImplements directory-based hierarchical retrieval with recursive search and rerank scoring.## Key Classes- **HierarchicalRetriever**: Main retriever class with recursive search algorithm- **RetrieverMode**: THINKING (with rerank) vs QUICK (vector only)## Key Methods- `retrieve()`: Execute hierarchical retrieval with intent-based queries- `_recursive_search()`: Recursive directory traversal using priority queue- `_merge_starting_points()`: Combine root directories with global search results## Algorithm1. Determine starting directories based on context type2. Global vector search to supplement starting points3. Recursive search using priority queue (score propagation)4. Convergence detection (stop after 3 unchanged rounds)## AccessRead full implementation at `viking://resources/openviking/retrieve/hierarchical_retriever.py`
# Get L1 overviewoverview = await client.overview("viking://resources/docs/auth")print(overview)# Use overview to decide if L2 is neededif "OAuth 2.0" in overview and needs_oauth_details: # Load full OAuth documentation (L2) content = await client.read("viking://resources/docs/auth/oauth.md")
# OAuth 2.0 Authentication## OverviewOAuth 2.0 is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service.## Authorization Code Flow### Step 1: Authorization RequestDirect the user to the authorization endpoint:
GET /oauth/authorize?
response_type=code&
client_id=YOUR_CLIENT_ID&
redirect_uri=YOUR_REDIRECT_URI&
scope=read write
Copy
### Step 2: User AuthorizationThe user authenticates and grants permission...[... full documentation continues for several thousand tokens ...]
When you need to quickly filter relevant contexts from search results:
Copy
results = await client.find("authentication")# L0 abstracts are already in resultsfor ctx in results.resources: if "OAuth" in ctx.abstract: # Quick check using L0 print(f"Relevant: {ctx.uri}")
Understand content scope - Use L1
When you need to understand what a resource contains before loading:
Copy
# Get L1 overview firstoverview = await client.overview("viking://resources/docs/auth")# Understand structure and decide which L2 to loadif "OAuth 2.0" in overview: print("This contains OAuth documentation")
Detailed information extraction - Use L2
Only load L2 when you need the complete content:
Copy
# Load L2 only after confirming relevance via L0/L1content = await client.read("viking://resources/docs/auth/oauth.md")# Now you have the full contentprint(content)
Building context for LLM - Use L1
L1 is usually sufficient for building context:
Copy
# Get L1 overviews for multiple resourcescontext_parts = []for uri in relevant_uris: overview = await client.overview(uri) context_parts.append(f"## {uri}\n{overview}")# Build prompt with L1 content (saves tokens)prompt = "\n\n".join(context_parts)