通过配置统一调用 OpenAI(Responses / Chat Completions)、Anthropic Messages、
Gemini 和 Ollama 原生协议的异步 Python 库,四种协议均使用官方 SDK。
兼容服务使用相同的 OpenAI SDK,通过 ProviderProfile 和 ModelProfile 描述差异,
不内置厂商或模型清单。需要 Python 3.12+。
安装
pip install my-llm-py
# 或
uv add my-llm-py
PyPI 分发名为 my-llm-py,Python 导入名为 my_llm。
使用
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_py-${package_version}-py3-none-any.whl" "dist/my_llm_py-${package_version}.tar.gz"
构建、安装验证和 GitHub 自动测试说明见 构建与验证指南。
许可证
本项目采用 Apache License 2.0。
Metadata
Release files for my-llm-py 0.1.4
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.4.tar.gz | 84.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| my_llm_py-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 149.2 kB
Release files / my_llm_py-0.1.4.tar.gz
| Download URL | my_llm_py-0.1.4.tar.gz |
|---|---|
| Size | 84.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cacd4300eba7c202e1afc3b9295951025f501b4dbade339678231e424207e3b4
|
|
BLAKE2b-256 checksum How to use checksums |
d3a505a0cdfcdb918ef15f3988b355d991a83d71f0e6f2782915abd577522f82
|
| 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.4-py3-none-any.whl
| Download URL | my_llm_py-0.1.4-py3-none-any.whl |
|---|---|
| Size | 64.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1a11d019d54c1af9ea8e08e06b308fc18c71b3dafbe7983959e8f364a691367a
|
|
BLAKE2b-256 checksum How to use checksums |
5c036d8935db9b2316c6712563d74de9f12fec5f76eef3f804183aa397ebe5c3
|
| 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.4
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.