Relation Extraction
Extract relationships between entities in text using the Knowledgator Python SDK.
Example Usage
1. Using Unified Inference (extract.it)
from knowledgator import Knowledgator
kg = Knowledgator(api_key="YOUR_API_KEY")
result = kg.extract.it(
text="Steve Jobs founded Apple Inc. in Cupertino, California.",
tasks={
"entities": {
"type": "span",
"labels": ["person", "organization", "location"],
"threshold": 0.5,
},
"relations": {
"type": "relation",
"labels": ["founded", "located_in"],
"entity_labels": ["person", "organization", "location"],
"threshold": 0.5,
},
},
adapter_id="your-lora-adapter-id", # Optional LoRA adapter
)
for rel in result.outputs["relations"].value:
print(f"{rel.head.text} ({rel.head.label}) --[{rel.relation}]--> {rel.tail.text} ({rel.tail.label})")
2. Using Convenience Method (extract.relations)
from knowledgator import Knowledgator
kg = Knowledgator(api_key="YOUR_API_KEY")
result = kg.extract.relations(
text="Steve Jobs founded Apple Inc. in Cupertino, California.",
relations=["founded", "located_in"],
entities=["person", "organization", "location"],
threshold=0.5,
adapter_id="your-lora-adapter-id", # Optional LoRA adapter
)
for rel in result.outputs["relations"].value:
print(f"{rel.head.text} ({rel.head.label}) --[{rel.relation}]--> {rel.tail.text} ({rel.tail.label})")
LoRA Adapters vs. Open Models
adapter_id is for fine-tuned LoRA adapters attached to base services. If you trained a standalone Open Model, query it via kg.model(model_id).call("relations", ...) instead.
Expected Output
Steve Jobs (person) --[founded]--> Apple Inc. (organization)
Apple Inc. (organization) --[located_in]--> California (location)
Cupertino (location) --[located_in]--> California (location)
Result Object Structure
The live API returns response records under the root "outputs" object:
{
"outputs": {
"entities": {
"type": "span",
"value": [
{
"label": "person",
"text": "Steve Jobs",
"start": 0,
"end": 10,
"source": "",
"score": 0.9997
},
{
"label": "organization",
"text": "Apple Inc.",
"start": 19,
"end": 29,
"source": "",
"score": 0.9995
},
{
"label": "location",
"text": "Cupertino",
"start": 33,
"end": 42,
"source": "",
"score": 0.8948
},
{
"label": "location",
"text": "California",
"start": 44,
"end": 54,
"source": "",
"score": 0.8948
}
],
"threshold": 0.5
},
"relations": {
"type": "relation",
"value": [
{
"head": {
"label": "person",
"text": "Steve Jobs",
"start": 0,
"end": 10,
"source": ""
},
"relation": "founded",
"tail": {
"label": "organization",
"text": "Apple Inc.",
"start": 19,
"end": 29,
"source": ""
},
"score": 0.9961
},
{
"head": {
"label": "organization",
"text": "Apple Inc.",
"start": 19,
"end": 29,
"source": ""
},
"relation": "located_in",
"tail": {
"label": "location",
"text": "California",
"start": 44,
"end": 54,
"source": ""
},
"score": 0.5938
},
{
"head": {
"label": "location",
"text": "Cupertino",
"start": 33,
"end": 42,
"source": ""
},
"relation": "located_in",
"tail": {
"label": "location",
"text": "California",
"start": 44,
"end": 54,
"source": ""
},
"score": 0.5938
}
],
"threshold": 0.5
}
}
}