camel.runtime 包

本页内容

camel.runtime 包#

子包#

子模块#

camel.runtime.api 模块#

camel.runtime.base 模块#

class camel.runtime.base.BaseRuntime[来源]#

基类: ABC

所有CAMEL运行时的抽象基类。

abstract add(funcs: FunctionTool | List[FunctionTool], *args: Any, **kwargs: Any) BaseRuntime[来源]#

向运行时添加一个新工具。

get_tools() List[FunctionTool][来源]#

返回运行时中的所有工具列表。

abstract reset(*args: Any, **kwargs: Any) Any[来源]#

将运行时重置为其初始状态。

camel.runtime.configs 模块#

class camel.runtime.configs.TaskConfig(*, cmd: str | List[str], stdout: bool = True, stderr: bool = True, stdin: bool = False, tty: bool = False, privileged: bool = False, user: str = '', detach: bool = False, stream: bool = False, socket: bool = False, environment: Dict[str, str] | List[str] | None = None, workdir: str | None = None, demux: bool = False)[来源]#

基类:BaseModel

用于在容器内运行命令的任务配置。

Arttributes:

cmd (str or list): 要执行的命令 stdout (bool): 附加到标准输出。(默认: :obj: True) stderr (bool): 附加到标准错误。(默认: :obj: True) stdin (bool): 附加到标准输入。(默认: :obj: False) tty (bool): 分配一个伪终端。(默认: :obj: False) privileged (bool): 以特权模式运行。(默认: :obj: False) user (str): 执行命令的用户。(默认: :obj: “”) detach (bool): 如果为true,则从执行命令中分离。

(默认值: :obj: False)

stream (bool): 流式传输响应数据。(默认: :obj: False) socket (bool): 返回连接套接字以允许自定义

读/写操作。(默认: :obj: False)

environment (dict or list): A dictionary or a list of strings in

以下格式 ["PASSWORD=xxx"]{"PASSWORD": "xxx"}。 (默认值: :obj: None)

workdir (str): Path to working directory for this exec session.

(默认值: :obj: None)

demux (bool): Return stdout and stderr separately. (default: :obj:

)

cmd: str | List[str]#
demux: bool#
detach: bool#
environment: Dict[str, str] | List[str] | None#
model_config: ClassVar[ConfigDict] = {}#

模型的配置,应该是一个符合[ConfigDict][pydantic.config.ConfigDict]的字典。

privileged: bool#
socket: bool#
stderr: bool#
stdin: bool#
stdout: bool#
stream: bool#
tty: bool#
user: str#
workdir: str | None#

camel.runtime.docker_runtime 模块#

class camel.runtime.docker_runtime.DockerRuntime(image: str, port: int = 8000, remove: bool = True, **kwargs)[来源]#

基类:BaseRuntime

一个表示使用Docker运行时环境的类。 该类会自动封装要在Docker容器中执行的函数。

Parameters:
  • image (str) – 用于运行时的Docker镜像名称。

  • port (int) – 用于运行时API的端口号。(默认: :obj: 8000)

  • remove (bool) – 是否在停止容器后将其移除。(默认: :obj: True)

  • kwargs (dict) - 传递给Docker客户端的额外关键字参数。

add(funcs: FunctionTool | List[FunctionTool], entrypoint: str, redirect_stdout: bool = False, arguments: Dict[str, Any] | None = None) DockerRuntime[来源]#

向运行时添加一个函数或函数列表。

Parameters:
  • funcs (Union[FunctionTool, List[FunctionTool]]) – 要添加的函数或函数列表。

  • entrypoint (str) – 函数的入口点。

  • redirect_stdout (bool) - 是否返回函数的stdout输出。(默认值: :obj: False)

  • arguments (Optional[Dict[str, Any]]) – 函数的参数。(默认值: :obj: None)

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

add_task(task: 任务配置) DockerRuntime[来源]#

添加一个任务,在构建时在容器内运行命令。 类似于docker exec

Parameters:

任务 (TaskConfig) – 任务的配置。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

build(time_out: int = 15) DockerRuntime[来源]#

构建Docker容器并启动它。

Parameters:

time_out (int) – 等待容器启动的秒数。(默认: :obj: 15)

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

copy(source: str, dest: str) DockerRuntime[来源]#

将文件或目录复制到容器中。

Parameters:
  • source (str) - 文件的本地路径。

  • dest (str) - 容器中要复制文件的目标路径。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

property docs: str#

获取API文档的URL。

Returns:

API文档的URL。

Return type:

字符串

exec_run(task: TaskConfig) Any[来源]#

在这个容器内运行一个命令。类似于docker exec

Parameters:

