Skip to content

Python SDK Guide

Welcome to the Python SDK Guide for Mira. This SDK is designed to enable Python developers to easily incorporate Mira's API services into their applications. This document will provide instructions on installing the SDK, setting it up, and using it to make API requests.

Installation

To get started with the Mira Python SDK, you'll need to install it using pip. Make sure you have Python installed on your machine, then run the following command:

bash
pip install mira-sdk

Configuration

Before you start making API calls, you need to configure the SDK with your API keys. This is necessary for authenticating your requests to the Mira platform.

python
from mira_sdk import Client

# Initialize the client with your API key
client = Client(api_key='YOUR_API_KEY')

Making Your First Request

With the SDK installed and configured, you are now ready to execute your first flow. Here is how you can use the Python SDK to call a flow:

python
from mira_sdk import Client

# Initialize the client
client = Client(api_key='YOUR_API_KEY')

def execute_flow():
    input = {
        "query": "What is the capital of France?"
    }

    try:
        output = client.run('@klok/qna', input)
        print('Output:', output)
    except Exception as e:
        print('Error executing flow:', e)

execute_flow()

Handling Responses and Errors

Handling responses and errors correctly is crucial for building robust applications. The SDK is designed to raise exceptions when errors occur, so you should handle these appropriately:

python
def execute_flow():
    input = {
        "query": "What is the tallest building in the world?"
    }

    try:
        output = client.run('@klok/building-height', input)
        print('Output:', output)
    except Exception as e:
        print('Error:', e)

execute_flow()

Advanced Usage

Asynchronous Execution

The Python SDK supports asynchronous operations, which are essential for performing non-blocking API calls. Here's an example using async/await:

python
import asyncio
from mira_sdk import Client

client = Client(api_key='YOUR_API_KEY')

async def fetch_data():
    input = {
        "city": "New York"
    }

    try:
        output = await client.exec_async('@klok/weather', input)
        print(output)
    except Exception as e:
        print('Error:', e)

loop = asyncio.get_event_loop()
loop.run_until_complete(fetch_data())

Caching Responses

To improve performance, especially with frequent identical requests, you might want to implement caching:

python
from cachetools import cached, TTLCache

# Cache configuration
cache = TTLKCache(maxsize=100, ttl=300)  # 100 items, 5 minutes TTL

@cached(cache)
def get_cached_response(flow_name, params):
    return client.run(flow_name, params)

# Usage
response = get_cached_response('@klok/weather', {"city": "New York"})
print(response)

Conclusion

The Mira Python SDK is a powerful tool for integrating Mira's API services into your Python applications. By following this guide, you should now have a good understanding of how to install the SDK, configure it, and make API calls, as well as handle responses and exceptions efficiently. For additional support or to explore more complex integration scenarios, visit our Community Support or consult the API documentation.