Skip to content

Latest commit

 

History

History
159 lines (128 loc) · 3.43 KB

File metadata and controls

159 lines (128 loc) · 3.43 KB

Graph Administration

The graph admin API provides direct CRUD operations on the knowledge graph, bypassing the document pipeline.

import { createLightRAGGraphAdmin } from "@gsrag/core";

const admin = createLightRAGGraphAdmin(storages);

Inspection

// List all graph labels
const labels = await admin.getGraphLabels();

// Search labels
const results = await admin.searchLabels("Al", { limit: 10 });

// Get popular labels
const popular = await admin.getPopularLabels(20);

// Check if entity exists
const { exists } = await admin.entityExists("Ada Lovelace");

// Get graph neighborhood
const graph = await admin.getKnowledgeGraph({
  label: "Ada Lovelace",
  maxDepth: 2,
  maxNodes: 100,
});
// Returns { nodes: KnowledgeGraphNode[], edges: KnowledgeGraphEdge[], isTruncated: boolean }

// Get entity details
const entity = await admin.getEntityInfo("Ada Lovelace", { includeVectorData: false });

// Get relation details
const relation = await admin.getRelationInfo("Ada Lovelace", "Charles Babbage");

Create

// Create entity
await admin.createEntity("Ada Lovelace", {
  entityType: "Person",
  description: "First computer programmer.",
  sourceId: "doc-001",
  filePath: "history.txt",
});

// Create relation
await admin.createRelation("Ada Lovelace", "Charles Babbage", {
  keywords: "collaborator, mentor",
  description: "Ada worked with Babbage on the Analytical Engine.",
  weight: 1.0,
  sourceId: "doc-001",
});

Update

// Edit entity
await admin.editEntity("Ada Lovelace", {
  description: "First computer programmer. Wrote the first algorithm.",
}, {
  allowRename: false,   // allow renaming the entity
  allowMerge: false,    // allow merging with existing entities
});

// Edit relation
await admin.editRelation("Ada Lovelace", "Charles Babbage", {
  description: "Updated description.",
  keywords: "collaborator, mentor, analytical engine",
});

Delete

// Delete entity and all its relationships
await admin.deleteEntity("Ada Lovelace");

// Delete a specific relationship
await admin.deleteRelation("Ada Lovelace", "Charles Babbage");

Merge

// Merge multiple entities into one
await admin.mergeEntities(
  ["Ada Lovelace", "Ada Byron", "Countess of Lovelace"],
  "Ada Lovelace"
);

Custom Knowledge Graph

Insert a fully pre-built knowledge graph (chunks, entities, and relationships):

import type { CustomKgInput } from "@gsrag/core";

const customKg: CustomKgInput = {
  chunks: [
    {
      content: "Ada Lovelace was a mathematician...",
      filePath: "custom_kg",
      id: "chunk-1",
    },
  ],
  entities: [
    {
      entityName: "Ada Lovelace",
      entityType: "Person",
      description: "Mathematician.",
      sourceId: "chunk-1",
    },
  ],
  relationships: [
    {
      srcId: "Ada Lovelace",
      tgtId: "Charles Babbage",
      keywords: "collaborator",
      description: "Collaborated on Analytical Engine.",
      sourceId: "chunk-1",
    },
  ],
};

await admin.insertCustomKnowledgeGraph(customKg);

Entity Data Shape

interface EntityCreateData {
  entityType?: string;
  description?: string;
  sourceId?: string;
  filePath?: string;
  // Plus any custom properties
}

Relation Data Shape

interface RelationCreateData {
  keywords?: string;
  description?: string;
  weight?: number;
  sourceId?: string;
  filePath?: string;
  // Plus any custom properties
}