adeebjamal commited on
Commit
a1708b1
·
1 Parent(s): fa42dfc

Optimised internet access logic

Browse files
Files changed (2) hide show
  1. main.py +14 -12
  2. models/schemas.py +1 -0
main.py CHANGED
@@ -61,7 +61,8 @@ WEB_SEARCH_GATE_MAX_TOKENS = int(os.environ.get("WEB_SEARCH_GATE_MAX_TOKENS", "3
61
 
62
 
63
  def _should_use_web_search(query: str, history: list) -> bool:
64
- """Decide if we should use web search, with an LLM-based gate in auto mode."""
 
65
  if WEB_SEARCH_MODE == "always":
66
  return True
67
  if WEB_SEARCH_MODE == "off":
@@ -85,10 +86,10 @@ def _should_use_web_search(query: str, history: list) -> bool:
85
 
86
  verdict = gate_output.strip().upper()
87
  if verdict.startswith("YES"):
88
- logger.info(f"Web search required for query")
89
  return True
90
  if verdict.startswith("NO"):
91
- logger.info(f"Web search not required for query")
92
  return False
93
 
94
  logger.info(f"Unclear gate response '{gate_output}'. Defaulting to no web search.")
@@ -96,7 +97,7 @@ def _should_use_web_search(query: str, history: list) -> bool:
96
 
97
 
98
  def _duckduckgo_search(query: str, max_results: int) -> List[Dict[str, str]]:
99
- """Fetch web search snippets from DuckDuckGo without API keys."""
100
  if DDGS is None:
101
  logger.warning("ddgs is not installed. Skipping web search.")
102
  return []
@@ -123,7 +124,7 @@ def _duckduckgo_search(query: str, max_results: int) -> List[Dict[str, str]]:
123
 
124
 
125
  def _build_query_with_web_context(user_query: str, web_results: List[Dict[str, str]]) -> str:
126
- """Attach concise web evidence for the LLM to ground its answer."""
127
  if not web_results:
128
  return user_query
129
 
@@ -143,7 +144,7 @@ def _build_query_with_web_context(user_query: str, web_results: List[Dict[str, s
143
 
144
 
145
  def _format_sources_block(web_results: List[Dict[str, str]]) -> str:
146
- """Render source URLs for transparency in final output."""
147
  if not web_results:
148
  return ""
149
 
@@ -153,19 +154,20 @@ def _format_sources_block(web_results: List[Dict[str, str]]) -> str:
153
  source_lines.append(f"{idx}. {result['title']} - {result['url']}")
154
  return "\n".join(source_lines)
155
 
156
- def _process_ask_in_background(task_id: str, conversation_id: int, query: str, max_tokens: int, history: list):
157
- """Background worker: runs LLM generation and saves result to DB. Updates task_store on completion."""
158
  logger.info(f"[Task {task_id}] Background processing started for conversation {conversation_id}")
159
  start_time = time.time()
160
 
161
  try:
162
- # 0. Optional internet retrieval decided by routing mode / LLM gate
163
  web_results: List[Dict[str, str]] = []
164
  query_for_model = query
165
- if _should_use_web_search(query, history):
166
- logger.info(f"[Task {task_id}] Running DuckDuckGo search for query")
167
  web_results = _duckduckgo_search(query, WEB_SEARCH_MAX_RESULTS)
168
  query_for_model = _build_query_with_web_context(query, web_results)
 
 
169
 
170
  # 1. Generate full response from LLM (streaming internally, collecting all chunks)
171
  logger.info(f"[Task {task_id}] Starting LLM generation...")
@@ -312,7 +314,7 @@ async def ask_question(req: AskQuestionRequest):
312
  # 4. Dispatch background thread for LLM generation + DB save
313
  thread = threading.Thread(
314
  target=_process_ask_in_background,
315
- args=(task_id, req.conversation_id, req.query, req.max_tokens, history),
316
  daemon=True
317
  )
318
  thread.start()
 
61
 
62
 
63
  def _should_use_web_search(query: str, history: list) -> bool:
64
+ """Legacy gate kept for compatibility; /ask now uses client-provided use_internet."""
65
+ logger.info(f"Deciding if we should use web search for query: {query}")
66
  if WEB_SEARCH_MODE == "always":
67
  return True
68
  if WEB_SEARCH_MODE == "off":
 
86
 
87
  verdict = gate_output.strip().upper()
88
  if verdict.startswith("YES"):
89
+ logger.info("Web search required for query")
90
  return True
91
  if verdict.startswith("NO"):
92
+ logger.info("Web search not required for query")
93
  return False
94
 
95
  logger.info(f"Unclear gate response '{gate_output}'. Defaulting to no web search.")
 
97
 
98
 
99
  def _duckduckgo_search(query: str, max_results: int) -> List[Dict[str, str]]:
100
+ logger.info(f"Fetching web search snippets from DuckDuckGo for query: {query}")
101
  if DDGS is None:
102
  logger.warning("ddgs is not installed. Skipping web search.")
103
  return []
 
124
 
125
 
126
  def _build_query_with_web_context(user_query: str, web_results: List[Dict[str, str]]) -> str:
127
+ logger.info(f"Building query with web context for user query: {user_query}")
128
  if not web_results:
129
  return user_query
130
 
 
144
 
145
 
146
  def _format_sources_block(web_results: List[Dict[str, str]]) -> str:
147
+ logger.info(f"Rendering source URLs for transparency in final output")
148
  if not web_results:
149
  return ""
150
 
 
154
  source_lines.append(f"{idx}. {result['title']} - {result['url']}")
155
  return "\n".join(source_lines)
156
 
157
+ def _process_ask_in_background(task_id: str, conversation_id: int, query: str, max_tokens: int, history: list, use_internet: bool):
 
158
  logger.info(f"[Task {task_id}] Background processing started for conversation {conversation_id}")
159
  start_time = time.time()
160
 
161
  try:
162
+ # 0. Optional internet retrieval controlled by client request payload
163
  web_results: List[Dict[str, str]] = []
164
  query_for_model = query
165
+ if use_internet:
166
+ logger.info(f"[Task {task_id}] use_internet=true, running DuckDuckGo search")
167
  web_results = _duckduckgo_search(query, WEB_SEARCH_MAX_RESULTS)
168
  query_for_model = _build_query_with_web_context(query, web_results)
169
+ else:
170
+ logger.info(f"[Task {task_id}] use_internet=false, skipping web search")
171
 
172
  # 1. Generate full response from LLM (streaming internally, collecting all chunks)
173
  logger.info(f"[Task {task_id}] Starting LLM generation...")
 
314
  # 4. Dispatch background thread for LLM generation + DB save
315
  thread = threading.Thread(
316
  target=_process_ask_in_background,
317
+ args=(task_id, req.conversation_id, req.query, req.max_tokens, history, req.use_internet),
318
  daemon=True
319
  )
320
  thread.start()
models/schemas.py CHANGED
@@ -12,6 +12,7 @@ class AskQuestionRequest(BaseModel):
12
  conversation_id: int
13
  query: str
14
  max_tokens: int = 500
 
15
 
16
  class RenameConversationRequest(BaseModel):
17
  conversation_id: int
 
12
  conversation_id: int
13
  query: str
14
  max_tokens: int = 500
15
+ use_internet: bool = False
16
 
17
  class RenameConversationRequest(BaseModel):
18
  conversation_id: int