Package: grafana-openai-monitoring
OpenAI Monitoring: Monitor OpenAI API Usage with Grafana Cloudgrafana-openai-monitoringis a Python library that provides a decorators to monitor chat completions and Completions endpoints of the OpenAI API. It facilitates sending metrics and logs toGrafana Cloud, allowing you to track and analyze OpenAI API usage and responses.InstallationYou can installgrafana-openai-monitoringusing pip:pipinstallgrafana-openai-monitoringUsageThe following tables shows which OpenAI function correspons to which monitoing function in this libraryOpenAI FunctionMonitoring FunctionChatCompletion.createchat_v2.monitorCompletion.createchat_v1.monitorChatCompletionsTo monitor ChatCompletions using the OpenAI API, you can use thechat_v2.monitordecorator. This decorator automatically tracks API calls and sends metrics and logs to the specified Grafana Cloud endpoints.Here's how to set it up:fromopenaiimportOpenAIfromgrafana_openai_monitoringimportchat_v2client=OpenAI(api_key="YOUR_OPENAI_API_KEY",)# Apply the custom decorator to the OpenAI API function. To use with AsyncOpenAI, Pass `use_async` = True in this function.client.chat.completions.create=chat_v2.monitor(client.chat.completions.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 client.chat.completions.create will be automatically trackedresponse=client.chat.completions.create(model="gpt-4",max_tokens=100,messages=[{"role":"user","content":"What is Grafana?"}])print(response)CompletionsTo monitor completions using the OpenAI API, you can use thechat_v1.monitordecorator. This decorator adds monitoring capabilities to the OpenAI API function and sends metrics and logs to the specified Grafana Cloud endpoints.Here's how to apply it:fromopenaiimportOpenAIfromgrafana_openai_monitoringimportchat_v1client=OpenAI(api_key="YOUR_OPENAI_API_KEY",)# Apply the custom decorator to the OpenAI API functionclient.completions.create=chat_v1.monitor(client.completions.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 client.completions.create will be automatically trackedresponse=client.completions.create(model="davinci",max_tokens=100,prompt="Isn't Grafana the best?")print(response)ConfigurationTo 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 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 theCloud Access Policy tokenrequired for authentication.After configuring the parameters, the monitored API function will automatically log and track the requests and responses to the specified endpoints.CompatibilityPython 3.7.1 and aboveDependenciesOpenAIrequestsLicenseThis project is licensed under the  GPL-3.0 license - see theLICENSEfor details.