Metadata-Version: 2.4
Name: lamia-lang
Version: 0.1.0
Summary: Human language meets Python. Write AI-powered scripts in plain English.
Author: Sergey Sargsyan
License: MIT License
        
        Copyright (c) 2025-2026 Sergey Sargsyan
        
        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/lamia-lang/lamia
Project-URL: Documentation, https://lamia-lang.github.io/lamia/
Project-URL: Repository, https://github.com/lamia-lang/lamia
Project-URL: Issues, https://github.com/lamia-lang/lamia/issues
Keywords: lamia,llm,ai,automation,web-scraping,validation,pydantic,openai,anthropic,ollama
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Code Generators
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: python-dotenv>=0.19.0
Requires-Dist: requests>=2.25.0
Requires-Dist: aiohttp>=3.9.0
Requires-Dist: pyyaml>=6.0.0
Requires-Dist: typing-extensions>=4.0.0
Requires-Dist: openai>=1.12.0
Requires-Dist: anthropic>=0.18.1
Requires-Dist: pydantic>=2.0.0
Requires-Dist: beautifulsoup4>=4.12.0
Requires-Dist: mistune>=3.0.0
Requires-Dist: lxml>=4.9.0
Requires-Dist: cssselect>=1.4.0
Requires-Dist: selenium>=4.10.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: black>=22.0.0; extra == "dev"
Requires-Dist: isort>=5.0.0; extra == "dev"
Requires-Dist: flake8>=4.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.18.0; extra == "dev"
Provides-Extra: docs
Requires-Dist: mkdocs>=1.6.0; extra == "docs"
Requires-Dist: mkdocs-material>=9.6.0; extra == "docs"
Requires-Dist: mkdocstrings[python]>=0.30.0; extra == "docs"
Requires-Dist: mkdocs-autorefs>=1.4.0; extra == "docs"
Requires-Dist: mkdocs-gen-files>=0.5.0; extra == "docs"
Requires-Dist: mkdocs-literate-nav>=0.6.0; extra == "docs"
Requires-Dist: mkdocs-section-index>=0.3.0; extra == "docs"
Requires-Dist: mkdocs-git-revision-date-plugin>=0.3.0; extra == "docs"
Requires-Dist: mkdocs-minify-plugin>=0.8.0; extra == "docs"
Requires-Dist: mkdocs-redirects>=1.2.0; extra == "docs"
Dynamic: license-file

<div align="center">
  <h1>Lamia</h1>
  <p><strong>Write AI-powered scripts in plain English.</strong></p>
  <img src="assets/lamia.png" alt="Lamia" width="360">
</div>

---

Lamia extends Python with human-readable syntax for AI commands, web automation, and file operations. Write what you want in plain English - Lamia handles the LLM calls, validates the output, and returns structured data.

- Write AI prompts as Python functions - no SDK boilerplate
- Built-in validators: get your expected results in HTML, JSON, CSV, XML, Markdown formats back - guaranteed
- Web automation with automatic data extraction into Pydantic models
- Multi-model support: OpenAI, Anthropic, Ollama (and extensible)
- Model evaluation to find the cheapest model that still passes validation

## Installation

```bash
pip install .
```

## Quick Start

Create a `.hu` file and run it with `lamia your_script.hu`:

```python
# Ask AI and create a login from using our model
page = "Create a login form" -> HTML[LoginForm]

# Read a local file as typed JSON
config = "./config.json" -> JSON[OnlyTheConfigsWeNeed]

# Scrape a website into a Pydantic model
quote = "https://finance.yahoo.com/quote/AAPL" -> HTML[StockQuote]
```

A real-world example - extract stock quotes from Yahoo Finance into a CSV:

```python
class StockQuote(BaseModel):
    ticker: str = Field(description="Stock ticker symbol, e.g. AAPL")
    open: float = Field(description="Open price from the Quote Summary section")
    bid: str = Field(description="Bid price from the Quote Summary section")
    ask: str = Field(description="Ask price from the Quote Summary section")
    bid_size: int = Field(description="Bid size (number of lots) from the Quote Summary")
    ask_size: int = Field(description="Ask size (number of lots) from the Quote Summary")

for ticker in ["QQQ", "VOO", "VGT"]:
    "extract the stock quote data from https://finance.yahoo.com/quote/{ticker}" -> File(CSV[StockQuote], "stocks.csv", append=True)
```
### Running from Python

Lamia can be used as a Python library as well.

```python
from lamia import Lamia

lamia = Lamia()

ai_response = lamia.run(
    "Create a login form",
    "openai:gpt4o",
    "anthropic:claude",
    return_type=HTML[LoginForm]
)
```

### Using Lamia Claude Pro or Max Subscription

