Skip to main content

Library to monitor your OpenAI usage and send metrics and logs to Grafana Cloud

Project description

OpenAI Monitoring: Monitor OpenAI API Usage with Grafana Cloud

Tests Pylint

grafana-openai-monitoring is a Python library that provides a decorator to monitor chat completions using the OpenAI API. It facilitates sending metrics and logs to Grafana Cloud, allowing you to track and analyze OpenAI API usage and responses.

Installation

You can install grafana-openai-monitoring using pip:

pip install grafana-openai-monitoring

Usage

The following tables shows which OpenAI function correspons to which monitoing function in this library

OpenAI Function Monitoring Function
ChatCompletion.create chat_v2.monitor
Completion.create chat_v1.monitor

ChatCompletions

import openai
from grafana_openai_monitoring import chat_v2

# Set your OpenAI API key
openai.api_key = "YOUR_OPENAI_API_KEY"

# Apply the custom decorator to the OpenAI API function
openai.ChatCompletion.create = chat_v2.monitor(
    openai.ChatCompletion.create,
    metrics_url="YOUR_PROMETHEUS_METRICS_URL",  # Example: "https://prometheus.grafana.net/api/prom"
    logs_url="YOUR_LOKI_LOGS_URL",  # Example: "https://logs.example.com/loki/api/v1/push/"
    metrics_username="YOUR_METRICS_USERNAME",  # Example: "123456"
    logs_username="YOUR_LOGS_USERNAME",  # Example: "987654"
    access_token="YOUR_ACCESS_TOKEN"  # Example: "glc_eyasdansdjnaxxxxxxxxxxx"
)

# Now any call to openai.ChatCompletion.create will be automatically tracked
response = openai.ChatCompletion.create(model="gpt-4", max_tokens=1, messages=[{"role": "user", "content": "What is Grafana?"}])
print(response)

Completions

import openai
from grafana_openai_monitoring import chat_v1

# Set your OpenAI API key
openai.api_key = "YOUR_OPENAI_API_KEY"

# Apply the custom decorator to the OpenAI API function
openai.Completion.create = chat_v1.monitor(
    openai.Completion.create,
    metrics_url="YOUR_PROMETHEUS_METRICS_URL",  # Example: "https://prometheus.grafana.net/api/prom"
    logs_url="YOUR_LOKI_LOGS_URL",  # Example: "https://logs.example.com/loki/api/v1/push/"
    metrics_username="YOUR_METRICS_USERNAME",  # Example: "123456"
    logs_username="YOUR_LOGS_USERNAME",  # Example: "987654"
    access_token="YOUR_ACCESS_TOKEN"  # Example: "glc_eyasdansdjnaxxxxxxxxxxx"
)

# Now any call to openai.Completion.create will be automatically tracked
response = openai.Completion.create(model="gpt-4", max_tokens=1, prompt="What is Grafana?")
print(response)

Configuration

To use the grafana-openai-monitoring library effectively, you need to provide the following information:

  • YOUR_OPENAI_API_KEY: Replace this with your actual OpenAI API key.
  • YOUR_PROMETHEUS_METRICS_URL: Replace with the URL with your Prometheus URL.
  • YOUR_LOKI_LOGS_URL: Replace with the URL where you want to send Loki logs.
  • YOUR_METRICS_USERNAME: Replace with the username for Prometheus.
  • YOUR_LOGS_USERNAME: Replace with the username for Loki.
  • YOUR_ACCESS_TOKEN: Replace with the Cloud Access Policy token required for authentication.

After configuring the parameters, the monitored API function will automatically log and track the requests and responses to the specified endpoints.

Compatibility

Python 3.7.1 and above

Dependencies

Grafana Cloud openai requests time

License

This project is licensed under the MIT License - see the LICENSE for details.

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

grafana_openai_monitoring-0.0.4.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

grafana_openai_monitoring-0.0.4-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file grafana_openai_monitoring-0.0.4.tar.gz.

File metadata

File hashes

Hashes for grafana_openai_monitoring-0.0.4.tar.gz
Algorithm Hash digest
SHA256 32e479d44873d532cc8f6ef9e48a37ebab84adfd4ce4b5f16effd6beebee7813
MD5 4b049db0d534ae5470d29daceb0368ed
BLAKE2b-256 e77046cc2b249bc5669d3c3b9bdb5dd081684d33ad3b74bdbe8b774976e63ee5

See more details on using hashes here.

File details

Details for the file grafana_openai_monitoring-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for grafana_openai_monitoring-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6a2f3732732437c05ebcf23fe238cda855e76c46c1351317d06c68b89791bd1e
MD5 77fbae3b14f888c5dd6c89dbbdaf2b51
BLAKE2b-256 c9fefa0d8bb78aca6578ce68e4bb91fbec0b81c8a5950fbdc4a2ee50065c4fac

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