|
5 | 5 | from rdagent.oai.llm_utils import APIBackend
|
6 | 6 |
|
7 | 7 |
|
| 8 | +def _worker(system_prompt, user_prompt): |
| 9 | + api = APIBackend() |
| 10 | + return api.build_messages_and_create_chat_completion( |
| 11 | + system_prompt=system_prompt, |
| 12 | + user_prompt=user_prompt, |
| 13 | + ) |
| 14 | + |
| 15 | + |
8 | 16 | class TestChatCompletion(unittest.TestCase):
|
9 | 17 | def test_chat_completion(self) -> None:
|
10 | 18 | system_prompt = "You are a helpful assistant."
|
@@ -45,6 +53,127 @@ def test_chat_multi_round(self) -> None:
|
45 | 53 | response2 = session.build_chat_completion(user_prompt=user_prompt_2)
|
46 | 54 | assert response2 is not None
|
47 | 55 |
|
| 56 | + def test_chat_cache(self) -> None: |
| 57 | + """ |
| 58 | + Tests: |
| 59 | + - Single process, ask same question, enable cache |
| 60 | + - 2 pass |
| 61 | + - cache is not missed & same question get different answer. |
| 62 | + """ |
| 63 | + from rdagent.core.conf import RD_AGENT_SETTINGS |
| 64 | + from rdagent.core.utils import LLM_CACHE_SEED_GEN |
| 65 | + from rdagent.oai.llm_conf import LLM_SETTINGS |
| 66 | + |
| 67 | + system_prompt = "You are a helpful assistant." |
| 68 | + user_prompt = f"Give me {2} random country names, list {2} cities in each country, and introduce them" |
| 69 | + |
| 70 | + origin_value = ( |
| 71 | + RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, |
| 72 | + LLM_SETTINGS.use_chat_cache, |
| 73 | + LLM_SETTINGS.dump_chat_cache, |
| 74 | + ) |
| 75 | + |
| 76 | + LLM_SETTINGS.use_chat_cache = True |
| 77 | + LLM_SETTINGS.dump_chat_cache = True |
| 78 | + |
| 79 | + RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen = True |
| 80 | + |
| 81 | + LLM_CACHE_SEED_GEN.set_seed(10) |
| 82 | + response1 = APIBackend().build_messages_and_create_chat_completion( |
| 83 | + system_prompt=system_prompt, |
| 84 | + user_prompt=user_prompt, |
| 85 | + ) |
| 86 | + response2 = APIBackend().build_messages_and_create_chat_completion( |
| 87 | + system_prompt=system_prompt, |
| 88 | + user_prompt=user_prompt, |
| 89 | + ) |
| 90 | + |
| 91 | + LLM_CACHE_SEED_GEN.set_seed(20) |
| 92 | + response3 = APIBackend().build_messages_and_create_chat_completion( |
| 93 | + system_prompt=system_prompt, |
| 94 | + user_prompt=user_prompt, |
| 95 | + ) |
| 96 | + response4 = APIBackend().build_messages_and_create_chat_completion( |
| 97 | + system_prompt=system_prompt, |
| 98 | + user_prompt=user_prompt, |
| 99 | + ) |
| 100 | + |
| 101 | + LLM_CACHE_SEED_GEN.set_seed(10) |
| 102 | + response5 = APIBackend().build_messages_and_create_chat_completion( |
| 103 | + system_prompt=system_prompt, |
| 104 | + user_prompt=user_prompt, |
| 105 | + ) |
| 106 | + response6 = APIBackend().build_messages_and_create_chat_completion( |
| 107 | + system_prompt=system_prompt, |
| 108 | + user_prompt=user_prompt, |
| 109 | + ) |
| 110 | + |
| 111 | + # Reset, for other tests |
| 112 | + ( |
| 113 | + RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, |
| 114 | + LLM_SETTINGS.use_chat_cache, |
| 115 | + LLM_SETTINGS.dump_chat_cache, |
| 116 | + ) = origin_value |
| 117 | + |
| 118 | + assert ( |
| 119 | + response1 != response3 and response2 != response4 |
| 120 | + ), "Responses sequence should be determined by 'init_chat_cache_seed'" |
| 121 | + assert ( |
| 122 | + response1 == response5 and response2 == response6 |
| 123 | + ), "Responses sequence should be determined by 'init_chat_cache_seed'" |
| 124 | + assert ( |
| 125 | + response1 != response2 and response3 != response4 and response5 != response6 |
| 126 | + ), "Same question should get different response when use_auto_chat_cache_seed_gen=True" |
| 127 | + |
| 128 | + def test_chat_cache_multiprocess(self) -> None: |
| 129 | + """ |
| 130 | + Tests: |
| 131 | + - Multi process, ask same question, enable cache |
| 132 | + - 2 pass |
| 133 | + - cache is not missed & same question get different answer. |
| 134 | + """ |
| 135 | + from rdagent.core.conf import RD_AGENT_SETTINGS |
| 136 | + from rdagent.core.utils import LLM_CACHE_SEED_GEN, multiprocessing_wrapper |
| 137 | + from rdagent.oai.llm_conf import LLM_SETTINGS |
| 138 | + |
| 139 | + system_prompt = "You are a helpful assistant." |
| 140 | + user_prompt = f"Give me {2} random country names, list {2} cities in each country, and introduce them" |
| 141 | + |
| 142 | + origin_value = ( |
| 143 | + RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, |
| 144 | + LLM_SETTINGS.use_chat_cache, |
| 145 | + LLM_SETTINGS.dump_chat_cache, |
| 146 | + ) |
| 147 | + |
| 148 | + LLM_SETTINGS.use_chat_cache = True |
| 149 | + LLM_SETTINGS.dump_chat_cache = True |
| 150 | + |
| 151 | + RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen = True |
| 152 | + |
| 153 | + func_calls = [(_worker, (system_prompt, user_prompt)) for _ in range(4)] |
| 154 | + |
| 155 | + LLM_CACHE_SEED_GEN.set_seed(10) |
| 156 | + responses1 = multiprocessing_wrapper(func_calls, n=4) |
| 157 | + LLM_CACHE_SEED_GEN.set_seed(20) |
| 158 | + responses2 = multiprocessing_wrapper(func_calls, n=4) |
| 159 | + LLM_CACHE_SEED_GEN.set_seed(10) |
| 160 | + responses3 = multiprocessing_wrapper(func_calls, n=4) |
| 161 | + |
| 162 | + # Reset, for other tests |
| 163 | + ( |
| 164 | + RD_AGENT_SETTINGS.use_auto_chat_cache_seed_gen, |
| 165 | + LLM_SETTINGS.use_chat_cache, |
| 166 | + LLM_SETTINGS.dump_chat_cache, |
| 167 | + ) = origin_value |
| 168 | + for i in range(len(func_calls)): |
| 169 | + assert ( |
| 170 | + responses1[i] != responses2[i] and responses1[i] == responses3[i] |
| 171 | + ), "Responses sequence should be determined by 'init_chat_cache_seed'" |
| 172 | + for j in range(i + 1, len(func_calls)): |
| 173 | + assert ( |
| 174 | + responses1[i] != responses1[j] and responses2[i] != responses2[j] |
| 175 | + ), "Same question should get different response when use_auto_chat_cache_seed_gen=True" |
| 176 | + |
48 | 177 |
|
49 | 178 | if __name__ == "__main__":
|
50 | 179 | unittest.main()
|
0 commit comments