Skip to content

Commit

Permalink
feat: max content size option for TextToGraph
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <danscode@selman.org>
  • Loading branch information
dselman committed Jun 28, 2024
1 parent f5a246b commit 5d30e3c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/TextToGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class TextToGraph {
logger: options.logger,
openAiOptions: options.openAiOptions,
systemPrompt: options.textToGraphPrompt ? options.textToGraphPrompt : TEXT_TO_GRAPH_PROMPT,
maxContextSize: options.maxContextSize
});
}

Expand All @@ -34,7 +35,7 @@ export class TextToGraph {
* @returns an object that describes which nodes and relationships were added
*/
async mergeText(text: string) {
const messages = await this.conversation.runMessages([this.conversation.getSystemMessage()], `Add the nodes in this text to the knowledge graph: ${text}`);
const messages = await this.conversation.runMessages([this.conversation.getSystemMessage()], text);
const relationships:Array<string> = [];
const nodes:Array<string> = [];
for(let n=0; n < messages.length; n++) {
Expand Down
2 changes: 1 addition & 1 deletion src/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ War for the Planet of the Apes. Pitched by Ball as Apocalypto but with apes and
the story follows a new protagonist Noa (Owen Teague) as he tries to steer the apes away from the totalitarian future
they are headed towards. Freya Allan, Kevin Durand, Dichen Lachman, and William H. Macy star.`;

const textToGraph = new TextToGraph(graphModel, {logger});
const textToGraph = new TextToGraph(graphModel, {logger, maxContextSize: 64000});
const results = await textToGraph.mergeText(text);
logger.success('added nodes and relationships to graph', results);
}
Expand Down
50 changes: 50 additions & 0 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,56 @@ Do not enclose the result in a markdown code block.
`}
}

export function getTextToGraphPrompt(ctoModel:string, text:string) : ChatCompletionSystemMessageParam {
return {
role: 'system',
content: `Convert the natural language query delimited by triple quotes to a Neo4J Cypher query.
Just return the Cypher query, without an explanation for how it works.
The nodes and edges in Neo4j are described by the following Accord Project Concerto model:
\`\`\`
${ctoModel}
\`\`\`
Concerto properties with the @vector_index decorator have a Neo4J vector index. The name
of the vector index is the lowercase name of the declaration with '_' and the lowercase
name of the property appended.
For example: 'movie_summary' is the name of the vector index for the 'Movie.summary' property.
Concerto declarations with any properties with the @fulltext_index decorator have a
full text index. The name of the full text index is the lowercase name of the declaration
with '_fulltext' appended.
For example: movie_fulltext is the name of the full text index for the 'Movie' declaration.
Use the token ${EMBEDDINGS_MAGIC} to denote the embedding vector for input text.
Here is an example Neo4J Cypher query that matches 3 movies summaries by conceptual similarity
(using vector cosine similarity):
\`\`\`
MATCH (l:Movie)
CALL db.index.vector.queryNodes('movie_summary', 10, ${EMBEDDINGS_MAGIC} )
YIELD node AS similar1, score
MATCH (similar)
RETURN similar.identifier as identifier, similar.summary as content, score limit 3
\`\`\`
Use 10 as the second argument to db.index.vector.queryNodes.
Here is an example Neo4J Cypher query that finds the shortest path between two people 'Dan Selman' and 'Ann Selman':
\`\`\`
MATCH
(a:Person {identifier: 'Dan Selman'}),
(b:Person {identifier: 'Ann Selman'}),
p = shortestPath((a)-[:RELATED_TO*]-(b))
RETURN p
\`\`\`
Convert the following natural language query to Neo4J Cypher: """${text}
"""
Do not enclose the result in a markdown code block.
`}
}

export const CONVERSATION_PROMPT = `Please use our knowledge graph, which you can access using tools to answer the following questions.
Query the knowledge graph using the 'chat_with_data' tool before trying other tools.`;

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export type TextToGraphOptions = {
* An optional prompt to further guide conversion of text to graph
*/
textToGraphPrompt?: string

/**
* The maximum context size for the conversation. Old messages
* will be automatically removed once the context size limit is
* reached
*/
maxContextSize?: number;
}

/**
Expand Down

0 comments on commit 5d30e3c

Please sign in to comment.