Metadata-Version: 2.4
Name: irovanai
Version: 0.2.2
Summary: A lightweight, dependency-free Python client for the Irovan AI API
Author: Irovan
License: MIT
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Project-URL: Homepage, https://github.com/irovan/irovanai
Project-URL: Repository, https://github.com/irovan/irovanai.git
Project-URL: Issues, https://github.com/irovan/irovanai/issues
Keywords: irovan, ai, chatbot, persian, farsi, api, llm
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed

# IrovanAI

A lightweight, dependency-free Python client for the Irovan AI API.

IrovanAI uses only the Python standard library and provides synchronous and asynchronous chat, persistent HTTP connections, automatic retries, concurrent requests, conversation sessions, and image support.

## Features

- Zero third-party runtime dependencies
- Persistent HTTP connections with keep-alive support
- Automatic retries for network errors, HTTP 5xx responses, and rate limits
- Concurrent requests with `chat_many`
- Conversation sessions with message history
- Image analysis and image generation
- Asynchronous client support
- Command-line interface
- Response caching
- Compatible with Python 3.8 and newer

## Installation

Install from PyPI:

```bash
pip install irovanai
```

Install directly from GitHub:

```bash
pip install git+https://github.com/irovan/irovanai.git
```

## API Key

Create or manage your API key at [Irovan AI](https://chat.irovan.ir).

You can provide the key directly or set the `IROVAN_API_KEY` environment variable.

Linux and macOS:

```bash
export IROVAN_API_KEY="YOUR_API_KEY"
```

Windows PowerShell:

```powershell
$env:IROVAN_API_KEY = "YOUR_API_KEY"
```

## Quick Start

```python
from irovanai import Irovan

ai = Irovan(api_key="YOUR_API_KEY")
response = ai.chat("Hello!")
print(response.reply)
```

If the environment variable is configured, the key can be omitted:

```python
from irovanai import Irovan

ai = Irovan()
print(ai.chat("What is Python?"))
```

For a one-line request:

```python
from irovanai import ask

print(ask("Hello!", api_key="YOUR_API_KEY"))
```

## Models

Choose a model per request or set a default when creating the client:

```python
ai.chat("Hello!", model="slim")
ai.chat("Explain quantum computing.", model="plus")
```

## Concurrent Requests

Send multiple messages concurrently with `chat_many`:

```python
messages = [
    "Hello!",
    "What is Python?",
    "Give me a programming tip."
]

responses = ai.chat_many(messages, workers=8)

for response in responses:
    print(response.reply)
```

Responses preserve the order of the input messages. Set `return_exceptions=True` to return individual errors instead of stopping the operation.

## Conversation Sessions

Use a session to maintain conversation history:

```python
session = ai.session(system="You are a helpful assistant.")

print(session.send("My name is Alex.").reply)
print(session.send("What is my name?").reply)

session.reset()
```

The session history is included in each request because the API processes requests independently.

## Image Support

Analyze an image:

```python
response = ai.analyze_image(
    "photo.png",
    "What is shown in this image?"
)

print(response.reply)
```

Generate and save an image:

```python
image = ai.generate_image("A fresh cucumber on a clean white background")
image.save("cucumber.png")
```

## Asynchronous Usage

```python
import asyncio
from irovanai import AsyncIrovan

async def main():
    ai = AsyncIrovan(api_key="YOUR_API_KEY")

    responses = await ai.chat_many([
        "Hello!",
        "How are you?"
    ])

    for response in responses:
        print(response.reply)

    ai.close()

asyncio.run(main())
```

## Response Caching

Enable an in-memory cache by setting `cache_size`:

```python
ai = Irovan(
    api_key="YOUR_API_KEY",
    cache_size=100
)
```

Repeated text requests can then reuse cached responses.

## Configuration

```python
from irovanai import Irovan

ai = Irovan(
    api_key="YOUR_API_KEY",
    model="slim",
    timeout=120,
    retries=2,
    cache_size=0
)
```

| Parameter | Description | Default |
|---|---|---|
| `api_key` | API key. Can also be provided through `IROVAN_API_KEY`. | Environment variable |
| `model` | Default model for requests. | `slim` |
| `base_url` | Irovan API base URL. | `https://chat.irovan.ir` |
| `timeout` | Request timeout in seconds. | `120` |
| `retries` | Number of retry attempts. | `2` |
| `cache_size` | Maximum number of cached responses. `0` disables caching. | `0` |

## Error Handling

IrovanAI provides specific exception classes for common API and network errors:

```python
from irovanai import (
    Irovan,
    AuthenticationError,
    RateLimitError,
    NetworkError,
    APIError
)

try:
    ai = Irovan(api_key="YOUR_API_KEY")
    response = ai.chat("Hello!")
    print(response.reply)

except AuthenticationError:
    print("The API key is missing or invalid.")

except RateLimitError:
    print("The request limit has been reached.")

except NetworkError as error:
    print(f"Network error: {error}")

except APIError as error:
    print(f"API error: {error}")
    print(f"HTTP status: {error.status_code}")
```

## Command-Line Interface

Run a single request:

```bash
irovanai "Hello!" --model slim
```

Analyze an image:

```bash
irovanai "What is in this picture?" --model plus --image photo.png
```

Start an interactive session:

```bash
irovanai
```

You can also use the module entry point:

```bash
python -m irovanai "Hello!"
```

## Welcome Message

The client displays `Welcome irovanai` once per process by default.

Disable the message by setting `IROVAN_QUIET`:

Linux and macOS:

```bash
export IROVAN_QUIET=1
```

Windows PowerShell:

```powershell
$env:IROVAN_QUIET = "1"
```

## Development and Tests

Clone the repository and install it in editable mode:

```bash
git clone https://github.com/irovan/irovanai.git
cd irovanai
pip install -e .
```

Run the test suite:

```bash
python -m unittest discover -s tests -v
```

Build the distribution packages:

```bash
python -m pip install --upgrade build
python -m build
```

## License

IrovanAI is distributed under the MIT License. See [LICENSE](LICENSE) for details.

## Links

- **Website:** [irovan.ir](https://irovan.ir)
- **AI Platform:** [chat.irovan.ir](https://chat.irovan.ir)
- **GitHub:** [github.com/irovan/irovanai](https://github.com/irovan/irovanai)
