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.5.2.tar.gz (46.1 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.5.2-py3-none-any.whl (49.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: basalt_sdk-0.5.2.tar.gz
  • Upload date:
  • Size: 46.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.1

File hashes

Hashes for basalt_sdk-0.5.2.tar.gz
Algorithm Hash digest
SHA256 33bad35ca96ac9f9d8ff32a276299be6928d73c77a2ee5c43a973f200d2c4176
MD5 1262fe4221583344aea6a98308b674a3
BLAKE2b-256 b8c9d059ca8f0697b3b1fe167e396861822f323fa522b8126720575ce76d640b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: basalt_sdk-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 49.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.1

File hashes

Hashes for basalt_sdk-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fd6dff1a483511ce915d736dc64db96dfce4f4d8f097399ec7e88ac141de04a1
MD5 9cd0ebcdfdc8a32e59322417b176a99e
BLAKE2b-256 c32695a35495b416f25ee989cefb43765801eccc177ff662c77a3169a05edbb0

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