Skip to content

reka-ai/reka-sdk-python

Repository files navigation

Reka Python Library

pypi fern shield

The Reka Python Library provides convenient access to the Reka API from applications written in Python.

The library includes type definitions for all request and response fields, and offers both synchronous and asynchronous clients powered by httpx.

Installation

Add this dependency to your project's build file:

pip install reka-api
# or
poetry add reka-api

Usage

You need to add your API key for authentication. You can either do that directly when instantiating a Reka client, or by setting the REKA_API_KEY environment variable.

Then simply import Reka and start making calls to our API.

from reka import ChatMessage
from reka.client import Reka

client = Reka(
    api_key="YOUR_API_KEY",
) # or just client = Reka() if set via env

client.chat.create(
    messages=[
        ChatMessage(
            content="What is the fifth prime number?",
            role="user",
        )
    ],
    model="reka-core-20240501",
)

or for multimodality you can do the following:

client.chat.create(
    messages=[
        ChatMessage(
            role="user",
            content=[
                {
                    "type": "video_url":
                    "video_url": "https://fun_video"
                },
                {
                    "type": "text",
                    "text": "What is this video about?"
                },
            ],
        )
    ],
    model="reka-core-20240501",
)

Note that the model should work best when you put media content before the text content.

Typing

To construct payloads you can either use the dedicated types like ChatMessage or construct directly from a dictionary like so:

client.chat.create(
    messages=[
        {   
            "role": "user",
            "content": "What is the fifth prime number?"
        }
    ],
    model="reka-core-20240501",
)

V2 SDK

In case you have workloads running the previous version of our SDK, you can import it like this import reka.v2 as rekav2 and then continue to use it as you are already familiar, e.g.

rekav2.API_KEY = "your-api-key"

response = rekav2.chat("What is the capital of the UK?")

Async Client

The SDK also exports an async client so that you can make non-blocking calls to our API.

import asyncio
from reka import ChatMessage
from reka.client import AsyncReka

client = AsyncReka(
    api_key="YOUR_API_KEY",
)

async def main() -> None:
    await client.chat.create(
        messages=[
            ChatMessage(
                content="What is the fifth prime number?",
                role="user",
            )
        ],
        model="reka-core-20240501",
    )
asyncio.run(main())

Streaming

The SDK supports streaming endpoints. To take advantage of this feature for chat, use chat_stream.

from reka import ChatMessage
from reka.client import Reka

client = Reka(
    api_key="YOUR_API_KEY",
)

stream = client.chat.create_stream(
    messages=[
        ChatMessage(
            content="Tell me a short story",
            role="user",
        )
    ],
    model="reka-core-20240501",
)

for message in stream:
    print(message.responses[0].chunk.content, end='\n---\n')

Exception Handling

All errors thrown by the SDK will be subclasses of ApiError.

import reka

try:
    client.chat.create(...)
except reka.core.ApiError as e: # Handle all errors
  print(e.status_code)
  print(e.body)

Advanced

Timeouts

By default, requests time out after 60 seconds. You can configure this with a timeout option at the client or request level.

from reka.client import Reka

client = Reka(
    ...,
    # All timeouts are 20 seconds
    timeout=20.0,
)

# Override timeout for a specific method
client.chat.create(..., {
    timeout_in_seconds=20.0
})

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retriable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retriable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the max_retries request option to configure this behavior.

client.chat.create(..., {
     max_retries=1
})

Custom HTTP client

You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies and transports.

import httpx

from reka.client import Reka

client = Reka(...,
    httpx_client=httpx.Client(
        proxies="http://my.test.proxy.example.com",
        transport=httpx.HTTPTransport(local_address="0.0.0.0"),
    ),
)

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!