Metadata-Version: 2.4
Name: aisearchapi-client
Version: 1.0.3
Summary: Python client library for AI Search API - search and retrieve intelligent responses with context awareness
Home-page: https://github.com/aisearchapi/aisearchapi-python
Author: AI Search API
Author-email: AI Search API <admin@aisearchapi.io>
Maintainer-email: AI Search API <admin@aisearchapi.io>
License: MIT License
        
        Copyright (c) 2025 aisearchapi
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/aisearchapi/aisearchapi-python
Project-URL: Documentation, https://aisearchapi.readthedocs.io/
Project-URL: Repository, https://github.com/aisearchapi/aisearchapi-python
Project-URL: Issues, https://github.com/aisearchapi/aisearchapi-python/issues
Project-URL: Changelog, https://github.com/aisearchapi/aisearchapi-python/blob/main/CHANGELOG.md
Keywords: ai,search,api,llm,embeddings,semantic-search,machine-learning
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.25.0
Requires-Dist: typing-extensions>=4.0.0; python_version < "3.10"
Provides-Extra: dev
Requires-Dist: pytest>=6.0; extra == "dev"
Requires-Dist: pytest-cov>=2.0; extra == "dev"
Requires-Dist: black>=22.0; extra == "dev"
Requires-Dist: isort>=5.0; extra == "dev"
Requires-Dist: flake8>=4.0; extra == "dev"
Requires-Dist: mypy>=0.900; extra == "dev"
Requires-Dist: types-requests>=2.25.0; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx>=4.0; extra == "docs"
Requires-Dist: sphinx-rtd-theme>=1.0; extra == "docs"
Provides-Extra: test
Requires-Dist: pytest>=6.0; extra == "test"
Requires-Dist: pytest-cov>=2.0; extra == "test"
Requires-Dist: pytest-asyncio>=0.18.0; extra == "test"
Requires-Dist: responses>=0.18.0; extra == "test"
Dynamic: author
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python

# AI Search API Python Client

