
LightAgent
轻量框架,快速构建带工具和记忆的 OpenAI 兼容智能体
项目预览
项目数据
项目介绍
Star 增长趋势
本站每日记录该项目 GitHub 星标变化,观测越久曲线越完整
数据来源:GitHub API 逐日快照 · 收录于 2026-08-09 · 当前星标 ★ 1243
技术标签
使用教程
环境要求
- Python 环境(PyPI 上提供 lightagent 包)
- 可用的 OpenAI 兼容 API:api_key 与 base_url
- 使用长期记忆时需额外安装 mem0ai(可选)
- 使用工具调用时需自行提供带 tool_info 元数据的函数
安装与启动步骤
-
1安装 LightAgent
使用 pip 从 PyPI 安装最新版 lightagent 包,这是整个框架的唯一必需依赖。
pip install lightagent -
2可选安装 Mem0
只有需要 Mem0 记忆后端时才安装 mem0ai;不装也能正常跑基础对话。
pip install mem0ai -
3创建 Agent 实例
用 model、api_key、base_url 三个参数初始化 LightAgent,后两个换成你自己的真实值。
from LightAgent import LightAgent agent = LightAgent(model="gpt-4.1", api_key="sk-xxxxxxxx", base_url="https://api.openai.com/v1") -
4运行一次对话
调用 agent.run 传入问题,默认返回字符串,直接 print 即可看到模型回复。
response = agent.run("Hello, who are you?") print(response) -
5开启追踪日志
加 result_format="object"、trace=True 拿到结构化结果与 trace,再遍历导出的事件。
result = agent.run("Hello, who are you?", result_format="object", trace=True) print(result.content) print(result.trace_id) print(result.trace) for event in agent.export_trace(): print(event["type"], event["data"]) -
6接入自定义工具
在构造 LightAgent 时通过 tools 传入函数列表,函数需暴露 tool_info 元数据,数量不限。
agent = LightAgent( model="gpt-4.1", api_key="sk-xxxxxxxx", base_url="https://api.openai.com/v1", tools=[search_news, get_weather], ) -
7编排 LightFlow 工作流
用 LightFlow 串起多步任务并持久化检查点,失败后可用 run_id 调用 resume 从断点续跑。
from LightAgent import JsonLightFlowStore, LightAgent, LightFlow research_agent = LightAgent(model="gpt-4.1", api_key="sk-xxxxxxxx", base_url="https://api.openai.com/v1") writer_agent = LightAgent(model="gpt-4.1", api_key="sk-xxxxxxxx", base_url="https://api.openai.com/v1") store = JsonLightFlowStore(".lightflow_runs") flow = ( LightFlow(store=store) .step("research", agent=research_agent, timeout=30) .step("write", agent=writer_agent, depends_on=["research"], max_retry=2) ) result = flow.run("Analyze this company", run_id="report-001", trace=True) if not result.success: result = flow.resume("report-001") print(result.status) print(flow.get_run("report-001")["steps"])
关键配置
| 配置项 | 必填 | 说明 | 示例 |
|---|---|---|---|
model | 是 | 使用的模型名称 | gpt-4.1 |
api_key | 是 | OpenAI 兼容服务的密钥 | sk-xxxxxxxx |
base_url | 是 | OpenAI 兼容服务的接口地址 | https://api.openai.com/v1 |
tools | 否 | 注册给智能体的工具函数列表,函数需带 tool_info | [search_news, get_weather] |
hooks | 否 | 生命周期钩子,可观察、替换或阻断请求 | [redact_before_model, PolicyHook(...)] |
input_guardrails | 否 | 输入/工具/输出三类安全护栏策略 | [my_input_guardrail] |
如何确认成功
执行 agent.run("Hello, who are you?") 后能 print 出模型回复文本,说明安装与密钥配置均正常。
常见问题
Q:可以用非 OpenAI 官方模型吗?
A:可以。只要服务商兼容 OpenAI API,把 base_url 改成其接口地址、api_key 换成对应密钥即可。
Q:必须安装 mem0ai 吗?
A:不是必需。mem0ai 属于可选安装,只有准备使用 Mem0 作为记忆后端时才需要 pip 安装。
Q:怎么看到一次运行内部发生了什么?
A:调用时传 trace=True(配合 result_format="object"),再用 agent.export_trace() 遍历事件列表打印类型和数据。
Q:工作流跑到一半失败怎么办?
A:LightFlow 配了 JsonLightFlowStore 后会持久化检查点,用同一个 run_id 调用 flow.resume("report-001") 即可续跑。
Q:工具函数有什么要求?
A:函数需要暴露 tool_info 元数据,README 示例中是以 tools=[search_news, get_weather] 的形式注册进 LightAgent。
注意事项
- 示例里的 api_key、base_url 必须替换成自己的真实值,切勿硬编码提交到仓库。
- 追踪默认关闭且不改变 agent.run() 的返回类型,需要显式传 trace=True 才记录事件。
- 安全敏感的回调建议用 PolicyHook 包装,异常或超时会 fail closed,普通观测钩子则保持失败隔离。
- JsonLightFlowStore(".lightflow_runs") 会在该目录写入运行记录,可按需更换路径。
核心亮点
- 内置 guardrails 机制,可灵活定义输入输出安全过滤规则,增强生产环境可靠性
- 提供完整的生命周期钩子(lifecycle hooks),方便在智能体运行各阶段注入自定义逻辑
- 原生支持 MCP(模型上下文协议),可无缝接入丰富的第三方工具生态
不足之处
- 项目较新,文档和社区生态尚待完善
- 多智能体协作与工作流功能相对基础,复杂场景可能需要额外扩展
适用场景
- 快速搭建企业内部智能客服或知识库问答助手
- 构建自动化工作流,如数据处理、报告生成等任务编排
- 作为教学或研究用途,学习 Agent 框架的设计与实现
替代项目
LangChain、LlamaIndex、AutoGen
同类项目推荐
BossConsole
开源
在原生桌面控制台上,多线程运行你的 AI 代理,集成浏览器、终端和 MCP 工具。
Open-source, multi-platform harness for AI agents - a native, multi-threaded operato···
agentic-fleet
开源
基于微软框架,让多智能体协作更聪明、更高效。
Adaptive Agentic AI Reasoning using Microsoft Agent Framework -- Join the Discord fo···
mate
开源
数据库驱动配置,50+模型接入,轻松编排多智能体生产应用。
Production-ready multi-agent orchestration engine built on Google ADK. Database-driv···
higgsfield-mcp-unified
开源
把 85 个图像视频模型接进 Claude,本地就能调
Local-first MCP server for the Higgsfield AI API: 85 typed image & video models (See···
LiteMultiAgent
开源
轻量构建多智能体应用,快速编排协作流程
The Library for LLM-based multi-agent applications
FABULA-LLM-5
开源
让本地小模型也能硬核完成任务,并给出完成证明
A local-first coding agent that doesn't take "done" on faith: the engine keeps the r···
intellagent
开源
用合成交互数据自动诊断智能体短板,快速优化性能
A framework for comprehensive diagnosis and optimization of agents using simulated, ···
graphmind
开源
把代码库变成AI能记住的知识图谱,让AI更懂你的项目
Local-first code intelligence for AI assistants. Turns your codebase into a knowledg···
QwenPaw
开源
通义出的个人助手,装好就能聊,手机电脑随处用
Your Personal AI Assistant; easy to install, deploy on your own machine or on the cl···