starfriend commited on
Commit
0dda2f2
·
verified ·
1 Parent(s): 30f6ce2

Upload 2 files

Browse files
backend/research_fallback.py CHANGED
@@ -87,7 +87,10 @@ def clear_llama_circuit() -> None:
87
  with _LOCK:
88
  _UNAVAILABLE_UNTIL = 0.0
89
 
90
- def deepseek_research_answer(context: str, question: str, application: str) -> tuple[str, str]:
 
 
 
91
  """Answer from supplied retrieved evidence, without independently browsing papers."""
92
  from openai import OpenAI
93
  key = os.getenv("DEEPSEEK_API_KEY", "").strip()
@@ -98,8 +101,16 @@ def deepseek_research_answer(context: str, question: str, application: str) -> t
98
  raise ValueError("Unsupported research fallback model")
99
  max_context_chars = max(3000, min(150000, int(os.getenv("WATERSCOPE_FALLBACK_MAX_CONTEXT_CHARS", "60000"))))
100
  max_tokens = max(256, min(8192, int(os.getenv("WATERSCOPE_FALLBACK_MAX_OUTPUT_TOKENS", "1600"))))
 
 
101
  client = OpenAI(base_url="https://api.deepseek.com", api_key=key, timeout=90.0, max_retries=0)
