轻量级 LLM/Agent 应用开发框架,通过 `@llm_function` 装饰器将函数签名与 docstring 转化为 Prompt,无需函数体实现即可获得类型安全的 LLM 能力。支持多 Provider、多模态、工具调用、流式响应、API Key 负载均衡与 Langfuse 可观测性集成。
SimpleLLMFunc 是一个面向 Python 开发者的 LLM/Agent 应用开发框架,核心理念是「LLM as Function / Prompt as Code / Code as Doc」。由 Jingzhe Ni 开发,采用 MIT 许可证开源。
核心特性#
装饰器驱动开发#
@llm_function— 将 async 函数转换为 LLM 驱动的函数,自动处理 Prompt 构建、API 调用、响应解析@llm_chat— 构建对话式 Agent,支持流式响应和工具调用@tool— 将 async 函数注册为 LLM 可调用工具,支持多模态返回@tui— 开箱即用的 Textual 终端 UI
类型安全与异步原生#
- Python 类型注解 + Pydantic 模型确保类型正确性
- 完全异步设计,原生 asyncio 支持,适配高并发场景
- IDE 代码补全和类型检查友好
多模态与多 Provider#
- 支持
Text、ImgUrl、ImgPath多模态输入/输出 - 兼容 OpenAI、Deepseek、Anthropic Claude、火山引擎 Ark、百度千帆、Ollama、vLLM 等
生产级能力#
- API Key 负载均衡:多 Key 配置,最小堆算法选择负载最低 Key
- 流量控制:Token Bucket 算法防止 API 限速
- 结构化日志:trace_id 自动追踪
- 可观测性:Langfuse 集成
内置工具#
- PyRepl:基于子进程的持久化 Python REPL
- SelfReference:持久化 Agent 内存合约
典型应用场景#
| 场景 | 描述 |
|---|---|
| 数据处理与分析 | 实体提取、文本分类、结构化数据提取 |
| 智能助手与对话 | 搜索信息、计算、调用 API 的 Agent |
| 批量数据处理 | async + asyncio.gather 高并发调用 |
| 多模态内容处理 | 图片分析、图文对比 |
| 快速原型验证 | 函数式组合快速验证业务逻辑 |
快速开始#
pip install SimpleLLMFunc
import asyncio
from SimpleLLMFunc import llm_function, OpenAICompatible
llm = OpenAICompatible.load_from_json_file("provider.json")["your_provider"]["model"]
@llm_function(llm_interface=llm)
async def classify_sentiment(text: str) -> str:
"""
Analyze the sentiment tendency of text.
Args:
text: Text to analyze
Returns:
Sentiment classification: 'positive', 'negative', or 'neutral'
"""
pass # Prompt as Code!
asyncio.run(classify_sentiment("This product is amazing!"))
环境要求#
- Python >=3.12, <4.0
相关项目#
- SimpleManus:同作者 Agent Framework,基于 SimpleLLMFunc 构建