> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Open agent spec 集成

> 使用 LangChain Python 集成 Open agent 规范。

本页面涵盖了与 [Open Agent Specification](https://oracle.github.io/agent-spec) 的所有 LangChain 集成。

Open Agent Spec 是 Oracle 开发的一个框架无关的声明式语言，用于定义智能体系统。它允许以可移植的 JSON/YAML 格式定义智能体和工作流，并在不同的运行时环境中执行。

## 安装和设置

您可以参考 [安装指南](https://oracle.github.io/agent-spec/installation.html) 来安装 `pyagentspec`。

随后，您可以通过其额外依赖项安装 LangGraph 适配器：

```bash theme={null}
pip install "pyagentspec[langgraph]"
```

## LangGraph 适配器

**LangGraph 适配器** 允许使用 LangGraph 实例化和执行 Open Agent Spec 配置。
`AgentSpecLoader` 类负责加载智能体 Spec 配置（JSON 或 YAML 格式），并在 LangGraph 环境中将其实例化为可运行的智能体。它还包括将声明的工具映射到相应的 Python 函数的支持。

以下示例展示了创建一个简单的 Agent Spec 智能体并将其转换为 LangGraph 助手。
从 Agent Spec 智能体创建开始：

```python theme={null}
# Create an 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],
)
```

该智能体可以随后导出为 JSON：

```python theme={null}
# Export the Agent Spec configuration
from pyagentspec.serialization import AgentSpecSerializer

agentspec_config = AgentSpecSerializer().to_json(agentspec_agent)
```

并使用 `AgentSpecLoader` 转换为 LangGraph 助手。
该示例还展示了工具的映射和对话的执行。

```python theme={null}
# 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}")
```

***

> 官方原文：[src/oss/python/integrations/providers/open\_agent\_spec.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/providers/open_agent_spec.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.