From 865a21938c0eeeca76943c57422630675fb309f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dvo=C5=99=C3=A1k?= Date: Wed, 27 Sep 2023 14:29:29 +0200 Subject: [PATCH] speed up enforce_stop_tokens helper function (#10984) **Description:** As long as `enforce_stop_tokens` returns a first occurrence, we can speed up the execution by setting the optional `maxsplit` parameter to 1. Tag maintainer: @agola11 @hwchase17 --------- Co-authored-by: Bagatur --- libs/langchain/langchain/llms/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain/langchain/llms/utils.py b/libs/langchain/langchain/llms/utils.py index 32fdb52eb3..b69c759eea 100644 --- a/libs/langchain/langchain/llms/utils.py +++ b/libs/langchain/langchain/llms/utils.py @@ -5,4 +5,4 @@ from typing import List def enforce_stop_tokens(text: str, stop: List[str]) -> str: """Cut off the text as soon as any stop words occur.""" - return re.split("|".join(stop), text)[0] + return re.split("|".join(stop), text, maxsplit=1)[0]