Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

87 lines
3.0 KiB
Python

import asyncio
from typing import Type
from uuid import uuid5
from pydantic import BaseModel
from cognee.tasks.summarization.exceptions import InvalidSummaryInputsError
from cognee.modules.chunking.models.DocumentChunk import DocumentChunk
from cognee.infrastructure.llm.extraction import extract_summary
from cognee.infrastructure.llm.pipeline_stage import pipeline_stage
from cognee.modules.cognify.config import get_cognify_config
from cognee.tasks.summarization.models import TextSummary
from cognee.modules.pipelines.tasks.task import task_summary
@task_summary("Summarized {n} chunk(s)")
async def summarize_text(
data_chunks: list[DocumentChunk], summarization_model: Type[BaseModel] = None
):
"""
Summarize the text contained in the provided data chunks.
If no summarization model is provided, the function retrieves the default model from the
configuration. It processes the data chunks asynchronously and returns summaries for
each chunk. If the provided list of data chunks is empty, it simply returns the list as
is.
Parameters:
-----------
- data_chunks (list[DocumentChunk]): A list of DocumentChunk objects containing text
to be summarized.
- summarization_model (Type[BaseModel]): An optional model used for summarizing
text. If not provided, the default is fetched from the configuration. (default
None)
Returns:
--------
A list of TextSummary objects, each containing the summary of a corresponding
DocumentChunk.
"""
if not isinstance(data_chunks, list):
raise InvalidSummaryInputsError("data_chunks must be a list.")
if not all(hasattr(c, "text") for c in data_chunks):
raise InvalidSummaryInputsError("each DocumentChunk must have a 'text' attribute.")
if len(data_chunks) == 0:
return data_chunks
# Skip LLM summarization for DLT row chunks — structured data
# doesn't benefit from text summarization.
from cognee.modules.data.processing.document_types import DltRowDocument
non_dlt_chunks = [
c for c in data_chunks if not isinstance(getattr(c, "is_part_of", None), DltRowDocument)
]
dlt_chunks = [c for c in data_chunks if c not in non_dlt_chunks]
if not non_dlt_chunks:
return data_chunks
if summarization_model is None:
cognee_config = get_cognify_config()
summarization_model = cognee_config.summarization_model
with pipeline_stage("summarization"):
chunk_summaries = await asyncio.gather(
*[extract_summary(chunk.text, summarization_model) for chunk in non_dlt_chunks]
)
summaries = [
TextSummary(
id=uuid5(chunk.id, "TextSummary"),
made_from=chunk,
source_chunk_id=str(chunk.id),
belongs_to_set=chunk.belongs_to_set,
text=chunk_summaries[chunk_index].summary,
importance_weight=chunk.importance_weight,
)
for (chunk_index, chunk) in enumerate(non_dlt_chunks)
]
return summaries + dlt_chunks