Skip to main content

Tools for testing, debugging, and evaluating LLM features.

Project description

Baserun

Twitter

Baserun is the testing and observability platform for LLM apps.

Quick Start

1. Install Baserun

pip install baserun

2. Set up Baserun in your application

Set the Baserun API key

Create an account at https://baserun.ai. Then generate an API key for your project in the settings tab. Set it as an environment variable:

export BASERUN_API_KEY="your_api_key_here"

Or set baserun.api_key to its value:

baserun.api_key = "br-..."

Initialize Baserun

At some point during your application's startup you need to call baserun.init(). This sets up the observability system and enables Baserun. If init is not called, Baserun will be disabled.

3. Set up your traces

A trace comprises a series of events executed within an your application. Tracing enables Baserun to display an LLM chain’s entire lifecycle, whether synchronous or asynchronous.

To start tracing add the @baserun.trace decorator to the function you want to observe (e.g. a request/response handler or your main function).

Here is a simple example. In this case, Baserun is initialized at application startup and the answer_question function is traced. The LLM call within that function will now be traced.

import sys
from openai import OpenAI
import baserun


@baserun.trace
def answer_question(question: str) -> str:
    client = OpenAI()
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": question}],
    )
    return response["choices"][0]["message"]["content"]


if __name__ == "__main__":
    baserun.init()
    print(answer_question(sys.argv[-1]))

4. (Optional) Set up User Sessions

If your application involves interaction with a user and you wish to associate logs and traces with a particular user, you can use User Sessions. You can do this using with_sessions:

from openai import OpenAI
import baserun

@baserun.trace
def use_sessions(prompt="What is the capitol of the US?") -> str:
    client = OpenAI()
    with baserun.with_session(user_identifier="example@test.com"):
        completion = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}],
        )
        content = completion.choices[0].message.content
        return content

5. (Optional) Set up your test suite

Use our pytest plugin and start immediately testing with Baserun. By default all OpenAI and Anthropic requests will be automatically logged.

# test_module.py

import openai

def test_paris_trip():
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        temperature=0.7,
        messages=[
            {
                "role": "user",
                "content": "What are three activities to do in Paris?"
            }
        ],
    )
    
    assert "Eiffel Tower" in response['choices'][0]['message']['content']

To run the test and log to baserun:

pytest --baserun test_module.py
...
========================Baserun========================
Test results available at: https://baserun.ai/runs/<id>
=======================================================

6. (Optional) Set up checks

Baserun supports checks (also more broadly known as "evaluations"). These are assertions that the LLM response you received matches whatever criteria you require. To use a check, you can use baserun.check like so:

from openai import OpenAI
import baserun

client = OpenAI()
completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "What is the capital of the United States?"}],
)
content = completion.choices[0].message.content
baserun.check(name="capital_answer", result="Washington" in content)

Further Documentation

For a deeper dive on all capabilities and more advanced usage, please refer to our Documentation.

License

MIT License

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

baserun-0.9.0.tar.gz (39.0 kB view details)

Uploaded Source

Built Distribution

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

baserun-0.9.0-py3-none-any.whl (43.2 kB view details)

Uploaded Python 3

File details

Details for the file baserun-0.9.0.tar.gz.

File metadata

  • Download URL: baserun-0.9.0.tar.gz
  • Upload date:
  • Size: 39.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.3

File hashes

Hashes for baserun-0.9.0.tar.gz
Algorithm Hash digest
SHA256 25c64d9ab73f026d31ef193a054a70a4d71f8d3d976f3d0c749af7a7a35a391e
MD5 ea34a089e845cb2c5eeba5ead5e1c4f5
BLAKE2b-256 d802f61eef5f2d5e04e8ff06c98c1e47683be80f789a67cb76c54934da675479

See more details on using hashes here.

File details

Details for the file baserun-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: baserun-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 43.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.3

File hashes

Hashes for baserun-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f383af25b3d070fd5194a866f196a68fe9d8ba6db0b107e4931348f47f9915f7
MD5 48bae18922aafc34b55bbb86da391059
BLAKE2b-256 e567662dd0946127a21e40f798879fb372404161dea1472d447a84269a9eeab6

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