Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ def get_graphrag_config(graphname=None):
graphrag_config["chunker"] = "semantic"
if "extractor" not in graphrag_config:
graphrag_config["extractor"] = "llm"
if "tg_memory_schema_on_startup" not in graphrag_config:
graphrag_config["tg_memory_schema_on_startup"] = True
# ``retrieval_include_entity`` is resolved at install time
# (see ``common.db.retriever_render.resolve_include_entity``).

Expand Down
20 changes: 20 additions & 0 deletions common/gsql/memory/GetLastNMemoryExchanges.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2024-2026 TigerGraph, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Returns up to n "message" vertices for a conversation_id, highest epoch_added first.
*/

CREATE OR REPLACE DISTRIBUTED QUERY get_last_n_memory_exchanges(STRING conv_id, UINT n=4) SYNTAX V2 {
TYPEDEF TUPLE<VERTEX m, UINT ep> MsgRow;
HeapAccum<MsgRow>(n, ep DESC) @@heap;

seeds = {message.*};
res = SELECT m FROM seeds:m
WHERE m.conversation_id == conv_id
ACCUM @@heap += MsgRow(m, m.epoch_added);

PRINT @@heap AS rows;
}
11 changes: 11 additions & 0 deletions common/gsql/memory/ListConversationsForUser.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2024-2026 TigerGraph, Inc.
*
* List conversation vertices for a given user_id (chat memory sidebar).
*/

CREATE OR REPLACE DISTRIBUTED QUERY list_conversations_for_user(STRING uid) SYNTAX V2 {
seeds = {conversation.*};
res = SELECT c FROM seeds:c WHERE c.user_id == uid;
PRINT res AS rows;
}
11 changes: 11 additions & 0 deletions common/gsql/memory/ListMessagesForConversation.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2024-2026 TigerGraph, Inc.
*
* List message vertices for a conversation_id attribute (full thread load).
*/

CREATE OR REPLACE DISTRIBUTED QUERY list_messages_for_conversation(STRING conv_id) SYNTAX V2 {
seeds = {message.*};
res = SELECT m FROM seeds:m WHERE m.conversation_id == conv_id;
PRINT res AS rows;
}
15 changes: 15 additions & 0 deletions common/gsql/memory/ListMessagesForMemory.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2024-2026 TigerGraph, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* List message vertices for a conversation_id (application id on message rows).
* Python callers MUST strip tracelog — memory pipeline must not use tracelog.
*/

CREATE OR REPLACE DISTRIBUTED QUERY list_messages_for_memory(STRING conv_id) SYNTAX V2 {
seeds = {message.*};
res = SELECT m FROM seeds:m WHERE m.conversation_id == conv_id;
PRINT res AS rows;
}
31 changes: 31 additions & 0 deletions common/gsql/memory/Memory_Schema.gsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024-2026 TigerGraph, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Chat memory on the same GraphRAG graph (Explore Graph: vertex types "conversation", "message").
* - conversation: one vertex per thread.
* - message: one vertex per Q&A (user_content + system_content on the same row).
* - epoch_*: UTC epoch SECONDS (UINT), same style as SupportAI DocumentChunk.
*/

CREATE SCHEMA_CHANGE JOB add_graphrag_chat_memory {
ADD VERTEX conversation(
PRIMARY_ID conversation_id STRING,
user_id STRING,
epoch_added UINT,
epoch_processed UINT
) WITH STATS="OUTDEGREE_BY_EDGETYPE", PRIMARY_ID_AS_ATTRIBUTE="true";

ADD VERTEX message(
PRIMARY_ID message_id STRING,
conversation_id STRING,
user_content STRING,
system_content STRING,
epoch_added UINT,
tracelog STRING
) WITH STATS="OUTDEGREE_BY_EDGETYPE", PRIMARY_ID_AS_ATTRIBUTE="true";

ADD DIRECTED EDGE has_message(FROM conversation, TO message) WITH REVERSE_EDGE="reverse_has_message";
}
9 changes: 9 additions & 0 deletions common/llm_services/base_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ def route_response_prompt(self):
Route the user question to one of: `functions`, `vectorstore`, or `history`.
## CRITICAL — Route to `history` FIRST when:
The conversation history is non-empty AND the question is about this conversation itself:
- What questions or messages were exchanged ("what did I ask", "previous questions", "earlier questions")
- What was said, discussed, or answered previously in this session
- Recalling, summarising, or listing prior exchanges in this chat
- Anything referencing "previous", "earlier", "before", "last time", "you said", "I asked", "we discussed", "prior" in the context of THIS conversation
If the conversation history is EMPTY, do NOT route to `history` — fall through to vectorstore or functions instead.
## Routing
- **`history`**: questions similar to previous ones, or that reference earlier answers / responses, or that refer to the same entities mentioned in a previous answer.
- **`vectorstore`**: questions best answered by text documents.
Expand Down
4 changes: 4 additions & 0 deletions common/memory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) 2024-2026 TigerGraph, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Loading
Loading