Skip to main content

LlmUnify is a library that abstracts connections to major LLM model providers, simplifying their invocation and interoperability.

Project description

LlmUnify

LlmUnify is a Python library designed to simplify and standardize interactions with multiple Large Language Model (LLM) providers. By offering a unified interface, it enables seamless integration and allows you to switch between providers or models without modifying your code. The library abstracts the complexity of invoking LLMs, supports streaming responses, and can be easily configured via environment variables or method arguments.

Supported Providers

  • AWS Bedrock (via boto3)
  • Azure AI Foundry (via azure-ai-inference)
  • Google Vertex Ai (via vertexai)
  • Ollama (via direct API calls)
  • IBM WatsonX (via ibm-watsonx-ai)

Future versions will include support for additional providers.

Installation

Install the core library from PyPI

To install the library directly from PyPI, run:

pip install llm-unify

Install with dependencies for a specific provider

To install the library with AWS-specific dependencies, use:

pip install 'llm-unify[aws]'

Install with all provider-specific dependencies

To install the library with all available provider-specific dependencies, use:

pip install 'llm-unify[all]'

Quickstart

Configuration and Authentication

LlmUnify retrieves provider-specific credentials and configuration from environment variables, with the option to override them using method arguments.

Example .env configuration:

OLLAMA_API_KEY=your_ollama_api_key
AWS_ACCESS_KEY_ID=your_aws_access_key_id
AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key

Minimal Example

from llmUnify import LlmOptions, LlmUnify
from dotenv import load_dotenv

# Load configurations from .env file
load_dotenv()

# Define options for text generation
options = LlmOptions(
    temperature=0.7,
    prompt="Write a motivational poem:",
)

# Generate a response specifying provider and model
result = LlmUnify.generate(
    model="<provider>:<model-name>",  # Provider and model name separated by ":"
    options=options,
)

print(result.generated_text)

Reusing Connectors

For repeated calls to the same provider, you can use a reusable connector:

from llmUnify import LlmOptions, LlmUnify
from dotenv import load_dotenv

# Load configurations from .env file
load_dotenv()

# Create a connector for a specific provider
connector = LlmUnify.get_connector(
    provider="<provider>",
)

# Define options for text generation
options = LlmOptions(prompt="List three ways to stay productive:")

# Generate a response in streaming mode
result = connector.generate_stream(model_name="model_name", options=options)

print(result.generated_text)

Usage Metrics Logging

LlmUnify can log usage statistics such as token counts, elapsed time, and optional call identifiers.

Enable logging via environment variables:

LLM_UNIFY_ENABLE_USAGE_METRICS=true
LLM_UNIFY_USAGE_METRICS_OUTPUT_PATH=llm_unify_usage_metrics.csv  # optional, default is ./llm_unify_usage_metrics.csv

Enable logging via code:

from llmUnify import configure_usage_metrics

UsageMetricsLogger.enable_usage_metrics(enabled=True, output_path="my_metrics.csv")

Pass a custom call name to identify calls in the logs:

result = LlmUnify.generate(
    provider="<provider>:<model-name>",
    options=options,
    call_name="<call_name>"
)

After each call with logging enabled, a new row will be appended to the CSV file at the specified path.

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

llmunify-1.0.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

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

llmunify-1.0.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file llmunify-1.0.0.tar.gz.

File metadata

  • Download URL: llmunify-1.0.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.18 Linux/6.11.0-1015-azure

File hashes

Hashes for llmunify-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2fc10e4cd304f836b63ffccbbdd8f13f0aeefb092d2f7aba178b26f3ebfe3ad6
MD5 8785c03934a20649a7b0b9c4f64e7687
BLAKE2b-256 97ddefdf5e0e8efafe190e2ad7144b18f2e26c3bec127631de9b76d6b81a45a2

See more details on using hashes here.

File details

Details for the file llmunify-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: llmunify-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.18 Linux/6.11.0-1015-azure

File hashes

Hashes for llmunify-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4995571e9d1f30ae1fb8c27416719f05ca50cd334ac36eaa7149a7b8d97cb494
MD5 0d2f0b9f264d06a21db99089cb84d8d7
BLAKE2b-256 f6726a5aabea6b9ed2fc8bc6eb9dbb30600de9cc9e3c948ef354132f55c007af

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