任务 (TaskConfig) – 任务的配置。

Returns:

一个包含(exit_code, output)的元组
exit_code: (int):

执行命令的退出码,如果streamsocketTrue则为None

output: (生成器, 字节, 或元组):

如果stream=True,则是一个生成响应块的生成器。 如果socket=True,则是连接的套接字对象。 如果demux=True,则是由stdout和stderr组成的字节元组。 其他情况下是包含响应数据的字节串。

Return type:

(执行结果)

Raises:

运行时错误 – 如果容器不存在。

mount(path: str, mount_path: str) DockerRuntime[来源]#

将本地目录挂载到容器中。

Parameters:
  • path (str) – 要挂载的本地路径。

  • mount_path (str) - 容器中挂载本地目录的路径。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

property ok: bool#

检查API服务器是否正在运行。

Returns:

API服务器是否正在运行。

Return type:

布尔值

reset() DockerRuntime[来源]#

重置DockerRuntime实例。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

stop(remove: bool | None = None) DockerRuntime[来源]#

停止Docker容器。

Parameters:

remove (可选[bool]) – 是否在停止容器后将其移除。(默认: :obj: None)

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

wait(timeout: int = 10) bool[来源]#

等待API服务器准备就绪。

Parameters:

timeout (int) – 等待的秒数。(默认: :obj: 10)

Returns:

API服务器是否已准备就绪。

Return type:

布尔值

camel.runtime.llm_guard_runtime 模块#

class camel.runtime.llm_guard_runtime.LLMGuardRuntime(prompt: str = "You are a function safety evaluator tasked with assessing the \npotential risk level of a given function based on both its description \nand parameters. Your goal is to determine if the function may pose any \nharm to the user's environment, such as deleting files, executing \narbitrary code, or accessing sensitive information. Pay special attention \nto the provided parameters even if a function has the potential to be \ndangerous, its actual parameters may indicate harmless behavior, and the \nrisk level should be adjusted accordingly. Use the `function_risk` tool to \nassign a risk score to the function, based on the following criteria:\n\n- **Score 1**: No harm. This includes simple operations like mathematical \n    calculations, content searches, or data retrievals that do not impact \n    the user's environment or access sensitive resources. This also \n    includes functions with potentially dangerous capabilities that have \n    harmless, controlled parameters that ensure safe execution.\n- **Score 2**: Minimal harm. The function might read user files, retrieve \n    non-sensitive data, or access other low-risk resources, \n    posing little risk to the user.\n- **Score 3**: Risk present. The function might delete files, modify the \n    file system, execute arbitrary code, or access sensitive data, which \n    could negatively impact the user's environment. However, if the \n    actual parameters provided clearly indicate safe and restricted \n    usage, this risk should be downgraded accordingly.\n\nWhen evaluating, always consider both the function's description and its \nspecific parameters. If the function appears risky due to its design but \nthe provided parameters indicate a safe and non-impactful operation, \nadjust the risk score to reflect this. Assign an appropriate risk score \nand provide a brief explanation of your reasoning based on the function's \ndescription and the actual parameters given.\nYOU MUST USE THE `function_risk` TOOL TO ASSESS THE RISK \nLEVEL OF EACH FUNCTION.\n", model: BaseModelBackend | None = None, verbose: bool = False)[来源]#

基类:BaseRuntime

一个运行时环境,用于通过语言模型评估函数的风险级别。

Parameters:
  • prompt (str) – 用于语言模型的提示词。(默认值: GUARDPROMPT)

  • model (BaseModelBackend) – 要使用的语言模型。(默认: :obj: None)

  • verbose (bool) - 是否打印详细输出。(默认: :obj: False)

add(funcs: FunctionTool | List[FunctionTool], threshold: int = 2) LLMGuardRuntime[来源]#

向运行时添加一个函数或函数列表。

Parameters:
  • funcs (FunctionToolList[FunctionTool]) - 要添加的函数或函数列表。

  • threshold (int) – 函数的风险阈值。 (默认值: 2)

Returns:

当前运行时间。

Return type:

LLMGuardRuntime

reset() LLMGuardRuntime[来源]#

将运行时重置为其初始状态。

camel.runtime.remote_http_runtime 模块#

class camel.runtime.remote_http_runtime.RemoteHttpRuntime(host: str, port: int = 8000, python_exec: str = 'python3')[来源]#

基类:BaseRuntime

一个在远程HTTP服务器上运行函数的运行时环境。 您需要先在远程服务器上运行API服务器。

Parameters:
  • host (str) – 远程服务器的主机地址。

  • port (int) – 远程服务器的端口号。(默认值: :obj: 8000)

  • python_exec (str) – 用于运行API服务器的Python可执行文件。 (默认: :obj: python3)

