Skip to main content

Python SDK to extract relevant metrics from Small Language Model inference calls.

Project description

cognitor-py

cognitor-py is the Python SDK of our Cognitor platform. It is used to get detailed tracing and observability for Small Language Models applications. All metrics can be saved to a self-hosted instance of the Cognitor platform or to a local file.

At this time, cognitor-py supports HuggingFace transformers models.

Installation

pip install cognitor

Usage

Available logging modes

  • log_type="database": Logs are sent to a self-hosted instance of the Cognitor platform. This requires a running instance of the Cognitor platform, otherwise the logs will fail to be sent.
  • log_type="file": Logs are saved to a local file in JSON format. This is useful for development and debugging, but does not provide the same level of querying and visualization capabilities as the database mode

Inference monitoring

from cognitor import Cognitor

# Initialize your model and tokenizer
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
pipe = pipeline("text-generation", model=model_name, tokenizer=tokenizer)

cognitor = Cognitor(
    model_name=model_name,
    tokenizer=tokenizer,
    log_type="database", # or "file" for local file logging
    # Database connection parameters (only needed if log_type="database")
    host="localhost",
    port=5432,
    user="postgres",
    password="postgres",
    dbname="cognitor"
)

# Run inference within the monitor context
with cognitor.monitor() as m:
    input_text = "Once upon a time,"
    with m.track():
        output = pipe(input_text, max_length=50)
    m.capture(input_data=input_text, output=output)

Training monitoring

from cognitor import Cognitor, HFTrainingCallback
from transformers import Trainer, TrainingArguments

cognitor = Cognitor(
    model_name="my-model",
    log_type="database", # or "file" for local file logging
    # Database connection parameters (only needed if log_type="database")
    host="localhost",
    port=5432,
    user="postgres",
    password="postgres",
    dbname="cognitor",
)

training_args = TrainingArguments(
    output_dir="./output",
    num_train_epochs=3,
    per_device_train_batch_size=16,
    eval_strategy="epoch",
    logging_strategy="steps",
    logging_steps=1,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    callbacks=[HFTrainingCallback(cognitor)],
)

# call new_training_run() to create a new training run ID for the callback-logged steps. If not called, all steps will be logged under the same training run ID.
cognitor.new_training_run()

trainer.train()

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

cognitor-0.2.0.tar.gz (262.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cognitor-0.2.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file cognitor-0.2.0.tar.gz.

File metadata

  • Download URL: cognitor-0.2.0.tar.gz
  • Upload date:
  • Size: 262.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cognitor-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4ae2da821c25ec22e2f48b29f0a7784d814e543fbd823e200647a7a39a58f247
MD5 9793d22345b2f144ef4908a665694a49
BLAKE2b-256 cb9812c832a2693eaab5979cb99899b6eb3e0c58a95fca0e19ecb4c918e71f7c

See more details on using hashes here.

File details

Details for the file cognitor-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cognitor-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cognitor-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a499cfeb76ad93c4e17ff712f60b8fd99b63426120ed67c7471a65903947d0d
MD5 2b465dda64fc410e479a022073e3dc03
BLAKE2b-256 6ad1c55409d5876a0ed6acbc555abf1bd287784ee841607995af1d080f53a12f

See more details on using hashes here.

Supported by

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