Metadata-Version: 2.4
Name: luanapi-sdk
Version: 0.1.0
Summary: OpenAI-compatible Python SDK for Luan API (luanapi.xingluan.cn)
Author-email: Your Name <you@example.com>
License: MIT
Keywords: luanapi,openai,llm,sdk,chat-completions
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.25.0
Provides-Extra: dev
Requires-Dist: build>=1.0.0; extra == "dev"
Requires-Dist: twine>=4.0.0; extra == "dev"
Requires-Dist: pytest>=7.0.0; extra == "dev"
Dynamic: license-file

# luanapi-sdk

OpenAI-compatible Python SDK for [Luan API](https://luanapi.xingluan.cn).

接口格式与 OpenAI Chat Completions 完全兼容，使用习惯接近 `openai` 官方 SDK：传入 `api_key` 后即可调用 `client.chat.completions.create(...)`。

## 安装

```bash
pip install luanapi-sdk
```

## 快速开始

```python
from luanapi import LuanAI

client = LuanAI(api_key="YOUR_TOKEN")  # 也可设置环境变量 LUANAPI_API_KEY

# 非流式
resp = client.chat.completions.create(
    model="minimax-m2.5",
    messages=[
        {"role": "system", "content": "你是一个专业、友善的AI助手。"},
        {"role": "user", "content": "hello"},
    ],
    temperature=0.8,
    max_tokens=4096,
)
print(resp["choices"][0]["message"]["content"])
```

## 流式调用

```python
stream = client.chat.completions.create(
    model="minimax-m2.5",
    messages=[{"role": "user", "content": "讲个笑话"}],
    stream=True,
    stream_options={"include_usage": True},
    top_p=1,
    temperature=0.8,
    presence_penalty=0.2,
    frequency_penalty=0.2,
)

for chunk in stream:
    choices = chunk.get("choices") or []
    if choices:
        delta = choices[0].get("delta") or {}
        if delta.get("content"):
            print(delta["content"], end="", flush=True)
    if chunk.get("usage"):
        print("\n[usage]", chunk["usage"])
```

## API

### `LuanAI(api_key, *, base_url=..., timeout=60, default_headers=None)`

| 参数 | 说明 |
| --- | --- |
| `api_key` | Bearer Token；不传则读取环境变量 `LUANAPI_API_KEY`。 |
| `base_url` | 默认 `https://luanapi.xingluan.cn/runningai/open/v1`。 |
| `timeout` | 默认 60s。 |
| `default_headers` | 注入到所有请求的额外 header。 |

### `client.chat.completions.create(**kwargs)`

接受所有 OpenAI 风格参数：`model`、`messages`、`temperature`、`top_p`、`max_tokens`、`presence_penalty`、`frequency_penalty`、`stream`、`stream_options`、`tools`、…

- `stream=False` → 返回 `dict`
- `stream=True` → 返回生成器，逐条 yield 已解析的 JSON chunk（自动处理 `data:` 前缀与 `[DONE]`）

## 异常

```python
from luanapi import LuanAPIError, AuthenticationError, APIStatusError
```

- `AuthenticationError`：401 或缺少 token
- `APIStatusError`：非 2xx 响应，包含 `status_code`、`response_body`
- `LuanAPIError`：所有错误的基类

## 发布到 PyPI

```bash
pip install --upgrade build twine
python -m build
python -m twine upload dist/*
```

发布到 TestPyPI：

```bash
python -m twine upload --repository testpypi dist/*
pip install --index-url https://test.pypi.org/simple/ luanapi-sdk
```

## License

MIT
