Skip to main content

极简的智能体开发框架

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(name) 装饰器定义智能体节点,并通过 WorkFlow 类按顺序执行:

from xbrain.core.xbrain_agent import Agent, WorkFlow

@Agent(name="agent_a")
class A:
    def run(self, input):
        return f"{input} -> 处理后的数据A"

@Agent(name="agent_b")
class B:
    def run(self, input):
        return f"{input} -> 处理后的数据B"

# 创建工作流并指定执行顺序
workflow = WorkFlow(agent_names=["agent_a", "agent_b"])

# 执行工作流
result = workflow.run("起始输入")
print(result)  # "起始输入 -> 处理后的数据A -> 处理后的数据B"

⚙️配置文件位置

  • 使用 xbrain.utils.config.Config 管理配置
  • 配置文件写入到用户目录:~/xbrain/config.yaml
  • 也可通过 config.set_openai_config(base_url, api_key, model) 动态设置并持久化

🤝如何贡献

你可以通过 Fork 项目、提交 PR 或在 Issue 中提出你的想法和建议。具体操作可参考 贡献指南

建议阅读 《提问的智慧》《如何向开源社区提问题》《如何有效地报告 Bug》《如何向开源项目提交无法解答的问题》

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyxbrain-2.0.3.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyxbrain-2.0.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pyxbrain-2.0.3.tar.gz.

File metadata

  • Download URL: pyxbrain-2.0.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pyxbrain-2.0.3.tar.gz
Algorithm Hash digest
SHA256 fbe662cb4bf6aa52b9133f1a3095befa92f2fce49d0c32299b1391a10121f452
MD5 3efd1d2e9563cff5ea8453cdb5121985
BLAKE2b-256 ef4f99bef75e642b4aac8c7a5fcdd060b3039464264c43dead34c4e282230c93

See more details on using hashes here.

File details

Details for the file pyxbrain-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyxbrain-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pyxbrain-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bc06280279ed88261349530fac88a25d2603166638c740afe401d47b06926d2a
MD5 564b8b7c7db5796e63e50a84931ee435
BLAKE2b-256 3bbea81ab5725e3fb52b487b9512cf36053a8e018a79a45bbfdd1502c95945ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page