Apply Rule¶
Here, we'll demonstrate how to add and apply a rule from the SDK.
The WhyHow Graph Studio includes a human in the loop rule-based entity extraction and resolution system which you can use to merge entities in your graph. (This gives users the power to perform their own personalized, use-case specific entity resolution instead of just relying on LLMs and semantic similarity.) By saving this merge as a rule, they are automatically applied to any other graph that is created in this workspace. In this way, users can make entity extraction and graph creation better with time.
Using the apply_rule()
method, you can perform this same action from the SDK, and save a a rule to your graph. In the example below, we will add a rule that will merge the following entities into "Meta":
- "Meta (Facebook)"
- "Meta, Inc."
- "Facebook.com (Meta)"
- "Facebook (now Meta):Meta"
# Apply rule
from whyhow import WhyHow
client = WhyHow(api_key="<whyhow api key>", base_url="https://api.whyhow.ai")
response = client.graphs.apply_rule(
graph_id="<graph id>",
from_strings=["Meta (Facebook)", "Meta, Inc.", "Facebook.com (Meta)", "Facebook (now Meta):Meta"],
to_string="Meta",
entity_type="company",
save_as_rule=True
)
When you run this code, WhyHow will detect all entities that match the rule for the given graph, and it will merge any of the detected from_strings
entities into "Meta". By default, the merge will be saved as a rule and applied to the entire workspace. If you just want to apply the rule to the graph and not save it, you can set save_as_rule=False
.
Now, whenever you add a new node or triple containing any of the from_strings
mentioned in the rule, they will be automatically merged into "Meta."