Transforms
BaseGraphTransformation
dataclass
BaseGraphTransformation(name: str = '', filter_nodes: Callable[[Node], bool] = lambda: default_filter())
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
NodeFilter
dataclass
NodeFilter(name: str = '', filter_nodes: Callable[[Node], bool] = lambda: default_filter())
Bases: BaseGraphTransformation
custom_filter
abstractmethod
async
custom_filter(node: Node, kg: KnowledgeGraph) -> bool
Abstract method to filter a node based on a prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
Node
|
The node to be filtered. |
required |
Returns:
Type | Description |
---|---|
bool
|
A boolean indicating whether the node should be filtered. |
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
Source code in src/ragas/testset/transforms/base.py
RelationshipBuilder
dataclass
RelationshipBuilder(name: str = '', filter_nodes: Callable[[Node], bool] = lambda: default_filter())
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
Splitter(name: str = '', filter_nodes: Callable[[Node], bool] = lambda: default_filter())
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, max_token_limit: int = 32000, tokenizer: Encoding = DEFAULT_TOKENIZER, property_name: str = 'headlines', prompt: HeadlinesExtractorPrompt = HeadlinesExtractorPrompt(), max_num: int = 5)
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, max_token_limit: int = 32000, tokenizer: Encoding = DEFAULT_TOKENIZER, property_name: str = 'keyphrases', prompt: KeyphrasesExtractorPrompt = KeyphrasesExtractorPrompt(), max_num: int = 5)
Bases: LLMBasedExtractor
Extracts top 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, max_token_limit: int = 32000, tokenizer: Encoding = DEFAULT_TOKENIZER, 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, max_token_limit: int = 32000, tokenizer: Encoding = DEFAULT_TOKENIZER, 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. |
CustomNodeFilter
dataclass
CustomNodeFilter(name: str = '', filter_nodes: Callable[[Node], bool] = lambda: default_filter(), llm: BaseRagasLLM = llm_factory(), scoring_prompt: PydanticPrompt = QuestionPotentialPrompt(), min_score: int = 2, rubrics: Dict[str, str] = lambda: DEFAULT_RUBRICS())
Bases: LLMBasedNodeFilter
returns True if the score is less than min_score
SummaryCosineSimilarityBuilder
dataclass
SummaryCosineSimilarityBuilder(name: str = '', filter_nodes: Callable[[Node], bool] = lambda: default_filter(), 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
default_transforms
default_transforms(documents: List[Document], llm: BaseRagasLLM, embedding_model: BaseRagasEmbeddings) -> 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.
Returns:
Type | Description |
---|---|
Transforms
|
A list of transformation steps to be applied to the knowledge graph. |
Source code in src/ragas/testset/transforms/default.py
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 |
|
apply_transforms
apply_transforms(kg: KnowledgeGraph, transforms: Transforms, run_config: RunConfig = RunConfig(), callbacks: Optional[Callbacks] = None)
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.