Concepts Relations
Connect and organize concepts for smarter AI
The Knowledge Graph establishes semantic and hierarchical relationships between your concepts — such as synonyms, hyponyms, and hypernyms — laying the foundation for more intelligent, context-aware systems.
These relationships are crucial for building robust knowledge representations that enhance both computer vision and natural language processing capabilities.
Whether you're managing a product catalog, powering a search engine, or organizing complex datasets, using concept relationships allow you to structure, search, and retrieve information more effectively — making your AI applications significantly smarter and more precise.
Types of Concepts Relations
-
Hyponym — This represents an “is a kind of” relationship. For instance, if you define the relationship as 'honey' (subject), 'hyponym' (predicate), 'food' (object), it can be interpreted as "honey is a kind of food." This helps systems understand that "honey" belongs to a broader category.
-
Hypernym — This is the inverse of a hyponym, signifying a “parent of” relationship. Using the same example, if you define 'food' (subject), 'hypernym' (predicate), 'honey' (object), it is read as "food is a parent of honey." When a hyponym is defined, the corresponding hypernym is automatically inferred in queries, and vice versa, ensuring consistency in how concept hierarchies are interpreted.
-
Synonym — This relationship connects two concepts that essentially mean the same thing, similar to an “is” statement. For example, defining 'puppy' as a synonym of 'pup' allows the model to treat them interchangeably. This relationship is bidirectional, so adding one synonym automatically implies the reverse, making searches and classifications more flexible and inclusive.
Use Case Examples
-
Enhanced search and retrieval — Concept relationships enable more intelligent searches. For example, a search for “Animal” can automatically include related hyponyms like “Dog” and “Cat,” returning broader and more relevant results.
-
Improved data organization — Hierarchical relationships help structure complex datasets more effectively. For example, hypernyms allow models to group specific entities under broader categories, improving organizational structures.
-
Contextual understanding — When models grasp the semantic links between concepts, they can better interpret context. For instance, recognizing that “Puppy” is a synonym of “Dog” ensures all relevant information is considered during classification or prediction.
-
Dynamic content delivery — In use cases like personalized content, search recommendations, or targeted advertising, concept relationships allow systems to infer user intent and deliver more relevant, meaningful results.
Click here to learn how to leverage concept relations for more powerful and precise search queries.
How to Create Relations
To create a relation between two concepts, you first have to create them in your app. You can see the previous section on how to create concepts.
Each relation should have a specified predicate, which can be hyponym, hypernym, or synonym.
Below is an example of how to create a relation between two concepts.
Before using the Python SDK, Node.js SDK, or any of our gRPC clients, ensure they are properly installed on your machine. Refer to their respective installation guides for instructions on how to install and initialize them.
- Python SDK
- Python (gRPC)
- JavaScript (REST)
- Node.js (gRPC)
- Java (gRPC)
- PHP (gRPC)
- cURL
import os
from clarifai.client.app import App
# Set your Personal Access Token (PAT)
os.environ["CLARIFAI_PAT"] = "YOUR_PAT_HERE"
app = App(user_id="YOUR_USER_ID_HERE", app_id="YOUR_APP_ID_HERE")
# A hyponym relation between "cat" and "animal", indicating that "cat" is a kind of "animal"
# A synonym relation between "cat" and "kitten", suggests that these two concepts are essentially the same
app.create_concept_relations("cat", ["animal", "kitten"], ["hyponym", "synonym"])
###################################################################################
# In this section, we set the user authentication, app ID, subject concept ID,
# object concept ID, and predicate. Change these strings to run your own example.
###################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to whatever relations you want to create
SUBJECT_CONCEPT_ID = 'honey'
OBJECT_CONCEPT_ID = 'food'
PREDICATE = "hypernym" # This can be hypernym, hyponym, or synonym
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
post_concept_relation_response = stub.PostConceptRelations(
service_pb2.PostConceptRelationsRequest(
user_app_id=resources_pb2.UserAppIDSet(
user_id=USER_ID,
app_id=APP_ID
),
concept_id=SUBJECT_CONCEPT_ID,
concept_relations=[
resources_pb2.ConceptRelation(
object_concept=resources_pb2.Concept(id=OBJECT_CONCEPT_ID),
predicate=PREDICATE
)
]
),
metadata=metadata
)
if post_concept_relation_response.status.code != status_code_pb2.SUCCESS:
print(post_concept_relation_response.status)
raise Exception("Post concept relation failed, status: " + post_concept_relation_response.status.description)
print(post_concept_relation_response)
<!--index.html file-->
<script>
////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, subject concept ID,
// object concept ID, and predicate. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to whatever relations you want to create
const SUBJECT_CONCEPT_ID = 'honey';
const OBJECT_CONCEPT_ID = 'food';
const PREDICATE = "hypernym"; // This can be hypernym, hyponym, or synonym
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"concept_relations": [
{
"object_concept": {
"id": OBJECT_CONCEPT_ID
},
"predicate": PREDICATE
}
]
});
const requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + PAT
},
body: raw
};
fetch("https://api.clarifai.com/v2/users/me/apps/" + APP_ID + "/concepts/" + SUBJECT_CONCEPT_ID + "/relations", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
//index.js file
////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, subject concept ID,
// object concept ID, and predicate. Change these strings to run your own example.
////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to whatever relations you want to create
const SUBJECT_CONCEPT_ID = 'honey';
const OBJECT_CONCEPT_ID = 'food';
const PREDICATE = "hypernym"; // This can be hypernym, hyponym, or synonym.
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostConceptRelations(
{
user_app_id: {
"user_id": USER_ID,
"app_id": APP_ID
},
concept_id: SUBJECT_CONCEPT_ID,
concept_relations: [
{
object_concept: {
id: OBJECT_CONCEPT_ID,
},
predicate: PREDICATE
}
]
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Create concept relations failed, status: " + response.status.description);
}
}
);
package com.clarifai.example;
import com.clarifai.grpc.api.*;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
///////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, subject concept ID,
// object concept ID, and predicate. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change these to whatever relations you want to create
static final String SUBJECT_CONCEPT_ID = "honey";
static final String OBJECT_CONCEPT_ID = "food";
static final String PREDICATE = "hypernym"; // This can be hypernym, hyponym, or synonym
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiConceptRelationResponse postConceptRelationsResponse = stub.postConceptRelations(
PostConceptRelationsRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID).build())
.setConceptId(SUBJECT_CONCEPT_ID)
.addConceptRelations(
ConceptRelation.newBuilder()
.setObjectConcept(Concept.newBuilder().setId(OBJECT_CONCEPT_ID).build())
.setPredicate(PREDICATE).build())
.build()
);
if (postConceptRelationsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post concept relations failed, status: " + postConceptRelationsResponse.getStatus());
}
System.out.println(postConceptRelationsResponse);
}
}
<?php
require __DIR__ . "/vendor/autoload.php";
////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, subject concept ID,
// object concept ID, and predicate. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////////
$USER_ID = "YOUR_USER_ID_HERE";
// Your PAT (Personal Access Token) can be found in the Account's Security section
$PAT = "YOUR_PAT_HERE";
$APP_ID = "YOUR_APP_ID_HERE";
// Change these to whatever relations you want to create
$SUBJECT_CONCEPT_ID = "honey";
$OBJECT_CONCEPT_ID = "food";
$PREDICATE = "hypernym"; // This can be hypernym, hyponym, or synonym
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
use Clarifai\ClarifaiClient;
use Clarifai\Api\PostConceptRelationsRequest;
use Clarifai\Api\ConceptRelation;
use Clarifai\Api\Concept;
use Clarifai\Api\Status\StatusCode;
use Clarifai\Api\UserAppIDSet;
$client = ClarifaiClient::grpc();
$metadata = ["Authorization" => ["Key " . $PAT]];
$userDataObject = new UserAppIDSet([
"user_id" => $USER_ID,
"app_id" => $APP_ID
]);
// Let's make a RPC call to the Clarifai platform. It uses the opened gRPC client channel to communicate a
// request and then wait for the response
[$response, $status] = $client->PostConceptRelations(
// The request object carries the request along with the request status and other metadata related to the request itself
new PostConceptRelationsRequest([
"user_app_id" => $userDataObject,
"concept_id" => $SUBJECT_CONCEPT_ID,
"concept_relations" => [
new ConceptRelation([
"object_concept" => new Concept(["id" => $OBJECT_CONCEPT_ID ]),
"predicate" => $PREDICATE
])
]
]),
$metadata
)->wait();
// A response is returned and the first thing we do is check the status of it
// A successful response will have a status code of 0; otherwise, there is some error
if ($status->code !== 0) {
throw new Exception("Error: {$status->details}");
}
// In addition to the RPC response status, there is a Clarifai API status that reports if the operation was a success or failure
// (not just that the communication was successful)
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
print $response->getStatus()->getDetails();
throw new Exception("Failure response: " . $response->getStatus()->getDescription());
}
print $response->serializeToJsonString();
?>
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/concepts/YOUR_SUBJECT_CONCEPT_ID_HERE/relations" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
--data-raw '{
"concept_relations": [
{
"object_concept": {
"id": "YOUR_OBJECT_CONCEPT_ID_HERE"
},
"predicate": "hypernym"
}
]
}'
Raw Output Example
status {
code: SUCCESS
description: "Ok"
req_id: "0d0a5ec5df14d62a7d660f392ce26727"
}
concept_relations {
id: "2d794e5ede534500b4ac7da44ef570ee"
subject_concept {
id: "honey"
name: "honey"
value: 1.0
created_at {
seconds: 1643976334
nanos: 237961000
}
language: "en"
app_id: "a39423543bb941bf9ba2ee95fad11f0a"
visibility {
gettable: PRIVATE
}
user_id: "e5y2lteoz3s3iy"
}
object_concept {
id: "food"
name: "food"
value: 1.0
created_at {
seconds: 1643976326
nanos: 123719000
}
language: "en"
app_id: "a39423543bb941bf9ba2ee95fad11f0a"
visibility {
gettable: PRIVATE
}
user_id: "ei2leoz3s3iy"
}
predicate: "hypernym"
visibility {
gettable: PRIVATE
}
}
List Existing Relations
List All Relations
Below is an example of how to list all the existing relations between concepts.
- Python SDK
- Python (gRPC)
- JavaScript (REST)
- Node.js (gRPC)
- Java (gRPC)
- PHP (gRPC)
- cURL
import os
from clarifai.client.app import App
# Set your Personal Access Token (PAT)
os.environ["CLARIFAI_PAT"] = "YOUR_PAT_HERE"
app = App(user_id="YOUR_USER_ID_HERE", app_id="YOUR_APP_ID_HERE")
concept_relations = list(app.search_concept_relations())
for relation in concept_relations:
print("Subject_concept:", relation.subject_concept.id)
print('\t' "Object_concept:", relation.object_concept.id)
print('\t' "Predicate:", relation.predicate, '\n')
######################################################################################
# In this section, we set the user authentication, app ID, concept ID, and predicate.
# Change these strings to run your own example.
######################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to whatever concept you want to list its relations
CONCEPT_ID = 'honey'
PREDICATE = "hypernym" # This is optional. If skipped, all concept's relations will be returned
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
list_concept_relation_response = stub.ListConceptRelations(
service_pb2.ListConceptRelationsRequest(
user_app_id=resources_pb2.UserAppIDSet(
user_id=USER_ID,
app_id=APP_ID
),
concept_id=CONCEPT_ID,
predicate=PREDICATE
),
metadata=metadata
)
if list_concept_relation_response.status.code != status_code_pb2.SUCCESS:
print(list_concept_relation_response.status)
raise Exception("List concept relation failed, status: " + list_concept_relation_response.status.description)
for relation in list_concept_relation_response.concept_relations:
print(relation)
<!--index.html file-->
<script>
////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, concept ID, and predicate.
// Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to whatever concept you want to list its relations
CONCEPT_ID = 'honey';
PREDICATE = "hypernym"; // This is optional. If skipped, all concept's relations will be returned
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const requestOptions = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + PAT
},
};
fetch("https://api.clarifai.com/v2/users/" + USER_ID + "/apps/" + APP_ID + "/concepts/" + CONCEPT_ID + "/relations?predicate=" + PREDICATE, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
//index.js file
///////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, concept ID, and predicate.
// Change these strings to run your own example.
////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to whatever concept you want to list its relations
const CONCEPT_ID = 'honey';
const PREDICATE = "hypernym"; // This is optional. If skipped, all concept's relations will be returned
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.ListConceptRelations(
{
user_app_id: {
"user_id": USER_ID,
"app_id": APP_ID
},
concept_id: CONCEPT_ID,
predicate: PREDICATE
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("List concept relations failed, status: " + response.status.description);
}
for (const relation of response.concept_relations) {
console.log(relation);
}
}
);
package com.clarifai.example;
import com.clarifai.grpc.api.*;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, concept ID, and predicate.
// Change these strings to run your own example.
////////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change these to whatever concept you want to list its relations
static final String CONCEPT_ID = "honey";
static final String PREDICATE = "hypernym"; // This is optional. If skipped, all concept's relations will be returned
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiConceptRelationResponse listConceptRelationsResponse = stub.listConceptRelations(
ListConceptRelationsRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID).build())
.setConceptId(CONCEPT_ID)
.setPredicate(PREDICATE)
.build()
);
if (listConceptRelationsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("List concept relations failed, status: " + listConceptRelationsResponse.getStatus());
}
for (ConceptRelation relation: listConceptRelationsResponse.getConceptRelationsList()) {
System.out.println(relation);
}
}
}
<?php
require __DIR__ . "/vendor/autoload.php";
////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, concept ID, and predicate.
// Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////////
$USER_ID = "YOUR_USER_ID_HERE";
// Your PAT (Personal Access Token) can be found in the Account's Security section
$PAT = "YOUR_PAT_HERE";
$APP_ID = "YOUR_APP_ID_HERE";
// Change these to whatever concept you want to list its relations
$CONCEPT_ID = 'honey';
$PREDICATE = "hypernym"; // This is optional. If skipped, all concept's relations will be returned
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
use Clarifai\ClarifaiClient;
use Clarifai\Api\ListConceptRelationsRequest;
use Clarifai\Api\Status\StatusCode;
use Clarifai\Api\UserAppIDSet;
$client = ClarifaiClient::grpc();
$metadata = ["Authorization" => ["Key " . $PAT]];
$userDataObject = new UserAppIDSet([
"user_id" => $USER_ID,
"app_id" => $APP_ID
]);
// Let's make a RPC call to the Clarifai platform. It uses the opened gRPC client channel to communicate a
// request and then wait for the response
[$response, $status] = $client->ListConceptRelations(
// The request object carries the request along with the request status and other metadata related to the request itself
new ListConceptRelationsRequest([
"user_app_id" => $userDataObject,
"concept_id" => $CONCEPT_ID,
"predicate" => $PREDICATE
]),
$metadata
)->wait();
// A response is returned and the first thing we do is check the status of it
// A successful response will have a status code of 0; otherwise, there is some error
if ($status->code !== 0) {
throw new Exception("Error: {$status->details}");
}
// In addition to the RPC response status, there is a Clarifai API status that reports if the operation was a success or failure
// (not just that the communication was successful)
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
print $response->getStatus()->getDetails();
throw new Exception("Failure response: " . $response->getStatus()->getDescription());
}
foreach ($response->getConceptRelations() as $concept) {
echo $concept->SerializeToJsonString() . "</br>";
}
?>
# Setting the predicate parameter is optional. If skipped, all concept's relations will be returned.
curl -X GET "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/concepts/YOUR_CONCEPT_ID_HERE/relations?predicate=hypernym" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json"
Raw Output Example
id: "2d794e5ede534500b4ac7da44ef570ee"
subject_concept {
id: "honey"
name: "honey"
value: 1.0
created_at {
seconds: 1643976334
nanos: 237961000
}
language: "en"
app_id: "a39423543bb941bf9ba2ee95fad11f0a"
visibility {
gettable: PRIVATE
}
user_id: "ei2leoz3s3iy"
}
object_concept {
id: "food"
name: "food"
value: 1.0
created_at {
seconds: 1643976326
nanos: 123719000
}
language: "en"
app_id: "a39423543bb941bf9ba2ee95fad11f0a"
visibility {
gettable: PRIVATE
}
user_id: "ei2leoz3s3iy"
}
predicate: "hypernym"
visibility {
gettable: PRIVATE
}
List Specific Concept Relations
Below is an example of how to list the specific relations between concepts.
- Python SDK
import os
from clarifai.client.app import App
# Set your Personal Access Token (PAT)
os.environ["CLARIFAI_PAT"] = "YOUR_PAT_HERE"
app = App(user_id="YOUR_USER_ID_HERE", app_id="YOUR_APP_ID_HERE")
concept_relations = list(app.search_concept_relations(concept_id="cat", predicate="synonym"))
for relation in concept_relations:
print("Subject_concept:", relation.subject_concept.id)
print('\t' "Object_concept:", relation.object_concept.id)
print('\t' "Predicate:", relation.predicate, '\n')
List Relations in Tree Structure
You can set the show_tree
argument to True
in search_concept_relations()
to display concept relationships in a rich, hierarchical tree structure.
- Python SDK
import os
from clarifai.client.app import App
# Set your Personal Access Token (PAT)
os.environ["CLARIFAI_PAT"] = "YOUR_PAT_HERE"
app = App(user_id="YOUR_USER_ID_HERE", app_id="YOUR_APP_ID_HERE")
concept_relations = list(app.search_concept_relations(show_tree=True))
for relation in concept_relations:
print("Subject_concept:",relation.subject_concept.id)
print('\t'"Oject_concept:",relation.object_concept.id)
print('\t'"Predicate:",relation.predicate,'\n')
Delete Relations
Below is an example of how to delete relations between concepts.
You can use either of the following ways to retrieve the CONCEPT_RELATION_IDS
:
- Use the above List Existing Relations method to list ALL existing relations between concepts. Remember to omit the
predicate
parameter. - Log in to the Portal and access the relations details of your concept. Then, inspect the network activity under your browser's Network Tab. The IDs are under the
relations
category.
- Python (gRPC)
- JavaScript (REST)
- Node.js (gRPC)
- Java (gRPC)
- PHP (gRPC)
- cURL
###################################################################################
# In this section, we set the user authentication, app ID, object concept ID, and
# concept relation IDs. Change these strings to run your own example.
####################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to whatever relations you want to delete
OBJECT_CONCEPT_ID = 'YOUR_OBJECT_CONCEPT_ID_HERE'
CONCEPT_RELATION_IDS = ['0d9b0acb10fb4dac9a9d60a149d8fc5c','f5acf9c2a76143d78daf5f984693c52c']
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
delete_concept_relation_response = stub.DeleteConceptRelations(
service_pb2.DeleteConceptRelationsRequest(
user_app_id=resources_pb2.UserAppIDSet(
user_id=USER_ID,
app_id=APP_ID
),
concept_id=OBJECT_CONCEPT_ID,
ids=CONCEPT_RELATION_IDS
),
metadata=metadata
)
if delete_concept_relation_response.status.code != status_code_pb2.SUCCESS:
print(delete_concept_relation_response.status)
raise Exception("Delete concept relation failed, status: " + delete_concept_relation_response.status.description)
<!--index.html file-->
<script>
////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, object concept ID, and
// concept relation IDs. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to whatever relations you want to delete
const OBJECT_CONCEPT_ID = 'YOUR_OBJECT_CONCEPT_ID_HERE';
const CONCEPT_RELATION_IDS = ['0d9b0acb10fb4dac9a9d60a149d8fc5c','f5acf9c2a76143d78daf5f984693c52c'];
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const raw = JSON.stringify({
"ids": CONCEPT_RELATION_IDS
});
const requestOptions = {
method: 'DELETE',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + PAT
},
body: raw
};
fetch("https://api.clarifai.com/v2/users/" + USER_ID + "/apps/" + APP_ID + "/concepts/" + OBJECT_CONCEPT_ID + "/relations", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
</script>
//index.js file
//////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, object concept ID, and
// concept relation IDs. Change these strings to run your own example.
//////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the Account's Security section
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to whatever relations you want to delete
const OBJECT_CONCEPT_ID = 'YOUR_OBJECT_CONCEPT_ID_HERE';
const CONCEPT_RELATION_IDS = ['0d9b0acb10fb4dac9a9d60a149d8fc5c','f5acf9c2a76143d78daf5f984693c52c'];
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.DeleteConceptRelations(
{
user_app_id: {
"user_id": USER_ID,
"app_id": APP_ID
},
concept_id: OBJECT_CONCEPT_ID,
ids: CONCEPT_RELATION_IDS
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Delete concept relations failed, status: " + response.status.description);
}
}
);
package com.clarifai.example;
import com.clarifai.grpc.api.*;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.status.BaseResponse;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
/////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, object concept ID, and
// concept relation IDs. Change these strings to run your own example.
/////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change these to whatever relations you want to delete
static final String OBJECT_CONCEPT_ID = "YOUR_OBJECT_CONCEPT_ID_HERE";
static final String CONCEPT_RELATION_ID_1 = "0d9b0acb10fb4dac9a9d60a149d8fc5c";
static final String CONCEPT_RELATION_ID_2 = "f5acf9c2a76143d78daf5f984693c52c";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
BaseResponse deleteConceptRelationsResponse = stub.deleteConceptRelations(
DeleteConceptRelationsRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID).build())
.addIds(CONCEPT_RELATION_ID_1)
.addIds(CONCEPT_RELATION_ID_2)
.setConceptId(OBJECT_CONCEPT_ID)
.build()
);
if (deleteConceptRelationsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Delete concept relations failed, status: " + deleteConceptRelationsResponse.getStatus());
}
}
}
<?php
require __DIR__ . "/vendor/autoload.php";
////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, object concept ID, and
// concept relation IDs. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////////
$USER_ID = "YOUR_USER_ID_HERE";
// Your PAT (Personal Access Token) can be found in the Account's Security section
$PAT = "YOUR_PAT_HERE";
$APP_ID = "YOUR_APP_ID_HERE";
// Change these to whatever relations you want to delete
$OBJECT_CONCEPT_ID = "YOUR_OBJECT_CONCEPT_ID_HERE";
$CONCEPT_RELATION_IDS = ["0d9b0acb10fb4dac9a9d60a149d8fc5c","f5acf9c2a76143d78daf5f984693c52c"];
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
use Clarifai\ClarifaiClient;
use Clarifai\Api\DeleteConceptRelationsRequest;
use Clarifai\Api\Status\StatusCode;
use Clarifai\Api\UserAppIDSet;
$client = ClarifaiClient::grpc();
$metadata = ["Authorization" => ["Key " . $PAT]];
$userDataObject = new UserAppIDSet([
"user_id" => $USER_ID,
"app_id" => $APP_ID
]);
// Let's make a RPC call to the Clarifai platform. It uses the opened gRPC client channel to communicate a
// request and then wait for the response
[$response, $status] = $client->DeleteConceptRelations(
// The request object carries the request along with the request status and other metadata related to the request itself
new DeleteConceptRelationsRequest([
"user_app_id" => $userDataObject,
"concept_id" => $OBJECT_CONCEPT_ID,
"ids" => $CONCEPT_RELATION_IDS
]),
$metadata
)->wait();
// A response is returned and the first thing we do is check the status of it
// A successful response will have a status code of 0; otherwise, there is some error
if ($status->code !== 0) {
throw new Exception("Error: {$status->details}");
}
// In addition to the RPC response status, there is a Clarifai API status that reports if the operation was a success or failure
// (not just that the communication was successful)
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
print $response->getStatus()->getDetails();
throw new Exception("Failure response: " . $response->getStatus()->getDescription());
}
?>
curl -X DELETE "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/concepts/YOUR_OBJECT_CONCEPT_ID_HERE/relations" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
--data-raw '{
"ids": [
"CONCEPT_RELATION_ID_1", "CONCEPT_RELATION_ID_2"
]
}'