npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grafana-openai-monitoring

v0.0.4

Published

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

Downloads

181

Readme

OpenAI Monitoring: Monitor OpenAI API Usage with Grafana Cloud

Grafana GitHub Last Commit GitHub Contributors

Node.js Tests

grafana-openai-monitoring is an NPM Package that provides a way to monitor chat completions and Completions endpoints of 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 npm:

npm install grafana-openai-monitoring

Usage

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

| OpenAI Function | Monitoring Function | |---------------------------------|---------------------| | openai.ChatCompletion.create | chat_v2.monitor | | openai.Completion.create | chat_v1.monitor |

ChatCompletions

To monitor ChatCompletions using the OpenAI API, you can use the chat_v2.monitor decorator. This decorator automatically tracks API calls and sends metrics and logs to the specified Grafana Cloud endpoints.

Here's how to set it up:

import OpenAI from 'openai';
import { chat_v2 } from 'grafana-openai-monitoring';

const openai = new OpenAI({
  apiKey: 'YOUR_OPENAI_API_KEY',
});

// Patch method
chat_v2.monitor(openai, {
  metrics_url: 'YOUR_PROMETHEUS_METRICS_URL',
  logs_url: 'YOUR_LOKI_LOGS_URL',
  metrics_username: 'YOUR_METRICS_USERNAME',
  logs_username: 'YOUR_LOGS_USERNAME',
  access_token: 'YOUR_ACCESS_TOKEN',
});

// Now any call to openai.ChatCompletion.create will be automatically tracked
async function main() {
  const completion = await openai.completions.create({
    model: 'gpt-4',
    max_tokens: 100,
    messages: [{ role: 'user', content: 'What is Grafana?' }],
  });
  console.log(completion);
}

main();

Completions

To monitor completions using the OpenAI API, you can use the chat_v1.monitor decorator. 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:

import OpenAI from 'openai';
import { chat_v1 } from 'grafana-openai-monitoring';

const openai = a new OpenAI({
  apiKey: 'YOUR_OPENAI_API_KEY',
});

// Patch method
chat_v1.monitor(openai, {
  metrics_url: 'YOUR_PROMETHEUS_METRICS_URL',
  logs_url: 'YOUR_LOKI_LOGS_URL',
  metrics_username: 'YOUR_METRICS_USERNAME',
  logs_username: 'YOUR_LOGS_USERNAME',
  access_token: 'YOUR_ACCESS_TOKEN',
});

// Now any call to openai.Completion.create will be automatically tracked
async function main() {
  const completion = await openai.completions.create({
    model: 'davinci',
    max_tokens: 100,
    prompt: 'Isn\'t Grafana the best?',
  });
  console.log(completion);
}

main();

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 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

Node.js version 16 and above

Dependencies

License

This project is licensed under the GPL-3.0 license - see the LICENSE for details.