Skip to content

Commit 3baae2d

Browse files
fix: tf/dr flow improvements (#5380)
1 parent d7c223d commit 3baae2d

File tree

13 files changed

+161
-90
lines changed

13 files changed

+161
-90
lines changed

backend/onyx/agents/agent_search/dr/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727
DR_TIME_BUDGET_BY_TYPE = {
2828
ResearchType.THOUGHTFUL: 3.0,
2929
ResearchType.DEEP: 12.0,
30+
ResearchType.FAST: 0.5,
3031
}

backend/onyx/agents/agent_search/dr/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ResearchType(str, Enum):
88
LEGACY_AGENTIC = "LEGACY_AGENTIC" # only used for legacy agentic search migrations
99
THOUGHTFUL = "THOUGHTFUL"
1010
DEEP = "DEEP"
11+
FAST = "FAST"
1112

1213

1314
class ResearchAnswerPurpose(str, Enum):

backend/onyx/agents/agent_search/dr/nodes/dr_a0_clarification.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
from onyx.agents.agent_search.shared_graph_utils.utils import run_with_timeout
3636
from onyx.agents.agent_search.shared_graph_utils.utils import write_custom_event
3737
from onyx.agents.agent_search.utils import create_question_prompt
38+
from onyx.configs.agent_configs import TF_DR_TIMEOUT_LONG
39+
from onyx.configs.agent_configs import TF_DR_TIMEOUT_SHORT
3840
from onyx.configs.constants import DocumentSource
3941
from onyx.configs.constants import DocumentSourceDescription
4042
from onyx.configs.constants import TMP_DRALPHA_PERSONA_NAME
@@ -487,7 +489,7 @@ def clarifier(
487489
)
488490

489491
answer_tokens, _, _ = run_with_timeout(
490-
80,
492+
TF_DR_TIMEOUT_LONG,
491493
lambda: stream_llm_answer(
492494
llm=graph_config.tooling.primary_llm,
493495
prompt=create_question_prompt(
@@ -500,7 +502,7 @@ def clarifier(
500502
agent_answer_level=0,
501503
agent_answer_question_num=0,
502504
agent_answer_type="agent_level_answer",
503-
timeout_override=60,
505+
timeout_override=TF_DR_TIMEOUT_LONG,
504506
ind=current_step_nr,
505507
context_docs=None,
506508
replace_citations=True,
@@ -611,7 +613,7 @@ def clarifier(
611613

612614
clarification = None
613615

614-
if research_type != ResearchType.THOUGHTFUL:
616+
if research_type == ResearchType.DEEP:
615617
result = _get_existing_clarification_request(graph_config)
616618
if result is not None:
617619
clarification, original_question, chat_history_string = result
@@ -644,7 +646,7 @@ def clarifier(
644646
assistant_system_prompt, clarification_prompt
645647
),
646648
schema=ClarificationGenerationResponse,
647-
timeout_override=25,
649+
timeout_override=TF_DR_TIMEOUT_SHORT,
648650
# max_tokens=1500,
649651
)
650652
except Exception as e:
@@ -673,7 +675,7 @@ def clarifier(
673675
)
674676

675677
_, _, _ = run_with_timeout(
676-
80,
678+
TF_DR_TIMEOUT_LONG,
677679
lambda: stream_llm_answer(
678680
llm=graph_config.tooling.primary_llm,
679681
prompt=repeat_prompt,
@@ -682,7 +684,7 @@ def clarifier(
682684
agent_answer_level=0,
683685
agent_answer_question_num=0,
684686
agent_answer_type="agent_level_answer",
685-
timeout_override=60,
687+
timeout_override=TF_DR_TIMEOUT_LONG,
686688
answer_piece=StreamingType.MESSAGE_DELTA.value,
687689
ind=current_step_nr,
688690
# max_tokens=None,

backend/onyx/agents/agent_search/dr/nodes/dr_a1_orchestrator.py

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
from onyx.agents.agent_search.shared_graph_utils.utils import run_with_timeout
3131
from onyx.agents.agent_search.shared_graph_utils.utils import write_custom_event
3232
from onyx.agents.agent_search.utils import create_question_prompt
33+
from onyx.configs.agent_configs import TF_DR_TIMEOUT_LONG
34+
from onyx.configs.agent_configs import TF_DR_TIMEOUT_SHORT
3335
from onyx.kg.utils.extraction_utils import get_entity_types_str
3436
from onyx.kg.utils.extraction_utils import get_relationship_types_str
3537
from onyx.prompts.dr_prompts import DEFAULLT_DECISION_PROMPT
@@ -170,11 +172,39 @@ def orchestrator(
170172
reasoning_result = "(No reasoning result provided yet.)"
171173
tool_calls_string = "(No tool calls provided yet.)"
172174

173-
if research_type == ResearchType.THOUGHTFUL:
175+
if research_type not in ResearchType:
176+
raise ValueError(f"Invalid research type: {research_type}")
177+
178+
if research_type in [ResearchType.THOUGHTFUL, ResearchType.FAST]:
174179
if iteration_nr == 1:
175-
remaining_time_budget = DR_TIME_BUDGET_BY_TYPE[ResearchType.THOUGHTFUL]
180+
remaining_time_budget = DR_TIME_BUDGET_BY_TYPE[research_type]
181+
182+
elif remaining_time_budget <= 0:
183+
return OrchestrationUpdate(
184+
tools_used=[DRPath.CLOSER.value],
185+
current_step_nr=current_step_nr,
186+
query_list=[],
187+
iteration_nr=iteration_nr,
188+
log_messages=[
189+
get_langgraph_node_log_string(
190+
graph_component="main",
191+
node_name="orchestrator",
192+
node_start_time=node_start_time,
193+
)
194+
],
195+
plan_of_record=plan_of_record,
196+
remaining_time_budget=remaining_time_budget,
197+
iteration_instructions=[
198+
IterationInstructions(
199+
iteration_nr=iteration_nr,
200+
plan=None,
201+
reasoning="Time to wrap up.",
202+
purpose="",
203+
)
204+
],
205+
)
176206

177-
elif iteration_nr > 1:
207+
elif iteration_nr > 1 and remaining_time_budget > 0:
178208
# for each iteration past the first one, we need to see whether we
179209
# have enough information to answer the question.
180210
# if we do, we can stop the iteration and return the answer.
@@ -200,7 +230,7 @@ def orchestrator(
200230
reasoning_tokens: list[str] = [""]
201231

202232
reasoning_tokens, _, _ = run_with_timeout(
203-
80,
233+
TF_DR_TIMEOUT_LONG,
204234
lambda: stream_llm_answer(
205235
llm=graph_config.tooling.primary_llm,
206236
prompt=create_question_prompt(
@@ -211,7 +241,7 @@ def orchestrator(
211241
agent_answer_level=0,
212242
agent_answer_question_num=0,
213243
agent_answer_type="agent_level_answer",
214-
timeout_override=60,
244+
timeout_override=TF_DR_TIMEOUT_LONG,
215245
answer_piece=StreamingType.REASONING_DELTA.value,
216246
ind=current_step_nr,
217247
# max_tokens=None,
@@ -297,7 +327,7 @@ def orchestrator(
297327
decision_prompt,
298328
),
299329
schema=OrchestratorDecisonsNoPlan,
300-
timeout_override=35,
330+
timeout_override=TF_DR_TIMEOUT_SHORT,
301331
# max_tokens=2500,
302332
)
303333
next_step = orchestrator_action.next_step
@@ -320,7 +350,7 @@ def orchestrator(
320350
reasoning_result = "Time to wrap up."
321351
next_tool_name = DRPath.CLOSER.value
322352

323-
else:
353+
elif research_type == ResearchType.DEEP:
324354
if iteration_nr == 1 and not plan_of_record:
325355
# by default, we start a new iteration, but if there is a feedback request,
326356
# we start a new iteration 0 again (set a bit later)
@@ -348,7 +378,7 @@ def orchestrator(
348378
plan_generation_prompt,
349379
),
350380
schema=OrchestrationPlan,
351-
timeout_override=25,
381+
timeout_override=TF_DR_TIMEOUT_SHORT,
352382
# max_tokens=3000,
353383
)
354384
except Exception as e:
@@ -368,7 +398,7 @@ def orchestrator(
368398
)
369399

370400
_, _, _ = run_with_timeout(
371-
80,
401+
TF_DR_TIMEOUT_LONG,
372402
lambda: stream_llm_answer(
373403
llm=graph_config.tooling.primary_llm,
374404
prompt=repeat_plan_prompt,
@@ -377,7 +407,7 @@ def orchestrator(
377407
agent_answer_level=0,
378408
agent_answer_question_num=0,
379409
agent_answer_type="agent_level_answer",
380-
timeout_override=60,
410+
timeout_override=TF_DR_TIMEOUT_LONG,
381411
answer_piece=StreamingType.REASONING_DELTA.value,
382412
ind=current_step_nr,
383413
),
@@ -426,7 +456,7 @@ def orchestrator(
426456
decision_prompt,
427457
),
428458
schema=OrchestratorDecisonsNoPlan,
429-
timeout_override=15,
459+
timeout_override=TF_DR_TIMEOUT_LONG,
430460
# max_tokens=1500,
431461
)
432462
next_step = orchestrator_action.next_step
@@ -460,7 +490,7 @@ def orchestrator(
460490
)
461491

462492
_, _, _ = run_with_timeout(
463-
80,
493+
TF_DR_TIMEOUT_LONG,
464494
lambda: stream_llm_answer(
465495
llm=graph_config.tooling.primary_llm,
466496
prompt=repeat_reasoning_prompt,
@@ -469,7 +499,7 @@ def orchestrator(
469499
agent_answer_level=0,
470500
agent_answer_question_num=0,
471501
agent_answer_type="agent_level_answer",
472-
timeout_override=60,
502+
timeout_override=TF_DR_TIMEOUT_LONG,
473503
answer_piece=StreamingType.REASONING_DELTA.value,
474504
ind=current_step_nr,
475505
# max_tokens=None,
@@ -484,6 +514,9 @@ def orchestrator(
484514

485515
current_step_nr += 1
486516

517+
else:
518+
raise NotImplementedError(f"Research type {research_type} is not implemented.")
519+
487520
base_next_step_purpose_prompt = get_dr_prompt_orchestration_templates(
488521
DRPromptPurpose.NEXT_STEP_PURPOSE,
489522
ResearchType.DEEP,
@@ -498,48 +531,54 @@ def orchestrator(
498531
)
499532

500533
purpose_tokens: list[str] = [""]
534+
purpose = ""
501535

502-
try:
536+
if research_type in [ResearchType.THOUGHTFUL, ResearchType.DEEP]:
503537

504-
write_custom_event(
505-
current_step_nr,
506-
ReasoningStart(),
507-
writer,
508-
)
538+
try:
509539

510-
purpose_tokens, _, _ = run_with_timeout(
511-
80,
512-
lambda: stream_llm_answer(
513-
llm=graph_config.tooling.primary_llm,
514-
prompt=create_question_prompt(
515-
decision_system_prompt,
516-
orchestration_next_step_purpose_prompt,
540+
write_custom_event(
541+
current_step_nr,
542+
ReasoningStart(),
543+
writer,
544+
)
545+
546+
purpose_tokens, _, _ = run_with_timeout(
547+
TF_DR_TIMEOUT_LONG,
548+
lambda: stream_llm_answer(
549+
llm=graph_config.tooling.primary_llm,
550+
prompt=create_question_prompt(
551+
decision_system_prompt,
552+
orchestration_next_step_purpose_prompt,
553+
),
554+
event_name="basic_response",
555+
writer=writer,
556+
agent_answer_level=0,
557+
agent_answer_question_num=0,
558+
agent_answer_type="agent_level_answer",
559+
timeout_override=TF_DR_TIMEOUT_LONG,
560+
answer_piece=StreamingType.REASONING_DELTA.value,
561+
ind=current_step_nr,
562+
# max_tokens=None,
517563
),
518-
event_name="basic_response",
519-
writer=writer,
520-
agent_answer_level=0,
521-
agent_answer_question_num=0,
522-
agent_answer_type="agent_level_answer",
523-
timeout_override=60,
524-
answer_piece=StreamingType.REASONING_DELTA.value,
525-
ind=current_step_nr,
526-
# max_tokens=None,
527-
),
528-
)
564+
)
529565

530-
write_custom_event(
531-
current_step_nr,
532-
SectionEnd(),
533-
writer,
534-
)
566+
write_custom_event(
567+
current_step_nr,
568+
SectionEnd(),
569+
writer,
570+
)
535571

536-
current_step_nr += 1
572+
current_step_nr += 1
573+
574+
except Exception as e:
575+
logger.error("Error in orchestration next step purpose.")
576+
raise e
537577

538-
except Exception as e:
539-
logger.error(f"Error in orchestration next step purpose: {e}")
540-
raise e
578+
purpose = cast(str, merge_content(*purpose_tokens))
541579

542-
purpose = cast(str, merge_content(*purpose_tokens))
580+
elif research_type == ResearchType.FAST:
581+
purpose = f"Answering the question using the {next_tool_name}"
543582

544583
if not next_tool_name:
545584
raise ValueError("The next step has not been defined. This should not happen.")

backend/onyx/agents/agent_search/dr/nodes/dr_a2_closer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from onyx.agents.agent_search.shared_graph_utils.utils import write_custom_event
3434
from onyx.agents.agent_search.utils import create_question_prompt
3535
from onyx.chat.chat_utils import llm_doc_from_inference_section
36+
from onyx.configs.agent_configs import TF_DR_TIMEOUT_LONG
3637
from onyx.context.search.models import InferenceSection
3738
from onyx.db.chat import create_search_doc_from_inference_section
3839
from onyx.db.chat import update_db_session_with_messages
@@ -276,7 +277,7 @@ def closer(
276277
test_info_complete_prompt + (assistant_task_prompt or ""),
277278
),
278279
schema=TestInfoCompleteResponse,
279-
timeout_override=40,
280+
timeout_override=TF_DR_TIMEOUT_LONG,
280281
# max_tokens=1000,
281282
)
282283

@@ -311,10 +312,12 @@ def closer(
311312
writer,
312313
)
313314

314-
if research_type == ResearchType.THOUGHTFUL:
315+
if research_type in [ResearchType.THOUGHTFUL, ResearchType.FAST]:
315316
final_answer_base_prompt = FINAL_ANSWER_PROMPT_WITHOUT_SUB_ANSWERS
316-
else:
317+
elif research_type == ResearchType.DEEP:
317318
final_answer_base_prompt = FINAL_ANSWER_PROMPT_W_SUB_ANSWERS
319+
else:
320+
raise ValueError(f"Invalid research type: {research_type}")
318321

319322
estimated_final_answer_prompt_tokens = check_number_of_tokens(
320323
final_answer_base_prompt.build(
@@ -353,7 +356,7 @@ def closer(
353356

354357
try:
355358
streamed_output, _, citation_infos = run_with_timeout(
356-
240,
359+
int(3 * TF_DR_TIMEOUT_LONG),
357360
lambda: stream_llm_answer(
358361
llm=graph_config.tooling.primary_llm,
359362
prompt=create_question_prompt(
@@ -365,7 +368,7 @@ def closer(
365368
agent_answer_level=0,
366369
agent_answer_question_num=0,
367370
agent_answer_type="agent_level_answer",
368-
timeout_override=60,
371+
timeout_override=int(2 * TF_DR_TIMEOUT_LONG),
369372
answer_piece=StreamingType.MESSAGE_DELTA.value,
370373
ind=current_step_nr,
371374
context_docs=all_context_llmdocs,

0 commit comments

Comments
 (0)