Overview
The relations() method retrieves all resources linked to a given URI, enabling navigation through the context graph. Relations are directional connections created with link().
Relations help organize related information, such as linking documentation to code, or connecting related concepts.
Method Signature
def relations(uri: str) -> List[Dict[str, Any]]
Parameters
The Viking URI to query for relations
Response
Returns a list of relation objects:
Source URI (the queried resource)
Target URI (the linked resource)
Explanation of why resources are related
Timestamp when relation was created
Examples
List All Relations
from openviking import OpenViking
client = OpenViking(path="./data")
# Get all relations for a resource
relations = client.relations("viking://resources/docs/api.md")
for rel in relations:
print(f"Linked to: {rel['to_uri']}")
print(f"Reason: {rel['reason']}")
Navigate Related Content
# Find and read related documentation
doc_uri = "viking://resources/project/auth.py"
relations = client.relations(doc_uri)
# Read related docs
for rel in relations:
if "docs" in rel['to_uri']:
content = client.read(rel['to_uri'])
print(f"Related doc: {rel['to_uri']}")
print(f"Content: {content[:200]}...")
Create and Query Relations
# Link code to documentation
client.link(
from_uri="viking://resources/code/auth.py",
uris="viking://resources/docs/authentication.md",
reason="Implementation documentation"
)
# Query relations
relations = client.relations("viking://resources/code/auth.py")
print(f"Found {len(relations)} related resources")
Response Example
{
"status": "ok",
"result": [
{
"from_uri": "viking://resources/docs/api.md",
"to_uri": "viking://resources/examples/api_usage.py",
"reason": "Code example for API documentation",
"created_at": 1709712000.0
},
{
"from_uri": "viking://resources/docs/api.md",
"to_uri": "viking://resources/docs/quickstart.md",
"reason": "Related getting started guide",
"created_at": 1709712100.0
}
],
"time": 0.005
}
Use Cases
- Documentation Navigation - Link code to docs, examples to tutorials
- Knowledge Graphs - Build semantic connections between concepts
- Related Content Discovery - Surface relevant resources during retrieval
- Citation Tracking - Track references between documents
- link() - Create relations between resources
- unlink() - Remove relations
- find() - Semantic search can use relations for context