Quick Start Guide

Let’s walk through basic usage patterns of the Mira Network SDK.

Basic Chat Completion

Here’s a simple example to get started:

from mira_network import MiraClient

async def main():
    async with MiraClient(api_key="your-api-key") as client:
        response = await client.chat_completions_create(
            model="your-chosen-model",
            messages=[
                {"role": "user", "content": "What is the capital of France?"}
            ]
        )
        print(response["choices"][0]["message"]["content"])

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Streaming Responses

For real-time responses:

async def stream_response():
    async with MiraClient(api_key="your-api-key") as client:
        stream = await client.chat_completions_create(
            model="your-chosen-model",
            messages=[{"role": "user", "content": "Write a story"}],
            stream=True
        )
        async for chunk in stream:
            print(chunk["choices"][0]["delta"]["content"], end="")

Managing Conversations

Handle multi-turn conversations:

messages = [
    {"role": "system", "content": "You are a helpful assistant"},
    {"role": "user", "content": "Hi! Can you help me?"},
]

async with MiraClient(api_key="your-api-key") as client:
    response = await client.chat_completions_create(
        model="your-chosen-model",
        messages=messages
    )

Error Handling

Implement robust error handling:

try:
    response = await client.chat_completions_create(
        model="your-chosen-model",
        messages=[{"role": "user", "content": "Hello"}]
    )
except ValueError as e:
    print(f"Validation error: {e}")
except Exception as e:
    print(f"An error occurred: {e}")