Home

Awesome

Mona-OpenAI Integration Client

<p align="center"> <img src="https://github.com/monalabs/mona-sdk/blob/main/mona_logo.png?raw=true" alt="Mona's logo" width="180"/> </p> <p align="center"><a target="_blank" href="https://monalabs.wistia.com/medias/l6xmdj3cd6?wvideo=l6xmdj3cd6"><img src="https://embed-ssl.wistia.com/deliveries/c15bb616a389fa7d752968ccb3af2ab4.jpg?wistia-l6xmdj3cd6-1-l6xmdj3cd6-video-thumbnail=1&amp;image_play_button_size=2x&amp;image_crop_resized=960x540&amp;image_play_button=1&amp;image_play_button_color=66c7d1e0" width="400" height="225" style="width: 400px; height: 225px;"></a></p>

Use one line of code to get instant live monitoring for your OpenAI usage including:

Setting Up

$ pip install mona_openai

Quick Start

You can find boilerplate code for many use cases under the "examples" folder.

With Mona

Sign up for a free Mona account here.

from openai import OpenAI

from mona_openai import monitor_client

MONA_API_KEY = environ.get("MONA_API_KEY")
MONA_SECRET = environ.get("MONA_SECRET")
MONA_CREDS = {
    "key": MONA_API_KEY,
    "secret": MONA_SECRET,
}

# This is the name of the monitoring class on Mona
MONITORING_CONTEXT_NAME = "NEW_CHAT_CLIENT_CONTEXT"

openAI_client = monitor_client(OpenAI(api_key=environ.get("OPEN_AI_KEY")), MONA_CREDS, MONITORING_CONTEXT_NAME)

response = openAI_client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}
  ]
)
print(response.choices[0].message.content)

With Standard Logging

from openai import OpenAI
from os import environ

from mona_openai import monitor_client_with_logger

from mona_openai.loggers import StandardLogger
from logging import WARNING

MONA_API_KEY = environ.get("MONA_API_KEY")
MONA_SECRET = environ.get("MONA_SECRET")
MONA_CREDS = {
    "key": MONA_API_KEY,
    "secret": MONA_SECRET,
}

# This is the name of the monitoring class on Mona
MONITORING_CONTEXT_NAME = "NEW_CHAT_CLIENT_CONTEXT"

openAI_client = monitor_client_with_logger(OpenAI(api_key=environ.get("OPEN_AI_KEY")), StandardLogger(WARNING))

response = openAI_client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}
  ]
)
print(response.choices[0].message.content)

Supported OpenAI APIs

Currently this client supports just the Chat Completion API. Mona, not using this client, can support processes based on other APIs and also non-OpenAI-based apps. If you have a differrent use-case, we'd love to hear about it! Please email us at support@monalabs.io.

Usage

Initialization

The main functions exposed in this package are monitor_client and monitor_client_with_logger.

These functions return an openai client that wraps the original chat completion method with an equivalent API that also logs relevant metrics for monitoring behind the scenes.

See above quick start examples for usage.

Specs

The specs arg allows you to configure what should be monitored. It expects a python dict with the follwoing possible keys:

Using custom loggers

You don't have to have a Mona account to use this package. You can define specific loggers to log out the data to a file, memory, or just a given python logger.

This SDK provides a simple interface to implement your own loggers by inheriting from Logger under loggers/logger.py. Alternatively, by using the standard python logging library as in the example, you can create logging handlers to log the data out to any mechanism you choose (e.g., Kafka, Logstash, etc...)

Mona arguments you can add to the API call

Using OpenAI with REST calls instead of OpenAI's Python client

See rest examples in legacy example folder

Stream support

OpenAI allows receiving responses as a stream of tokens using the "stream" parameter. When this is done, Mona will collect all the tokens in memory (without interrupting the streaming process) and will create the analysis and log out the data the moment the stream is over. You don't need to do anything to make this happen.

Since for streaming responses OpenAI doesn't supply the full usage tokens summary, Mona uses the tiktoken package to calculate the tokens of the prompt and completion and log them for monitoring.

NOTE: Stream is currently only supported with SDK usage, and not with using REST directly.

Legacy LangChain support

You can use the exported monitor_langchain_llm to wrap a LangChain OpenAI LLM (chat or normal) with Mona's monitoring capabilities:

from mona_openai import monitor_langchain_llm

from langchain.llms import OpenAI

# Wrap the LLM object with Mona monitoring.
llm = monitor_langchain_llm(
    OpenAI(OPEN_AI_KEY),
    MONA_CREDS,
    MONITORING_CONTEXT_NAME)

See full example in completion_langchain.py in the examples folder.

Mona SDK

This package uses the mona_sdk package to export the relevant data to Mona. There are several environment variables you can use to configure the SDK's behavior. For example, you can set it up to raise exceptions when exporting data to Mona fails (it doesn't do that by default).

Monitoring for profanity

Mona uses the alt-profanity-check pacakge (https://pypi.org/project/alt-profanity-check/) to create both boolean predictions and probabilty scores for the existence of profanity both in the prompt and in the responses. We use the built in package methods for that. If you want, for example, to use a different probability threshold for the boolean prediction, you can do that by changing your Mona config on the Mona dashboard.

Using nest-asyncio

In environments in which there's a forever running event loop (e.g., Jupyter notebooks), the client might use nest_asyncio.apply() to run joint sync and async code.