From 671d4f060044220e1c7f9b5f360fc39d3a2829cf Mon Sep 17 00:00:00 2001 From: XianBW <36835909+XianBW@users.noreply.github.com> Date: Mon, 21 Oct 2024 12:03:59 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20move=20use=5Fauto=5Fchat=5Fcache=5Fsee?= =?UTF-8?q?d=5Fgen=20and=20init=5Fchat=5Fcache=5Fseed=20to=20LLMSett?= =?UTF-8?q?=E2=80=A6=20(#444)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * move use_auto_chat_cache_seed_gen and init_chat_cache_seed to LLMSettings * remove RD_AGENT_SETTINGS in llm_utils.py --- rdagent/core/conf.py | 10 ---------- rdagent/core/utils.py | 3 ++- rdagent/oai/llm_conf.py | 10 ++++++++++ rdagent/oai/llm_utils.py | 3 +-- test/oai/test_completion.py | 14 ++++++-------- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/rdagent/core/conf.py b/rdagent/core/conf.py index 04d97f78..9f57d32d 100644 --- a/rdagent/core/conf.py +++ b/rdagent/core/conf.py @@ -15,16 +15,6 @@ class RDAgentSettings(BaseSettings): # TODO: (xiao) think it can be a separate config. log_trace_path: str | None = None - # Behavior of returning answers to the same question when caching is enabled - use_auto_chat_cache_seed_gen: bool = False - """ - `_create_chat_completion_inner_function` provdies a feature to pass in a seed to affect the cache hash key - We want to enable a auto seed generator to get different default seed for `_create_chat_completion_inner_function` - if seed is not given. - So the cache will only not miss you ask the same question on same round. - """ - init_chat_cache_seed: int = 42 - # azure document intelligence configs azure_document_intelligence_key: str = "" azure_document_intelligence_endpoint: str = "" diff --git a/rdagent/core/utils.py b/rdagent/core/utils.py index f22904b0..3967f507 100644 --- a/rdagent/core/utils.py +++ b/rdagent/core/utils.py @@ -14,6 +14,7 @@ from filelock import FileLock from fuzzywuzzy import fuzz # type: ignore[import-untyped] from rdagent.core.conf import RD_AGENT_SETTINGS +from rdagent.oai.llm_conf import LLM_SETTINGS class RDAgentException(Exception): # noqa: N818 @@ -98,7 +99,7 @@ class CacheSeedGen: """ def __init__(self) -> None: - self.set_seed(RD_AGENT_SETTINGS.init_chat_cache_seed) + self.set_seed(LLM_SETTINGS.init_chat_cache_seed) def set_seed(self, seed: int) -> None: random.seed(seed) diff --git a/rdagent/oai/llm_conf.py b/rdagent/oai/llm_conf.py index 8244a921..ce40135d 100644 --- a/rdagent/oai/llm_conf.py +++ b/rdagent/oai/llm_conf.py @@ -20,6 +20,16 @@ class LLMSettings(BaseSettings): prompt_cache_path: str = str(Path.cwd() / "prompt_cache.db") max_past_message_include: int = 10 + # Behavior of returning answers to the same question when caching is enabled + use_auto_chat_cache_seed_gen: bool = False + """ + `_create_chat_completion_inner_function` provdies a feature to pass in a seed to affect the cache hash key + We want to enable a auto seed generator to get different default seed for `_create_chat_completion_inner_function` + if seed is not given. + So the cache will only not miss you ask the same question on same round. + """ + init_chat_cache_seed: int = 42 + # Chat configs openai_api_key: str = "" # TODO: simplify the key design. chat_openai_api_key: str = "" diff --git a/rdagent/oai/llm_utils.py b/rdagent/oai/llm_utils.py index 3b73f41b..921a5b5e 100644 --- a/rdagent/oai/llm_utils.py +++ b/rdagent/oai/llm_utils.py @@ -17,7 +17,6 @@ from typing import Any, Optional import numpy as np import tiktoken -from rdagent.core.conf import RD_AGENT_SETTINGS from rdagent.core.utils import LLM_CACHE_SEED_GEN, SingletonBaseClass from rdagent.log import LogColors from rdagent.log import rdagent_logger as logger @@ -596,7 +595,7 @@ class APIBackend: To make retries useful, we need to enable a seed. This seed is different from `self.chat_seed` for GPT. It is for the local cache mechanism enabled by RD-Agent locally. """ - if seed is None and RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen: + if seed is None and LLM_SETTINGS.use_auto_chat_cache_seed_gen: seed = LLM_CACHE_SEED_GEN.get_next_seed() # TODO: we can add this function back to avoid so much `self.cfg.log_llm_chat_content` diff --git a/test/oai/test_completion.py b/test/oai/test_completion.py index fce5cc3f..3b60f0f9 100644 --- a/test/oai/test_completion.py +++ b/test/oai/test_completion.py @@ -60,7 +60,6 @@ class TestChatCompletion(unittest.TestCase): - 2 pass - cache is not missed & same question get different answer. """ - from rdagent.core.conf import RD_AGENT_SETTINGS from rdagent.core.utils import LLM_CACHE_SEED_GEN from rdagent.oai.llm_conf import LLM_SETTINGS @@ -68,7 +67,7 @@ class TestChatCompletion(unittest.TestCase): user_prompt = f"Give me {2} random country names, list {2} cities in each country, and introduce them" origin_value = ( - RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, + LLM_SETTINGS.use_auto_chat_cache_seed_gen, LLM_SETTINGS.use_chat_cache, LLM_SETTINGS.dump_chat_cache, ) @@ -76,7 +75,7 @@ class TestChatCompletion(unittest.TestCase): LLM_SETTINGS.use_chat_cache = True LLM_SETTINGS.dump_chat_cache = True - RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen = True + LLM_SETTINGS.use_auto_chat_cache_seed_gen = True LLM_CACHE_SEED_GEN.set_seed(10) response1 = APIBackend().build_messages_and_create_chat_completion( @@ -110,7 +109,7 @@ class TestChatCompletion(unittest.TestCase): # Reset, for other tests ( - RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, + LLM_SETTINGS.use_auto_chat_cache_seed_gen, LLM_SETTINGS.use_chat_cache, LLM_SETTINGS.dump_chat_cache, ) = origin_value @@ -132,7 +131,6 @@ class TestChatCompletion(unittest.TestCase): - 2 pass - cache is not missed & same question get different answer. """ - from rdagent.core.conf import RD_AGENT_SETTINGS from rdagent.core.utils import LLM_CACHE_SEED_GEN, multiprocessing_wrapper from rdagent.oai.llm_conf import LLM_SETTINGS @@ -140,7 +138,7 @@ class TestChatCompletion(unittest.TestCase): user_prompt = f"Give me {2} random country names, list {2} cities in each country, and introduce them" origin_value = ( - RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, + LLM_SETTINGS.use_auto_chat_cache_seed_gen, LLM_SETTINGS.use_chat_cache, LLM_SETTINGS.dump_chat_cache, ) @@ -148,7 +146,7 @@ class TestChatCompletion(unittest.TestCase): LLM_SETTINGS.use_chat_cache = True LLM_SETTINGS.dump_chat_cache = True - RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen = True + LLM_SETTINGS.use_auto_chat_cache_seed_gen = True func_calls = [(_worker, (system_prompt, user_prompt)) for _ in range(4)] @@ -161,7 +159,7 @@ class TestChatCompletion(unittest.TestCase): # Reset, for other tests ( - RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, + LLM_SETTINGS.use_auto_chat_cache_seed_gen, LLM_SETTINGS.use_chat_cache, LLM_SETTINGS.dump_chat_cache, ) = origin_value