Skip to main content

Python client library for the Portkey API

Project description


Control Panel for AI Apps

pip install portkey-ai

Features

The Portkey SDK is built on top of the OpenAI SDK, allowing you to seamlessly integrate Portkey's advanced features while retaining full compatibility with OpenAI methods. With Portkey, you can enhance your interactions with OpenAI or any other OpenAI-like provider by leveraging robust monitoring, reliability, prompt management, and more features - without modifying much of your existing code.

AI Gateway

Unified API Signature
If you've used OpenAI, you already know how to use Portkey with any other provider.
Interoperability
Write once, run with any provider. Switch between any model from_any provider seamlessly.
Automated Fallbacks & Retries
Ensure your application remains functional even if a primary service fails.
Load Balancing
Efficiently distribute incoming requests among multiple models.
Semantic Caching
Reduce costs and latency by intelligently caching results.
Virtual Keys
Secure your LLM API keys by storing them in Portkey vault and using disposable virtual keys.
Request Timeouts
Manage unpredictable LLM latencies effectively by setting custom request timeouts on requests.

Observability

Logging
Keep track of all requests for monitoring and debugging.
Requests Tracing
Understand the journey of each request for optimization.
Custom Metadata
Segment and categorize requests for better insights.
Feedbacks
Collect and analyse weighted feedback on requests from users.
Analytics
Track your app & LLM's performance with 40+ production-critical metrics in a single place.

Usage

Prerequisites

  1. Sign up on Portkey and grab your Portkey API Key
  2. Add your OpenAI key to Portkey's Virtual Keys page and keep it handy
# Installing the SDK

$ pip install portkey-ai
$ export PORTKEY_API_KEY=PORTKEY_API_KEY

Making a Request to OpenAI

  • Portkey fully adheres to the OpenAI SDK signature. You can instantly switch to Portkey and start using our production features right out of the box.
  • Just replace from openai import OpenAI with from portkey_ai import Portkey:
from portkey_ai import Portkey

portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    virtual_key="VIRTUAL_KEY"
)

chat_completion = portkey.chat.completions.create(
    messages = [{ "role": 'user', "content": 'Say this is a test' }],
    model = 'gpt-4'
)

print(chat_completion)

Async Usage

  • Use AsyncPortkey instead of Portkey with await:
import asyncio
from portkey_ai import AsyncPortkey

portkey = AsyncPortkey(
    api_key="PORTKEY_API_KEY",
    virtual_key="VIRTUAL_KEY"
)

async def main():
    chat_completion = await portkey.chat.completions.create(
        messages=[{'role': 'user', 'content': 'Say this is a test'}],
        model='gpt-4'
    )

    print(chat_completion)

asyncio.run(main())

Compatibility with OpenAI SDK

Portkey currently supports all the OpenAI methods, including the legacy ones.

Methods OpenAI
V1.26.0
Portkey
V1.3.1
Audio
Chat
Embeddings
Images
Fine-tuning
Batch
Files
Models
Moderations
Assistants
Threads
Thread - Messages
Thread - Runs
Thread - Run - Steps
Vector Store
Vector Store - Files
Vector Store - Files Batches
Generations ❌ (Deprecated)
Completions ❌ (Deprecated)

Portkey-Specific Methods

Methods Portkey
V1.3.1
Feedback
Prompts

Check out Portkey docs for the full list of supported providers

follow on Twitter Discord

Contributing

Get started by checking out Github issues. Email us at support@portkey.ai or just ping on Discord to chat.

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

portkey_ai-1.9.3.tar.gz (259.9 kB view details)

Uploaded Source

Built Distribution

portkey_ai-1.9.3-py3-none-any.whl (469.9 kB view details)

Uploaded Python 3

File details

Details for the file portkey_ai-1.9.3.tar.gz.

File metadata

  • Download URL: portkey_ai-1.9.3.tar.gz
  • Upload date:
  • Size: 259.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for portkey_ai-1.9.3.tar.gz
Algorithm Hash digest
SHA256 3b03eba0430fc27d1fc75c43d1459fa077a85e3d5308ab8801fa087bfd278aab
MD5 873b5d9ede44f4192257b47f81b1a05d
BLAKE2b-256 e3a8bb7ab7f8c133418de0ac1a5ba3d1618a1bdf87e42f70aa2b1cb19c329e00

See more details on using hashes here.

File details

Details for the file portkey_ai-1.9.3-py3-none-any.whl.

File metadata

  • Download URL: portkey_ai-1.9.3-py3-none-any.whl
  • Upload date:
  • Size: 469.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for portkey_ai-1.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 724edc1a97e62a2542aed28e42bb5edc01add0702a7424d989b5359f4568f61e
MD5 8e206938430324d59bcec0b238fda6d9
BLAKE2b-256 cea058804983624f0501ae57abfa247d74dedd27e2754e51d2e28864dde19640

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page