Skip to main content

llmhive

PyPI Python License

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 开源。


🔗 相关项目

Metadata

Release files for llmhive 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmhive 0.1.2
File Size Uploaded
llmhive-0.1.2.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llmhive 0.1.2
File Interpreter ABI Platform
llmhive-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / llmhive-0.1.2.tar.gz

Download URL llmhive-0.1.2.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
39b2f6f36000777359f6a57c0841285e72376e8d6d62fb3278fbeb5b7729d2e5
BLAKE2b-256 checksum
How to use checksums
552af868dd5d1a40f38a1c40113e156cbdde08a8ee310446297dc7a33fd80bd6
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / llmhive-0.1.2-py3-none-any.whl

Download URL llmhive-0.1.2-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
021fd8ccb19ade0d779cf907105a327cda9402d173c88f3edc8fe42ae69c3b04
BLAKE2b-256 checksum
How to use checksums
7002bce28c5bdf07529b7533f89d239f9c8ab1d9102ef37417764137b1954a2a
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page