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 装饰器定义节点,工作流将按 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


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.0.tar.gz (6.6 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.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyxbrain-2.0.0.tar.gz
Algorithm Hash digest
SHA256 502110c36420e9a4739ebd74e28f25a06a6164ef355853f78c2278174d301c41
MD5 fa24424420f82b4b43da91f8561de910
BLAKE2b-256 fdd006a1da214a89e384a384c9d89878cc7f5a320fa67b65f5a5c84f9784cb2b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyxbrain-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de2a50a99a5dfcec3baa8ec27b274bf4618bed928288050aa41bcc5943b18597
MD5 ecfe9d03d1a579a63c72501284dea574
BLAKE2b-256 5a8beee4dba40f2b535f97becf99128b24b463a1395622ea26fefa4f1dfe028b

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