Skip to main content

Relation Extraction

Extract relationships between entities in text using the Knowledgator TypeScript SDK.


Example Usage​

1. Using Unified Inference (extract.it)​

import { createClient } from '@knowledgator/typescript-sdk';

const kg = createClient();

const result = await 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
});

const relationsTask = result.outputs.relations;
if (Array.isArray(relationsTask.value)) {
for (const rel of relationsTask.value) {
console.log(`${rel.head.text} (${rel.head.label}) --[${rel.relation}]--> ${rel.tail.text} (${rel.tail.label})`);
}
}

2. Using Convenience Method (extract.relations)​

import { createClient } from '@knowledgator/typescript-sdk';

const kg = createClient();

const result = await kg.extract.relations({
text: "Steve Jobs founded Apple Inc. in Cupertino, California.",
entities: ["person", "organization", "location"],
relations: ["founded", "located_in"],
threshold: 0.5,
adapter_id: "your-lora-adapter-id", // Optional LoRA adapter
});

for (const rel of result.outputs) {
console.log(`${rel.source.span} --[${rel.relation}]--> ${rel.target.span}`);
}
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
}
}
}