Coverage for src / local_deep_research / advanced_search_system / questions / flexible_browsecomp_question.py: 0%

26 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-01-11 00:51 +0000

1""" 

2Flexible BrowseComp question generator with less prescriptive instructions. 

3 

4Inherits from BrowseComp but gives the LLM more freedom in search strategy. 

5""" 

6 

7from typing import Dict, List 

8 

9from .browsecomp_question import BrowseCompQuestionGenerator 

10 

11 

12class FlexibleBrowseCompQuestionGenerator(BrowseCompQuestionGenerator): 

13 """ 

14 BrowseComp variant with simplified, less prescriptive prompts. 

15 

16 Gives the LLM more freedom to explore different search strategies 

17 instead of strict entity-combination rules. 

18 """ 

19 

20 def _generate_progressive_searches( 

21 self, 

22 query: str, 

23 current_knowledge: str, 

24 entities: Dict[str, List[str]], 

25 questions_by_iteration: dict, 

26 results_by_iteration: dict, 

27 num_questions: int, 

28 iteration: int, 

29 ) -> List[str]: 

30 """Generate searches with more freedom and less rigid instructions.""" 

31 

32 # Check if recent searches are failing 

33 recent_iterations = [i for i in range(max(1, iteration - 5), iteration)] 

34 zero_count = sum( 

35 1 for i in recent_iterations if results_by_iteration.get(i, 1) == 0 

36 ) 

37 searches_failing = zero_count >= 3 

38 

39 # Simpler strategy hint 

40 if searches_failing: 

41 hint = "Recent searches returned 0 results. Try broader, simpler queries." 

42 else: 

43 hint = "Continue exploring to answer the query." 

44 

45 # Much simpler prompt - less prescriptive 

46 prompt = f"""Generate {num_questions} new search queries for: {query} 

47 

48{hint} 

49 

50Available terms: {", ".join(entities["names"] + entities["temporal"] + entities["descriptors"])} 

51 

52Previous searches with results: 

53{self._format_previous_searches(questions_by_iteration, results_by_iteration)} 

54 

55Current findings: 

56{current_knowledge[: self.knowledge_truncate_length] if self.knowledge_truncate_length else current_knowledge} 

57 

58Create {num_questions} diverse search queries. Avoid exact duplicates. One per line. 

59""" 

60 

61 response = self.model.invoke(prompt) 

62 content = ( 

63 response.content if hasattr(response, "content") else str(response) 

64 ) 

65 

66 # Extract searches 

67 searches = [] 

68 for line in content.strip().split("\n"): 

69 line = line.strip() 

70 if line and not line.endswith(":") and len(line) > 5: 

71 for prefix in [ 

72 "Q:", 

73 "Search:", 

74 "-", 

75 "*", 

76 "•", 

77 "1.", 

78 "2.", 

79 "3.", 

80 "4.", 

81 "5.", 

82 ]: 

83 if line.startswith(prefix): 

84 line = line[len(prefix) :].strip() 

85 if line: 

86 searches.append(line) 

87 

88 # If not enough, fall back to parent's logic 

89 if len(searches) < num_questions: 

90 parent_searches = super()._generate_progressive_searches( 

91 query, 

92 current_knowledge, 

93 entities, 

94 questions_by_iteration, 

95 results_by_iteration, 

96 num_questions - len(searches), 

97 iteration, 

98 ) 

99 searches.extend(parent_searches) 

100 

101 return searches[:num_questions]