Skip to main content

A Python SDK for interacting with the BotBrigade LLM API.

Project description

BotBrigade LLM Python SDK

Overview

The BotBrigade LLM Python SDK provides an easy way to interact with the BotBrigade LLM API for text generation, model listing, and streaming responses. This SDK supports both synchronous and asynchronous operations using httpx.

Installation

Ensure you have Python 3.7+ installed.

pip install botbrigade_llm

Initialization

Import the LLMClient and create an instance with your API key:

from botbrigade_llm import LLMClient

client = LLMClient(api_key="your_api_key")

Alternatively, you can set the API key as an environment variable:

export BBS_API_KEY="your_api_key"

And initialize the client without explicitly passing the key:

client = LLMClient()

Listing Available Models

The SDK allows you to retrieve a list of available LLM models.

Synchronous

models = client.list_models()
print(models)

Asynchronous

import asyncio

async def get_models():
    models = await client.alist_models()
    print(models)

asyncio.run(get_models())

Generating Responses

The SDK supports both synchronous and asynchronous text generation.

Message Structure

The API supports different message roles:

  • user: The user's input message.
  • system: A system-level instruction to guide the model's behavior.
  • assistant: Previous responses from the assistant, used to provide conversation history.

Example:

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "How do I check if a string contains a substring in Python?"},
    {"role": "assistant", "content": "You can use the 'in' keyword in Python."}
]

Synchronous Response Generation

response = client.responses.create(
    model="claudia-1",
    messages=[{"role": "user", "content": "Tell me a joke"}],
    max_tokens=100,
    temperature=0.7,
)
print(response)

Asynchronous Response Generation

async def generate():
    response = await client.responses.acreate(
        model="claudia-1",
        messages=[{"role": "user", "content": "Tell me a joke"}],
        max_tokens=100,
        temperature=0.7,
    )
    print(response)

asyncio.run(generate())

Non-Stream Response Format

All responses follow a standardized structure:

{
  "id": "chatcmpl-1234567890",
  "object": "chat.completion",
  "created": 1710823456,
  "model": "claudia-1",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I assist you today?",
        "refusal": null,
        "annotations": []
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ]
}

Handling System Prompts

A system prompt is a special instruction that helps set the behavior and personality of the model. It acts as a guideline for how the model should respond throughout the conversation. This can be used to establish a role, enforce constraints, or define response styles.

System prompts are included as part of the messages list and should be provided at the beginning of the conversation. Here’s an example:

messages = [
    {"role": "system", "content": "Talk like a pirate."},
    {"role": "user", "content": "Are semicolons optional in JavaScript?"}
]

By providing a system prompt, you ensure that all responses align with the intended instructions throughout the interaction.

Handling Image Inputs

The SDK supports both image URLs and Base64-encoded images as inputs. You can include images in your messages as follows:

Using Image URLs

messages = [
    {"role": "user", "content": [
        {"type": "text", "text": "What is in this image?"},
        {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
    ]}
]

Using Base64-Encoded Images

If an image URL is not available, you can encode an image in Base64 and send it as follows:

import base64

with open("image.jpg", "rb") as image_file:
    base64_image = base64.b64encode(image_file.read()).decode('utf-8')

messages = [
    {"role": "user", "content": [
        {"type": "text", "text": "What is in this image?"},
        {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
    ]}
]

The SDK will handle sending the correct format based on the API specifications.

Streaming Responses

If stream=True, the response is streamed instead of returning a single object. The API returns data as Server-Sent Events (SSE).

Synchronous Streaming

for chunk in client.responses.create(
    model="claudia-1",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
):
    print(chunk)

Asynchronous Streaming

async def stream_response():
    async for chunk in await client.responses.acreate(
        model="claudia-1",
        messages=[{"role": "user", "content": "Tell me a story"}],
        stream=True
    ):
        print(chunk)

asyncio.run(stream_response())

Optional Payload Parameters

The SDK allows the following optional parameters for create() and acreate():

Parameter Type Description
temperature float Sampling temperature (higher values make output more random). Default: 1.0
max_tokens int Maximum number of tokens in the response. Default: None (unlimited)
top_p float Nucleus sampling probability. Default: 1.0
frequency_penalty float Penalizes repeated tokens. Default: 0.0
presence_penalty float Encourages new tokens. Default: 0.0
stream bool Whether to stream responses. Default: False

Closing the Client

To properly close the HTTP connection, use:

Synchronous

client.close()

Asynchronous

asyncio.run(client.aclose())

License

This SDK is licensed under MIT License.

Support

For issues and contributions, please submit a GitHub issue or contact BotBrigade Support.

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

botbrigade_llm-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

botbrigade_llm-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file botbrigade_llm-0.1.1.tar.gz.

File metadata

  • Download URL: botbrigade_llm-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for botbrigade_llm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a986e89218c28821a9d4d25e101edfa5ea09a6a50161b1e61237a014a3246db
MD5 7af4c3638db5dbb06815e105d27bb740
BLAKE2b-256 9bd79b6cf4d4cdd17d7d41edac6e5892c3d0b57e2f716bf3fd0f874926c22bf0

See more details on using hashes here.

File details

Details for the file botbrigade_llm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: botbrigade_llm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for botbrigade_llm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c957367222b4e0c3c5945584af7328fa57740831c661672f4d9e43b8577a0d6c
MD5 4a370f917bbbfa4f7ad5f7bfbf600e4f
BLAKE2b-256 d4b125794b440e08237898bf24ea3867077e8922f0b0b6be384f51499ee5ceae

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