add(funcs: FunctionTool | List[FunctionTool], entrypoint: str, redirect_stdout: bool = False, arguments: Dict[str, Any] | None = None) RemoteHttpRuntime[来源]#

向运行时添加一个函数或函数列表。

Parameters:
  • funcs (Union[FunctionTool, List[FunctionTool]]) – 要添加的函数或函数列表。

  • entrypoint (str) – 函数的入口点。

  • redirect_stdout (bool) - 是否返回函数的stdout输出。(默认值: :obj: False)

  • arguments (Optional[Dict[str, Any]]) – 函数的参数。(默认值: :obj: None)

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

build() RemoteHttpRuntime[来源]#

构建API服务器。

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

property docs: str#

获取API文档的URL。

Returns:

API文档的URL。

Return type:

字符串

property ok: bool#

检查API服务器是否正在运行。

Returns:

API服务器是否正在运行。

Return type:

布尔值

reset() RemoteHttpRuntime[来源]#

重置API服务器。

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

stop() RemoteHttpRuntime[来源]#

停止API服务器。

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

wait(timeout: int = 10) bool[来源]#

等待API服务器准备就绪。

Parameters:

timeout (int) – 等待的秒数。(默认: :obj: 10)

Returns:

API服务器是否已准备就绪。

Return type:

布尔值

模块内容#

class camel.runtime.BaseRuntime[来源]#

基类: ABC

所有CAMEL运行时的抽象基类。

abstract add(funcs: FunctionTool | List[FunctionTool], *args: Any, **kwargs: Any) BaseRuntime[来源]#

向运行时添加一个新工具。

get_tools() List[FunctionTool][来源]#

返回运行时中的所有工具列表。

abstract reset(*args: Any, **kwargs: Any) Any[来源]#

将运行时重置为其初始状态。

class camel.runtime.DockerRuntime(image: str, port: int = 8000, remove: bool = True, **kwargs)[来源]#

基类:BaseRuntime

一个表示使用Docker运行时环境的类。 该类会自动封装要在Docker容器中执行的函数。

Parameters:
  • image (str) – 用于运行时的Docker镜像名称。

  • port (int) – 用于运行时API的端口号。(默认: :obj: 8000)

  • remove (bool) – 是否在停止容器后将其移除。(默认: :obj: True)

  • kwargs (dict) - 传递给Docker客户端的额外关键字参数。

add(funcs: FunctionTool | List[FunctionTool], entrypoint: str, redirect_stdout: bool = False, arguments: Dict[str, Any] | None = None) DockerRuntime[来源]#

向运行时添加一个函数或函数列表。

Parameters:
  • funcs (Union[FunctionTool, List[FunctionTool]]) – 要添加的函数或函数列表。

  • entrypoint (str) – 函数的入口点。

  • redirect_stdout (bool) - 是否返回函数的stdout输出。(默认值: :obj: False)

  • arguments (Optional[Dict[str, Any]]) – 函数的参数。(默认值: :obj: None)

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

add_task(task: 任务配置) DockerRuntime[来源]#

添加一个任务,在构建时在容器内运行命令。 类似于docker exec

Parameters:

任务 (TaskConfig) – 任务的配置。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

build(time_out: int = 15) DockerRuntime[来源]#

构建Docker容器并启动它。

Parameters:

time_out (int) – 等待容器启动的秒数。(默认: :obj: 15)

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

copy(source: str, dest: str) DockerRuntime[来源]#

将文件或目录复制到容器中。

Parameters:
  • source (str) - 文件的本地路径。

  • dest (str) - 容器中要复制文件的目标路径。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

property docs: str#

获取API文档的URL。

Returns:

API文档的URL。

Return type:

字符串

exec_run(task: 任务配置) Any[来源]#

在这个容器内运行一个命令。类似于docker exec

Parameters:

任务 (TaskConfig) – 任务的配置。

Returns:

一个包含(exit_code, output)的元组
exit_code: (int):

执行命令的退出码,如果streamsocketTrue则为None

output: (生成器, 字节, 或元组):

如果stream=True,则是一个生成响应块的生成器。 如果socket=True,则是一个连接用的套接字对象。 如果demux=True,则是一个包含两个字节的元组:stdout和stderr。 否则是一个包含响应数据的字节串。

Return type:

(执行结果)

Raises:

运行时错误 – 如果容器不存在。

mount(path: str, mount_path: str) DockerRuntime[来源]#

将本地目录挂载到容器中。

Parameters:
  • path (str) – 要挂载的本地路径。

  • mount_path (str) - 容器中挂载本地目录的路径。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

