| 1 | from graphrag_sdk.kg import KnowledgeGraph |
| 2 | from .agent import Agent |
| 3 | from graphrag_sdk.models import GenerativeModelChatSession |
| 4 | |
| 5 | |
| 6 | class KGAgent(Agent): |
| 7 | """Represents an Agent for a FalkorDB Knowledge Graph. |
| 8 | |
| 9 | Args: |
| 10 | agent_id (str): The ID of the agent. |
| 11 | kg (KnowledgeGraph): The knowledge graph to query. |
| 12 | introduction (str): The introduction to the agent. |
| 13 | |
| 14 | Examples: |
| 15 | >>> from graphrag_sdk import KnowledgeGraph, Orchestrator |
| 16 | >>> from graphrag_sdk.agents.kg_agent import KGAgent |
| 17 | >>> orchestrator = Orchestrator(model) |
| 18 | >>> kg = KnowledgeGraph("test_kg", ontology, model) |
| 19 | >>> agent = KGAgent("test_agent", kg, "This is a test agent.") |
| 20 | >>> orchestrator.register_agent(agent) |
| 21 | |
| 22 | """ |
| 23 | |
| 24 | _interface = [ |
| 25 | { |
| 26 | "name": "prompt", |
| 27 | "type": "string", |
| 28 | "required": True, |
| 29 | "description": "The prompt to ask the agent.", |
| 30 | } |
| 31 | ] |
| 32 | |
| 33 | def __init__(self, agent_id: str, kg: KnowledgeGraph, introduction: str): |
| 34 | """ |
| 35 | Initializes a KGAgent object. |
| 36 | |
| 37 | Args: |
| 38 | agent_id (str): The ID of the agent. |
| 39 | kg (KnowledgeGraph): The knowledge graph associated with the agent. |
| 40 | introduction (str): The introduction of the agent. |
| 41 | """ |
| 42 | super().__init__() |
| 43 | self.agent_id = agent_id |
| 44 | self.introduction = introduction |
| 45 | self.kg = kg |
| 46 | |
| 47 | @property |
| 48 | def agent_id(self) -> str: |
| 49 | """ |
| 50 | Returns the ID of the agent. |
| 51 | |
| 52 | Returns: |
| 53 | str: The ID of the agent. |
| 54 | """ |
| 55 | return self._agent_id |
| 56 | |
| 57 | @agent_id.setter |
| 58 | def agent_id(self, value): |
| 59 | """ |
| 60 | Sets the agent ID. |
| 61 | |
| 62 | Parameters: |
| 63 | value (str): The ID of the agent. |
| 64 | |
| 65 | Returns: |
| 66 | None |
| 67 | """ |
| 68 | self._agent_id = value |
| 69 | |
| 70 | @property |
| 71 | def introduction(self) -> str: |
| 72 | """ |
| 73 | Returns the introduction of the agent. |
| 74 | |
| 75 | :return: The introduction of the agent. |
| 76 | :rtype: str |
| 77 | """ |
| 78 | return self._introduction |
| 79 | |
| 80 | @introduction.setter |
| 81 | def introduction(self, value): |
| 82 | """ |
| 83 | Sets the introduction of the agent. |
| 84 | |
| 85 | Parameters: |
| 86 | value (str): The introduction of the agent. |
| 87 | |
| 88 | Returns: |
| 89 | None |
| 90 | """ |
| 91 | self._introduction = value |
| 92 | |
| 93 | @property |
| 94 | def interface(self) -> list[dict]: |
| 95 | """ |
| 96 | Returns the interface of the KG agent. |
| 97 | |
| 98 | Returns: |
| 99 | list[dict]: The interface of the KG agent. |
| 100 | """ |
| 101 | return self._interface |
| 102 | |
| 103 | @property |
| 104 | def kg(self) -> KnowledgeGraph: |
| 105 | """ |
| 106 | Returns the KnowledgeGraph object associated with this agent. |
| 107 | |
| 108 | Returns: |
| 109 | KnowledgeGraph: The KnowledgeGraph object. |
| 110 | """ |
| 111 | return self._kg |
| 112 | |
| 113 | @kg.setter |
| 114 | def kg(self, value: KnowledgeGraph): |
| 115 | """ |
| 116 | Sets the knowledge graph for the agent. |
| 117 | |
| 118 | Parameters: |
| 119 | value (KnowledgeGraph): The knowledge graph to be set. |
| 120 | |
| 121 | Returns: |
| 122 | None |
| 123 | """ |
| 124 | self._kg = value |
| 125 | |
| 126 | def run( |
| 127 | self, params: dict, session: GenerativeModelChatSession | None = None |
| 128 | ) -> tuple[str, GenerativeModelChatSession]: |
| 129 | """ |
| 130 | Ask the agent a question. |
| 131 | |
| 132 | Args: |
| 133 | params (dict): The parameters for the agent. |
| 134 | session (GenerativeModelChatSession | None): The chat session to use for the agent. Defaults to None. |
| 135 | |
| 136 | Returns: |
| 137 | tuple[str, GenerativeModelChatSession]: The agent's response and the updated chat session. |
| 138 | |
| 139 | """ |
| 140 | (output, chat_session) = self._kg.ask(params["prompt"], session) |
| 141 | return (output, chat_session) |
| 142 | |
| 143 | def __repr__(self): |
| 144 | """ |
| 145 | Returns a string representation of the KGAgent object. |
| 146 | |
| 147 | The string representation includes the Agent ID, Knowledge Graph Name, |
| 148 | Interface, and Introduction. |
| 149 | |
| 150 | Returns: |
| 151 | str: A string representation of the KGAgent object. |
| 152 | """ |
| 153 | return f""" |
| 154 | --- |
| 155 | Agent ID: {self.agent_id} |
| 156 | Knowledge Graph Name: {self._kg.name} |
| 157 | Interface: {self.interface} |
| 158 | |
| 159 | Introduction: {self.introduction} |
| 160 | """ |