Skip to content

Commit 2551d20

Browse files
authored
chore: show llm filter calls info, remove redundant remove_timestamp param in Env (microsoft#1040)
* show llm calls info * remove `remove_timestamp` param in Env * use cache default
1 parent f4370a4 commit 2551d20

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

rdagent/log/ui/ds_trace.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
extract_loopid_func_name,
2323
is_valid_session,
2424
)
25+
from rdagent.utils.agent.tpl import T
2526
from rdagent.utils.repo.diff import generate_diff_from_dict
2627

2728
if "show_stdout" not in state:
@@ -44,7 +45,7 @@ def convert_defaultdict_to_dict(d):
4445
return d
4546

4647

47-
# @st.cache_data(persist=True)
48+
@st.cache_data(persist=True)
4849
def load_data(log_path: Path):
4950
data = defaultdict(lambda: defaultdict(dict))
5051
llm_data = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
@@ -60,8 +61,6 @@ def load_data(log_path: Path):
6061
if ei is not None:
6162
ei = int(ei)
6263
if "debug_" in msg.tag:
63-
if "debug_tpl" in msg.tag and "filter_" in msg.content["uri"]:
64-
continue
6564
if ei is not None:
6665
llm_data[li][fn][ei].append(
6766
{
@@ -201,9 +200,9 @@ def workspace_win(workspace, cmp_workspace=None, cmp_name="last code."):
201200
def show_text(text, lang=None):
202201
"""显示文本代码块"""
203202
if lang:
204-
st.code(text, language=lang, wrap_lines=True)
203+
st.code(text, language=lang, wrap_lines=True, line_numbers=True)
205204
elif "\n" in text:
206-
st.code(text, language="python", wrap_lines=True)
205+
st.code(text, language="python", wrap_lines=True, line_numbers=True)
207206
else:
208207
st.code(text, language="html", wrap_lines=True)
209208

@@ -220,6 +219,8 @@ def llm_log_win(llm_d: list):
220219
for d in llm_d:
221220
if "debug_tpl" in d["tag"]:
222221
uri = d["obj"]["uri"]
222+
if "filter_redundant_text" in uri:
223+
continue
223224
tpl = d["obj"]["template"]
224225
cxt = d["obj"]["context"]
225226
rd = d["obj"]["rendered"]
@@ -240,32 +241,19 @@ def llm_log_win(llm_d: list):
240241
with t1:
241242
try:
242243
rdict = json.loads(resp)
243-
if "code" in rdict:
244-
code = rdict["code"]
245-
st.markdown(":red[**Code in response dict:**]")
246-
st.code(code, language="python", wrap_lines=True, line_numbers=True)
247-
rdict.pop("code")
248-
elif "spec" in rdict:
249-
spec = rdict["spec"]
250-
st.markdown(":red[**Spec in response dict:**]")
251-
st.markdown(spec)
252-
rdict.pop("spec")
253-
else:
254-
showed_keys = []
255-
for k, v in rdict.items():
256-
if k.endswith(".py"):
257-
st.markdown(f":red[**{k}**]")
258-
st.code(v, language="python", wrap_lines=True, line_numbers=True)
259-
showed_keys.append(k)
260-
for k in showed_keys:
261-
rdict.pop(k)
262-
st.write(":red[**Other parts (except for the code or spec) in response dict:**]")
244+
showed_keys = []
245+
for k, v in rdict.items():
246+
if k.endswith(".py") or k.endswith(".md"):
247+
st.markdown(f":red[**{k}**]")
248+
st.code(v, language="python", wrap_lines=True, line_numbers=True)
249+
showed_keys.append(k)
250+
for k in showed_keys:
251+
rdict.pop(k)
252+
if len(showed_keys) > 0:
253+
st.write(":red[**Other parts (except for the code or spec) in response dict:**]")
263254
st.json(rdict)
264255
except:
265-
try:
266-
st.json(resp)
267-
except:
268-
show_text(resp)
256+
show_text(resp)
269257
with t2:
270258
show_text(user)
271259
with t3:
@@ -466,18 +454,37 @@ def replace_ep_path(p: Path):
466454
return p
467455

468456

457+
def get_llm_call_stats(llm_data: dict) -> tuple[int, int]:
458+
total_llm_call = 0
459+
total_filter_call = 0
460+
filter_sys_prompt = T("rdagent.utils.prompts:filter_redundant_text.system").r()
461+
for li, loop_d in llm_data.items():
462+
for fn, loop_fn_d in loop_d.items():
463+
for k, v in loop_fn_d.items():
464+
for d in v:
465+
if "debug_llm" in d["tag"]:
466+
total_llm_call += 1
467+
if filter_sys_prompt == d["obj"]["system"]:
468+
total_filter_call += 1
469+
return total_llm_call, total_filter_call
470+
471+
469472
def summarize_win():
470473
st.header("Summary", divider="rainbow")
471474
with st.container(border=True):
472475
min_id, max_id = get_state_data_range(state.data)
473-
info0, info1, info2, info3 = st.columns([2, 1, 1, 1])
476+
info0, info1, info2, info3, info4, info5 = st.columns([1, 1, 1, 1, 1, 1])
474477
show_trace_dag = info0.toggle("Show trace DAG", key="show_trace_dag")
475-
with info1.popover("LITELLM_SETTINGS", icon="⚙️"):
476-
st.write(state.data.get("SETTINGS", {}).get("LITELLM_SETTINGS", "No settings found."))
477-
with info2.popover("RD_AGENT_SETTINGS", icon="⚙️"):
478-
st.write(state.data.get("SETTINGS", {}).get("RD_AGENT_SETTINGS", "No settings found."))
479-
with info3.popover("RDLOOP_SETTINGS", icon="⚙️"):
480-
st.write(state.data.get("SETTINGS", {}).get("RDLOOP_SETTINGS", "No settings found."))
478+
with info1.popover("LITELLM", icon="⚙️"):
479+
st.write(state.data.get("settings", {}).get("LITELLM_SETTINGS", "No settings found."))
480+
with info2.popover("RD_AGENT", icon="⚙️"):
481+
st.write(state.data.get("settings", {}).get("RD_AGENT_SETTINGS", "No settings found."))
482+
with info3.popover("RDLOOP", icon="⚙️"):
483+
st.write(state.data.get("settings", {}).get("RDLOOP_SETTINGS", "No settings found."))
484+
485+
llm_call, llm_filter_call = get_llm_call_stats(state.llm_data)
486+
info4.metric("LLM Calls", llm_call)
487+
info5.metric("LLM Filter Calls", f"{llm_filter_call}({round(llm_filter_call / llm_call * 100, 2)}%)")
481488
if show_trace_dag:
482489
st.markdown("### Trace DAG")
483490
final_trace_loop_id = max_id

rdagent/utils/env.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,15 @@ def __run_with_retry(
213213
local_path: str = ".",
214214
env: dict | None = None,
215215
running_extra_volume: Mapping = MappingProxyType({}),
216-
remove_timestamp: bool = True,
217216
) -> EnvResult:
218-
# TODO: remove_timestamp can be implemented in a shallower way...
219217
for retry_index in range(self.conf.retry_count + 1):
220218
try:
221219
start = time.time()
222220
log_output, return_code = self._run(
223-
entry, local_path, env, running_extra_volume=running_extra_volume, remove_timestamp=remove_timestamp
221+
entry,
222+
local_path,
223+
env,
224+
running_extra_volume=running_extra_volume,
224225
)
225226
end = time.time()
226227
logger.info(f"Running time: {end - start} seconds")
@@ -316,7 +317,10 @@ def _get_path_stem(path: str) -> str | None:
316317
result = self.cached_run(entry_add_timeout, local_path, env, running_extra_volume)
317318
else:
318319
result = self.__run_with_retry(
319-
entry_add_timeout, local_path, env, running_extra_volume, remove_timestamp=False
320+
entry_add_timeout,
321+
local_path,
322+
env,
323+
running_extra_volume,
320324
)
321325

322326
return result
@@ -327,7 +331,6 @@ def cached_run(
327331
local_path: str = ".",
328332
env: dict | None = None,
329333
running_extra_volume: Mapping = MappingProxyType({}),
330-
remove_timestamp: bool = True,
331334
) -> EnvResult:
332335
"""
333336
Run the folder under the environment.
@@ -364,7 +367,7 @@ def cached_run(
364367
ret = pickle.load(f)
365368
self.unzip_a_file_into_a_folder(str(target_folder / f"{key}.zip"), local_path)
366369
else:
367-
ret = self.__run_with_retry(entry, local_path, env, running_extra_volume, remove_timestamp)
370+
ret = self.__run_with_retry(entry, local_path, env, running_extra_volume)
368371
with open(target_folder / f"{key}.pkl", "wb") as f:
369372
pickle.dump(ret, f)
370373
self.zip_a_folder_into_a_file(local_path, str(target_folder / f"{key}.zip"))
@@ -846,20 +849,12 @@ def _f() -> dict:
846849

847850
return _f()
848851

849-
def replace_time_info(self, input_string: str) -> str:
850-
"""To remove any time related information from the logs since it will destroy the cache mechanism"""
851-
"""We currently set this function as default, but it can be changed in the future"""
852-
datetime_pattern = r"\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?\b"
853-
output_string = re.sub(datetime_pattern, "[DATETIME]", input_string)
854-
return output_string
855-
856852
def _run(
857853
self,
858854
entry: str | None = None,
859855
local_path: str = ".",
860856
env: dict | None = None,
861857
running_extra_volume: Mapping = MappingProxyType({}),
862-
remove_timestamp: bool = True,
863858
**kwargs: Any,
864859
) -> tuple[str, int]:
865860
if env is None:
@@ -918,7 +913,6 @@ def _run(
918913
print(table)
919914
for log in logs:
920915
decoded_log = log.strip().decode()
921-
decoded_log = self.replace_time_info(decoded_log) if remove_timestamp else decoded_log
922916
Console().print(decoded_log, markup=False)
923917
log_output += decoded_log + "\n"
924918
exit_status = container.wait()["StatusCode"]

0 commit comments

Comments
 (0)