Skip to main content

Basalt SDK for python

Project description

Basalt SDK

Basalt is a powerful tool for managing AI prompts, monitoring AI applications, and their release workflows. This SDK is the official Python package for interacting with your Basalt prompts and monitoring your AI applications.

Installation

Install the Basalt SDK via pip:

pip install basalt-sdk

Usage

Importing and Initializing the SDK

To get started, import the Basalt class and initialize it with your API key:

from basalt import Basalt

# Basic initialization with API key
basalt = Basalt(api_key="my-dev-api-key")

# Specify a log_level
basalt = Basalt(api_key="my-dev-api-key", log_level="debug")

# Or with an environment variable
import os
basalt = Basalt(api_key=os.getenv("BASALT_API_KEY"))

Prompt SDK

The Prompt SDK allows you to interact with your Basalt prompts.

For a complete working example, check out our Prompt SDK Demo Notebook.

Available Methods

Prompts

Your Basalt instance exposes a prompt property for interacting with your Basalt prompts:

  • Get a Prompt

    Retrieve a specific prompt using a slug, and optional filters tag and version. Without tag or version, the production version of your prompt is selected by default.

    Example Usage:

    error, result = basalt.prompt.get('prompt-slug')
    
    # With optional tag or version parameters
    error, result = basalt.prompt.get(slug='prompt-slug', tag='latest')
    error, result = basalt.prompt.get(slug='prompt-slug', version='1.0.0')
    
    # If your prompt has variables,
    # pass them when fetching your prompt
    error, result = basalt.prompt.get(slug='prompt-slug', variables={ 'name': 'John Doe' })
    
    # Handle the result by unwrapping the error / value
    if error:
        print('Could not fetch prompt', error)
    else:
        # Use the prompt with your AI provider of choice
        # Example: OpenAI
        openai_client.chat_completion.create(
            model='gpt-4',
            messages=[{'role': 'user', 'content': result.prompt}]
        )
    

Monitor SDK

The Monitor SDK allows you to track and monitor your AI application's execution through traces, logs, and generations.

For a complete working example, check out our Monitor SDK Demo Notebook.

Creating a Trace

A trace represents a complete execution flow in your application:

# Create a trace
trace = basalt.monitor.create_trace(
    "slug",  # Chain slug - identifies this type of workflow
    {
        "input": "What are the benefits of AI in healthcare?",
        "user": {"id": "user123", "name": "John Doe"},
        "organization": {"id": "org123", "name": "Healthcare Inc"},
        "metadata": {"source": "web", "priority": "high"}
    }
)

Adding Logs to a Trace

Logs represent individual steps or operations within a trace:

# Create a log for content moderation
moderation_log = trace.create_log({
    "type": "span",
    "name": "content-moderation",
    "input": trace.input,
    "metadata": {"model": "text-moderation-latest"},
    "user": {"id": "user123", "name": "John Doe"},
    "organization": {"id": "org123", "name": "Healthcare Inc"}
})

# Update and end the log
moderation_log.update({"metadata": {"completed": True}})
moderation_log.end({"flagged": False, "categories": [], "scores": {}})

Creating and Managing Generations

Generations are special types of logs specifically for AI model interactions:

# Create a log for the main processing
main_log = trace.create_log({
    "type": "span",
    "name": "main-processing",
    "user": {"id": "user123", "name": "John Doe"},
    "organization": {"id": "org123", "name": "Healthcare Inc"},
    "input": trace.input
})

# Create a generation within the main log using a prompt from Basalt
generation = main_log.create_generation({
    "name": "healthcare-benefits-generation",
    "input": trace.input,
    "prompt": {
        "slug": "prompt-slug", # This tells the SDK to fetch the prompt from Basalt
        "version": "0.1" # This specifies the version to use
    }
})

# Or create a generation not managed in Basalt
generation = main_log.create_generation({
    "name": "healthcare-benefits-generation",
    "user": {"id": "user123", "name": "John Doe"},
    "organization": {"id": "org123", "name": "Healthcare Inc"},
    "input": trace.input
})

# End the generation with the response
generation.end("AI generated response")

# End the log and trace
main_log.end("Final output")
trace.end("End of trace")

Complex Workflows with Nested Logs

You can create complex workflows with nested logs and multiple generations:

# Create a nested log
nested_log = parent_log.create_log({
    "type": "span",
    "name": "nested-process",
    "metadata": {"key": "value"},
    "input": parent_log.input
})

# Create generations within nested logs
nested_generation = nested_log.create_generation({
    "name": "nested-generation",
    "input": nested_log.input,
    "prompt": {"slug": "another-prompt", "version": "0.1"},
    "variables": {"variable_example": "test variable"}
})

# End all logs in reverse order
nested_generation.end("Generation output")
nested_log.end("Nested log output")
parent_log.end("Parent log output")
trace.end("End of trace")

License

MIT

Project details


Download files

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

Source Distribution

basalt_sdk-0.3.0.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

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

basalt_sdk-0.3.0-py3-none-any.whl (45.1 kB view details)

Uploaded Python 3

File details

Details for the file basalt_sdk-0.3.0.tar.gz.

File metadata

  • Download URL: basalt_sdk-0.3.0.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for basalt_sdk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 633bda9e835f13f48bc3e13b12f4bf91889699e3c4b9d906f11f89a9a52a077e
MD5 5ccb1c094b3ea1f03bed35bbfa1f6051
BLAKE2b-256 9a72c6921de52d33cf3ea724b1eb7368bcde0701d4d79008ef24e73d6db313fd

See more details on using hashes here.

File details

Details for the file basalt_sdk-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: basalt_sdk-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 45.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for basalt_sdk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3de3851ef2d0b7e06a7557bc6e9056093d3ca0111c64a446aeeec47c01f4cb26
MD5 ceb06d8b2265d2a2af319c0f64eee91e
BLAKE2b-256 18ecdff6cdf9f6ebdb401bb6417763c28bf57ddb36cbeef819ee52020bad2db9

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