Metadata-Version: 2.4
Name: laiwenai
Version: 0.0.7
Summary: A lightweight OpenAI-compatible LLM client wrapper with defaults, retries, logging, and manual JSON mode.
Author: LaiwenAI
License-Expression: MIT
Keywords: openai,llm,client,json,retry
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
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 :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: openai>=1.0.0
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: tenacity>=8.0.0
Dynamic: license-file

# LaiwenAI

LaiwenAI is a lightweight Python wrapper for OpenAI-compatible LLM endpoints. It keeps the familiar OpenAI SDK style while adding project-local `.env` defaults, retries, optional logging, and a small `laiw` command line helper.

## Install

```bash
pip install laiwenai
```

## Initialize

Run this in your project directory:

```bash
laiw init
```

`laiw init` checks only the current working directory. It creates or completes `.env.example` and `.env`, asks for `LAIWEN_API_KEY`, lets other values use defaults, then runs `laiw test`.

Default `.env` values:

```env
LAIWEN_API_KEY=your_api_key_here
LAIWEN_BASE_URL=http://111.228.11.28:3000/v1
LAIWEN_MODEL=DeepSeek-V4-Flash
LAIWEN_EMBED_MODEL=text-embedding-3-small
LAIWEN_TEMP=0
LAIWEN_TIMEOUT=30.0
LAIWEN_RETRIES=1
LAIWEN_LOG_CONSOLE=False
LAIWEN_LOG_FILE=False
LAIWEN_LOG_DIR=logs
```

## CLI

```bash
laiw test
```

`laiw test` checks the current directory `.env` and loads config from that file; values in the current `.env` override same-name system environment variables. It first prints the resolved environment config with `LAIWEN_API_KEY` masked, then runs a chat smoke test.

Example output:

```text
环境参数：
  LAIWEN_API_KEY=your_a...here
  LAIWEN_BASE_URL=http://111.228.11.28:3000/v1
  LAIWEN_MODEL=DeepSeek-V4-Flash
  LAIWEN_EMBED_MODEL=text-embedding-3-small
  LAIWEN_TEMP=0.0
  LAIWEN_TIMEOUT=30.0
  LAIWEN_RETRIES=1
  LAIWEN_LOG_CONSOLE=False
  LAIWEN_LOG_FILE=False
  LAIWEN_LOG_DIR=logs
```

To test JSON-mode output:

```bash
laiw test --json
```

To test chat and embeddings:

```bash
laiw test --embedding
```

To run all smoke tests:

```bash
laiw test --all
```

## Python Usage

```python
from laiwenai import LaiwenAI

client = LaiwenAI()

response = client.chat.completions.create(
    messages=[
        {"role": "user", "content": "你是什么模型？请用中文简短回复：我是XXX模型。"}
    ]
)

print(response.choices[0].message.content)
```

You can still pass OpenAI SDK parameters:

```python
response = client.chat.completions.create(
    model=None,
    messages=[{"role": "user", "content": "Hello"}],
    temperature=None,
    timeout=None,
    response_format={"type": "json_object"},
)
```

Embeddings:

```python
embedding = client.embeddings.create(input="Hello")
print(len(embedding.data[0].embedding))
```

## OpenAI 兼容接口

当你传入自定义 `base_url` 时，必须显式传入 `api_key`，避免把默认密钥误用于另一个服务商。

```python
from laiwenai import LaiwenAI

client = LaiwenAI(
    api_key="your-provider-key",
    base_url="http://111.228.11.28:3000/v1",
    model="DeepSeek-V4-Flash",
)
```

## Configuration

| Key | Description |
| --- | --- |
| `LAIWEN_API_KEY` | Default API key. Required unless passed explicitly. |
| `LAIWEN_BASE_URL` | Default OpenAI-compatible base URL. |
| `LAIWEN_MODEL` | Default chat/responses model. |
| `LAIWEN_EMBED_MODEL` | Default embedding model. |
| `LAIWEN_TEMP` | Default temperature when omitted or `None`. |
| `LAIWEN_TIMEOUT` | Default timeout when omitted or `None`. |
| `LAIWEN_RETRIES` | Retry attempts around API calls. |
| `LAIWEN_LOG_CONSOLE` | Enable structured console logs. |
| `LAIWEN_LOG_FILE` | Enable structured file logs. |
| `LAIWEN_LOG_DIR` | Directory for `laiwenai.log`. |

## Notes

`LaiwenAI()` loads `.env` only from the current working directory. If `.env` is missing, initialization fails with a clear error.

## License

MIT
