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

# 部署其他框架

> 使用 LangGraph 功能 API 将使用 Strands、CrewAI 或其他框架构建的智能体部署到 LangSmith。

本指南展示如何使用 [功能 API](/oss/python/langgraph/functional-api) 在 [LangSmith 部署](/langsmith/deployment) 上部署 [Strands 智能体](https://strandsagents.com/latest/documentation/docs/)，并为 [LangSmith 可观测性](/langsmith/observability) 设置追踪。您可以对 CrewAI、AutoGen、Google ADK 等其他框架采用相同的方法。

使用功能 API 并部署到 LangSmith 部署具有以下优势：

* 生产部署：将您的集成解决方案部署到 [LangSmith 部署](/langsmith/deployment)，实现可扩展的生产使用。
* 增强功能：通过功能 API，您可以将现有智能体与 [持久化](/oss/python/langgraph/persistence)、[流式处理](/langsmith/streaming)、[短期和长期记忆](/oss/python/concepts/memory) 等功能集成，只需对现有代码进行最小改动。
* 多智能体系统：构建 [多智能体系统](/oss/python/langchain/multi-agent)，其中各个智能体使用不同的框架构建。

## 先决条件

* Python 3.9+
* 依赖项：`pip install strands-agents strands-agents-tools langgraph`
* 环境变量中的 AWS 凭证

## 1. 定义 Strands 智能体

使用预构建工具创建一个 Strands 智能体。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from strands import Agent
from strands_tools import file_read, file_write, python_repl, shell, journal

agent = Agent(
        tools=[file_read, file_write, python_repl, shell, journal],
        system_prompt="您是一位专注于 Web 框架的专家级软件开发助手。您的任务是分析项目结构并识别映射关系。",
        model="us.anthropic.claude-sonnet-4-20250514-v1:0",
    )
```

## 2. 使用功能 API 部署到 LangSmith 部署

[功能 API](/oss/python/langgraph/functional-api) 允许您集成和部署 LangChain 以外的框架。功能 API 还提供了额外优势，可以以最小的代码改动，将其他关键功能（持久化、记忆、人在回路、流式处理）与您现有的智能体结合使用。

它使用两个关键构建块：

* **[`@entrypoint`](https://reference.langchain.com/python/langgraph/func/entrypoint)**：将函数标记为工作流的起点，封装逻辑并管理执行流程，包括处理长时间运行的任务和中断。
* **[`@task`](https://reference.langchain.com/python/langgraph/func/task)**：表示一个离散的工作单元，例如 API 调用或数据处理步骤，可以在入口点内异步执行。任务返回一个类似 future 的对象，可以等待或同步解析。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from strands.types.content import Message

from langgraph.func import entrypoint, task
import operator

@task
def invoke_strands(messages: list[Message]):
    # 使用现有消息运行智能体；可以使用 messages[-1] 调用最终消息
    result = agent(messages)
    # 返回结果消息
    return [result.message]

@entrypoint()
def workflow(messages: list[Message], previous: list[Message]):
    messages = operator.add(previous or [], messages)
    response = invoke_strands(messages).result()
    return entrypoint.final(value=response, save=operator.add(messages, response))
```

## 3. 使用 OpenTelemetry 设置追踪

在您的环境变量中设置以下内容：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 关闭 LangSmith 默认追踪，因为我们只想使用 OpenTelemetry 进行追踪
LANGSMITH_TRACING=false

OTEL_EXPORTER_OTLP_ENDPOINT = "https://api.smith.langchain.com/otel/"

OTEL_EXPORTER_OTLP_HEADERS = "x-api-key=您的-langsmith-api-key,Langsmith-Project=您的追踪项目名称"
```

<Note>
  如果您是 [自托管 LangSmith](/langsmith/self-hosted)，请将 `OTEL_EXPORTER_OTLP_ENDPOINT` 端点替换为您的 LangSmith API 端点，并附加 `/api/v1/otel`。例如：`OTEL_EXPORTER_OTLP_ENDPOINT = "https://ai-company.com/api/v1/otel"`
</Note>

<Note>
  Strands 的 OTel 追踪包含同步代码。在这种情况下，您可能需要设置 `BG_JOB_ISOLATED_LOOPS=true`，以便在独立于服务 API 事件循环的隔离事件循环中执行后台运行。
</Note>

在您的主智能体中，设置以下内容：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from strands.telemetry import StrandsTelemetry

strands_telemetry = StrandsTelemetry()
strands_telemetry.setup_otlp_exporter()
strands_telemetry.setup_meter()
```

## 4. 准备部署

至此，要部署到 LangSmith，请创建如下文件结构：

```
my-strands-agent/
├── agent.py          # 您的主智能体代码
├── requirements.txt  # Python 依赖项
└── langgraph.json   # LangGraph 配置
```

要部署您的智能体，请遵循 [部署到云端](/langsmith/deploy-to-cloud) 指南。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\langsmith\deploy-other-frameworks.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
