转换
BaseGraphTransformation
dataclass
Bases: ABC
Abstract base class for graph transformations on a KnowledgeGraph.
transform
abstractmethod
async
transform(kg: KnowledgeGraph) -> Any
Abstract method to transform the KnowledgeGraph. Transformations should be idempotent, meaning that applying the transformation multiple times should yield the same result as applying it once.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
Any
|
The transformed knowledge graph. |
Source code in src/ragas/testset/transforms/base.py
filter
filter(kg: KnowledgeGraph) -> KnowledgeGraph
Filters the KnowledgeGraph and returns the filtered graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be filtered. |
required |
Returns:
Type | Description |
---|---|
KnowledgeGraph
|
The filtered knowledge graph. |
Source code in src/ragas/testset/transforms/base.py
generate_execution_plan
abstractmethod
generate_execution_plan(
kg: KnowledgeGraph,
) -> List[Coroutine]
Generates a list of coroutines to be executed in sequence by the Executor. This coroutine will, upon execution, write the transformation into the KnowledgeGraph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
List[Coroutine]
|
A list of coroutines to be executed in parallel. |
Source code in src/ragas/testset/transforms/base.py
Extractor
dataclass
Extractor(
name: str = "",
filter_nodes: Callable[
[Node], bool
] = lambda: default_filter(),
)
Bases: BaseGraphTransformation
Abstract base class for extractors that transform a KnowledgeGraph by extracting specific properties from its nodes.
Methods:
Name | Description |
---|---|
transform |
Transforms the KnowledgeGraph by extracting properties from its nodes. |
extract |
Abstract method to extract a specific property from a node. |
transform
async
transform(
kg: KnowledgeGraph,
) -> List[Tuple[Node, Tuple[str, Any]]]
Transforms the KnowledgeGraph by extracting properties from its nodes. Uses
the filter
method to filter the graph and the extract
method to extract
properties from each node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[Node, Tuple[str, Any]]]
|
A list of tuples where each tuple contains a node and the extracted property. |
Examples:
>>> kg = KnowledgeGraph(nodes=[Node(id=1, properties={"name": "Node1"}), Node(id=2, properties={"name": "Node2"})])
>>> extractor = SomeConcreteExtractor()
>>> extractor.transform(kg)
[(Node(id=1, properties={"name": "Node1"}), ("property_name", "extracted_value")),
(Node(id=2, properties={"name": "Node2"}), ("property_name", "extracted_value"))]
Source code in src/ragas/testset/transforms/base.py
extract
abstractmethod
async
extract(node: Node) -> Tuple[str, Any]
Abstract method to extract a specific property from a node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
The node from which to extract the property. |
required |
Returns:
Type | Description |
---|---|
Tuple[str, Any]
|
A tuple containing the property name and the extracted value. |
Source code in src/ragas/testset/transforms/base.py
generate_execution_plan
generate_execution_plan(
kg: KnowledgeGraph,
) -> List[Coroutine]
Generates a list of coroutines to be executed in parallel by the Executor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
List[Coroutine]
|
A list of coroutines to be executed in parallel. |
Source code in src/ragas/testset/transforms/base.py
RelationshipBuilder
dataclass
Bases: BaseGraphTransformation
Abstract base class for building relationships in a KnowledgeGraph.
Methods:
Name | Description |
---|---|
transform |
Transforms the KnowledgeGraph by building relationships. |
transform
abstractmethod
async
transform(kg: KnowledgeGraph) -> List[Relationship]
Transforms the KnowledgeGraph by building relationships.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
List[Relationship]
|
A list of new relationships. |
Source code in src/ragas/testset/transforms/base.py
generate_execution_plan
generate_execution_plan(
kg: KnowledgeGraph,
) -> List[Coroutine]
Generates a list of coroutines to be executed in parallel by the Executor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
List[Coroutine]
|
A list of coroutines to be executed in parallel. |
Source code in src/ragas/testset/transforms/base.py
Splitter
dataclass
Bases: BaseGraphTransformation
Abstract base class for splitters that transform a KnowledgeGraph by splitting its nodes into smaller chunks.
Methods:
Name | Description |
---|---|
transform |
Transforms the KnowledgeGraph by splitting its nodes into smaller chunks. |
split |
Abstract method to split a node into smaller chunks. |
transform
async
transform(
kg: KnowledgeGraph,
) -> Tuple[List[Node], List[Relationship]]
Transforms the KnowledgeGraph by splitting its nodes into smaller chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
Tuple[List[Node], List[Relationship]]
|
A tuple containing a list of new nodes and a list of new relationships. |
Source code in src/ragas/testset/transforms/base.py
split
abstractmethod
async
split(node: Node) -> Tuple[List[Node], List[Relationship]]
Abstract method to split a node into smaller chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
The node to be split. |
required |
Returns:
Type | Description |
---|---|
Tuple[List[Node], List[Relationship]]
|
A tuple containing a list of new nodes and a list of new relationships. |
Source code in src/ragas/testset/transforms/base.py
generate_execution_plan
generate_execution_plan(
kg: KnowledgeGraph,
) -> List[Coroutine]
Generates a list of coroutines to be executed in parallel by the Executor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kg
|
KnowledgeGraph
|
The knowledge graph to be transformed. |
required |
Returns:
Type | Description |
---|---|
List[Coroutine]
|
A list of coroutines to be executed in parallel. |
Source code in src/ragas/testset/transforms/base.py
Parallel
Parallel(*transformations: BaseGraphTransformation)
EmbeddingExtractor
dataclass
EmbeddingExtractor(
name: str = "",
filter_nodes: Callable[
[Node], bool
] = lambda: default_filter(),
property_name: str = "embedding",
embed_property_name: str = "page_content",
embedding_model: BaseRagasEmbeddings = embedding_factory(),
)
Bases: Extractor
A class for extracting embeddings from nodes in a knowledge graph.
Attributes:
Name | Type | Description |
---|---|---|
property_name |
str
|
The name of the property to store the embedding |
embed_property_name |
str
|
The name of the property containing the text to embed |
embedding_model |
BaseRagasEmbeddings
|
The embedding model used for generating embeddings |
extract
async
extract(node: Node) -> Tuple[str, Any]
Extracts the embedding for a given node.
Raises:
Type | Description |
---|---|
ValueError
|
If the property to be embedded is not a string. |
Source code in src/ragas/testset/transforms/extractors/embeddings.py
HeadlinesExtractor
dataclass
HeadlinesExtractor(
name: str = "",
filter_nodes: Callable[
[Node], bool
] = lambda: default_filter(),
llm: BaseRagasLLM = llm_factory(),
merge_if_possible: bool = True,
property_name: str = "headlines",
prompt: HeadlinesExtractorPrompt = HeadlinesExtractorPrompt(),
)
Bases: LLMBasedExtractor
Extracts the headlines from the given text.
Attributes:
Name | Type | Description |
---|---|---|
property_name |
str
|
The name of the property to extract. |
prompt |
HeadlinesExtractorPrompt
|
The prompt used for extraction. |
KeyphrasesExtractor
dataclass
KeyphrasesExtractor(
name: str = "",
filter_nodes: Callable[
[Node], bool
] = lambda: default_filter(),
llm: BaseRagasLLM = llm_factory(),
merge_if_possible: bool = True,
property_name: str = "keyphrases",
prompt: KeyphrasesExtractorPrompt = KeyphrasesExtractorPrompt(),
)
Bases: LLMBasedExtractor
Extracts top 5 keyphrases from the given text.
Attributes:
Name | Type | Description |
---|---|---|
property_name |
str
|
The name of the property to extract. |
prompt |
KeyphrasesExtractorPrompt
|
The prompt used for extraction. |
SummaryExtractor
dataclass
SummaryExtractor(
name: str = "",
filter_nodes: Callable[
[Node], bool
] = lambda: default_filter(),
llm: BaseRagasLLM = llm_factory(),
merge_if_possible: bool = True,
property_name: str = "summary",
prompt: SummaryExtractorPrompt = SummaryExtractorPrompt(),
)
Bases: LLMBasedExtractor
Extracts a summary from the given text.
Attributes:
Name | Type | Description |
---|---|---|
property_name |
str
|
The name of the property to extract. |
prompt |
SummaryExtractorPrompt
|
The prompt used for extraction. |
TitleExtractor
dataclass
TitleExtractor(
name: str = "",
filter_nodes: Callable[
[Node], bool
] = lambda: default_filter(),
llm: BaseRagasLLM = llm_factory(),
merge_if_possible: bool = True,
property_name: str = "title",
prompt: TitleExtractorPrompt = TitleExtractorPrompt(),
)
Bases: LLMBasedExtractor
Extracts the title from the given text.
Attributes:
Name | Type | Description |
---|---|---|
property_name |
str
|
The name of the property to extract. |
prompt |
TitleExtractorPrompt
|
The prompt used for extraction. |
SummaryCosineSimilarityBuilder
dataclass
SummaryCosineSimilarityBuilder(
name: str = "",
property_name: str = "summary_embedding",
new_property_name: str = "summary_cosine_similarity",
threshold: float = 0.1,
)
Bases: CosineSimilarityBuilder
filter
filter(kg: KnowledgeGraph) -> KnowledgeGraph
Filters the knowledge graph to only include nodes with a summary embedding.
Source code in src/ragas/testset/transforms/relationship_builders/cosine.py
apply_transforms
apply_transforms(
kg: KnowledgeGraph,
transforms: Transforms,
run_config: RunConfig = RunConfig(),
)
Apply a list of transformations to a knowledge graph in place.
Source code in src/ragas/testset/transforms/engine.py
rollback_transforms
rollback_transforms(
kg: KnowledgeGraph, transforms: Transforms
)
Rollback a list of transformations from a knowledge graph.
Note
This is not yet implemented. Please open an issue if you need this feature.
Source code in src/ragas/testset/transforms/engine.py
default_transforms
Creates and returns a default set of transforms for processing a knowledge graph.
This function defines a series of transformation steps to be applied to a knowledge graph, including extracting summaries, keyphrases, titles, headlines, and embeddings, as well as building similarity relationships between nodes.
The transforms are applied in the following order: 1. Parallel extraction of summaries and headlines 2. Embedding of summaries for document nodes 3. Splitting of headlines 4. Parallel extraction of embeddings, keyphrases, and titles 5. Building cosine similarity relationships between nodes 6. Building cosine similarity relationships between summaries
Returns:
Type | Description |
---|---|
Transforms
|
A list of transformation steps to be applied to the knowledge graph. |