fazeel007 commited on
Commit
e782c0f
Β·
1 Parent(s): 1a1b3eb

Fix ai search

Browse files
Files changed (1) hide show
  1. server/routes.ts +63 -22
server/routes.ts CHANGED
@@ -839,6 +839,15 @@ export async function registerRoutes(app: Express): Promise<Server> {
839
  if (queryLower.includes('gpt')) {
840
  searchQueries.push('GPT', 'Generative Pre-trained Transformer');
841
  }
 
 
 
 
 
 
 
 
 
842
 
843
  // Search with each query and combine results
844
  const allSearchResults = new Map<number, any>();
@@ -931,43 +940,75 @@ export async function registerRoutes(app: Express): Promise<Server> {
931
  searchRequest.query.toLowerCase().includes('machine learning') ||
932
  searchRequest.query.toLowerCase().includes('neural network');
933
 
934
- // Parallel search of external sources
935
- const searchPromises = [];
 
 
936
 
937
  // GitHub search for code and AI-related queries
938
  if ((isCodeQuery || isAIQuery) && process.env.GITHUB_TOKEN) {
939
  console.log('πŸ™ Searching GitHub...');
940
- searchPromises.push(
941
- searchGitHubRepos(searchRequest.query, Math.min(3, Math.ceil(searchRequest.limit / 3)))
942
- .then(repos => repos.map((repo, index) =>
943
- transformGitHubRepoToDocument(repo, index + allDocuments.length, searchRequest.query)
944
- ))
 
 
 
 
 
 
 
 
 
 
 
 
945
  );
946
  }
947
 
948
  // Always include web search for comprehensive coverage
949
  console.log('🌍 Searching web...');
950
- searchPromises.push(
951
- searchWeb(searchRequest.query, Math.min(3, Math.ceil(searchRequest.limit / 3)))
952
- .then(webResults => webResults.map((result, index) =>
953
- transformWebResultToDocument(result, index + allDocuments.length, searchRequest.query)
954
- ))
 
 
 
 
 
 
 
 
 
 
 
 
955
  );
956
 
957
- // Wait for all external searches to complete
958
- if (searchPromises.length > 0) {
959
  try {
960
- const externalResults = await Promise.all(searchPromises);
961
- const flattenedResults = externalResults.flat();
 
 
 
 
962
 
963
- console.log(`🌐 Found ${flattenedResults.length} external results`);
964
 
965
  // Combine local and external results, keeping local results prioritized
966
- allDocuments = [...allDocuments, ...flattenedResults]
967
- .sort((a, b) => b.relevanceScore - a.relevanceScore)
968
- .slice(0, searchRequest.limit);
969
- } catch (externalError) {
970
- console.log('🌐 External search failed, using local results only');
 
 
971
  }
972
  }
973
 
 
839
  if (queryLower.includes('gpt')) {
840
  searchQueries.push('GPT', 'Generative Pre-trained Transformer');
841
  }
842
+ if (queryLower.includes('transformer') || queryLower.includes('attention')) {
843
+ searchQueries.push('Attention Is All You Need', 'transformer', 'attention mechanism');
844
+ }
845
+ if (queryLower.includes('constitutional')) {
846
+ searchQueries.push('Constitutional AI', 'harmlessness', 'AI feedback');
847
+ }
848
+ if (queryLower.includes('rag') || queryLower.includes('retrieval')) {
849
+ searchQueries.push('Retrieval-Augmented Generation', 'retrieval augmented', 'knowledge-intensive');
850
+ }
851
 
852
  // Search with each query and combine results
853
  const allSearchResults = new Map<number, any>();
 
940
  searchRequest.query.toLowerCase().includes('machine learning') ||
941
  searchRequest.query.toLowerCase().includes('neural network');
942
 
943
+ // Query analysis for external search triggers
944
+
945
+ // Enhanced external search with better error handling and timeouts
946
+ const externalSearchPromises = [];
947
 
948
  // GitHub search for code and AI-related queries
949
  if ((isCodeQuery || isAIQuery) && process.env.GITHUB_TOKEN) {
950
  console.log('πŸ™ Searching GitHub...');
951
+ externalSearchPromises.push(
952
+ Promise.race([
953
+ searchGitHubRepos(searchRequest.query, Math.min(3, Math.ceil(searchRequest.limit / 3)))
954
+ .then(repos => ({
955
+ type: 'github',
956
+ results: repos.map((repo, index) =>
957
+ transformGitHubRepoToDocument(repo, index + allDocuments.length, searchRequest.query)
958
+ )
959
+ }))
960
+ .catch(error => {
961
+ console.log('πŸ™ GitHub search failed:', error.message);
962
+ return { type: 'github', results: [] };
963
+ }),
964
+ new Promise((_, reject) =>
965
+ setTimeout(() => reject(new Error('GitHub search timeout')), 8000)
966
+ )
967
+ ]).catch(() => ({ type: 'github', results: [] }))
968
  );
969
  }
970
 
971
  // Always include web search for comprehensive coverage
972
  console.log('🌍 Searching web...');
973
+ externalSearchPromises.push(
974
+ Promise.race([
975
+ searchWeb(searchRequest.query, Math.min(3, Math.ceil(searchRequest.limit / 3)))
976
+ .then(webResults => ({
977
+ type: 'web',
978
+ results: webResults.map((result, index) =>
979
+ transformWebResultToDocument(result, index + allDocuments.length, searchRequest.query)
980
+ )
981
+ }))
982
+ .catch(error => {
983
+ console.log('🌍 Web search failed:', error.message);
984
+ return { type: 'web', results: [] };
985
+ }),
986
+ new Promise((_, reject) =>
987
+ setTimeout(() => reject(new Error('Web search timeout')), 5000)
988
+ )
989
+ ]).catch(() => ({ type: 'web', results: [] }))
990
  );
991
 
992
+ // Wait for external searches with timeout protection
993
+ if (externalSearchPromises.length > 0) {
994
  try {
995
+ const externalResults = await Promise.all(externalSearchPromises);
996
+
997
+ // Flatten and combine results
998
+ const githubResults = externalResults.find((r: any) => r.type === 'github')?.results || [];
999
+ const webResults = externalResults.find((r: any) => r.type === 'web')?.results || [];
1000
+ const allExternalResults = [...githubResults, ...webResults];
1001
 
1002
+ console.log(`🌐 Found ${allExternalResults.length} external results (GitHub: ${githubResults.length}, Web: ${webResults.length})`);
1003
 
1004
  // Combine local and external results, keeping local results prioritized
1005
+ if (allExternalResults.length > 0) {
1006
+ allDocuments = [...allDocuments, ...allExternalResults]
1007
+ .sort((a, b) => b.relevanceScore - a.relevanceScore)
1008
+ .slice(0, searchRequest.limit);
1009
+ }
1010
+ } catch (externalError: any) {
1011
+ console.log('🌐 External search failed:', externalError?.message || externalError);
1012
  }
1013
  }
1014