util

Module Contents

Classes

GeneratorWithReturn

Generator wrapper to capture the return value.

Functions

load_class_from_string(class_path[, path])

create_object([config])

Create an instance based on the configuration where 'type' is a

async_as_completed(futures)

A asynchronous wrapper for asyncio.as_completed

filter_suffix(response[, suffixes])

Filter response with suffixes.

truncate_text(text[, max_num, side])

中英文混合场景下,根据 side 参数截断文本。总共保留 approx max_num 个“词/字”。

lagent.utils.util.load_class_from_string(class_path, path=None)[源代码]
参数:

class_path (str) –

lagent.utils.util.create_object(config=None)[源代码]

Create an instance based on the configuration where ‘type’ is a preserved key to indicate the class (path). When accepting non-dictionary input, the function degenerates to an identity.

参数:

config (Union[Dict, Any]) –

async lagent.utils.util.async_as_completed(futures)[源代码]

A asynchronous wrapper for asyncio.as_completed

参数:

futures (Iterable[asyncio.Future]) –

lagent.utils.util.filter_suffix(response, suffixes=None)[源代码]

Filter response with suffixes.

参数:
  • response (Union[str, List[str]]) – generated responses by LLMs.

  • suffixes (str) – a list of suffixes to be deleted.

返回:

a clean response.

返回类型:

str

lagent.utils.util.truncate_text(text, max_num=4000, side='middle')[源代码]

中英文混合场景下,根据 side 参数截断文本。总共保留 approx max_num 个“词/字”。

定义“单位”逻辑: 1. 连续的英文/数字被视为 1 个单位 (如 “Python”, “123”) 2. 单个汉字或标点被视为 1 个单位 (如 “中”, “。”, “,”)

参数:
  • text (str) – 原始文本

  • max_num (int) – 截取的单位数量

  • side (str) – 截断模式,可选 ‘left’, ‘right’, ‘middle’ ‘left’: 保留尾部(截断头部) ‘right’: 保留头部(截断尾部) ‘middle’: 保留头尾(截断中间)

class lagent.utils.util.GeneratorWithReturn(generator)[源代码]

Generator wrapper to capture the return value.

参数:

generator (Generator) –

generator
ret = None