插件(自定义 / 外部)#

两种 L2 适配器类型在启动时从 用户提供 的 Python 模块加载适配器类,因此您可以将 LMCache 指向您自己的存储后端,而无需修改 LMCache 源代码。这是 vLLM 的 kv_connector_module_path--l2-adapter 类似项。

  • plugin: 加载一个实现 ``L2AdapterInterface``(完整的 L2 适配器契约)的纯 Python 类。

  • native_plugin: 加载一个 pybind 封装的 C++ 连接器,暴露一个六方法异步批处理契约,并将其包装在 NativeConnectorL2Adapter 中。请将其用于本地后端 -- 另见 添加原生后端

plugin#

动态导入 module_path 并实例化 class_name,该类必须是 L2AdapterInterface 的子类(在加载时进行验证;不匹配会引发 TypeError)。

必填字段:

  • module_path (str): 包含适配器类的模块的点分 Python 导入路径。该模块必须可以被 LMCache 进程导入(已安装或在 PYTHONPATH 中)。

  • class_name (str): module_path 中实现 L2AdapterInterface 的类的名称。

可选字段:

  • adapter_params (字典,默认 {}): 转发给适配器构造函数的任意字典。

  • config_class_name (str): 在 module_path 中的配置类名称,该类是 L2AdapterConfigBase 的子类。当设置(或自动发现)时,工厂通过 from_dict(adapter_params) 构建它,并将配置对象(而不是原始的 adapter_params 字典)传递给适配器构造函数,以匹配内置适配器的约定。省略时的发现顺序:模块中的 <class_name>Config,然后是适配器类上的 config_class_name 属性;否则传递原始字典。

配置示例:

# Raw-dict mode: adapter_params is passed straight to the constructor
--l2-adapter '{"type": "plugin", "module_path": "my_plugin.l2", "class_name": "MyL2Adapter", "adapter_params": {"host": "localhost"}}'

# Config-class mode: my_plugin.l2.MyL2AdapterConfig.from_dict(adapter_params) is built and passed
--l2-adapter '{"type": "plugin", "module_path": "my_plugin.l2", "class_name": "MyL2Adapter", "config_class_name": "MyL2AdapterConfig", "adapter_params": {"host": "localhost"}}'

请参阅 examples/lmc_external_l2_adapter/ 以获取完整的参考插件。

native_plugin#

动态导入 module_path,使用 adapter_params 作为构造函数关键字参数实例化 class_name,验证实例是否暴露所需的异步批处理方法(event_fdsubmit_batch_getsubmit_batch_setsubmit_batch_existsdrain_completionsclose),并将其包装在 NativeConnectorL2Adapter 中。可选的 submit_batch_delete 允许 L2 逐出删除;如果没有它,删除操作将无效(记录为警告)。

必填字段:

  • module_path (str): 包含连接器类的模块的点分 Python 导入路径。

  • class_name (str): module_path 中连接器类的名称。

可选字段:

  • adapter_params (dict, default {}): 作为关键字参数传递给连接器构造函数。

  • max_capacity_gb (float, default 0): 用于使用跟踪/逐出的 GB 级别的 L2 总容量。 0 禁用总逐出。

配置示例:

# Native pybind connector with constructor kwargs
--l2-adapter '{"type": "native_plugin", "module_path": "my_ext.connector", "class_name": "MyConnectorClient", "adapter_params": {"host": "localhost", "port": 1234}}'

请参阅 examples/lmc_external_native_connector/ 以获取完整的参考本地连接器。