跳转到内容

Cognee

Cognee图检索增强生成 #

Cognee GraphRAG,负责处理知识图谱中信息的添加、存储、处理和检索。

与检索非结构化文本片段的传统RAG模型不同,graphRAG采用知识图谱。 知识图谱将实体表示为节点,将其关系表示为边,通常采用结构化的语义格式。 这使得系统能够检索关于实体、其关系及其属性的更精确和结构化的信息。

属性: llm_api_key: str: 所需LLM的API密钥。 llm_provider: str: 所需LLM的提供商(默认:"openai")。 llm_model: str: 所需LLM的模型(默认:"gpt-4o-mini")。 graph_db_provider: str: 图数据库提供商(默认:"kuzu")。 支持的提供商:"neo4j", "networkx", "kuzu"。 graph_database_url: str: 图数据库的URL。 graph_database_username: str: 访问图数据库的用户名。 graph_database_password: str: 访问图数据库的密码。 vector_db_provider: str: 向量数据库提供商(默认:"lancedb")。 支持的提供商:"lancedb", "pgvector", "qdrant", "weviate"。 vector_db_url: str: 向量数据库的URL。 vector_db_key: str: 访问向量数据库的API密钥。 relational_db_provider: str: 关系型数据库提供商(默认:"sqlite")。 支持的提供商:"sqlite", "postgres"。 db_name: str: 数据库名称(默认:"cognee_db")。 db_host: str: 关系型数据库的主机地址。 db_port: str: 关系型数据库的端口。 db_username: str: 关系型数据库的用户名。 db_password: str: 关系型数据库的密码。

workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
class CogneeGraphRAG:
    """
    Cognee GraphRAG, handles adding, storing, processing and retrieving information from knowledge graphs.

    Unlike traditional RAG models that retrieve unstructured text snippets, graphRAG utilizes knowledge graphs.
    A knowledge graph represents entities as nodes and their relationships as edges, often in a structured semantic format.
    This enables the system to retrieve more precise and structured information about an entity, its relationships, and its properties.

    Attributes:
    llm_api_key: str: API key for desired LLM.
    llm_provider: str: Provider for desired LLM (default: "openai").
    llm_model: str: Model for desired LLM (default: "gpt-4o-mini").
    graph_db_provider: str: The graph database provider (default: "kuzu").
                            Supported providers: "neo4j", "networkx", "kuzu".
    graph_database_url: str: URL for the graph database.
    graph_database_username: str: Username for accessing the graph database.
    graph_database_password: str: Password for accessing the graph database.
    vector_db_provider: str: The vector database provider (default: "lancedb").
                             Supported providers: "lancedb", "pgvector", "qdrant", "weviate".
    vector_db_url: str: URL for the vector database.
    vector_db_key: str: API key for accessing the vector database.
    relational_db_provider: str: The relational database provider (default: "sqlite").
                            Supported providers: "sqlite", "postgres".
    db_name: str: The name of the databases (default: "cognee_db").
    db_host: str: Host for the relational database.
    db_port: str: Port for the relational database.
    db_username: str: Username for the relational database.
    db_password: str: Password for the relational database.

    """

    def __init__(
        self,
        llm_api_key: str,
        llm_provider: str = "openai",
        llm_model: str = "gpt-4o-mini",
        graph_db_provider: str = "kuzu",
        graph_database_url: str = "",
        graph_database_username: str = "",
        graph_database_password: str = "",
        vector_db_provider: str = "lancedb",
        vector_db_url: str = "",
        vector_db_key: str = "",
        relational_db_provider: str = "sqlite",
        relational_db_name: str = "cognee_db",
        relational_db_host: str = "",
        relational_db_port: str = "",
        relational_db_username: str = "",
        relational_db_password: str = "",
    ) -> None:
        cognee_lib.config.set_llm_config(
            {
                "llm_api_key": llm_api_key,
                "llm_provider": llm_provider,
                "llm_model": llm_model,
            }
        )

        cognee_lib.config.set_vector_db_config(
            {
                "vector_db_url": vector_db_url,
                "vector_db_key": vector_db_key,
                "vector_db_provider": vector_db_provider,
            }
        )
        cognee_lib.config.set_relational_db_config(
            {
                "db_path": "",
                "db_name": relational_db_name,
                "db_host": relational_db_host,
                "db_port": relational_db_port,
                "db_username": relational_db_username,
                "db_password": relational_db_password,
                "db_provider": relational_db_provider,
            }
        )

        cognee_lib.config.set_graph_db_config(
            {
                "graph_database_provider": graph_db_provider,
                "graph_database_url": graph_database_url,
                "graph_database_username": graph_database_username,
                "graph_database_password": graph_database_password,
            }
        )

        data_directory_path = str(
            pathlib.Path(
                os.path.join(pathlib.Path(__file__).parent, ".data_storage/")
            ).resolve()
        )

        cognee_lib.config.data_root_directory(data_directory_path)
        cognee_directory_path = str(
            pathlib.Path(
                os.path.join(pathlib.Path(__file__).parent, ".cognee_system/")
            ).resolve()
        )
        cognee_lib.config.system_root_directory(cognee_directory_path)
        cognee_lib.config.data_root_directory(data_directory_path)

    async def add(
        self, data: Union[Document, List[Document]], dataset_name: str = "main_dataset"
    ) -> None:
        """
        Add data to the specified dataset.
        This data will later be processed and made into a knowledge graph.

        Args:
            data (Union[Document, List[Document]]): The document(s) to be added to the graph.
                Can be a single Document or a list of Documents.
            dataset_name (str): Name of the dataset or node set where the data will be added.
                               Note: While cognee supports custom dataset organization, this integration
                               currently adds all data to 'main_dataset'. Full dataset_name support
                               will be added in a future version. This parameter is included to show
                               the intended API design.

        """
        # Convert LlamaIndex Document type to text
        text_data: List[str]
        if isinstance(data, List) and len(data) > 0:
            text_data = [doc.text for doc in data if isinstance(doc, Document)]
        elif isinstance(data, Document):
            text_data = [data.text]
        else:
            raise ValueError(
                "Invalid data type. Please provide a list of Documents or a single Document."
            )

        await cognee_lib.add(text_data, dataset_name)

    async def process_data(self, dataset_name: str = "main_dataset") -> None:
        """
        Process and structure data in the dataset and create a knowledge graph from it.

        This method takes the raw data that was previously added and transforms it into
        a structured knowledge graph with entities, relationships, and properties.

        Args:
            dataset_names (str): The name of the dataset to process into a knowledge graph.
                               Note: While cognee supports multiple datasets, this integration
                               currently processes 'main_dataset' only. Full dataset_names
                               support will be added in a future version. This parameter is
                               included to show the intended API design.

        """
        from cognee.modules.users.methods import get_default_user

        user = await get_default_user()
        await cognee_lib.cognify(dataset_name, user)

    async def rag_search(self, query: str) -> list:
        """
        Answer query using traditional RAG approach with document chunks.

        This method performs retrieval-augmented generation by finding the most
        relevant document chunks and generating a response based on them.

        Args:
            query (str): The question or query to answer.

        Returns:
            list: Search results containing relevant document chunks and generated responses.

        """
        user = await cognee_lib.modules.users.methods.get_default_user()
        return await cognee_lib.search(
            query_type=cognee_lib.SearchType.RAG_COMPLETION,
            query_text=query,
            user=user,
        )

    async def search(self, query: str) -> list:
        """
        Search the knowledge graph for relevant information using graph-based retrieval.

        This method leverages the graph structure to find related entities, relationships,
        and contextual information that traditional RAG might miss.

        Args:
            query (str): The question or search term to match against entities and relationships in the graph.

        Returns:
            list: Search results containing graph-based insights and related information.

        """
        user = await cognee_lib.modules.users.methods.get_default_user()
        return await cognee_lib.search(
            query_type=cognee_lib.SearchType.GRAPH_COMPLETION,
            query_text=query,
            user=user,
        )

    async def get_related_nodes(self, node_id: str) -> list:
        """
        Find nodes and relationships connected to a specific node in the knowledge graph.

        This method explores the graph structure to discover entities and concepts
        that are directly or indirectly related to the specified node.

        Args:
            node_id (str): The identifier or name of the node to find connections for.

        Returns:
            list: Related nodes, relationships, and insights connected to the specified node.

        """
        user = await cognee_lib.modules.users.methods.get_default_user()
        return await cognee_lib.search(
            query_type=cognee_lib.SearchType.INSIGHTS,
            query_text=node_id,
            user=user,
        )

    async def visualize_graph(
        self, open_browser: bool = False, output_file_path: str | None = None
    ) -> str:
        """
        Generate HTML visualization of the graph and optionally open in browser.

        Args:
            open_browser (bool): Whether to automatically open the visualization in the default browser. Defaults to False.
            output_file_path (str | None): Directory path where the HTML file will be saved.
                                         If None, saves to user's home directory. Defaults to None.

        Returns:
            str: Full path to the generated HTML visualization file.

        Raises:
            ValueError: If output_file_path is provided but is not a valid directory.

        """
        # Determine the full file path for the visualization
        if output_file_path:
            if not os.path.isdir(output_file_path):
                raise ValueError(
                    f"The provided path '{output_file_path}' is not a directory"
                )
            full_file_path = os.path.join(output_file_path, "graph_visualization.html")
        else:
            home_dir = os.path.expanduser("~")
            full_file_path = os.path.join(home_dir, "graph_visualization.html")

        # Generate the visualization using cognee
        await cognee_lib.visualize_graph(full_file_path)

        # Open in browser if requested
        if open_browser:
            webbrowser.open(f"file://{os.path.abspath(full_file_path)}")

        return full_file_path

