Skip to content

Commit

Permalink
feat: store models in graph
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman committed May 30, 2024
1 parent de758ed commit 2b51c2d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Conversation } from '../Conversation';
const NS = '[email protected]';

const MODEL = `
@description("Ask questions about movies and the actors and directors related to movies.")
namespace ${NS}
import [email protected].{GraphNode}
Expand Down Expand Up @@ -78,6 +79,13 @@ async function run() {
await graphModel.deleteGraph();
await graphModel.dropIndexes();
await graphModel.createIndexes();
await graphModel.mergeConcertoModels();
const models = await graphModel.queryConcertoModels();
if(models) {
models.forEach(m => {
console.log(m);
});
}
const context = await graphModel.openSession();

const { session } = context;
Expand Down
46 changes: 44 additions & 2 deletions src/graphmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ROOT_MODEL, ROOT_NAMESPACE } from "./model";
import { getTextChecksum, textToCypher } from "./functions";
import { RunnableToolFunction } from "openai/lib/RunnableFunction";

const MODELS_SEP = '----/----';

/**
* Provides typed-access to Neo4J graph database
* with the nodes and relationships for the graph defined
Expand Down Expand Up @@ -59,6 +61,39 @@ export class GraphModel {
throw new Error('No neo4j driver!');
}

/**
* Stores the Concerto Models in the graph
* @returns promise to indicate the operation is complete
*/
async mergeConcertoModels() {
const context = await this.openSession();
const { session } = context;
await session.executeWrite(async transaction => {
await this.mergeNode(transaction, '[email protected]', { identifier: '000', content: this.getConcertoModels() });
});
this.options.logger?.info('Stored Concerto Models');
return this.closeSession(context);
}

/**
* Reads the concerto model from the graph. To write it
* use the mergeConcertoModels method.
* @returns returns the concerto model from the graph
*/
async queryConcertoModels() : Promise<Array<string>|undefined> {
const context = await this.openSession();
let result:Array<string>|undefined = undefined;
const { session } = context;
await session.executeRead(async transaction => {
const models = await this.query('MATCH(m:ConcertoModels where m.identifier="000") return m.content;', undefined, transaction);
if(models.records.length === 1) {
result = models.records[0].get('m.content').split(MODELS_SEP).filter( m => m.trim().length > 0);
}
});
await this.closeSession(context);
return result;
}

/**
* Closes a database context.
* @param context the database context
Expand Down Expand Up @@ -478,14 +513,21 @@ export class GraphModel {
}
}

/**
* Returns the Concerto models as a string
* @returns the concerto models as a string
*/
getConcertoModels() : string {
return this.modelManager.getModels().reduce((prev, cur) => prev += MODELS_SEP + cur.content, '');
}

/**
* Converts a natural language query string to a Cypher query (without running it)
* @param text the input text
* @returns the Cypher query
*/
async textToCypher(text: string): Promise<string | null> {
const ctoModels = this.modelManager.getModels().reduce((prev, cur) => prev += cur.content, '');
return textToCypher(this.options, text, ctoModels);
return textToCypher(this.options, text, this.getConcertoModels());
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const ROOT_NAMESPACE = '[email protected]';
* The concerto graph model, defines internal nodes
*/
export const ROOT_MODEL = `namespace ${ROOT_NAMESPACE}
concept ConcertoModels extends GraphNode {
o String content
}
concept GraphNode identified by identifier {
o String identifier
}
Expand Down

0 comments on commit 2b51c2d

Please sign in to comment.