-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dan Selman <[email protected]>
- Loading branch information
Showing
3 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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()); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|