异步调用 OpenAI Chat Completions、OpenAI Responses、Anthropic Messages、Gemini 和 Ollama。请求使用官方 SDK,输入、输出与流事件采用五种协议的标准字典,不另定义消息类型。 需要 Python 3.12+,不内置厂商或模型清单。
安装
pip install my-llm-py
# 或 uv add my-llm-py
调用
ProviderProfile.protocol 选择上游 API,stdio_protocol 选择调用者的输入输出协议。
两者独立;例如用 Chat 消息调用 Responses API,并继续收到 Chat 消息。
import asyncio
import os
from my_llm import ModelProfile, MyLLM, ProviderProfile
async def main():
llm = MyLLM(
provider=ProviderProfile(
protocol="openai_responses",
api_key=os.environ["OPENAI_API_KEY"],
),
model=ModelProfile(model=os.environ["LLM_MODEL"]),
stdio_protocol="openai_chat",
)
message = await llm.generate(messages=[{"role": "user", "content": "你好"}])
print(message["content"])
asyncio.run(main())
五个协议名为 openai_chat、openai_responses、anthropic、gemini、ollama。
省略 stdio_protocol 时,首次调用从整个 messages/tools 列表识别一次并绑定到实例。
纯文本消息等重叠输入默认使用 openai_chat;需要其他格式时显式指定。
同一实例的 messages 与 tools 使用同一种协议,不混用历史格式。
async def print_answer(llm):
# 此示例的 llm 使用 stdio_protocol="openai_chat"。
stream = await llm.generate(
messages=[{"role": "user", "content": "你好"}], stream=True,
)
async with stream:
async for event in stream:
for choice in event.get("choices", []):
print(choice["delta"].get("content", ""), end="", flush=True)
return stream.result # 完成后的标准消息字典
流式工具调用完整后才返回;本库不执行工具。SDK Client 每次请求创建并关闭。
Provider、Model 和连接配置在构造时保存副本,生成时仅接受消息、工具和生成选项。
replay_reasoning_content_when_tool_calls 仅在思考已开启且 assistant 有工具调用时补空思维链。
独立格式转换
MyCodec 无状态,不绑定 profile,可复用于任意转换路径。
from my_llm import MyCodec
codec = MyCodec()
converted = codec.convert(
[{"role": "user", "content": "你好"}],
input_format="openai_chat", target_format="gemini",
)
assert converted["messages"] == [{"role": "user", "parts": [{"text": "你好"}]}]
消息、工具和流事件各有对称的 encode/decode 接口;profile=None 使用标准协议。
同协议往返保留原生签名、未知字段和嵌套字典。
本地开发与验证
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
构建与验证指南 包含安装和发行包验证步骤。 项目采用 Apache License 2.0。
Metadata
Release files for my-llm-py 1.0.0
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-1.0.0.tar.gz | 93.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| my_llm_py-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 176.0 kB
Release files / my_llm_py-1.0.0.tar.gz
| Download URL | my_llm_py-1.0.0.tar.gz |
|---|---|
| Size | 93.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a96c6b18d5d34fa5d768cc2d3a21996d87ed7dcaba51482403bc56d5d3f18d66
|
|
BLAKE2b-256 checksum How to use checksums |
e5f1c4f051ada4fd68e7c474663fdfdd23dcf4847ab0e28604168988b99646c5
|
| 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-1.0.0-py3-none-any.whl
| Download URL | my_llm_py-1.0.0-py3-none-any.whl |
|---|---|
| Size | 82.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
83de9f5f6957600e880d2925e9833d55d4b3a08f2ee7a9c4329a8119ff34ed9a
|
|
BLAKE2b-256 checksum How to use checksums |
7428482a5b327fbf6dffd0cf2c8e0069d2da74cae32f9a7d06e826dbe56de809
|
| 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 1.0.0
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.