Skip to main content

A Gateway for LLM API interactions

Project description

LLM Gateway

Open-source library built for fast and reliable connections to different LLM providers.

Typescript library: https://www.npmjs.com/package/llm-gateway

The LLM Gateway is a lightweight, open-source library built for fast and reliable connections to LLMs.

It simplifies integrations with multiple providers, offering fallbacks, caching, and minimal latency with a client-side solution.

  • Minimize Downtime: Automatic retries and fallbacks to secondary providers like Azure or Entropic.
  • Automatic input params convertion: Automatically convert input params between OpenAI, Anthropic and Azure formats for fallbacks.
  • Faster Responses: Direct client-side requests for low latency.
  • Unified Control: A single interface to manage requests across LLMs.
  • Unified Output: Consistent output format across LLMs.
    openAI/AnthropicOutput:{...}
    llmGatewayOutput: {
        type: 'text' | 'tool_calls';
        content: string; - content for text output
        tool_name: string; - name of the tool for tool_calls
        arguments: string - arguments for the tool.
    }[]
}
  • Easy Model Switching: Change between OpenAI, Anthropic, and Azure models with a simple configuration change.

Contribute, fork, or raise issues— so we can make it better together.

Starring this repo helps other developers discover the LLM Gateway! ⭐

Installation

To install the library, use npm or yarn:

pip install llmgateway

Usage

Here's a basic example of how to use the LLM Gateway library:

from src.llmgateway import LLMGateway
from src.types import ChatCompletionParams, EnumLLMProvider, LLMGatewayParams, Message

gateway = LLMGateway(
    LLMGatewayParams(
        provider=EnumLLMProvider.OPENAI,
        api_key='your-api-key'
    )
)
response = gateway.chat_completion(
    ChatCompletionParams(
        messages=[
            Message(role="user", content="Write a story about a cat.")
        ],
        model="gpt-4o-mini",
        temperature=0.7,
        max_completion_tokens=800
    )
)

LLM Fallbacks Configuration

The LLM Gateway library supports configuring fallbacks to ensure that if one model fails, another can be used as a backup. This is useful for maintaining service availability and reliability.

Example Configuration

from src.llmgateway import LLMGateway
from src.types import ChatCompletionParams, EnumLLMProvider, LLMGatewayConfig, LLMGatewayParams, Message


gateway = LLMGateway(
    LLMGatewayParams(
                provider= EnumLLMProvider.ANTHROPIC,
                api_key= keys['ANTHROPIC_API_KEY']
            )
    , LLMGatewayConfig(
        fallbacks= {
            'fallback_provider':LLMGatewayParams(
        provider=EnumLLMProvider.OPENAI,
        api_key=keys['OPENAI_KEY_FOR_FREE_DEMO']
    ) ,
            'fallback_model': 'gpt-4o-mini'
        }
    )
)
response = gateway.chat_completion(
    ChatCompletionParams(
        messages=[
            Message(role="user", content="Write a story about a cat.")
        ],
        model="claude-3-5-sonnet-latest",
        temperature=0.7,
    )
)
print(response)

Streaming Responses

The LLM Gateway supports streaming responses from all providers, with a unified interface that works consistently across OpenAI, Anthropic, and Azure.

Basic Streaming Example

from src.llmgateway import LLMGateway
from src.types import ChatCompletionParams, EnumLLMProvider, LLMGatewayParams, Message


gateway = LLMGateway(
    LLMGatewayParams(
        provider=EnumLLMProvider.OPENAI,
        api_key='your-api-key'
    )
)

stream = gateway.chat_completion_stream(
    ChatCompletionParams(
        messages=[
            Message(role="user", content="Write a story about a cat.")
        ],
        model="gpt-4o-mini",
        temperature=0.7,
        max_tokens=800
    )
)

for chunk in stream:
    print(chunk)

All streaming examples work consistently across different providers (OpenAI, Anthropic, Azure) and automatically handle format conversion when falling back to a different provider.

Configuration

  • apiKey: Your API key for the chosen LLM provider.
  • modelType: The type of LLM provider you want to use (OPENAI, ANTHROPIC, AZUREOPENAI).
  • endpoint: (Optional) The endpoint for OpenAI models.
  • deployment: (Optional) The deployment name for Azure models.
  • apiVersion: (Optional) The API version for Azure models.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License.

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

open_llm_gateway-0.1.3.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

open_llm_gateway-0.1.3-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file open_llm_gateway-0.1.3.tar.gz.

File metadata

  • Download URL: open_llm_gateway-0.1.3.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.6

File hashes

Hashes for open_llm_gateway-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ba93481c8166e0ed5284a8850bdbf3c1add7ae338eaf4032a3e7732e33631d0b
MD5 ae14770e7c8fb1c198593b9b2e8a326a
BLAKE2b-256 d881363316b4852a9e76dab4e1b3ce440c27c5d6764993cc4abadb70a91d9834

See more details on using hashes here.

File details

Details for the file open_llm_gateway-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for open_llm_gateway-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c630ac5facb8ea05844bee404d37d82c1585a87cdfff6cde86aaf45715b34e88
MD5 f4f6b1b5a38157b9edf91f1054be488e
BLAKE2b-256 f44bcb032b83c43c042b403d6992be421ac8c46e19c474ef8c71a242dac4e9a8

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