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.2.tar.gz (7.0 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.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyxbrain-2.0.2.tar.gz
  • Upload date:
  • Size: 7.0 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.2.tar.gz
Algorithm Hash digest
SHA256 f30764d3075aa446a7205c3ec0adb139574e57528b28f33db38546398ec9e603
MD5 308437316b04dce3b53c2ed5cecd194e
BLAKE2b-256 cbd5904c37ffaba6da813b817b481709d6638aa0a3cc8bb132755cb21f6fb338

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyxbrain-2.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 93b515dd1287b10750748083fd2266778abf8921d393e97d41495d8d3d0944b3
MD5 c1c992c5f91b00a10fc675768839d070
BLAKE2b-256 d6fdf0a4c93daa8d6eb6ef303394d2cf2a08a68688c51a3329460492ccb23a11

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