Python library for interacting with Large Language Models.
Project description
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 CallerContext
caller = OpenAICaller(model="gpt-4o-mini")
ctx = CallerContext(
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, CallerContext
from collections.abc import Iterator
class MyLLMCaller(Caller):
def __init__(self, model: str):
self.model = model
def call(self, ctx: CallerContext) -> 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 开源。
🔗 相关项目
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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.1.tar.gz.
File metadata
- Download URL: llmhive-0.1.1.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edcb1c428c1adbfb1c43ee4f34132ed038c27a1bb3fc6e56e04c30de3ec964c6
|
|
| MD5 |
7208837e2d6472537065ebe5399fa684
|
|
| BLAKE2b-256 |
4fea1592b647b52d812a1ee9df631c22de0a2162b31b50f8150f441cdd927ef3
|
File details
Details for the file llmhive-0.1.1-py3-none-any.whl.
File metadata
- Download URL: llmhive-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
568417131e12edea2d64698afcd96ecbe1459796c7f52c63ad4c2a58e44bf5ce
|
|
| MD5 |
3efe64452359d8733308793863cffeee
|
|
| BLAKE2b-256 |
10c656ed85a04dd36932caa250c930590bb3b01883d0f4c8cb99b6ebbe074b14
|