| 1 | from graphrag_sdk.ontology import Ontology |
| 2 | from graphrag_sdk.model_config import KnowledgeGraphModelConfig |
| 3 | from graphrag_sdk.steps.graph_query_step import GraphQueryGenerationStep |
| 4 | from graphrag_sdk.steps.qa_step import QAStep |
| 5 | from graphrag_sdk.fixtures.prompts import GRAPH_QA_SYSTEM, CYPHER_GEN_SYSTEM |
| 6 | from falkordb import Graph |
| 7 | |
| 8 | |
| 9 | class ChatSession: |
| 10 | """ |
| 11 | Represents a chat session with a Knowledge Graph. |
| 12 | |
| 13 | Args: |
| 14 | model_config (KnowledgeGraphModelConfig): The model configuration to use. |
| 15 | ontology (Ontology): The ontology to use. |
| 16 | graph (Graph): The graph to query. |
| 17 | |
| 18 | Examples: |
| 19 | >>> from graphrag_sdk import KnowledgeGraph, Orchestrator |
| 20 | >>> from graphrag_sdk.ontology import Ontology |
| 21 | >>> from graphrag_sdk.model_config import KnowledgeGraphModelConfig |
| 22 | >>> model_config = KnowledgeGraphModelConfig.with_model(model) |
| 23 | >>> kg = KnowledgeGraph("test_kg", model_config, ontology) |
| 24 | >>> chat_session = kg.start_chat() |
| 25 | >>> chat_session.send_message("What is the capital of France?") |
| 26 | """ |
| 27 | |
| 28 | def __init__(self, model_config: KnowledgeGraphModelConfig, ontology: Ontology, graph: Graph): |
| 29 | """ |
| 30 | Initializes a new ChatSession object. |
| 31 | |
| 32 | Args: |
| 33 | model_config (KnowledgeGraphModelConfig): The model configuration. |
| 34 | ontology (Ontology): The ontology object. |
| 35 | graph (Graph): The graph object. |
| 36 | |
| 37 | Attributes: |
| 38 | model_config (KnowledgeGraphModelConfig): The model configuration. |
| 39 | ontology (Ontology): The ontology object. |
| 40 | graph (Graph): The graph object. |
| 41 | cypher_chat_session (CypherChatSession): The Cypher chat session object. |
| 42 | qa_chat_session (QAChatSession): The QA chat session object. |
| 43 | """ |
| 44 | self.model_config = model_config |
| 45 | self.graph = graph |
| 46 | self.ontology = ontology |
| 47 | self.cypher_chat_session = ( |
| 48 | model_config.cypher_generation.with_system_instruction( |
| 49 | CYPHER_GEN_SYSTEM.replace("#ONTOLOGY", str(ontology.to_json())) |
| 50 | ).start_chat() |
| 51 | ) |
| 52 | self.qa_chat_session = model_config.qa.with_system_instruction( |
| 53 | GRAPH_QA_SYSTEM |
| 54 | ).start_chat() |
| 55 | |
| 56 | def send_message(self, message: str): |
| 57 | """ |
| 58 | Sends a message to the chat session. |
| 59 | |
| 60 | Args: |
| 61 | message (str): The message to send. |
| 62 | |
| 63 | Returns: |
| 64 | str: The response to the message. |
| 65 | """ |
| 66 | cypher_step = GraphQueryGenerationStep( |
| 67 | graph=self.graph, |
| 68 | chat_session=self.cypher_chat_session, |
| 69 | ontology=self.ontology, |
| 70 | ) |
| 71 | |
| 72 | (context, cypher) = cypher_step.run(message) |
| 73 | |
| 74 | if not cypher or len(cypher) == 0: |
| 75 | return "I am sorry, I could not find the answer to your question" |
| 76 | |
| 77 | qa_step = QAStep( |
| 78 | chat_session=self.qa_chat_session, |
| 79 | ) |
| 80 | |
| 81 | answer = qa_step.run(message, cypher, context) |
| 82 | |
| 83 | return answer |