Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Improved github search functionallity

dim-tsoukalasf0deb7b5fbdc
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_github.py b/src/local_deep_research/web_search_engines/engines/search_engine_github.py
index 6205cc5fbe..ad6a369906 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_github.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_github.py
@@ -3,6 +3,7 @@ import logging
import base64
import os
import time
+import json
from typing import Dict, List, Any, Optional, Union
from langchain_core.language_models import BaseLLM
@@ -77,6 +78,64 @@ class GitHubSearchEngine(BaseSearchEngine):
logger.warning(f"GitHub API rate limit exceeded. Waiting {wait_time:.0f} seconds.")
time.sleep(min(wait_time, 60)) # Wait at most 60 seconds
+ def _optimize_github_query(self, query: str) -> str:
+ """
+ Optimize the GitHub search query using LLM to improve search results.
+
+ Args:
+ query: Original search query
+
+ Returns:
+ Optimized GitHub search query
+ """
+ # Get LLM from config if not already set
+ if not self.llm:
+ try:
+ self.llm = config.get_llm()
+ if not self.llm:
+ logger.warning("No LLM available for query optimization")
+ return query
+ except Exception as e:
+ logger.error(f"Error getting LLM from config: {e}")
+ return query
+
+ prompt = f"""Transform this GitHub search query into an optimized version for the GitHub search API. Follow these steps:
+ 1. Strip question words (e.g., 'what', 'are', 'is'), stop words (e.g., 'and', 'as', 'of', 'on'), and redundant terms (e.g., 'repositories', 'repos', 'github') since they're implied by the search context.
+ 2. Keep only domain-specific keywords and avoid using "-related" terms.
+ 3. Add GitHub-specific filters with dynamic thresholds based on query context:
+ - For stars: Use higher threshold (e.g., 'stars:>1000') for mainstream topics, lower (e.g., 'stars:>50') for specialized topics
+ - For language: Detect programming language from query or omit if unclear
+ - For search scope: Use 'in:name,description,readme' for general queries, 'in:file' for code-specific queries
+ 4. For date ranges, adapt based on query context:
+ - For emerging: Use 'created:>2024-01-01'
+ - For mature: Use 'pushed:>2023-01-01'
+ - For historical research: Use 'created:2020-01-01..2024-01-01'
+ 5. For excluding results, adapt based on query:
+ - Exclude irrelevant languages based on context
+ - Use 'NOT' to exclude competing terms
+ 6. Ensure the output is a concise, space-separated string with no punctuation or extra text beyond keywords and filters.
+
+
+ Original query: "{query}"
+
+ Return ONLY the optimized query, ready for GitHub's search API. Do not include explanations or additional text."""
+
+ try:
+ response = self.llm.invoke(prompt)
+ optimized_query = response.content.strip()
+
+ # Validate the optimized query
+ if optimized_query and len(optimized_query) > 0:
+ logger.info(f"LLM optimized query from '{query}' to '{optimized_query}'")
+ return optimized_query
+ else:
+ logger.warning("LLM returned empty query, using original")
+ return query
+
+ except Exception as e:
+ logger.error(f"Error optimizing query with LLM: {e}")
+ return query
+
def _search_github(self, query: str) -> List[Dict[str, Any]]:
"""
Perform a GitHub search based on the configured search type.
@@ -90,20 +149,22 @@ class GitHubSearchEngine(BaseSearchEngine):
results = []
try:
- # Optimize GitHub query format
- github_query = query
-
+ # Optimize GitHub query using LLM
+ github_query = self._optimize_github_query(query)
+ print('--------------------------------')
+ print(github_query)
+ print('--------------------------------')
# For long queries, focus on keywords and add filters for better results
- if len(query) > 80:
- # Extract key terms if it's a recommendation request
- if "recommend" in query.lower() or "looking for" in query.lower():
- github_query = "stars:>100 " + " ".join([
- word for word in query.split()
- if len(word) > 3 and word.lower() not in
- ["recommend", "recommended", "github", "repositories", "looking", "developers"]
- ])
+ # if len(github_query) > 80:
+ # # Extract key terms if it's a recommendation request
+ # if "recommend" in github_query.lower() or "looking for" in github_query.lower():
+ # github_query = "stars:>100 " + " ".join([
+ # word for word in github_query.split()
+ # if len(word) > 3 and word.lower() not in
+ # ["recommend", "recommended", "github", "repositories", "looking", "developers"]
+ # ])
- logger.info(f"Optimized GitHub query: {github_query}")
+ logger.info(f"Final GitHub query: {github_query}")
# Construct search parameters
params = {
@@ -658,4 +719,68 @@ class GitHubSearchEngine(BaseSearchEngine):
self.search_endpoint = f"{self.api_base}/search/{search_type}"
logger.info(f"Set GitHub search type to: {search_type}")
else:
- logger.error(f"Invalid GitHub search type: {search_type}")
\ No newline at end of file
+ logger.error(f"Invalid GitHub search type: {search_type}")
+
+ def _filter_for_relevance(self, previews: List[Dict[str, Any]], query: str) -> List[Dict[str, Any]]:
+ """
+ Filter GitHub search results for relevance using LLM.
+
+ Args:
+ previews: List of preview dictionaries
+ query: Original search query
+
+ Returns:
+ List of relevant preview dictionaries
+ """
+ if not self.llm or not previews:
+ return previews
+
+ # Create a specialized prompt for GitHub results
+ prompt = f"""Analyze these GitHub search results and rank them by relevance to the query.
+Consider:
+1. Repository stars and activity (higher is better)
+2. Match between query intent and repository description
+3. Repository language and topics
+4. Last update time (more recent is better)
+5. Whether it's a fork (original repositories are preferred)
+
+Query: "{query}"
+
+Results:
+{json.dumps(previews, indent=2)}
+
+Return ONLY a JSON array of indices in order of relevance (most relevant first).
+Example: [0, 2, 1, 3]
+Do not include any other text or explanation."""
+
+ try:
+ response = self.llm.invoke(prompt)
+ response_text = response.content.strip()
+
+ # Extract JSON array from response
+ start_idx = response_text.find('[')
+ end_idx = response_text.rfind(']')
+
+ if start_idx >= 0 and end_idx > start_idx:
+ array_text = response_text[start_idx:end_idx+1]
+ ranked_indices = json.loads(array_text)
+
+ # Return the results in ranked order
+ ranked_results = []
+ for idx in ranked_indices:
+ if idx < len(previews):
+ ranked_results.append(previews[idx])
+
+ # Limit to max_filtered_results if specified
+ if self.max_filtered_results and len(ranked_results) > self.max_filtered_results:
+ logger.info(f"Limiting filtered results to top {self.max_filtered_results}")
+ return ranked_results[:self.max_filtered_results]
+
+ return ranked_results
+ else:
+ logger.info("Could not find JSON array in response, returning no previews")
+ return []
+
+ except Exception as e:
+ logger.error(f"Error filtering GitHub results: {e}")
+ return []
\ No newline at end of file