Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/grok_search/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from grok_search.providers.grok import GrokSearchProvider
from grok_search.logger import log_info
from grok_search.config import config
from grok_search.sources import SourcesCache, merge_sources, new_session_id, split_answer_and_sources
from grok_search.sources import SourcesCache, merge_sources, new_session_id, sanitize_answer_text, split_answer_and_sources
from grok_search.planning import engine as planning_engine, _split_csv
except ImportError:
from .providers.grok import GrokSearchProvider
from .logger import log_info
from .config import config
from .sources import SourcesCache, merge_sources, new_session_id, split_answer_and_sources
from .sources import SourcesCache, merge_sources, new_session_id, sanitize_answer_text, split_answer_and_sources
from .planning import engine as planning_engine, _split_csv

import asyncio
Expand Down Expand Up @@ -203,6 +203,7 @@ async def _safe_firecrawl() -> list[dict] | None:
firecrawl_results = gathered[idx]

answer, grok_sources = split_answer_and_sources(grok_result)
answer = sanitize_answer_text(answer)
extra = _extra_results_to_sources(tavily_results, firecrawl_results)
all_sources = merge_sources(grok_sources, extra)

Expand Down
14 changes: 14 additions & 0 deletions src/grok_search/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
_SOURCES_FUNCTION_PATTERN = re.compile(
r"(?im)(^|\n)\s*(sources|source|citations|citation|references|reference|citation_card|source_cards|source_card)\s*\("
)
_THINK_TAG_PATTERN = re.compile(
r"<think\b[^>]*>.*?</think>",
re.IGNORECASE | re.DOTALL,
)


def new_session_id() -> str:
Expand Down Expand Up @@ -67,6 +71,16 @@ def merge_sources(*source_lists: list[dict]) -> list[dict]:
return merged


def sanitize_answer_text(text: str) -> str:
"""Remove model reasoning tags from answer text while preserving content."""
raw = (text or "").strip()
if not raw:
return ""
cleaned = _THINK_TAG_PATTERN.sub("", raw)
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned).strip()
return cleaned


def split_answer_and_sources(text: str) -> tuple[str, list[dict]]:
raw = (text or "").strip()
if not raw:
Expand Down