llmhive
llmhive 是一个 Python 库,用于以统一的方式调用多种大语言模型(LLM),屏蔽底层 SDK 的差异,支持流式输出和插件式扩展。
✨ 特性
- 🚀 统一 API:无论是 OpenAI、Gemini、Ollama 等,都可以用相同的调用方式。
- 📡 支持流式输出:方便做实时响应的应用(如 CLI、聊天机器人、WebSocket 服务)。
- 🧩 插件化:可以很方便地扩展新的模型调用器(Caller)。
- 🔒 类型安全:基于现代 Python 类型提示,开发体验友好。
- 🛠️ 使用 OpenAI Python SDK 调用标准及兼容 API。
📦 安装
pip install llmhive
openai 是主依赖。OpenAI、Gemini 和 Ollama 都通过 OpenAI-compatible API 调用,不需要安装 provider 专用 SDK。
🚀 快速上手
下面示例展示如何调用 OpenAI ChatGPT,并支持流式输出:
from llmhive.callers.openai_caller import OpenAICaller
from llmhive.message import Message
from llmhive.caller import PromptContext
caller = OpenAICaller(model="gpt-4o-mini")
ctx = PromptContext(
system_prompt="You are a helpful assistant.",
history=[
Message(role="user", content="Hello!"),
],
is_stream=True,
)
for chunk in caller.call(ctx):
print(chunk, end="", flush=True)
💭 处理思考过程
启用思考模式后,最终回答仍通过 ModelTextEvent 返回;模型的推理过程会单独作为
ModelThinkingEvent 产出。调用方可自行写入 stderr 或日志,而不会影响最终回答:
from llmhive.types.event import ModelTextEvent, ModelThinkingEvent
for event in assistant.run("Explain the result", enable_thinking=True):
if isinstance(event, ModelThinkingEvent):
logger.debug("model thinking: %s", event.content)
elif isinstance(event, ModelTextEvent):
print(event.content, end="", flush=True)
🌐 支持的模型提供商
| Provider | API endpoint | Example Caller |
|---|---|---|
| OpenAI | OpenAI API | OpenAICaller |
| Gemini | Google OpenAI-compatible API | GeminiCaller |
| Ollama | Local OpenAI-compatible API | OllamaCaller |
未来计划支持:Anthropic Claude、Mistral、AWS Bedrock 等。
🧩 扩展新模型
要支持新的 LLM,只需继承 Caller 基类,实现 call 方法:
from llmhive.caller import Caller, PromptContext
from collections.abc import Iterator
class MyLLMCaller(Caller):
def __init__(self, model: str):
self.model = model
def call(self, ctx: PromptContext) -> Iterator[str]:
# 这里实现你的模型调用逻辑
yield "Hello from MyLLM!"
然后就可以像内置的 Caller 一样使用它。
📖 开发
克隆仓库:
git clone https://github.com/yourname/llmhive.git
cd llmhive
安装依赖:
uv sync --all-packages
运行测试:
pytest
📜 许可证
本项目基于 Apache 2.0 License 开源。
🔗 相关项目
Metadata
Release files for llmhive 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 | Actions |
|---|---|---|---|---|
| llmhive-0.1.3.tar.gz | 32.0 kB | Without provenance | Details for llmhive-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llmhive-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for llmhive-0.1.3-py3-none-any.whl |
Total release size: 39.0 kB
Release files / llmhive-0.1.3.tar.gz
| Download URL | llmhive-0.1.3.tar.gz |
|---|---|
| Size | 32.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2dbabace90dfd7a7f715d3bb02c30e839484037058e274061f2c151a709bea9b
|
|
BLAKE2b-256 checksum How to use checksums |
70fbdb5070566596d1f47399dde855f9475fdc6df01ca86ba56e2a218cf4f1fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llmhive-0.1.3-py3-none-any.whl
| Download URL | llmhive-0.1.3-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
028abb4a1d1c80886392a21c2c06a3818aa0e1d139c0d0e30785b88e9bdc99aa
|
|
BLAKE2b-256 checksum How to use checksums |
35c8c5618e8c0a23ee17d4ee1d4a2c08df19105254dff6526ff32d573d167481
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for llmhive 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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.