Skip to main content

A client library for the OpenWebUI API, compatible with OpenAI Python SDK

Project description

OpenWebUI Client

PyPI version Python versions Documentation License: MIT

A client library for the OpenWebUI API, compatible with the OpenAI Python SDK but with extensions specific to OpenWebUI features.

Installation

pip install openwebui-client

Quick Start

from openwebui_client import OpenWebUIClient

# Initialize client
client = OpenWebUIClient(
    api_key="your_api_key",  # Optional if set in environment variable
    base_url="http://your-openwebui-instance:5000",
    default_model="gpt-4"
)

# Basic chat completion
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, world!"}
    ]
)
print(response.choices[0].message.content)

Using Function Calling / Tools

The client supports OpenAI-compatible function calling with tools:

# Direct tool usage with chat completions
response = client.chat.completions.create(
    model="your_model",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the current time?"}
    ],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_current_time",
                "description": "Get the current time.",
                "parameters": {
                    "type": "object",
                    "properties": {},
                    "required": [],
                },
            },
        }
    ],
)

# Check if the model used tools
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    print(f"Tool called: {tool_call.function.name}")

Using the Tool Registry

The client includes a tool registry for easier management of tools:

# Define tool functions
def get_weather(location: str, unit: str = "celsius") -> str:
    """Get the current weather in a given location.

    Args:
        location: The location to get weather for
        unit: The temperature unit to use (celsius or fahrenheit)

    Returns:
        str: A string describing the current weather
    """
    return f"The weather in {location} is sunny and 25°{unit[0]}"

# Register tools with the client
client.tool_registry.register(get_weather)

# Use chat_with_tools for automatic tool handling
response = client.chat_with_tools(
    messages=[{"role": "user", "content": "What's the weather like in Toronto?"}],
    max_tool_calls=5,
)

print(response)  # Will contain the final response after any tool calls

File Operations

# Upload a file to OpenWebUI
uploaded_file = client.files.from_path(file)

# Upload multiple files to OpenWebUI
with open("document.pdf", "rb") as file:
    uploaded_files = client.files.from_paths(
        files=[
            (file1, None),
            (file2, {"xMetaField": "xMetaValue"})
        ]
    )

uploaded_files.append(uploaded_file)

# Use file with chat completion
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Summarize this document for me."}
    ],
    files=uploaded_files
)

Features

  • OpenAI Compatibility: Use the familiar OpenAI Python SDK interfaces
  • File Upload: Upload and process files with OpenWebUI
  • File-Aware Chat Completions: Reference files in chat completions
  • Typed Interface: Full type hints for better IDE integration

Documentation

Full documentation is available at https://bemade.github.io/openwebui-client/

Development

# Install development dependencies
pip install -e ".[dev]"

# Run tests
python -m pytest

# Build documentation
cd docs
make html

License

This project is licensed under the MIT License - see the LICENSE file for details.

Related Projects

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

openwebui_client-0.2.1.tar.gz (591.9 kB view details)

Uploaded Source

Built Distribution

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

openwebui_client-0.2.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file openwebui_client-0.2.1.tar.gz.

File metadata

  • Download URL: openwebui_client-0.2.1.tar.gz
  • Upload date:
  • Size: 591.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for openwebui_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5122661498a0b3c9a829be99380163f46c1084cea9bd91bcff9c94597190a1e6
MD5 1f369bb4d316d26158b58e0e2d4c0e65
BLAKE2b-256 679504549f8598ccd75cfdd0e98aa5b26c26b849a6ae7b69eb3b850e29dbb10a

See more details on using hashes here.

File details

Details for the file openwebui_client-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openwebui_client-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b8d0d2607ba26935851f12dcfa003451d48b3740465de4c207ebafd3041571c
MD5 6b3d480dd4cb0206c792c11acf363fdf
BLAKE2b-256 ca9b369c678742fb5ccc96eeb17f7b2cfdbbe638e74a7fb210aaaa9c9425e389

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