llmhive
llmhive 是一个 Python 库,用于以统一的方式调用多种大语言模型(LLM),屏蔽底层 SDK 的差异,支持流式输出和插件式扩展。
✨ 特性
- 🚀 统一 API:无论是 OpenAI、Gemini、Ollama 等,都可以用相同的调用方式。
- 📡 支持流式输出:方便做实时响应的应用(如 CLI、聊天机器人、WebSocket 服务)。
- 🧩 插件化:可以很方便地扩展新的模型调用器(Caller)。
- 🔒 类型安全:基于现代 Python 类型提示,开发体验友好。
- 🛠️ 适配 [pyproject.toml] 可选依赖,按需安装。
📦 安装
默认安装基础库:
pip install llmhive
安装特定模型支持:
# OpenAI
pip install llmhive[openai]
# Google Gemini
pip install llmhive[gemini]
# Ollama (本地模型)
pip install llmhive[ollama]
🚀 快速上手
下面示例展示如何调用 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)
🌐 支持的模型提供商
| Provider | Extra Dependency | Example Caller |
|---|---|---|
| OpenAI | pip install llmhive[openai] |
OpenAICaller |
| Gemini | pip install llmhive[gemini] |
GeminiCaller |
| Ollama | pip install llmhive[ollama] |
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
安装依赖:
pip install -e '.[openai,gemini,ollama]'
运行测试:
pytest
📜 许可证
本项目基于 Apache 2.0 License 开源。
🔗 相关项目
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
llmhive-0.1.2.tar.gz
(13.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmhive-0.1.2.tar.gz.
File metadata
- Download URL: llmhive-0.1.2.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39b2f6f36000777359f6a57c0841285e72376e8d6d62fb3278fbeb5b7729d2e5
|
|
| MD5 |
94b1216138776aabf65a8257a76344e4
|
|
| BLAKE2b-256 |
552af868dd5d1a40f38a1c40113e156cbdde08a8ee310446297dc7a33fd80bd6
|
File details
Details for the file llmhive-0.1.2-py3-none-any.whl.
File metadata
- Download URL: llmhive-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
021fd8ccb19ade0d779cf907105a327cda9402d173c88f3edc8fe42ae69c3b04
|
|
| MD5 |
e135fcb406a971abadf9ee420a34a812
|
|
| BLAKE2b-256 |
7002bce28c5bdf07529b7533f89d239f9c8ab1d9102ef37417764137b1954a2a
|