Knowledge Graph
Connect the knowledge gained by different models.
The Knowledge Graph uses Clarifai's concept mapping model to establish a hierarchical relationship between your concepts. and to uses three different predicates to organize your concepts: hypernyms, hyponyms, and synonyms.
Hyponym represents an 'is a kind of' relation. The following relationship: 'honey' (subject), 'hyponym' (predicate), 'food' (object) is more easily be read as 'honey' 'is a kind of' 'food'.
Hypernym is the opposite of 'hyponym'. When you add one of the relationships the opposite will automatically appear for you in queries. The 'hypernym' can be read as 'is a parent of' so: 'food' (subject), 'hypernym' (predicate), 'honey' (object) can more easily be read as:'food' is a parent of 'honey'.
Synonym The 'synonym' relation defines two concepts that essential mean the same thing. This is more like a "is" relationship. So for example a 'synonym' relationship could be: "puppy" is "pup" The reverse is also true once the former is added so: "pup" is "puppy" will appear in queries as well.

Create

To create a relation between two concepts, you first have to create them in your custom model. See the Concepts page on how to do that programatically.
Each relation has to have specified a predicate, which can be hyponym, hypernym, or synonym.
Python
Java
NodeJS
cURL
Javascript (REST)
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
post_concept_relation_response = stub.PostConceptRelations(
5
service_pb2.PostConceptRelationsRequest(
6
user_app_id=resources_pb2.UserAppIDSet(
7
app_id="{YOUR_APP_ID}"
8
),
9
concept_id="{YOUR_SUBJECT_CONCEPT_ID}",
10
concept_relations=[
11
resources_pb2.ConceptRelation(
12
object_concept=resources_pb2.Concept(id="{YOUR_OBJECT_CONCEPT_ID}"),
13
predicate="hypernym" # This can be hypernym, hyponym, or synonym.
14
)
15
]
16
),
17
metadata=metadata
18
)
19
20
if post_concept_relation_response.status.code != status_code_pb2.SUCCESS:
21
print("There was an error with your request!")
22
print("\tCode: {}".format(post_concept_relation_response.outputs[0].status.code))
23
print("\tDescription: {}".format(post_concept_relation_response.outputs[0].status.description))
24
print("\tDetails: {}".format(post_concept_relation_response.outputs[0].status.details))
25
raise Exception("Post concept relation failed, status: " + post_concept_relation_response.status.description)
Copied!
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
MultiConceptRelationResponse postConceptRelationsResponse = stub.postConceptRelations(
8
PostConceptRelationsRequest.newBuilder()
9
.setUserAppId(
10
UserAppIDSet.newBuilder()
11
.setAppId("{YOUR_APP_ID}")
12
.build()
13
)
14
.setConceptId("{YOUR_SUBJECT_CONCEPT_ID}")
15
.addConceptRelations(
16
ConceptRelation.newBuilder()
17
.setObjectConcept(Concept.newBuilder().setId("{YOUR_OBJECT_CONCEPT_ID}").build())
18
.setPredicate("hypernym").build()) // This can be hypernym, hypnonym, or synonym.
19
.build()
20
);
21
22
if (postConceptRelationsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
23
throw new RuntimeException("Post concept relations failed, status: " + postConceptRelationsResponse.getStatus());
24
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
stub.PostConceptRelations(
5
{
6
user_app_id: {
7
app_id: "{YOUR_APP_ID}"
8
},
9
concept_id: "{YOUR_SUBJECT_CONCEPT_ID}",
10
concept_relations: [
11
{
12
object_concept: {
13
id: "{YOUR_OBJECT_CONCEPT_ID}",
14
},
15
predicate: "hypernym" // This can be hypernym, hyponym, or synonym.
16
}
17
]
18
},
19
metadata,
20
(err, response) => {
21
if (err) {
22
throw new Error(err);
23
}
24
25
if (response.status.code !== 10000) {
26
throw new Error("Create concept relations failed, status: " + response.status.description);
27
}
28
}
29
);
Copied!
1
curl -X POST 'https://api.clarifai.com/v2/users/me/apps/{YOUR_APP_ID}/concepts/{YOUR_SUBJECT_CONCEPT_ID}/relations' \
2
-H 'Authorization: Key {YOUR_PERSONAL_ACCESS_TOKEN}' \
3
-H 'Content-Type: application/json' \
4
--data-raw '{
5
"concept_relations": [
6
{
7
"object_concept": {
8
"id": "{YOUR_OBJECT_CONCEPT_ID}"
9
},
10
"predicate": "hypernym"
11
}
12
]
13
}'
Copied!
1
const appId = '{YOUR_APP_ID}'
2
const subjectConceptId = '{YOUR_SUBJECT_CONCEPT_ID}'
3
4
const raw = JSON.stringify({
5
"user_app_id": {
6
"user_id": "{YOUR_USER_ID}",
7
"app_id": "{YOUR_APP_ID}"
8
},
9
"concept_relations": [
10
{
11
"object_concept": {
12
"id": "{YOUR_OBJECT_CONCEPT_ID}"
13
},
14
"predicate": "hypernym"
15
}
16
]
17
});
18
19
const requestOptions = {
20
method: 'POST',
21
headers: {
22
'Accept': 'application/json',
23
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
24
},
25
body: raw
26
};
27
28
fetch(`https://api.clarifai.com/v2/users/me/apps/${appId}/concepts/${subjectConceptId}/relations`, requestOptions)
29
.then(response => response.text())
30
.then(result => console.log(result))
31
.catch(error => console.log('error', error));
Copied!

