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.1.tar.gz (262.7 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.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cognitor-0.2.1.tar.gz
  • Upload date:
  • Size: 262.7 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.1.tar.gz
Algorithm Hash digest
SHA256 af13cc6f79cd0e50e6eb41687a038b30a5c3a5c646253c1424185f79bb988640
MD5 1f0d40bc7198518227253fa8fe3c6d07
BLAKE2b-256 55ea6f1149e1433891c2c3049a41886456b5a4661f1f3ad93347f22221690018

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cognitor-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ad7863805a0b7a115e97b6d6b4e270d65597bcb4828afec45f09028cb09f566
MD5 33a750c939cf1c48cf3686267f1d8e8b
BLAKE2b-256 547965663e2489763337944817cb84b9f1c81b59b33db64fbb62a66905a83536

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