MCPcopy Create free account
hub / github.com/AQ-MedAI/MedMemoryBench / query

Method query

methods/graph_rag.py:673–727  ·  view source on GitHub ↗

Query the knowledge graph. Args: question: The question to answer system_message: Optional system message (unused) Returns: AgentResponse with answer and retrieval info

(
        self,
        question: str,
        system_message: Optional[str] = None,
        **kwargs
    )

Source from the content-addressed store, hash-verified

671 )
672
673 def query(
674 self,
675 question: str,
676 system_message: Optional[str] = None,
677 **kwargs
678 ) -> AgentResponse:
679 """Query the knowledge graph.
680
681 Args:
682 question: The question to answer
683 system_message: Optional system message (unused)
684
685 Returns:
686 AgentResponse with answer and retrieval info
687 """
688 # Ensure token usage is attributed to query phase
689 get_usage_tracker().set_phase("query")
690
691 start_time = time.time()
692
693 if not self._graph_built or self._graph_rag is None:
694 logger.error("[GraphRAG] Graph not built, cannot query")
695 return AgentResponse(
696 output="Error: Knowledge graph not built. Please ensure memorize() was called with is_last_session=True.",
697 query_time=time.time() - start_time,
698 retrieved_count=0,
699 retrieved_memories=[],
700 extra={"error": "graph_not_built"}
701 )
702
703 try:
704 response, retrieval_context = self._graph_rag.query(question)
705 response_text = response
706 except Exception as e:
707 logger.error(f"[GraphRAG] Query failed: {e}")
708 response_text = f"Error: {e}"
709 retrieval_context = ""
710
711 query_time = time.time() - start_time
712
713 return AgentResponse(
714 output=response_text,
715 query_time=query_time,
716 retrieved_count=self.top_k,
717 retrieved_memories=[
718 {"memory": retrieval_context, "type": "graph_rag_retrieval"}
719 ],
720 extra={
721 "method": "graph_rag",
722 "chunk_size": self.chunk_size,
723 "edges_threshold": self.edges_threshold,
724 "graph_nodes": len(self._graph_rag.knowledge_graph.graph.nodes) if self._graph_rag else 0,
725 "graph_edges": len(self._graph_rag.knowledge_graph.graph.edges) if self._graph_rag else 0,
726 }
727 )
728
729 def reset(self) -> None:
730 """Reset agent state for new evaluation context (new persona).

Callers

nothing calls this directly

Calls 5

get_usage_trackerFunction · 0.90
AgentResponseClass · 0.85
set_phaseMethod · 0.80
errorMethod · 0.65
queryMethod · 0.45

Tested by

no test coverage detected