graphzep
Version:
GraphZep: A temporal knowledge graph memory system for AI agents based on the Zep paper
168 lines (142 loc) • 4.44 kB
text/typescript
import { BaseGraphDriver } from './driver.js';
import { GraphProvider } from '../types/index.js';
export interface RDFDriverConfig {
uri?: string;
username?: string;
password?: string;
database?: string;
inMemory?: boolean;
sparqlEndpoint?: string;
customOntologyPath?: string;
cacheSize?: number;
batchSize?: number;
}
export interface RDFTriple {
subject: string;
predicate: string;
object: string | { value: string; type: string; datatype?: string; language?: string };
}
export class SimplifiedRDFDriver extends BaseGraphDriver {
provider = GraphProvider.RDF;
private triples: RDFTriple[] = [];
private config: RDFDriverConfig;
private isConnected = false;
constructor(config: RDFDriverConfig = {}) {
super(
config.uri || 'memory://graphzep',
config.username || '',
config.password || '',
config.database || 'default'
);
this.config = {
inMemory: true,
cacheSize: 10000,
batchSize: 1000,
...config
};
}
async connect(): Promise<void> {
this.isConnected = true;
}
async close(): Promise<void> {
this.isConnected = false;
}
async createIndexes(): Promise<void> {
// No-op for simplified implementation
}
async executeQuery<T = any>(query: string, _params?: Record<string, any>): Promise<T> {
if (!this.isConnected) {
throw new Error('RDF driver not connected');
}
// Simplified SPARQL simulation - in real implementation would use proper SPARQL engine
console.log('Executing SPARQL query:', query);
// Return mock results for demo
return [] as any;
}
/**
* Add RDF triples to the store
*/
async addTriples(triples: RDFTriple[]): Promise<void> {
this.triples.push(...triples);
}
/**
* Get all stored triples
*/
getTriples(): RDFTriple[] {
return [...this.triples];
}
/**
* Clear all triples
*/
clearTriples(): void {
this.triples = [];
}
/**
* Serialize to basic formats
*/
async serialize(format: 'turtle' | 'rdf-xml' | 'json-ld' | 'n-triples' = 'turtle'): Promise<string> {
switch (format) {
case 'turtle':
return this.serializeToTurtle();
case 'json-ld':
return this.serializeToJsonLD();
default:
return this.serializeToTurtle();
}
}
private serializeToTurtle(): string {
const prefixes = [
'@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .',
'@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .',
'@prefix owl: <http://www.w3.org/2002/07/owl#> .',
'@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .',
'@prefix zep: <http://graphzep.ai/ontology#> .',
'@prefix zepmem: <http://graphzep.ai/memory#> .',
'@prefix zepent: <http://graphzep.ai/entity#> .',
''
].join('\n');
const tripleStrings = this.triples.map(triple => {
const object = typeof triple.object === 'string'
? `<${triple.object}>`
: `"${triple.object.value}"`;
return `<${triple.subject}> <${triple.predicate}> ${object} .`;
});
return prefixes + tripleStrings.join('\n');
}
private serializeToJsonLD(): string {
const context = {
'@context': {
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'owl': 'http://www.w3.org/2002/07/owl#',
'xsd': 'http://www.w3.org/2001/XMLSchema#',
'zep': 'http://graphzep.ai/ontology#',
'zepmem': 'http://graphzep.ai/memory#',
'zepent': 'http://graphzep.ai/entity#'
}
};
const graph = this.triples.map(triple => ({
'@id': triple.subject,
[triple.predicate]: typeof triple.object === 'string'
? { '@id': triple.object }
: { '@value': triple.object.value, '@type': triple.object.datatype }
}));
return JSON.stringify({ ...context, '@graph': graph }, null, 2);
}
/**
* Execute basic SPARQL-like queries
*/
async executeSPARQL(query: string): Promise<any[]> {
console.log('SPARQL Query:', query);
// Simple pattern matching for demo
if (query.includes('SELECT')) {
return this.triples.map((triple, index) => ({
subject: triple.subject,
predicate: triple.predicate,
object: triple.object,
index
}));
}
return [];
}
}