极简的智能体开发框架
Project description
✨特性
- 装饰器一键接入 Function Call(Pydantic 模型自动生成工具描述)
- 工作流
Agent管线,按level顺序编排执行 - 结构化响应解析:可传入
response_format(Pydantic)强类型返回
🧱环境要求
- Python ≥ 3.8
- 有效的 OpenAI API Key
📦安装
pip install pyxbrain
🚀快速开始:接入一个工具
在你的项目目录下创建一个 demo.py 文件:
from pydantic import BaseModel
from xbrain.core import xbrain_tool
class GenerateTag(BaseModel):
topic: str
@xbrain_tool.Tool(model=GenerateTag)
def generate_tag(topic: str):
return f"tag: {topic}"
在包的 __init__.py 文件中导入 demo.py:
from demo import *
在项目入口处配置并运行 XBrain,此时 demo.py 中的 generate_tag 被成功接入:
from xbrain.core.chat import run
from xbrain.utils.config import Config
config = Config()
config.set_openai_config(
base_url="https://api.openai.com/v1",
api_key="YOUR_OPENAI_API_KEY",
model="gpt-4o-2024-08-06",
)
messages = [{"role": "user", "content": "请为主题“Python”生成标签"}]
res = run(messages, user_prompt="你是一个能调用工具的助手")
print(res)
📐结构化响应(可选)
如果你希望模型严格返回某个结构,可以传入 response_format:
from pydantic import BaseModel
class Summary(BaseModel):
title: str
keywords: list[str]
messages = [{"role": "user", "content": "请总结并给出关键词"}]
res = run(messages, user_prompt="结构化助手", response_format=Summary)
print(res) # 返回满足 Summary 的内容
🧩工作流 Agent
使用 @Agent 装饰器定义节点,工作流将按 level 由小到大依次执行:
from xbrain.core.xbrain_agent import Agent, work_flow_run
@Agent
class A:
level = 1
def run(self, input):
return "下一步输入"
@Agent
class B:
level = 2
def run(self, input):
return "最终输出"
print(work_flow_run("起始输入")) # "最终输出"
⚙️配置文件位置
- 使用
xbrain.utils.config.Config管理配置 - 配置文件写入到用户目录:
~/xbrain/config.yaml - 也可通过
config.set_openai_config(base_url, api_key, model)动态设置并持久化
🤝如何贡献
你可以通过 Fork 项目、提交 PR 或在 Issue 中提出你的想法和建议。具体操作可参考 贡献指南。
建议阅读 《提问的智慧》、《如何向开源社区提问题》、《如何有效地报告 Bug》 与 《如何向开源项目提交无法解答的问题》。
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 pyxbrain-2.0.1.tar.gz.
File metadata
- Download URL: pyxbrain-2.0.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbabcfffb995c3fedb06976ec38fa66f610bd178ba016e1a87c9dbf57401d5e5
|
|
| MD5 |
2c847c3c52d78f77740ac82a234a702d
|
|
| BLAKE2b-256 |
a4b2ea877ca39db8983e483156ca45b60149992bfc5c8ed491e3f2ccaed46904
|
File details
Details for the file pyxbrain-2.0.1-py3-none-any.whl.
File metadata
- Download URL: pyxbrain-2.0.1-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f2227f189fff5311c893b0d8284aca772cdcc21507038fc075ef4ca9f82a4b2
|
|
| MD5 |
ec229c9cc5d245ad83b9e2bd21c5210a
|
|
| BLAKE2b-256 |
b2b050175486b9a0466021d1148d609b2e7bf0a1c751ae354622e397608b8fca
|