如何启动 CAMEL RemoteHttpRuntime 远程运行服务并通过 HTTP 调用注册的工具?
【免费下载链接】camel🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel
在 CAMEL 中,RemoteHttpRuntime把工具的执行放到一个基于 FastAPI 的 HTTP 服务上:客户端注册好工具后,每次调用工具实际上都是向该服务发起一次 HTTP 请求,而不是在本地进程内直接执行。本文以MathToolkit为例,完整走一遍“注册工具 → 启动 HTTP 服务 → 等待就绪 → 通过 HTTP 调用工具 → 验证结果 → 停止服务”的路径,让工具执行发生在独立的服务进程中,便于隔离和扩展到远程机器。
准备条件
- Python 版本需满足
>=3.10 且 <=3.14,用python3 --version检查。 - 安装 CAMEL:
pip install camel-ai部分功能依赖可选依赖包,如需完整依赖可安装pip install 'camel-ai[all]'。安装后用pip show camel-ai确认。
- 客户端与 API 服务进程都要能
import你注册的模块。示例中使用camel.toolkits内置的MathToolkit,无需额外准备。
启动服务并注册工具
仓库中的可运行脚本是 examples/runtimes/remote_http_runtime.py,核心代码如下:
from camel.runtimes import RemoteHttpRuntime from camel.toolkits import MathToolkit if __name__ == "__main__": runtime = ( RemoteHttpRuntime("localhost") .add(MathToolkit().get_tools(), "camel.toolkits.MathToolkit") .build() ) print("Waiting for runtime to be ready...") runtime.wait() print("Runtime is ready.") add, sub, mul = runtime.get_tools()[:3] print(f"Add 1 + 2: {add.func(1, 2)}") print(f"Subtract 5 - 3: {sub.func(5, 3)}") print(f"Multiply 2 * 3: {mul.func(2, 3)}") print("Documents: ", runtime.docs)各构造参数的含义来自 camel/runtimes/remote_http_runtime.py 的文档字符串:
| 参数 | 默认值 | 说明 |
|---|---|---|
host | 必填 | 远程服务器的地址 |
port | 8000 | 远程服务器的端口 |
python_exec | "python3" | 用于启动 API 服务的 Python 可执行文件 |
add(funcs, entrypoint)的entrypoint是服务进程里通过importlib加载的模块路径,例如camel.toolkits.MathToolkit;它是类路径时,服务端会实例化该 Toolkit 并注册其全部工具。add还支持arguments参数,会以 JSON 形式附加到 entrypoint 后面,用于给 Toolkit 传构造参数(例如arguments={"verbose": True}),参考 examples/runtimes/shared_runtime_multi_toolkit.py。
build()会拉起一个子进程运行服务入口 camel/runtimes/api.py,并通过atexit注册清理逻辑;程序正常结束时服务会自动停止。示例运行方式:
python examples/runtimes/remote_http_runtime.py每次工具调用实际发生了什么
注册完成后,runtime.get_tools()返回的每个FunctionTool的.func都被替换成 HTTP 包装器:调用add.func(1, 2)时,客户端向服务发送请求(请求格式来自客户端实现 camel/runtimes/remote_http_runtime.py):
- 请求:
POST http://{host}:{port}/{工具函数名} - 请求体:
{"args": [...], "kwargs": {...}, "redirect_stdout": false} - 响应体:
{"output": "<工具返回值的 JSON 字符串>"}
也就是说,add.func(1, 2)等价于向http://localhost:8000/add发 POST 请求。服务端的 FastAPI 应用会为每个注册的工具生成一个同名POST /{工具名}端点,工具在独立线程池中执行(见 camel/runtimes/api.py)。同一 entrypoint 对应的 Toolkit 实例在服务进程内有缓存,跨调用保持状态。
如果希望同时拿到工具函数的stdout,add时可传redirect_stdout=True,此时响应会额外包含"stdout"字段,客户端也会打印该输出。
在浏览器或请求工具中直接访问服务也可以核对端点:
http://localhost:8000/docs:FastAPI 文档页,列出全部 API 端点。runtime.docs属性返回这个 URL,脚本中打印的Documents: http://localhost:8000/docs即来源于此(运行期间有效)。GET http://localhost:8000/health:返回{"status": "ok", "toolkits": [...], "endpoints": [...]},可用于确认服务端加载了哪些 Toolkit 和端点。
结果验证
runtime.wait()每隔 1 秒GET一次服务根地址,直到连通或超时(timeout默认 10 秒),返回True/False;不确认返回值时建议显式检查,例如if not runtime.wait(timeout=30)。test/runtimes/test_remote_http_runtime.py 中对调用结果与runtime.docs的断言给出了核对方式。
脚本示例输出(文档示例,来自 examples/runtimes/remote_http_runtime.py 的注释):
Waiting for runtime to be ready... Runtime is ready. Add 1 + 2: 3 Subtract 5 - 3: 2 Multiply 2 * 3: 6 Documents: http://localhost:8000/docs判断标准:
- 打印出
Runtime is ready.,说明服务已就绪; - 三个算式输出
3、2、6,说明工具确实经由 HTTP 在服务进程中执行并返回了正确结果; Documents:指向http://localhost:8000/docs,且此时打开该页面能看到端点列表。
停止服务
- 手动停止:
runtime.stop(),终止 API 服务子进程; - 手动停止后如需重建:
runtime.reset()(先stop()再build()); - 不手动停止时,脚本结束会自动停止服务(
atexit注册的清理逻辑)。
限制与排查
- 服务进程绑定在
0.0.0.0:8000(见 camel/runtimes/api.py 中uvicorn.run的固定参数),客户端构造参数里的port默认为8000,两者默认一致;如需换端口,客户端port参数要与服务实际监听端口保持一致。 - 服务对非 200 响应的处理:客户端会把错误写入日志并返回
{"error": "Failed to execute function: ...", ...},因此调用后收到带error键的 dict 时,应检查服务是否还在运行、端点名是否正确。 - 类文档字符串明确说明:用于真正远程执行时,“You need to run the API server in the remote server first”,即远程机器上要先起好 API 服务,再把
host指向那台机器。 - 服务端加载 entrypoint 失败(
ImportError/AttributeError)时只会记错误日志、对应端点不会出现,此时/health返回的endpoints列表可用来确认哪些工具真正注册成功。 - 相关说明集中在 docs/key_modules/runtimes.md,其中还介绍了
LLMGuardRuntime、DockerRuntime、UbuntuDockerRuntime、DaytonaRuntime等其他运行时,按需查阅即可。
【免费下载链接】camel🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考