fazeel007 commited on
Commit
24425b1
Β·
1 Parent(s): a8d30b4

Fix nebius AI

Browse files
Files changed (2) hide show
  1. README.md +3 -1
  2. server/routes.ts +77 -4
README.md CHANGED
@@ -28,7 +28,9 @@ A production-ready AI-powered knowledge retrieval system featuring real document
28
 
29
  **Submitted to**: [Hugging Face Agents-MCP-Hackathon](https://huggingface.co/Agents-MCP-Hackathon)
30
 
31
- **Live Demo**: [Try KnowledgeBridge on Hugging Face Spaces](https://huggingface.co/spaces/YOUR_USERNAME/KnowledgeBridge)
 
 
32
 
33
  ### **πŸš€ "Show us the most incredible things that your agents can do!"**
34
 
 
28
 
29
  **Submitted to**: [Hugging Face Agents-MCP-Hackathon](https://huggingface.co/Agents-MCP-Hackathon)
30
 
31
+ **Live Demo**: [Try KnowledgeBridge on Hugging Face Spaces](https://huggingface.co/spaces/Agents-MCP-Hackathon/KnowledgeBridge
32
+
33
+ [Video Link]{https://drive.google.com/drive/folders/1iQafhb7PmO6zWW-JDq1eWGo8KN10Ctdf?usp=sharing}
34
 
35
  ### **πŸš€ "Show us the most incredible things that your agents can do!"**
36
 
server/routes.ts CHANGED
@@ -813,9 +813,82 @@ export async function registerRoutes(app: Express): Promise<Server> {
813
  const streaming = req.body.streaming === true;
814
  const startTime = Date.now();
815
 
816
- // First, search local storage
817
- const localResults = await storage.searchDocuments(searchRequest);
818
- let allDocuments = localResults.results || [];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
819
 
820
  // Validate URLs in local storage results as well
821
  if (allDocuments.length > 0) {
@@ -837,7 +910,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
837
  return isValid;
838
  });
839
 
840
- console.log(`Local URL validation completed. ${allDocuments.length}/${localResults.results.length} documents have valid URLs.`);
841
  }
842
  }
843
 
 
813
  const streaming = req.body.streaming === true;
814
  const startTime = Date.now();
815
 
816
+ let allDocuments: any[] = [];
817
+
818
+ // Use Nebius AI for semantic search (important for hackathon demo)
819
+ if (searchRequest.searchType === "semantic") {
820
+ console.log(`πŸ€– Using Nebius AI for semantic search: "${searchRequest.query}"`);
821
+
822
+ try {
823
+ // Get all documents for AI analysis
824
+ const allDocs = await storage.getDocuments(1000, 0);
825
+ console.log(`πŸ“š Found ${allDocs.length} documents for AI analysis`);
826
+
827
+ // Use Nebius AI to find semantically relevant documents
828
+ const prompt = `Given the search query: "${searchRequest.query}"
829
+
830
+ Analyze these documents and return ONLY a JSON array of the 5 most relevant document IDs, ranked by semantic relevance to the query.
831
+
832
+ Documents:
833
+ ${allDocs.slice(0, 15).map(doc => `ID: ${doc.id} | Title: ${doc.title} | Content: ${doc.content.substring(0, 150)}...`).join('\n')}
834
+
835
+ Return format: [4, 21, 23, 2, 7]`;
836
+
837
+ console.log(`πŸ”„ Sending prompt to Nebius AI...`);
838
+ const aiResponse = await nebiusClient.createChatCompletion({
839
+ model: 'deepseek-ai/DeepSeek-R1-0528',
840
+ messages: [{ role: 'user', content: prompt }],
841
+ max_tokens: 100,
842
+ temperature: 0.1
843
+ });
844
+
845
+ console.log(`πŸ“₯ Received AI response`);
846
+
847
+ if (aiResponse && aiResponse.choices && aiResponse.choices[0]) {
848
+ const cleanResponse = aiResponse.choices[0].message.content.trim();
849
+ console.log(`πŸ” AI Response: ${cleanResponse}`);
850
+
851
+ // Extract JSON array - be more flexible with the matching
852
+ const jsonMatch = cleanResponse.match(/\[[\d,\s]+\]/);
853
+
854
+ if (jsonMatch) {
855
+ try {
856
+ const docIds = JSON.parse(jsonMatch[0]);
857
+ console.log(`🎯 AI extracted document IDs: ${JSON.stringify(docIds)}`);
858
+
859
+ // Get the relevant documents in order
860
+ allDocuments = docIds
861
+ .map((id: number) => allDocs.find(doc => doc.id === id))
862
+ .filter((doc: any) => doc !== undefined)
863
+ .slice(0, searchRequest.limit)
864
+ .map((doc: any, index: number) => ({
865
+ ...doc,
866
+ relevanceScore: 1.0 - (index * 0.1),
867
+ rank: index + 1,
868
+ snippet: doc.content.substring(0, 200) + '...'
869
+ }));
870
+
871
+ console.log(`βœ… Nebius AI found ${allDocuments.length} semantically relevant documents`);
872
+ } catch (parseError) {
873
+ console.error(`❌ Failed to parse AI response JSON: ${parseError}`);
874
+ }
875
+ } else {
876
+ console.log(`❌ No JSON array found in AI response: ${cleanResponse}`);
877
+ }
878
+ } else {
879
+ console.log(`❌ No valid AI response received from Nebius`);
880
+ }
881
+ } catch (aiError) {
882
+ console.error('❌ Nebius AI semantic search failed:', aiError);
883
+ // Fallback to regular search
884
+ const localResults = await storage.searchDocuments(searchRequest);
885
+ allDocuments = localResults.results || [];
886
+ }
887
+ } else {
888
+ // Use regular keyword search for other search types
889
+ const localResults = await storage.searchDocuments(searchRequest);
890
+ allDocuments = localResults.results || [];
891
+ }
892
 
893
  // Validate URLs in local storage results as well
894
  if (allDocuments.length > 0) {
 
910
  return isValid;
911
  });
912
 
913
+ console.log(`Local URL validation completed. ${allDocuments.length} documents have valid URLs.`);
914
  }
915
  }
916