通过配置统一调用 OpenAI(Responses / Chat Completions)、Anthropic Messages、
Gemini 和 Ollama 原生协议的异步 Python 库,四种协议均使用官方 SDK。
兼容服务使用相同的 OpenAI SDK,通过 ProviderProfile 和 ModelProfile 描述差异,
不内置厂商或模型清单。需要 Python 3.12+。
使用
import asyncio
import os
from my_llm import ModelProfile, ProviderProfile, generate
async def main():
provider = ProviderProfile(
api_type="responses",
api_key=os.environ["OPENAI_API_KEY"],
usage_mode="native",
)
model = ModelProfile(model=os.environ["LLM_MODEL"])
message = await generate(
provider=provider,
model=model,
messages=[{"role": "user", "content": "你好"}],
reasoning_effort="none",
stream=False,
)
print(message["content"])
asyncio.run(main())
重复调用时可复用 MyLLM,构造时显式传入模型配置:
from my_llm import ModelProfile, MyLLM, ProviderProfile
llm = MyLLM(
provider=ProviderProfile(api_type="responses"),
model=ModelProfile(model="your-model-id", max_tokens=2048),
)
# 在异步函数中调用:
# message = await llm.generate(messages=[{"role": "user", "content": "你好"}])
stream=True 返回可关闭的异步流,调用时同样需要先 await:
async def print_answer(llm):
stream = await llm.generate(
messages=[{"role": "user", "content": "你好"}], stream=True,
)
async with stream:
async for chunk in stream:
print(chunk.content, end="", flush=True)
if chunk.tool_calls:
print(chunk.tool_calls) # 完整工具调用,由调用者执行本地函数
推理开启方式、强度映射、输出路径、消息名称与用量模式均可配置。Omit 可禁用 配置或删除某个字典键。流异常和缺少完成状态会明确报错。 Tools 可传 Responses 或 Chat Completions 格式,自动识别并按目标 API 转换。 Messages 同样支持两种格式:自动转换内容块、工具调用及工具结果,并允许混合历史。 Responses 结果保留原生 output items,支持工具调用后的历史回放。
调用参数、流式消费、工具回合及资源管理见
API 使用指南;
Profile 字段、默认值、继承规则及兼容服务适配见
配置指南。
通过 ProviderProfile(protocol="anthropic" / "gemini" / "ollama") 选择原生协议。
Anthropic 必须设置 ModelProfile.max_tokens 或调用时的 max_tokens。
原生协议的参数映射、推理配置和 SDK Client 类型见
本地开发与验证
uv sync --group dev --locked
uv run pytest -q
uv run ruff check src tests examples
uv run ruff format --check src tests examples
uv run python examples/local_demo.py
uv build
package_version=$(uv run python -c 'from my_llm import __version__; print(__version__)')
uv run twine check --strict "dist/my_llm-${package_version}-py3-none-any.whl" "dist/my_llm-${package_version}.tar.gz"
Metadata
Release files for my-llm-py 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| my_llm_py-0.1.3.tar.gz | 82.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| my_llm_py-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 146.2 kB
Release files / my_llm_py-0.1.3.tar.gz
| Download URL | my_llm_py-0.1.3.tar.gz |
|---|---|
| Size | 82.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
abb115d1dcb7957d756d05e03cf0efa0cb9513bccf4f6661ab0c948fe273aff8
|
|
BLAKE2b-256 checksum How to use checksums |
a6a9efd6a908750c2c0e32f992a84d332c0a7b122351631325a13f339c3ec277
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.12
|
Release files / my_llm_py-0.1.3-py3-none-any.whl
| Download URL | my_llm_py-0.1.3-py3-none-any.whl |
|---|---|
| Size | 63.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
33cfce59a82a2ae305978aede919250e3691b015c93afa68e5bc705f6c900878
|
|
BLAKE2b-256 checksum How to use checksums |
9f1cf1f64e4a84f9e683f331634c396ddbf25887b3f793bb25e2c4c2c319d655
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.12
|
Security for my-llm-py 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.