Skip to main content

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

uri
string
required
The Viking URI to query for relations

Response

Returns a list of relation objects:
from_uri
string
Source URI (the queried resource)
to_uri
string
Target URI (the linked resource)
reason
string
Explanation of why resources are related
created_at
number
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']}")
# 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

  1. Documentation Navigation - Link code to docs, examples to tutorials
  2. Knowledge Graphs - Build semantic connections between concepts
  3. Related Content Discovery - Surface relevant resources during retrieval
  4. Citation Tracking - Track references between documents
  • link() - Create relations between resources
  • unlink() - Remove relations
  • find() - Semantic search can use relations for context