Logging

Logging#

Imagine SDK uses standard Python logging to help users debug their application. The logger can be configured using the logger name imagine. If the logging level is set to DEBUG, the library will log the HTTP requests performed to the Imagine API.

The following snippet exemplifies one of the many ways users can set their logging to generate debug messages and print them to the console.

import logging.config

from imagine import ChatMessage, ImagineClient

logging.config.dictConfig(
    {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "standard": {
                "format": "%(name)s: %(message)s",
            },
        },
        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "standard",
            },
        },
        "loggers": {
            "imagine": {
                "handlers": ["console"],
                "level": "DEBUG",
                "propagate": False,
            },
        },
        "root": {
            "handlers": ["console"],
            "level": "INFO",
        },
    }
)

client = ImagineClient()

chat_response = client.chat(
    messages=[ChatMessage(role="user", content="What is the best Spanish cheese?")],
)  # This method call will generate a logging record

print(chat_response.first_content)

Alternatively, simple plain text logging to stdout can be enabled by setting the environment variable IMAGINE_DEBUG to a truthy value like 1, true, yes, etc. or passing debug=True when creating an instance of the client class. This is an examples of the latter:

from imagine import ChatMessage, ImagineClient

client = ImagineClient(debug=True)

chat_response = client.chat(
    messages=[ChatMessage(role="user", content="What is the best Spanish cheese?")],
)  # This method call will generate a logging record

print(chat_response.first_content)