Skip to main content

FtAi Agent Hub adapter for LangChain Deep Agent

Project description

ftai-deep-agent

LangChain DeepAgent 接入 FtAi Agent Hub 的适配器。

基于 ftai-langchain 构建,流式输出、工具调用、人机交互、自动重连,开箱即用。

如果你使用的是其他 LangChain / LangGraph 用法(非 DeepAgent),请直接使用 ftai-langchain

安装

uv add ftai-deep-agent

快速开始

1. 配置环境变量

创建 .env 文件:

AGENT_SECRET=sk-ftai-ag-xxxxx
# AGENT_HUB_URL=wss://ftai.chat/api/v1/completion/external-agents/ws  # 可选,有默认值

AGENT_SECRET 由 FtAi Agent Hub 管理后台生成,格式以 sk-ftai-ag- 开头。

2. 编写 Agent

import asyncio
import json
import os

import httpx
from dotenv import load_dotenv
from deepagents import create_deep_agent
from ftai_deep_agent import AgentHubClient

load_dotenv()


async def get_ip_info(ip: str) -> str:
    """查询 IP 地址的地理位置信息。

    Args:
        ip: 要查询的 IP 地址,如 "8.8.8.8"。
    """
    async with httpx.AsyncClient() as client:
        resp = await client.get(f"https://ipinfo.io/{ip}/json")
        return json.dumps(resp.json(), ensure_ascii=False)


client = AgentHubClient(
    secret=os.environ["AGENT_SECRET"],
)

agent = create_deep_agent(
    model="claude-sonnet-4-6",
    tools=[get_ip_info],
    system_prompt="你是一个网络工具助手。",
)


async def main() -> None:
    await client.run(agent)


if __name__ == "__main__":
    asyncio.run(main())
uv run python main.py

工作流程

用户 ──> FtAi Agent Hub ──WebSocket──> AgentHubClient ──> DeepAgent (LLM)
                                         │
                                         ├── stream_text      (流式文本)
                                         ├── stream_thinking  (思考过程)
                                         ├── tool_call        (工具调用)
                                         └── message_end      (完成)

API 参考

AgentHubClient

from ftai_deep_agent import AgentHubClient

client = AgentHubClient(
    secret="sk-ftai-ag-...",        # Agent 密钥(必填)
    # agent_hub_url="wss://...",    # 可选,默认读取 AGENT_HUB_URL 环境变量或内置默认地址
    reconnect_initial=2.0,          # 重连初始间隔(秒)
    reconnect_max=60.0,             # 重连最大间隔(秒)
)
属性 / 方法 说明
client.agent_id 认证成功后的 Agent ID(只读)
client.human_in_loop_tool 人机交互工具,加入 tools 列表即可
await client.run(agent) 连接网关并处理请求(阻塞,自动重连)
await client.stop() 优雅关闭连接

重连策略

场景 行为
连接断开 指数退避重连(2s → 4s → 8s ... 最大 60s)
关闭码 4000(被新连接替换) 立即重连,不退避
认证成功 退避间隔重置为初始值
认证失败 抛出 AuthError,不重试

编写工具

工具就是带有 docstring类型注解 的普通 Python 函数。

推荐使用 async def(SDK 运行在 async 事件循环中,同步 I/O 会阻塞整个 Agent):

async def search_docs(query: str, top_k: int = 5) -> str:
    """从知识库中搜索相关文档。

    Args:
        query: 搜索关键词。
        top_k: 返回结果数量,默认 5。
    """
    async with httpx.AsyncClient() as client:
        resp = await client.get(
            "https://your-api.example.com/search",
            params={"q": query, "limit": top_k},
        )
        return resp.text

示例

# 天气查询 Agent
uv run python examples/weather_agent/main.py

# IP 查询 Agent
uv run python examples/ip_info_agent/main.py

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

ftai_deep_agent-0.3.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ftai_deep_agent-0.3.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file ftai_deep_agent-0.3.1.tar.gz.

File metadata

  • Download URL: ftai_deep_agent-0.3.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ftai_deep_agent-0.3.1.tar.gz
Algorithm Hash digest
SHA256 6952cd81a198ed6622b625f7cb96411608236e808d928c04851208a2f20c102a
MD5 784a423bb9d1b333c7cdafec8ef851a4
BLAKE2b-256 e5dd879ce983083c7c52284ba5962d0fd2502f024aa8cf318e21de128730a985

See more details on using hashes here.

File details

Details for the file ftai_deep_agent-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: ftai_deep_agent-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ftai_deep_agent-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b1a735709d9e0e67df949269a0e0f2fc486ea13b906a600ed2bf4f4ff7e4014d
MD5 406b584c52f82acdb25c04decbe94c95
BLAKE2b-256 42c3b9f87c526f7690b1093e2179bf78e86dd1e5c22e0b0692709a4a7f0fa00d

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