以编程方式使用文档

本页涵盖所有与 Open Agent Specification.

Open Agent Spec is a framework-agnostic declarative language from Oracle for defining agentic systems. It allows to define agents and workflows in a portable JSON/YAML format that can be executed across different runtimes.

安装和设置

您可以参阅 安装指南 进行安装 pyagentspec.

您可以通过其额外依赖项安装 LangGraph Adapter:

pip install "pyagentspec[langgraph]"

## LangGraph Adapter **LangGraph Adapter** 允许使用 LangGraph 实例化和执行 Open Agent Spec 配置。 AgentSpecLoader 类负责加载 Agent Spec 配置(JSON 或 YAML 格式)并将其实例化为 LangGraph 环境中的可运行代理。它还包括将声明的工具映射到相应 Python 函数的支持。

以下示例展示了一个简单 Agent Spec Agent 的创建及其转换为 LangGraph 助手的过程。 从 Agent Spec Agent 创建开始:

# Create a Agent Spec agent
from pyagentspec.agent import Agent
from pyagentspec.llms.openaicompatibleconfig import OpenAiCompatibleConfig
from pyagentspec.property import FloatProperty
from pyagentspec.tools import ServerTool

subtraction_tool = ServerTool(
    name="subtraction-tool",
    description="subtract two numbers together",
    inputs=[FloatProperty(title="a"), FloatProperty(title="b")],
    outputs=[FloatProperty(title="difference")],
)

agentspec_llm_config = OpenAiCompatibleConfig(
    name="llama-3.3-70b-instruct",
    model_id="/storage/models/Llama-3.3-70B-Instruct",
    url="url.to.my.llm",
)

agentspec_agent = Agent(
    name="agentspec_tools_test",
    description="agentspec_tools_test",
    llm_config=agentspec_llm_config,
    system_prompt="Perform subtraction with the given tool.",
    tools=[subtraction_tool],
)

该 Agent 随后可以导出为 JSON:

# Export the Agent Spec configuration
from pyagentspec.serialization import AgentSpecSerializer

agentspec_config = AgentSpecSerializer().to_json(agentspec_agent)

并使用 AgentSpecLoader. 该示例还展示了工具的映射和对话的执行。

# Load and run the Agent Spec configuration with LangGraph
from pyagentspec.adapters.langgraph import AgentSpecLoader

def subtract(a: float, b: float) -> float:
    return a - b

async def main():
    loader = AgentSpecLoader(tool_registry={"subtraction-tool": subtract})
    assistant = loader.load_json(agentspec_config)

    while True:
        user_input = input("USER >> ")
        if user_input == "exit":
            break
        result = await assistant.ainvoke(
            input={"messages": [{"role": "user", "content": user_input}]},
        )
        print(f"AGENT >> {result['messages'][-1].content}")