Currently, Lamia supports only 3 LLM providers: OpenAI, Anthropic, and Ollama (local models). But you can easily extend it to support other providers by creating a new adapter by extending the `BaseLLMAdapter` class and placing it in the `extensions/adapters/llm` directory in the root of the project.

For more information see the Implementing a New Adapter section of the [Lamia LLM Adapters](lamia/adapters/llm/README.md) documentation.

Here is a ready to use adapter for Claude Pro or Max subscription. Just place it in the `extensions/adapters/llm` directory in the root of the project. IMPORTANT: Using this llm adapter might result your account being banned by Anthropic.  This is just to show you what can be an example having own (not supported by Lamia) llm adapter.


and add the following to your config.yaml file:
```yaml
model_chain:
  - name: "claude-max:claude-sonnet-4"
    max_retries: 3
```

```python
"""
Adapter for anthropic-max-router local proxy.

Routes requests through anthropic-max-router
(https://github.com/nsxdavid/anthropic-max-router) — an OpenAI-compatible
endpoint backed by Anthropic's Claude API via OAuth.
Works with Claude Pro ($20/mo) and Max ($100/$200/mo) subscriptions
for flat-rate billing instead of pay-per-token.

The router stores its OAuth tokens in .oauth-tokens.json relative to the
working directory, so all commands below use ~ as a stable anchor.
"""

import logging
from typing import Optional

import aiohttp

from lamia.adapters.llm.base import BaseLLMAdapter, LLMResponse
from lamia import LLMModel

logger = logging.getLogger(__name__)

DEFAULT_BASE_URL = "http://127.0.0.1:3000"


class ClaudeMaxAdapter(BaseLLMAdapter):
    """Adapter for a local claude-max-api proxy (OpenAI-compatible, no streaming)."""

    @classmethod
    def name(cls) -> str:
        return "claude-max"

    @classmethod
    def env_var_names(cls) -> list[str]:
        return [] # No env variables like API key names needed

    @classmethod
    def is_remote(cls) -> bool:
        return False

    def __init__(self, base_url: str = DEFAULT_BASE_URL):
        self.base_url = base_url.rstrip("/")
        self.session: Optional[aiohttp.ClientSession] = None

    async def async_initialize(self) -> None:
        if self.session is None:
            self.session = aiohttp.ClientSession(
                headers={"Content-Type": "application/json"},
                timeout=aiohttp.ClientTimeout(total=600),
            )

    async def generate(self, prompt: str, model: LLMModel) -> LLMResponse:
        if self.session is None:
            await self.async_initialize()
        assert self.session is not None

        model_name = model.get_model_name_without_provider() or "claude-sonnet-4"

        payload: dict = {
            "model": model_name,
            "messages": [{"role": "user", "content": prompt}],
            "stream": False,
        }

        if model.temperature is not None:
            payload["temperature"] = model.temperature
        if model.max_tokens is not None:
            payload["max_tokens"] = model.max_tokens
        if model.top_p is not None:
            payload["top_p"] = model.top_p

        url = f"{self.base_url}/v1/chat/completions"
        logger.debug("Requesting %s with model=%s", url, model_name)

        async with self.session.post(url, json=payload) as response:
            if response.status != 200:
                error_text = await response.text()
                raise RuntimeError(
                    f"claude-max-api error (status {response.status}): {error_text}"
                )

            data = await response.json()

        usage_data = data.get("usage", {})

        return LLMResponse(
            text=data["choices"][0]["message"]["content"],
            raw_response=data,
            usage={
                "prompt_tokens": usage_data.get("prompt_tokens", 0),
                "completion_tokens": usage_data.get("completion_tokens", 0),
                "total_tokens": usage_data.get("total_tokens", 0),
            },
            model=model_name,
        )

    async def close(self) -> None:
        if self.session:
            await self.session.close()
            self.session = None

```

## Module Documentation

| Module | Description |
|--------|-------------|
| **[Hybrid Syntax](lamia/interpreter/README.md)** | `.hu` file syntax: LLM commands, file operations, web actions, sessions, `-> File(...)` write syntax |
| **[Validation](lamia/validation/README.md)** | Validators for HTML, JSON, YAML, XML, Markdown, CSV, Pydantic models |
| **[Web Adapters](lamia/adapters/web/README.md)** | Browser automation (Selenium, Playwright) and HTTP clients |
| **[LLM Adapters](lamia/adapters/llm/README.md)** | Implementing new LLM provider adapters |
| **[Engine](lamia/engine/README.md)** | Core engine, LLM manager, configuration |
| **[Selector Resolution](lamia/engine/managers/web/selector_resolution/README.md)** | CSS/XPath and AI-powered natural language selectors |
| **[Evaluation](lamia/eval/README.md)** | Model evaluation to find cost-effective models |

## Development

See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, doc building, and code style guidelines.