[![PyPI version](https://badge.fury.io/py/aisearchapi.svg)](https://badge.fury.io/py/aisearchapi-client)
[![Python Support](https://img.shields.io/pypi/pyversions/aisearchapi.svg)](https://pypi.org/project/aisearchapi-client/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A Python client library for the AI Search API that provides intelligent search capabilities with context awareness and semantic understanding.

## Features

- **🔍 Intelligent Search**: Leverage advanced embedding techniques for semantic search
- **🎯 Context Awareness**: Provide conversation context to enhance search results
- **⚡ Simple API**: Easy-to-use interface with comprehensive error handling
- **🛡️ Type Safe**: Full type hints support for better development experience
- **🔄 Flexible Responses**: Support for both text and markdown formatted responses
- **💰 Balance Tracking**: Check your API credit usage

## Installation

Install the package using pip:

```bash
pip install aisearchapi-client
```

Or install from source:

```bash
git clone https://github.com/aisearchapi/aisearchapi-python.git
cd aisearchapi-python
pip install -e .
```

<!-- For development with optional dependencies: -->

<!-- ```bash
pip install -e ".[dev,test]"
``` -->

## Quick Start

### Basic Usage

```python
from aisearchapi_client import AISearchAPIClient

# Initialize the client
client = AISearchAPIClient(api_key='your-api-key-here')

# Perform a basic search
result = client.search(
    prompt='What is machine learning and how does it work?',
    response_type='markdown'
)

print("Answer:", result.answer)
print("Sources:", result.sources)
print(f"Total time: {result.total_time}ms")

# Check your account balance
balance = client.balance()
print(f"Available credits: {balance.available_credits}")

# Always close the client when done
client.close()
```

### Using Context Manager (Recommended)

```python
from aisearchapi_client import AISearchAPIClient, ChatMessage

# Use context manager for automatic resource cleanup
with AISearchAPIClient(api_key='your-api-key-here') as client:
    
    # Search with conversation context
    result = client.search(
        prompt='What are the main advantages and disadvantages?',
        context=[
            ChatMessage(role='user', content='I am researching solar energy for my home'),
            ChatMessage(role='user', content='I live in a sunny climate with high electricity costs')
        ],
        response_type='text'
    )
    
    print("Contextual Answer:", result.answer)
```

### Advanced Configuration

```python
from aisearchapi_client import AISearchAPIClient

client = AISearchAPIClient(
    api_key='your-api-key-here',
    base_url='https://api.aisearchapi.io',  # Custom base URL
    timeout=60  # Custom timeout in seconds
)
```

## API Reference

### `AISearchAPIClient`

#### Constructor

```python
AISearchAPIClient(
    api_key: str,
    base_url: str = "https://api.aisearchapi.io",
    timeout: int = 30
)
```

- **api_key** (str): Your API bearer token
- **base_url** (str, optional): Base URL for the API endpoints
- **timeout** (int, optional): Request timeout in seconds

#### Methods

##### `search(prompt, context=None, response_type=None)`

Perform an AI-powered search with optional conversation context.

**Parameters:**
- **prompt** (str): The main search query
- **context** (List[ChatMessage], optional): Conversation context for enhanced understanding
- **response_type** (str, optional): Response format ('text' or 'markdown')

**Returns:**
- **SearchResponse**: Object containing answer, sources, response_type, and total_time

**Example:**
```python
result = client.search(
    prompt='Explain quantum computing',
    context=[ChatMessage(role='user', content='I am a computer science student')],
    response_type='markdown'
)
```

##### `balance()`

Check your current account balance and available API credits.

**Returns:**
- **BalanceResponse**: Object containing available_credits

**Example:**
```python
balance = client.balance()
if balance.available_credits < 10:
    print("Warning: Low credit balance!")
```

### Data Classes

#### `ChatMessage`

```python
@dataclass
class ChatMessage:
    role: str  # Currently only 'user' is supported
    content: str  # The message content
```

#### `SearchResponse`

```python
@dataclass
class SearchResponse:
    answer: str  # AI-generated response
    sources: List[str]  # List of sources used
    response_type: str  # Format of the response
    total_time: int  # Processing time in milliseconds
```

#### `BalanceResponse`

```python
@dataclass
class BalanceResponse:
    available_credits: int  # Number of available API credits
```

## Error Handling

The client provides comprehensive error handling with custom exception types:

```python
from aisearchapi_client import AISearchAPIClient, AISearchAPIError

try:
    with AISearchAPIClient(api_key='your-api-key') as client:
        result = client.search(prompt='Your search query')
        print(result.answer)
        
except AISearchAPIError as e:
    print(f"API Error [{e.status_code}]: {e.description}")
    if e.response:
        print("Response data:", e.response)
        
except ValueError as e:
    print(f"Validation Error: {e}")
    
except Exception as e:
    print(f"Unexpected error: {e}")
```

## Error Codes

| Status Code | Error Type | Description |
|------------|------------|-------------|
| 401 | Unauthorized | Invalid API key |
| 429 | Too Many Requests | Rate limit exceeded |
| 433 | Quota Exceeded | Account message quota reached |
| 500 | Server Error | Internal server error |
| 503 | Service Unavailable | API temporarily down |

## Environment Variables

You can set your API key using environment variables:

```bash
export AI_SEARCH_API_KEY="your-api-key-here"
```

Then use it in your code:

```python
import os
from aisearchapi_client import AISearchAPIClient

api_key = os.getenv('AI_SEARCH_API_KEY')
client = AISearchAPIClient(api_key=api_key)
```

## Examples

Check out the [examples](examples/) directory for more comprehensive usage examples:

- [contextual_search.py](examples/contextual_search.py) - Using conversation context
- [error_handling.py](examples/error_handling.py) - Comprehensive error handling
- [async_usage.py](examples/async_usage.py) - Async/await patterns

## Requirements

- Python 3.8 or higher
- requests >= 2.25.0
- typing-extensions >= 4.0.0 (for Python < 3.10)

## Development

### Setting up for development

```bash
git clone https://github.com/aisearchapi/aisearchapi-python.git
cd aisearchapi-python

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode with all dependencies
pip install -e ".[dev,test]"
```

### Running tests

```bash
pytest
```

### Code formatting

```bash
# Format code
black .
isort .

# Lint code  
flake8 .
mypy .
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Support

- **Documentation**: [https://docs.aisearchapi.io/](https://docs.aisearchapi.io)
- **Issues**: [GitHub Issues](https://github.com/aisearchapi/aisearchapi-python/issues)
- **Email**: admin@aisearchapi.io

## Changelog

## Acknowledgments

- Built with [requests](https://requests.readthedocs.io/) for HTTP handling
- Type hints support with [typing-extensions](https://github.com/python/typing_extensions)
- Inspired by modern API client design patterns
