Skip to content

PollyKG

The PollyKG class provides an interface to interact with the Polly Knowledge Graph (KG) API. It enables users to execute and manage Gremlin and OpenCypher queries, retrieve node and relationship data, and analyze graph structures efficiently. This class simplifies access to the KG engine, allowing seamless querying and data exploration. It is designed for users who need to extract insights from complex graph-based datasets.

Parameters:

  • token (str, default: None ) –

    Authentication token from polly

Usage

from polly.polly_kg import PollyKG

kg = PollyKG(token)

get_engine_status

get_engine_status()

Retrieve a status of the Polly Knowledge Graph.

Returns:

  • dict ( dict ) –

    A dictionary containing status information about the engine, such as gremlin, opencypher.

Raises:

  • ResourceNotFoundError

    Raised when the specified graph does not exist.

  • AccessDeniedError

    Raised when the user does not have permission to access the graph status.

  • RequestFailureException

    Raised when the request fails due to an unexpected error.

Examples:

>>> kg = PollyKG()
>>> status = kg.get_engine_status()

get_graph_summary

get_graph_summary()

Retrieve a summary of the Polly Knowledge Graph.

Returns:

  • dict ( dict ) –

    A dictionary containing summary information about the graph, such as node counts, edge counts, and other metadata.

Raises:

  • ResourceNotFoundError

    Raised when the specified graph summary does not exist.

  • AccessDeniedError

    Raised when the user does not have permission to access the graph summary.

  • RequestFailureException

    Raised when the request fails due to an unexpected error.

Examples:

>>> kg = PollyKG()
>>> summary = kg.get_graph_summary()

run_gremlin_query

run_gremlin_query(query)

Execute a Gremlin query against the Polly KG endpoint.

Parameters:

  • query (str) –

    The Gremlin query to execute.

Returns:

  • dict ( dict ) –

    The query execution results.

Raises:

  • InvalidParameterException

    Raised when the query is empty or None.

  • RequestFailureException

    Raised when the request fails due to an unexpected error.

  • QueryFailedException

    Raised when the query execution fails due to a timeout.

Examples:

>>> kg = PollyKG()
>>> query = "your query"
>>> summary = kg.run_gremlin_query(query)

run_opencypher_query

run_opencypher_query(query)

Execute a opencypher query against the Polly KG endpoint.

Parameters:

  • query (str) –

    The opencypher query to execute.

Returns:

  • dict ( dict ) –

    The query execution results.

Raises:

  • InvalidParameterException

    Raised when the query is empty or None.

  • RequestFailureException

    Raised when the request fails due to an unexpected error.

  • QueryFailedException

    Raised when the query execution fails due to a timeout.

Examples:

>>> kg = PollyKG()
>>> query = "your query"
>>> summary = kg.run_opencypher_query(query)

Examples

PollyKG class of polly-python can be initialised using the code block below:-

import os
from polly.auth import Polly
from polly.polly_kg import PollyKG
token = os.environ['POLLY_REFRESH_TOKEN']
Polly.auth(token)
kg = PollyKG()

get_graph_summary()

Users can get summary of the Polly Knowledge Graph with example shown below:-

summary = kg.get_graph_summary()
INFO:root:Graph Summary !

{
    "numNodes": 9999999,
    "numEdges": 9999999,
    "numNodeLabels": 99,
    "numEdgeLabels": 99,
    "nodeLabels": [
        "disease",
        "exposure",
        "example",
        "example2"
    ],
    "edgeLabels": [
        "enzyme",
        "side effect",
        "example",
        "example2"
    ],
    "numNodeProperties": 99,
    "numEdgeProperties": 99,
    "nodeProperties": [
        {
            "name": 999999
        }
    ],
    "edgeProperties": [
        {
            "weights": 999999
        }
    ],
    "totalNodePropertyValues": 999999,
    "totalEdgePropertyValues": 999999
}

get_engine_status()

Users can get status of the Polly Knowledge Graph with example shown below:-

status = kg.get_engine_status()
INFO:root:Graph Status !

{
    "status": "healthy",
    "gremlin": {
        "version": "version"
    },
    "opencypher": {
        "version": "version"
    }
}

run_gremlin_query()

#running a gremline query 
query="gremline_query"
kg.run_gremlin_query(query)
INFO:root:Run Complete !

{
    "data": {
        "@type": "g:List",
        "@value": [
            {
                "@type": "g:Map",
                "@value": [
                    "id",
                    "9999",
                    "properties",
                    {
                        "@type": "g:Map",
                        "@value": [
                            "name",
                            {"@type": "g:List", "@value": ["PPPPPP"]},
                        ],
                    },
                ],
            },
            {
                "@type": "g:Map",
                "@value": [
                    "id",
                    "55555",
                    "properties",
                    {
                        "@type": "g:Map",
                        "@value": [
                            "name",
                            {"@type": "g:List", "@value": ["PPPPPP"]},
                        ],
                    },
                ],
            },
        ],
    },
    "meta": {"@type": "g:Map", "@value": []},
}

run_opencypher_query()

#running a opencypher query 
query="opencypher_query"
kg.run_opencypher_query(query)
INFO:root:Run Complete !

[
    {
        "node": {
            "~id": "9999",
            "~entityType": "node",
            "~labels": ["gene/property"],
            "~properties": {"name": "PPPPPP"},
        }
    }
]