Skip to main content

OpenAI-compatible Python SDK for Luan API (luanapi.xingluan.cn)

Project description

luanapi-sdk

OpenAI-compatible Python SDK for Luan API.

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

安装

pip install luanapi-sdk

快速开始

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"])

流式调用

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 风格参数:modelmessagestemperaturetop_pmax_tokenspresence_penaltyfrequency_penaltystreamstream_optionstools、…

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

异常

from luanapi import LuanAPIError, AuthenticationError, APIStatusError
  • AuthenticationError:401 或缺少 token
  • APIStatusError:非 2xx 响应,包含 status_coderesponse_body
  • LuanAPIError:所有错误的基类

发布到 PyPI

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

发布到 TestPyPI:

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

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

luanapi_sdk-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

luanapi_sdk-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file luanapi_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: luanapi_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for luanapi_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a317692593d5b0c4666d86dbf8a025266dfe54754b15c60fc4b2eac72d78de2f
MD5 83ec76e2141b44e624f6317d5ee1f441
BLAKE2b-256 c870d56a0dd53c217c6efc12e505e7bbb4ce6c3a80f20d53dd9c8a6c22cf06f7

See more details on using hashes here.

File details

Details for the file luanapi_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: luanapi_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for luanapi_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cf8fbd56780db6c9d291ccdb19e48f21732d21167ea3e5521e4eb1a33777a8b
MD5 e04dfb1f5348c898e9508c20e186bc99
BLAKE2b-256 fa12a458395cdc9490e51bcbc97eb997e089b5483fc741bd97059aab6b8e2143

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page