Skip to main content

An out-of-the-box chat module supporting custom tool registration and invocation via OpenAI SDK

Project description

chatbot-oai

An out-of-the-box, lightweight Python module for building production-ready chat applications with OpenAI-compatible APIs. Features automatic tool/function calling, multi-round reasoning loops, flexible provider routing, and built-in token tracking.

📦 Installation

pip install chatbot-oai

🚀 Quick Start

A minimal working example using the default OpenRouter endpoint:

from chatbot_oai import BotConfig, ChatBot

bot = ChatBot(BotConfig())
response = bot.chat("Hello! Please introduce yourself briefly.")
print(response)

⚙️ Configuration

All settings are managed through the BotConfig dataclass. When instantiated without arguments, it defaults to the OpenRouter platform and reads OPENROUTER_API_KEY from your environment.

🔑 Environment Variable Setup

The module validates that your API key exists at initialization. Set it in your shell:

export OPENROUTER_API_KEY="sk-xxxxxxxx"

It's NOT recommanded to deliver API Key directly like bot_config = BotConfig(api_key="sk-xxxxxxxx")

🌍 Multi-Provider Support

Switch endpoints effortlessly by overriding base_url, env_key_name, and model. Provider-specific quirks can be passed via extra_body.

Local vLLM / llama.cpp

bot_conf = BotConfig(
    base_url="http://localhost:9998/v1",
    env_key_name="PRIVATE_KEY",  # Many local servers require a dummy key
    model="Qwopus-9B-Q4_K_M.gguf",
)

DeepSeek Official API

bot_conf = BotConfig(
    base_url="https://api.deepseek.com",
    env_key_name="DEEPSEEK_API_KEY",
    model="deepseek-v4-flash",
    extra_body={"thinking": {"type": "disabled"}},
)

🛠️ Custom Tools & Automatic Function Calling

Register Python functions as tools. The LLM will automatically decide when to call them, the module will execute them, feed results back into the context, and continue until no more tools are requested.

1. Define Your Function

def get_weather(city: str) -> str:
    """Returns weather information for a given city."""
    # Replace with real API call or mock data
    return f"The weather in {city} is currently sunny with 22°C."

2. Create the OpenAI-Compatible Schema

weather_schema = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Retrieve current weather conditions for a specific city.",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"}
            },
            "required": ["city"]
        }
    }
}

3. Register & Use

bot = ChatBot(BotConfig())
bot.tool_register(weather_schema, "get_weather", get_weather)

response = bot.chat("What's the weather in Beijing?")
print(response)

🔁 How It Works Internally

  1. User sends a prompt → LLM decides whether to call a tool
  2. If tool calls are returned, _tools_handler() parses JSON arguments, executes the registered Python callable, and captures output
  3. Failed executions are caught, logged, and returned as structured error strings so the LLM can self-correct
  4. Tool results are appended to conversation history as "tool" role messages
  5. Loop continues until completion.choices[0].message.tool_calls is empty or max_tool_call_round is reached

You can use bot.context to show full chat contexts.

📄 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

chatbot_oai-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

chatbot_oai-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file chatbot_oai-0.1.0.tar.gz.

File metadata

  • Download URL: chatbot_oai-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chatbot_oai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9299040980a450200c17bd3ce6fb1ef5b335214e98a95e4416b617781a2dd7fc
MD5 23f5ecfed0974e4de48459235cca7f4f
BLAKE2b-256 1c889d221ae062b30bc2f6524bcae3adddbd8b560fc7460af5326d182bf71e22

See more details on using hashes here.

File details

Details for the file chatbot_oai-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: chatbot_oai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chatbot_oai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e966790e9ad784864b6b98cfa8c7400fcc0fa0c73a58398f16f7bc7bbc8949da
MD5 198f8d180eeb9fcfd6112b08781c9fbe
BLAKE2b-256 4771dca6234d7c4d82f42477a85a9adc420ffa904db81fb5bf9e64f4232a39f8

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