From bc5a7c0111c9e8a8a0ef590ff11593fe81287dd7 Mon Sep 17 00:00:00 2001 From: John Walz Date: Tue, 11 Feb 2025 12:41:30 -0500 Subject: [PATCH 1/4] fix: context length issues --- validmind/ai/test_descriptions.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/validmind/ai/test_descriptions.py b/validmind/ai/test_descriptions.py index b52bc3820..fefa978fd 100644 --- a/validmind/ai/test_descriptions.py +++ b/validmind/ai/test_descriptions.py @@ -7,6 +7,8 @@ from concurrent.futures import ThreadPoolExecutor from typing import List, Optional, Union +import tiktoken + from ..client_config import client_config from ..logging import get_logger from ..utils import NumpyEncoder, md_to_html, test_id_to_name @@ -35,6 +37,25 @@ def _get_llm_global_context(): return context if context_enabled and context else None +def _truncate_summary(summary: str, max_tokens: int = 100_000): + if len(summary) < max_tokens: + # since string itself is less than max_tokens, definitely small enough + return summary + + # TODO: better context length handling + encoding = tiktoken.encoding_for_model("gpt-4o") + summary_tokens = encoding.encode(summary) + + if len(summary_tokens) > max_tokens: + summary = ( + encoding.decode(summary_tokens[:max_tokens]) + + "...[truncated]" + + encoding.decode(summary_tokens[-100:]) + ) + + return summary + + def generate_description( test_id: str, test_description: str, @@ -107,7 +128,13 @@ def wrapped(): title=title, ) except Exception as e: - logger.error(f"Failed to generate description: {e}") + if "maximum context length" in str(e): + logger.warning( + f"Test result {test_id} is too large to generate a description" + ) + else: + logger.warning(f"Failed to generate description for {test_id}: {e}") + logger.warning(f"Using default description for {test_id}") return test_description From e140d7f60933399bf0f60109b35a0f88fc83acc4 Mon Sep 17 00:00:00 2001 From: John Walz Date: Tue, 11 Feb 2025 12:42:10 -0500 Subject: [PATCH 2/4] 2.8.10 --- pyproject.toml | 2 +- validmind/__version__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c42db1a9e..3a5b25455 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ description = "ValidMind Library" license = "Commercial License" name = "validmind" readme = "README.pypi.md" -version = "2.8.9" +version = "2.8.10" [tool.poetry.dependencies] python = ">=3.8.1,<3.12" diff --git a/validmind/__version__.py b/validmind/__version__.py index e71de367e..71322b8ca 100644 --- a/validmind/__version__.py +++ b/validmind/__version__.py @@ -1 +1 @@ -__version__ = "2.8.9" +__version__ = "2.8.10" From c4d65dd5c39e045cfcdd85a160a1b10872eab925 Mon Sep 17 00:00:00 2001 From: John Walz Date: Tue, 11 Feb 2025 13:00:22 -0500 Subject: [PATCH 3/4] chore: adding warning when truncating test result for llm --- validmind/ai/test_descriptions.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/validmind/ai/test_descriptions.py b/validmind/ai/test_descriptions.py index fefa978fd..10ff33fe7 100644 --- a/validmind/ai/test_descriptions.py +++ b/validmind/ai/test_descriptions.py @@ -37,7 +37,7 @@ def _get_llm_global_context(): return context if context_enabled and context else None -def _truncate_summary(summary: str, max_tokens: int = 100_000): +def _truncate_summary(summary: str, test_id: str, max_tokens: int = 100_000): if len(summary) < max_tokens: # since string itself is less than max_tokens, definitely small enough return summary @@ -47,6 +47,10 @@ def _truncate_summary(summary: str, max_tokens: int = 100_000): summary_tokens = encoding.encode(summary) if len(summary_tokens) > max_tokens: + logger.warning( + "Truncating test result due to context length restrictions..." + f"Generated description for {test_id} may be innacurate" + ) summary = ( encoding.decode(summary_tokens[:max_tokens]) + "...[truncated]" @@ -100,7 +104,7 @@ def generate_description( "test_name": test_name, "test_description": test_description, "title": title, - "summary": summary, + "summary": _truncate_summary(summary, test_id), "figures": [ figure._get_b64_url() for figure in ([] if tables else figures) ], From 634d6b3036ef9badd930a7118a49a3dd6d5892bb Mon Sep 17 00:00:00 2001 From: John Walz Date: Tue, 11 Feb 2025 13:04:04 -0500 Subject: [PATCH 4/4] chore: updating warning message --- validmind/ai/test_descriptions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validmind/ai/test_descriptions.py b/validmind/ai/test_descriptions.py index 10ff33fe7..48747627a 100644 --- a/validmind/ai/test_descriptions.py +++ b/validmind/ai/test_descriptions.py @@ -48,8 +48,8 @@ def _truncate_summary(summary: str, test_id: str, max_tokens: int = 100_000): if len(summary_tokens) > max_tokens: logger.warning( - "Truncating test result due to context length restrictions..." - f"Generated description for {test_id} may be innacurate" + f"Truncating {test_id} due to context length restrictions..." + " Generated description may be innacurate" ) summary = ( encoding.decode(summary_tokens[:max_tokens])