Skip to main content

Nebuly SDK

The SDK for instrumenting applications for tracking AI costs.

Setup

To set up the code quality checks for this project:

  1. Clone the repository
  2. Run the setup command to install the necessary requirements, including Poetry for handling dependencies
make setup

Code Formatting and Linting

The code formatting and linting checks help maintain consistent style and identify potential issues. Black and Ruff are automatically invoked with each commit, but they can also be utilized independently without committing changes:

  • To display the issues detected by the linter
make lint
  • To automatically apply the formatter changes and the suggested changes by the linter, use the following command
make lint-fix

Supported Providers

- OpenAI
- Azure OpenAI
- Cohere
- Anthropic
- HuggingFace pipelines
- HuggingFace HUB
- LangChain
- LlamaIndex
- Amazon Bedrock
- Amazon SageMaker
- Google PALM API
- Google VertexAI

Usage

Make sure you initialize Nebuly before importing other libraries like openai, cohere, huggingface, etc.

Simple usage

In the simple case, you can just import nebuly and call the init function with your API key. This will automatically setup all the tracking for you. After that, you can call the other libraries as normal.

Example with OpenAI

import os
import nebuly

api_key = os.getenv("NEBULY_API_KEY")
nebuly.init(api_key=api_key)

import os
from openai import OpenAI

client = OpenAI()
chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-3.5-turbo",
    user_id="user-123",
    feature_flags=["new-feature_flag"],
)

Advanced usage: Context managers

In the simple case, each call will be stored as a separate Interaction, you can use context managers to group more calls in a single Interaction:

Example with OpenAI and Cohere

import os
import nebuly
from nebuly.contextmanager import new_interaction

api_key = os.getenv("NEBULY_API_KEY")
nebuly.init(api_key=api_key)

# Setup OpenAI
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

# Setup Cohere
import cohere

co = cohere.Client(os.getenv("COHERE_API_KEY"))

with new_interaction(user_id="test_user", user_group_profile="test_group") as interaction:
    # interaction.set_input("Some custom input")
    # interaction.set_history([{"role": "user/assistant", "content": "sample content"}}])
    completion_1 = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are an helpful assistant"},
            {"role": "user", "content": "Hello world"}
        ]
    )
    completion_2 = co.generate(
        prompt='Please explain to me how LLMs work',
    )
    # interaction.set_output("Some custom output")

LangChain Callbacks

import os

from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from nebuly.providers.langchain import LangChainTrackingHandler

callback = LangChainTrackingHandler(
    user_id="test_user",
    api_key=os.getenv("NEBULY_API_KEY"),
)

llm = ChatOpenAI(temperature=0.9)
prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)

chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run(
    "colorful socks",
    callbacks=[callback],
)

LlamaIndex Callbacks

import os
from nebuly.providers.llama_index import LlamaIndexTrackingHandler

handler = LlamaIndexTrackingHandler(
    api_key=os.getenv("NEBULY_API_KEY"), user_id="test_user"
)

import llama_index
from llama_index import SimpleDirectoryReader, VectorStoreIndex

llama_index.global_handler = handler

documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")

Variants Usage

from nebuly.ab_testing import ABTesting

client = ABTesting("your_nebuly_api_key")

variants = client.get_variants(
  user="<user_id>",
  feature_flags=["feature_flag_a", "feature_flag_b"]
)
print(variants)

Metadata

Release files for nebuly 0.3.38

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nebuly 0.3.38
File Size Uploaded Provenance
nebuly-0.3.38.tar.gz 35.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for nebuly 0.3.38
File Interpreter ABI Platform
nebuly-0.3.38-py3-none-any.whl Python 3 none any Details

Total release size: 84.5 kB

Release files / nebuly-0.3.38.tar.gz

Download URL nebuly-0.3.38.tar.gz
Size 35.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f4b08ab20ddf3410b099b1e53837b23c0c5aae330a2d19885f9d8e6de99fbe6e
BLAKE2b-256 checksum
How to use checksums
a38772c93e529c102ccde80b45257e30c4ceb6034a7e067008ec72b02c0e61b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.11.12 Linux/6.8.0-1021-azure

Release files / nebuly-0.3.38-py3-none-any.whl

Download URL nebuly-0.3.38-py3-none-any.whl
Size 49.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f593e5583c1a53fd475c1907d2747d0619b4a140409a6fb575aa71051c16154b
BLAKE2b-256 checksum
How to use checksums
97a55c05777e3e3b063149d7c2c74397e4da4ed0d3afe9bc10590ec45864f867
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.11.12 Linux/6.8.0-1021-azure

Security for nebuly 0.3.38

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.38 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page