property ok: bool#

检查API服务器是否正在运行。

Returns:

API服务器是否正在运行。

Return type:

布尔值

reset() DockerRuntime[来源]#

重置DockerRuntime实例。

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

stop(remove: bool | None = None) DockerRuntime[来源]#

停止Docker容器。

Parameters:

remove (可选[bool]) – 是否在停止容器后将其移除。(默认: :obj: None)

Returns:

DockerRuntime 实例。

Return type:

DockerRuntime

wait(timeout: int = 10) bool[来源]#

等待API服务器准备就绪。

Parameters:

timeout (int) – 等待的秒数。(默认: :obj: 10)

Returns:

API服务器是否已准备就绪。

Return type:

布尔值

class camel.runtime.LLMGuardRuntime(prompt: str = "You are a function safety evaluator tasked with assessing the \npotential risk level of a given function based on both its description \nand parameters. Your goal is to determine if the function may pose any \nharm to the user's environment, such as deleting files, executing \narbitrary code, or accessing sensitive information. Pay special attention \nto the provided parameters even if a function has the potential to be \ndangerous, its actual parameters may indicate harmless behavior, and the \nrisk level should be adjusted accordingly. Use the `function_risk` tool to \nassign a risk score to the function, based on the following criteria:\n\n- **Score 1**: No harm. This includes simple operations like mathematical \n    calculations, content searches, or data retrievals that do not impact \n    the user's environment or access sensitive resources. This also \n    includes functions with potentially dangerous capabilities that have \n    harmless, controlled parameters that ensure safe execution.\n- **Score 2**: Minimal harm. The function might read user files, retrieve \n    non-sensitive data, or access other low-risk resources, \n    posing little risk to the user.\n- **Score 3**: Risk present. The function might delete files, modify the \n    file system, execute arbitrary code, or access sensitive data, which \n    could negatively impact the user's environment. However, if the \n    actual parameters provided clearly indicate safe and restricted \n    usage, this risk should be downgraded accordingly.\n\nWhen evaluating, always consider both the function's description and its \nspecific parameters. If the function appears risky due to its design but \nthe provided parameters indicate a safe and non-impactful operation, \nadjust the risk score to reflect this. Assign an appropriate risk score \nand provide a brief explanation of your reasoning based on the function's \ndescription and the actual parameters given.\nYOU MUST USE THE `function_risk` TOOL TO ASSESS THE RISK \nLEVEL OF EACH FUNCTION.\n", model: BaseModelBackend | None = None, verbose: bool = False)[来源]#

基类:BaseRuntime

一个运行时环境,用于通过语言模型评估函数的风险级别。

Parameters:
  • prompt (str) – 用于语言模型的提示词。(默认值: GUARDPROMPT)

  • model (BaseModelBackend) – 要使用的语言模型。(默认: :obj: None)

  • verbose (bool) – 是否打印详细输出。(默认: :obj: False)

add(funcs: FunctionTool | List[FunctionTool], threshold: int = 2) LLMGuardRuntime[来源]#

向运行时添加一个函数或函数列表。

Parameters:
  • funcs (FunctionToolList[FunctionTool]) - 要添加的函数或函数列表。

  • threshold (int) – 函数的风险阈值。 (默认值: 2)

Returns:

当前运行时间。

Return type:

LLMGuardRuntime

reset() LLMGuardRuntime[来源]#

将运行时重置为其初始状态。

class camel.runtime.RemoteHttpRuntime(host: str, port: int = 8000, python_exec: str = 'python3')[来源]#

基类:BaseRuntime

一个在远程HTTP服务器上运行函数的运行时环境。 您需要先在远程服务器上运行API服务器。

Parameters:
  • host (str) – 远程服务器的主机地址。

  • port (int) – 远程服务器的端口号。(默认值: :obj: 8000)

  • python_exec (str) – 用于运行API服务器的Python可执行文件。 (默认值: :obj: python3)

add(funcs: FunctionTool | List[FunctionTool], entrypoint: str, redirect_stdout: bool = False, arguments: Dict[str, Any] | None = None) RemoteHttpRuntime[来源]#

向运行时添加一个函数或函数列表。

Parameters:
  • funcs (Union[FunctionTool, List[FunctionTool]]) – 要添加的函数或函数列表。

  • entrypoint (str) – 函数的入口点。

  • redirect_stdout (bool) - 是否返回函数的stdout输出。(默认值: :obj: False)

  • arguments (Optional[Dict[str, Any]]) – 函数的参数。(默认值: :obj: None)

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

build() RemoteHttpRuntime[来源]#

构建API服务器。

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