添加 async #

add(data: Union[文档, List[文档]], dataset_name: str = 'main_dataset') -> None

将数据添加到指定数据集。 这些数据随后将被处理并构建成知识图谱。

参数:

名称 类型 描述 默认
data Union[文档, List[文档]]

要添加到图谱中的文档。 可以是单个文档或文档列表。

required
dataset_name str

数据集或节点集的名称,数据将被添加至此。 注意:虽然cognee支持自定义数据集组织,但此集成 目前将所有数据添加到'main_dataset'中。完整的dataset_name支持 将在未来版本中添加。包含此参数是为了展示 预期的API设计。

'main_dataset'
workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
async def add(
    self, data: Union[Document, List[Document]], dataset_name: str = "main_dataset"
) -> None:
    """
    Add data to the specified dataset.
    This data will later be processed and made into a knowledge graph.

    Args:
        data (Union[Document, List[Document]]): The document(s) to be added to the graph.
            Can be a single Document or a list of Documents.
        dataset_name (str): Name of the dataset or node set where the data will be added.
                           Note: While cognee supports custom dataset organization, this integration
                           currently adds all data to 'main_dataset'. Full dataset_name support
                           will be added in a future version. This parameter is included to show
                           the intended API design.

    """
    # Convert LlamaIndex Document type to text
    text_data: List[str]
    if isinstance(data, List) and len(data) > 0:
        text_data = [doc.text for doc in data if isinstance(doc, Document)]
    elif isinstance(data, Document):
        text_data = [data.text]
    else:
        raise ValueError(
            "Invalid data type. Please provide a list of Documents or a single Document."
        )

    await cognee_lib.add(text_data, dataset_name)

process_data async #

process_data(dataset_name: str = 'main_dataset') -> None

处理并结构化数据集中的数据,并从中创建知识图谱。

此方法接收先前添加的原始数据,并将其转换为包含实体、关系和属性的结构化知识图谱。

参数:

名称 类型 描述 默认
dataset_names str

要处理成知识图谱的数据集名称。 注意:虽然cognee支持多个数据集,但此集成 目前仅处理'main_dataset'。完整的数据集名称 支持将在未来版本中添加。包含此参数 是为了展示预期的API设计。

required
workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
async def process_data(self, dataset_name: str = "main_dataset") -> None:
    """
    Process and structure data in the dataset and create a knowledge graph from it.

    This method takes the raw data that was previously added and transforms it into
    a structured knowledge graph with entities, relationships, and properties.

    Args:
        dataset_names (str): The name of the dataset to process into a knowledge graph.
                           Note: While cognee supports multiple datasets, this integration
                           currently processes 'main_dataset' only. Full dataset_names
                           support will be added in a future version. This parameter is
                           included to show the intended API design.

    """
    from cognee.modules.users.methods import get_default_user

    user = await get_default_user()
    await cognee_lib.cognify(dataset_name, user)
rag_search(query: str) -> list

使用传统的RAG方法结合文档片段来回答问题。

该方法通过查找最相关的文档片段并基于它们生成响应来执行检索增强生成。

参数:

名称 类型 描述 默认
query str

需要回答的问题或查询。

required

返回:

名称 类型 描述
list list

包含相关文档片段和生成响应的搜索结果。

workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
async def rag_search(self, query: str) -> list:
    """
    Answer query using traditional RAG approach with document chunks.

    This method performs retrieval-augmented generation by finding the most
    relevant document chunks and generating a response based on them.

    Args:
        query (str): The question or query to answer.

    Returns:
        list: Search results containing relevant document chunks and generated responses.

    """
    user = await cognee_lib.modules.users.methods.get_default_user()
    return await cognee_lib.search(
        query_type=cognee_lib.SearchType.RAG_COMPLETION,
        query_text=query,
        user=user,
    )

搜索 async #

search(query: str) -> list

使用基于图谱的检索方法在知识图谱中搜索相关信息。

该方法利用图结构来查找传统RAG可能遗漏的相关实体、关系和上下文信息。

参数:

名称 类型 描述 默认
query str

用于与图中实体和关系进行匹配的问题或搜索词。

required

返回:

名称 类型 描述
list list

包含基于图表的洞察和相关信息的搜索结果。

workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
async def search(self, query: str) -> list:
    """
    Search the knowledge graph for relevant information using graph-based retrieval.

    This method leverages the graph structure to find related entities, relationships,
    and contextual information that traditional RAG might miss.

    Args:
        query (str): The question or search term to match against entities and relationships in the graph.

    Returns:
        list: Search results containing graph-based insights and related information.

    """
    user = await cognee_lib.modules.users.methods.get_default_user()
    return await cognee_lib.search(
        query_type=cognee_lib.SearchType.GRAPH_COMPLETION,
        query_text=query,
        user=user,
    )
get_related_nodes(node_id: str) -> list

在知识图谱中查找与特定节点相连的节点和关系。

该方法探索图结构,以发现与指定节点直接或间接相关的实体和概念。

参数:

名称 类型 描述 默认
node_id str

要查找连接的节点的标识符或名称。

required

返回:

名称 类型 描述
list list

与指定节点相关联的节点、关系和洞察信息。

workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
async def get_related_nodes(self, node_id: str) -> list:
    """
    Find nodes and relationships connected to a specific node in the knowledge graph.

    This method explores the graph structure to discover entities and concepts
    that are directly or indirectly related to the specified node.

    Args:
        node_id (str): The identifier or name of the node to find connections for.

    Returns:
        list: Related nodes, relationships, and insights connected to the specified node.

    """
    user = await cognee_lib.modules.users.methods.get_default_user()
    return await cognee_lib.search(
        query_type=cognee_lib.SearchType.INSIGHTS,
        query_text=node_id,
        user=user,
    )

visualize_graph async #

visualize_graph(open_browser: bool = False, output_file_path: str | None = None) -> str

生成图表的HTML可视化,并可选择在浏览器中打开。

参数:

名称 类型 描述 默认
open_browser bool

是否在默认浏览器中自动打开可视化界面。默认为 False。

False
output_file_path str | None

HTML文件将被保存的目录路径。 如果为None,则保存到用户的主目录。默认为None。

None

返回:

名称 类型 描述
str str

生成的HTML可视化文件的完整路径。

引发:

类型 描述
ValueError

如果提供了 output_file_path 但不是一个有效的目录。

workflows/handler.py 中的源代码llama_index/graph_rag/cognee/graph_rag.py
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
async def visualize_graph(
    self, open_browser: bool = False, output_file_path: str | None = None
) -> str:
    """
    Generate HTML visualization of the graph and optionally open in browser.

    Args:
        open_browser (bool): Whether to automatically open the visualization in the default browser. Defaults to False.
        output_file_path (str | None): Directory path where the HTML file will be saved.
                                     If None, saves to user's home directory. Defaults to None.

    Returns:
        str: Full path to the generated HTML visualization file.

    Raises:
        ValueError: If output_file_path is provided but is not a valid directory.

    """
    # Determine the full file path for the visualization
    if output_file_path:
        if not os.path.isdir(output_file_path):
            raise ValueError(
                f"The provided path '{output_file_path}' is not a directory"
            )
        full_file_path = os.path.join(output_file_path, "graph_visualization.html")
    else:
        home_dir = os.path.expanduser("~")
        full_file_path = os.path.join(home_dir, "graph_visualization.html")

    # Generate the visualization using cognee
    await cognee_lib.visualize_graph(full_file_path)

    # Open in browser if requested
    if open_browser:
        webbrowser.open(f"file://{os.path.abspath(full_file_path)}")

    return full_file_path

选项: 成员:- GraphRag