102
  instructions = (
 
 
 
 
 
 
103
  "You are WaterScope-AI, a research assistant for environmental science and engineering. "
104
  "Answer the user using ONLY the retrieved publication evidence provided. "
105
  "Respect the task type. For summaries, synthesize supported findings; for research gaps, "
@@ -112,6 +123,7 @@ def deepseek_research_answer(context: str, question: str, application: str) -> t
112
  model=model,
113
  instructions=instructions,
114
  input=[{"role":"user","content":(
 
115
  f"Research task: {application}\n\nRetrieved publication evidence:\n"
116
  f"{context[:max_context_chars]}\n\nUser question:\n{question}"
117
  )}],
 
87
  with _LOCK:
88
  _UNAVAILABLE_UNTIL = 0.0
89
 
90
+ def deepseek_research_answer(
91
+ context: str, question: str, application: str, *,
92
+ general_chat: bool = False, max_output_tokens: int | None = None,
93
+ ) -> tuple[str, str]:
94
  """Answer from supplied retrieved evidence, without independently browsing papers."""
95
  from openai import OpenAI
96
  key = os.getenv("DEEPSEEK_API_KEY", "").strip()
 
101
  raise ValueError("Unsupported research fallback model")
102
  max_context_chars = max(3000, min(150000, int(os.getenv("WATERSCOPE_FALLBACK_MAX_CONTEXT_CHARS", "60000"))))
103
  max_tokens = max(256, min(8192, int(os.getenv("WATERSCOPE_FALLBACK_MAX_OUTPUT_TOKENS", "1600"))))
104
+ if max_output_tokens is not None:
105
+ max_tokens = max(32, min(max_tokens, int(max_output_tokens)))
106
  client = OpenAI(base_url="https://api.deepseek.com", api_key=key, timeout=90.0, max_retries=0)
107
  instructions = (
108
+ "You are WaterScope-AI, a helpful conversational assistant focused on "
109
+ "environmental science and engineering. Answer the user's message naturally "
110
+ "and concisely. Do not invent citations or claim to have searched papers when "
111
+ "none were provided. For evidence-based discussion, invite the user to "
112
+ "select publications."
113
+ if general_chat else
114
  "You are WaterScope-AI, a research assistant for environmental science and engineering. "
115
  "Answer the user using ONLY the retrieved publication evidence provided. "
116
  "Respect the task type. For summaries, synthesize supported findings; for research gaps, "
 
123
  model=model,
124
  instructions=instructions,
125
  input=[{"role":"user","content":(
126
+ question if general_chat else
127
  f"Research task: {application}\n\nRetrieved publication evidence:\n"
128
  f"{context[:max_context_chars]}\n\nUser question:\n{question}"
129
  )}],
metadata_search/chat_api.py CHANGED
@@ -19,6 +19,8 @@ def _generate_answer(
19
  *,
20
  application: str | None = None,
21
  model: str | None = None,
 
 
22
  ) -> tuple[str, str, str]:
23
  from backend.research_fallback import (
24
  clear_llama_circuit, deepseek_research_answer, is_infrastructure_error,
@@ -29,19 +31,28 @@ def _generate_answer(
29
  requested_app = application or os.getenv("WATERSCOPE_APPLICATION", "auto")
30
  app_used = infer_application(message, explicit=requested_app if requested_app != "auto" else None)
31
  fallback_enabled = os.getenv("WATERSCOPE_ENABLE_RESEARCH_FALLBACK", "0") == "1"
 
 
 
 
 
 
 
 
32
  if fallback_enabled and llama_circuit_open():
33
- answer, fallback_model = deepseek_research_answer(prefix_context(context), message, app_used)
34
  return sanitize_answer(answer), f"deepseek_fallback:{app_used}", f"provider=deepseek;model={fallback_model};reason=llama_circuit_open"
35
  if fallback_enabled and llama_space_paused():
36
  _logger.info("Llama Space is PAUSED; skipping Llama and using DeepSeek fallback")
37
- answer, fallback_model = deepseek_research_answer(prefix_context(context), message, app_used)
38
  return sanitize_answer(answer), f"deepseek_fallback:{app_used}", f"provider=deepseek;model={fallback_model};reason=llama_space_paused"
39
  try:
40
  answer, app_used, diagnostics = agent_answer(
41
- prefix_context(context),
42
  message,
43
  application=requested_app,
44
  model=model or os.getenv("WATERSCOPE_MODEL"),
 
45
  )
46
  clear_llama_circuit()
47
  return sanitize_answer(answer), f"waterscope:{app_used}", diagnostics
@@ -54,7 +65,7 @@ def _generate_answer(
54
  type(exc).__name__,
55
  )
56
  mark_llama_unavailable()
57
- answer, fallback_model = deepseek_research_answer(prefix_context(context), message, app_used)
58
  return sanitize_answer(answer), f"deepseek_fallback:{app_used}", f"provider=deepseek;model={fallback_model};reason=llama_unavailable"
59
 
60
  def get_papers_payload() -> dict:
@@ -73,6 +84,43 @@ def chat_payload(body: dict) -> dict:
73
  if not message:
74
  raise ValueError("message is required")
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  if agent_mode and not document_ids:
77
  from metadata_search.agent_orchestrator import agent_chat_payload
78
 
 
19
  *,
20
  application: str | None = None,
21
  model: str | None = None,
22
+ general_chat: bool = False,
23
+ max_new_tokens: int | None = None,
24
  ) -> tuple[str, str, str]:
25
  from backend.research_fallback import (
26
  clear_llama_circuit, deepseek_research_answer, is_infrastructure_error,
 
31
  requested_app = application or os.getenv("WATERSCOPE_APPLICATION", "auto")
32
  app_used = infer_application(message, explicit=requested_app if requested_app != "auto" else None)
33
  fallback_enabled = os.getenv("WATERSCOPE_ENABLE_RESEARCH_FALLBACK", "0") == "1"
34
+ # No publication evidence for free-form chat. Never prepend paper-only
35
+ # grounding instructions to this path.
36
+ model_context = "" if general_chat else prefix_context(context)
37
+ def _fallback():
38
+ return deepseek_research_answer(
39
+ model_context, message, app_used,
40
+ general_chat=general_chat, max_output_tokens=max_new_tokens,
41
+ )
42
  if fallback_enabled and llama_circuit_open():
43
+ answer, fallback_model = _fallback()
44
  return sanitize_answer(answer), f"deepseek_fallback:{app_used}", f"provider=deepseek;model={fallback_model};reason=llama_circuit_open"
45
  if fallback_enabled and llama_space_paused():
46
  _logger.info("Llama Space is PAUSED; skipping Llama and using DeepSeek fallback")
47
+ answer, fallback_model = _fallback()
48
  return sanitize_answer(answer), f"deepseek_fallback:{app_used}", f"provider=deepseek;model={fallback_model};reason=llama_space_paused"
49
  try:
50
  answer, app_used, diagnostics = agent_answer(
51
+ model_context,
52
  message,
53
  application=requested_app,
54
  model=model or os.getenv("WATERSCOPE_MODEL"),
55
+ max_new_tokens=max_new_tokens,
56
  )
57
  clear_llama_circuit()
58
  return sanitize_answer(answer), f"waterscope:{app_used}", diagnostics
 
65
  type(exc).__name__,
66
  )
67
  mark_llama_unavailable()
68
+ answer, fallback_model = _fallback()
69
  return sanitize_answer(answer), f"deepseek_fallback:{app_used}", f"provider=deepseek;model={fallback_model};reason=llama_unavailable"
70
 
71
  def get_papers_payload() -> dict:
 
84
  if not message:
85
  raise ValueError("message is required")
86
 
87
+ if not document_ids and body.get("general_chat") is True:
88
+ # Opt-in direct LLM route for short no-paper conversations. This does
89
+ # not run publication search or the agent orchestrator.
90
+ history = body.get("history")
91
+ if not isinstance(history, list):
92
+ history = []
93
+ transcript = []
94
+ for turn in history[-6:]:
95
+ if not isinstance(turn, dict) or turn.get("role") not in ("user", "assistant"):
96
+ continue
97
+ content = str(turn.get("content") or "").strip()[:1500]
98
+ if content:
99
+ transcript.append(f"{turn['role'].capitalize()}: {content}")
100
+ prompt = (
101
+ "Previous conversation (context only):\n" + "\n".join(transcript)
102
+ + "\n\nCurrent user message:\n" + message
103
+ if transcript else message
104
+ )
105
+ try:
106
+ requested_max = int(body.get("max_new_tokens", 300))
107
+ except (TypeError, ValueError):
108
+ requested_max = 300
109
+ requested_max = max(32, min(300, requested_max))
110
+ answer, strategy, diagnostics = _generate_answer(
111
+ prompt, "", application="general_qa", model=body.get("model"),
112
+ general_chat=True, max_new_tokens=requested_max,
113
+ )
114
+ return {
115
+ "answer": answer,
116
+ "strategy": strategy,
117
+ "diagnostics": diagnostics,
118
+ "general_chat": True,
119
+ "sources": [],
120
+ "chunks": [],
121
+ "retrieval": "none",
122
+ "llm_backend": "deepseek_fallback" if strategy.startswith("deepseek_fallback:") else llm_backend_name(),
123
+ }
124
  if agent_mode and not document_ids:
125
  from metadata_search.agent_orchestrator import agent_chat_payload
126