property docs: str#

获取API文档的URL。

Returns:

API文档的URL。

Return type:

字符串

property ok: bool#

检查API服务器是否正在运行。

Returns:

API服务器是否正在运行。

Return type:

布尔值

reset() RemoteHttpRuntime[来源]#

重置API服务器。

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

stop() RemoteHttpRuntime[来源]#

停止API服务器。

Returns:

当前运行时间。

Return type:

RemoteHttpRuntime

wait(timeout: int = 10) bool[来源]#

等待API服务器准备就绪。

Parameters:

timeout (int) – 等待的秒数。(默认: :obj: 10)

Returns:

API服务器是否已准备就绪。

Return type:

布尔值

class camel.runtime.TaskConfig(*, cmd: str | List[str], stdout: bool = True, stderr: bool = True, stdin: bool = False, tty: bool = False, privileged: bool = False, user: str = '', detach: bool = False, stream: bool = False, socket: bool = False, environment: Dict[str, str] | List[str] | None = None, workdir: str | None = None, demux: bool = False)[来源]#

基类:BaseModel

用于在容器内运行命令的任务配置。

Arttributes:

cmd (str or list): 要执行的命令 stdout (bool): 附加到标准输出。(默认: :obj: True) stderr (bool): 附加到标准错误。(默认: :obj: True) stdin (bool): 附加到标准输入。(默认: :obj: False) tty (bool): 分配一个伪终端。(默认: :obj: False) privileged (bool): 以特权模式运行。(默认: :obj: False) user (str): 执行命令的用户。(默认: :obj: “”) detach (bool): 如果为true,则从执行命令中分离。

(默认值: :obj: False)

stream (bool): 流式传输响应数据。(默认: :obj: False) socket (bool): 返回连接套接字以允许自定义

读/写操作。(默认: :obj: False)

environment (dict or list): A dictionary or a list of strings in

以下格式 ["PASSWORD=xxx"]{"PASSWORD": "xxx"}。 (默认值: :obj: None)

workdir (str): Path to working directory for this exec session.

(默认值: :obj: None)

demux (bool): Return stdout and stderr separately. (default: :obj:

)

cmd: str | List[str]#
demux: bool#
detach: bool#
environment: Dict[str, str] | List[str] | None#
model_config: ClassVar[ConfigDict] = {}#

模型的配置,应该是一个符合[ConfigDict][pydantic.config.ConfigDict]的字典。

privileged: bool#
socket: bool#
stderr: bool#
stdin: bool#
stdout: bool#
stream: bool#
tty: bool#
user: str#
workdir: str | None#
class camel.runtime.UbuntuDockerRuntime(image: str, port: int = 0, remove: bool = True, python_path: str = '/usr/bin/python3', **kwargs)[来源]#

基类:DockerRuntime

专为基于Ubuntu环境设计的Docker运行时。

此运行时环境包含针对Ubuntu容器的特定配置和设置,包括正确的Python路径处理和环境设置。它提供了执行Python文件、管理容器生命周期以及在Ubuntu容器内处理文件操作的方法。

python_path#

容器中Python解释器的路径

Type:

字符串

docker_config#

Docker容器设置的配置字典

Type:

字典

add(funcs: FunctionTool | List[FunctionTool], entrypoint: str, redirect_stdout: bool = False, arguments: dict | None = None) UbuntuDockerRuntime[来源]#

添加针对Ubuntu系统特定修改的功能到运行时环境中。

Parameters:
  • funcs - 要添加到运行时的函数

  • entrypoint – 函数执行的入口点

  • redirect_stdout – 是否重定向标准输出

  • arguments – 函数执行的可选参数

Returns:

用于方法链式调用的Self

build(time_out: int = 15) UbuntuDockerRuntime[来源]#

构建并初始化Ubuntu容器,进行正确配置。

Parameters:

time_out (int) - 构建操作的超时时间(秒)

Returns:

用于方法链的Self

exec_python_file(local_file_path: str, container_path: str | None = None, args: List[str] | None = None, env: dict | None = None, callback: Callable[[str], None] | None = None) None[来源]#

在Docker容器内执行Python文件。

Parameters:
  • local_file_path – 本地文件系统中Python文件的路径

  • container_path – 文件应该被复制到的容器路径

  • (容器 如果)

  • /tmp/ (文件将被复制到)

  • args – 传递给Python脚本的命令行参数列表

  • env – 为执行设置额外的环境变量

  • callback – 可选函数,用于处理每行输出 如果为None,输出将打印到stdout

Raises:
  • RuntimeError - 如果容器未在运行状态

  • FileNotFoundError - 如果找不到Python文件