List existing relations

Python
Java
NodeJS
cURL
Javascript (REST)
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
list_concept_relation_response = stub.ListConceptRelations(
5
service_pb2.ListConceptRelationsRequest(
6
user_app_id=resources_pb2.UserAppIDSet(
7
app_id="{YOUR_APP_ID}"
8
),
9
concept_id="{YOUR_CONCEPT_ID}",
10
predicate="hypernym" # This is optional. If skipped, all concept's relations will be returned.
11
),
12
metadata=metadata
13
)
14
15
if list_concept_relation_response.status.code != status_code_pb2.SUCCESS:
16
print("There was an error with your request!")
17
print("\tCode: {}".format(list_concept_relation_response.outputs[0].status.code))
18
print("\tDescription: {}".format(list_concept_relation_response.outputs[0].status.description))
19
print("\tDetails: {}".format(list_concept_relation_response.outputs[0].status.details))
20
raise Exception("List concept relation failed, status: " + list_concept_relation_response.status.description)
21
22
for relation in list_concept_relation_response.concept_relations:
23
print(relation)
Copied!
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
MultiConceptRelationResponse listConceptRelationsResponse = stub.listConceptRelations(
8
ListConceptRelationsRequest.newBuilder()
9
.setUserAppId(
10
UserAppIDSet.newBuilder()
11
.setAppId("{YOUR_APP_ID}")
12
.build()
13
)
14
.setConceptId("{YOUR_CONCEPT_ID}")
15
.setPredicate("hypernym") // This is optional. If skipped, all concept's relations will be returned.
16
.build()
17
);
18
19
20
if (listConceptRelationsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
21
throw new RuntimeException("List concept relations failed, status: " + listConceptRelationsResponse.getStatus());
22
}
23
24
for (ConceptRelation relation : listConceptRelationsResponse.getConceptRelationsList()) {
25
System.out.println(relation);
26
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
stub.ListConceptRelations(
5
{
6
user_app_id: {
7
app_id: "{YOUR_APP_ID}"
8
},
9
concept_id: "{YOUR_CONCEPT_ID}",
10
predicate: "hypernym" // This is optional. If skipped, all concept's relations will be returned.
11
},
12
metadata,
13
(err, response) => {
14
if (err) {
15
throw new Error(err);
16
}
17
18
if (response.status.code !== 10000) {
19
throw new Error("List concept relations failed, status: " + response.status.description);
20
}
21
22
for (const relation of response.concept_relations) {
23
console.log(relation);
24
}
25
}
26
);
Copied!
1
# Setting the predicate GET parameter is optional. If skipped, all concept's relations will be returned.
2
curl -X GET 'https://api.clarifai.com/v2/users/me/apps/{YOUR_APP_ID}/concepts/{YOUR_CONCEPT_ID}/relations?predicate=hypernym' \
3
-H 'Authorization: Key {YOUR_PERSONAL_ACCESS_TOKEN}' \
4
-H 'Content-Type: application/json'
Copied!
1
const appId = '{YOUR_APP_ID}'
2
const conceptId = '{YOUR_CONCEPT_ID}'
3
4
const requestOptions = {
5
method: 'GET',
6
headers: {
7
'Accept': 'application/json',
8
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
9
}
10
};
11
12
// Setting the predicate GET parameter is optional. If skipped, all concept's relations will be returned
13
fetch(`https://api.clarifai.com/v2/users/me/apps/${appId}/concepts/${conceptId}/relations?predicate=hypernym`, requestOptions)
14
.then(response => response.text())
15
.then(result => console.log(result))
16
.catch(error => console.log('error', error));
Copied!

Delete

Python
Java
NodeJS
cURL
Javascript (REST)
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
delete_concept_relation_response = stub.DeleteConceptRelations(
5
service_pb2.DeleteConceptRelationsRequest(
6
user_app_id=resources_pb2.UserAppIDSet(
7
app_id="{YOUR_APP_ID}"
8
),
9
concept_id="{YOUR_OBJECT_CONCEPT_ID}",
10
ids=["{YOUR_CONCEPT_RELATION_ID}"]
11
),
12
metadata=metadata
13
)
14
15
if delete_concept_relation_response.status.code != status_code_pb2.SUCCESS:
16
print("There was an error with your request!")
17
print("\tCode: {}".format(delete_concept_relation_response.outputs[0].status.code))
18
print("\tDescription: {}".format(delete_concept_relation_response.outputs[0].status.description))
19
print("\tDetails: {}".format(delete_concept_relation_response.outputs[0].status.details))
20
raise Exception("Delete concept relation failed, status: " + delete_concept_relation_response.status.description)
Copied!
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
BaseResponse deleteConceptRelationsResponse = stub.deleteConceptRelations(
8
DeleteConceptRelationsRequest.newBuilder()
9
.setUserAppId(
10
UserAppIDSet.newBuilder().setAppId("{YOUR_APP_ID}").build()
11
)
12
.addIds("{YOUR_CONCEPT_RELATION_ID}")
13
.setConceptId("{YOUR_OBJECT_CONCEPT_ID}")
14
.build()
15
);
16
17
if (deleteConceptRelationsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
18
throw new RuntimeException("Delete concept relations failed, status: " + deleteConceptRelationsResponse.getStatus());
19
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
stub.DeleteConceptRelations(
5
{
6
user_app_id: {
7
app_id: "{YOUR_APP_ID}"
8
},
9
concept_id: "{YOUR_OBJECT_CONCEPT_ID}",
10
ids: [
11
"{YOUR_CONCEPT_RELATION_ID}"
12
]
13
},
14
metadata,
15
(err, response) => {
16
if (err) {
17
throw new Error(err);
18
}
19
20
if (response.status.code !== 10000) {
21
throw new Error("Delete concept relations failed, status: " + response.status.description);
22
}
23
}
24
);
Copied!
1
curl -X DELETE 'https://api.clarifai.com/v2/users/me/apps/{YOUR_APP_ID}/concepts/{YOUR_OBJECT_CONCEPT_ID}/relations' \
2
-H 'Authorization: Key {YOUR_PERSONAL_ACCESS_TOKEN}' \
3
-H 'Content-Type: application/json' \
4
--data-raw '{
5
"ids": [
6
"{YOUR_CONCEPT_RELATION_ID}"
7
]
8
}'
Copied!
1
const appId = '{YOUR_APP_ID}'
2
const conceptId = '{YOUR_CONCEPT_ID}'
3
4
const raw = JSON.stringify({
5
"ids": [
6
"{YOUR_CONCEPT_RELATION_ID}"
7
]
8
})
9
10
const requestOptions = {
11
method: 'DELETE',
12
headers: {
13
'Accept': 'application/json',
14
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
15
}
16
};
17
18
fetch(`https://api.clarifai.com/v2/users/me/apps/${appId}/concepts/${conceptId}/relations`, requestOptions)
19
.then(response => response.text())
20
.then(result => console.log(result))
21
.catch(error => console.log('error', error));
Copied!
Last modified 57m ago
Copy link
Edit on GitHub