Skip to main content

A Python NLP Commonsense Reasoning library

Project description

kogito

A Python NLP Commonsense Reasoning library

Installation

Installation with pip

kogito can be installed using pip.

pip install kogito

It requires a minimum python version of 3.8.

Setup

kogito uses spacy under the hood for various text processing purposes, so, a spacy language package has to be installed before running the inference module.

python -m spacy download en_core_web_sm

By default, CommonsenseInference module uses en_core_web_sm to initialize spacy pipeline, but a different language pipeline can be specified as well.

Quickstart

kogito provides an easy interface to interact with commonsense reasoning models such as COMET to generate inferences from a text input. Here is a sample usage of the library where you can initialize an inference module, a custom commonsense reasoning model, and generate a knowledge graph from text on the fly.

from kogito.models.bart.comet import COMETBART
from kogito.inference import CommonsenseInference

# Load pre-trained model from HuggingFace
model = COMETBART.from_pretrained("mismayil/comet-bart-ai2")

# Initialize inference module with a spacy language pipeline
csi = CommonsenseInference(language="en_core_web_sm")

# Run inference
text = "PersonX becomes a great basketball player"
kgraph = csi.infer(text, model)

# Save output knowledge graph to JSON file
kgraph.to_jsonl("kgraph.json")

Here is an excerpt from the result of the above code sample:

{"head": "PersonX becomes a great basketball player", "relation": "Causes", "tails": [" PersonX practices every day.", " PersonX plays basketball every day", " PersonX practices every day"]}
{"head": "basketball", "relation": "ObjectUse", "tails": [" play with friends", " play basketball with", " play basketball"]}
{"head": "player", "relation": "CapableOf", "tails": [" play game", " win game", " play football"]}
{"head": "great basketball player", "relation": "HasProperty", "tails": [" good at basketball", " good at sports", " very good"]}
{"head": "become player", "relation": "isAfter", "tails": [" play game", " become coach", " play with"]}

This is just one way to generate commonsense inferences and kogito offers much more. For complete documentation, check out the kogito docs.

Development

Setup

kogito uses Poetry to manage its dependencies.

Install poetry from the official repository first:

curl -sSL https://install.python-poetry.org | python3 -

Then run the following command to install package dependencies:

poetry install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kogito-0.2.0.tar.gz (4.3 MB view hashes)

Uploaded Source

Built Distribution

kogito-0.2.0-py3-none-any.whl (4.3 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page