fix_kb_service.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. file_path = r'd:\aura\AuraK\server\src\knowledge-base\knowledge-base.service.ts'
  3. with open(file_path, 'r', encoding='utf-8') as f:
  4. lines = f.readlines()
  5. new_lines = []
  6. changed = False
  7. for line in lines:
  8. # Match the specific pattern: userId, followed by kb.embeddingModelId
  9. if 'userId,' in line and 'kb.embeddingModelId' in line:
  10. # Check if it's the specific call site with three arguments
  11. # Example: [chunk.content], userId, kb.embeddingModelId
  12. # Or: batchTexts, userId, kb.embeddingModelId
  13. if 'getEmbeddings' not in line: # It's probably a multi-line call
  14. pass# Handled below
  15. new_line = line.replace('userId,', '').replace(' ', ' ') # Simple fix for potential double spaces
  16. # But wait, let's be more precise
  17. if 'userId,' in line:
  18. new_line = line.replace('userId,', '').strip()
  19. # Re-add leading whitespace
  20. leading = line[:line.find(line.lstrip())]
  21. new_line = leading + new_line + '\n'
  22. new_lines.append(new_line)
  23. changed = True
  24. continue
  25. new_lines.append(line)
  26. # Let's try a simpler approach if the above is too complex
  27. with open(file_path, 'r', encoding='utf-8') as f:
  28. content = f.read()
  29. # Pattern 1: [chunk.content], userId, kb.embeddingModelId
  30. target1 = """ [chunk.content], // Single text
  31. userId,
  32. kb.embeddingModelId,"""
  33. replacement1 = """ [chunk.content], // Single text
  34. kb.embeddingModelId,"""
  35. # Pattern 2: chunkTexts, userId, kb.embeddingModelId
  36. target2 = """ chunkTexts,
  37. userId,
  38. kb.embeddingModelId,"""
  39. replacement2 = """ chunkTexts,
  40. kb.embeddingModelId,"""
  41. # Pattern 3: batchTexts, userId, kb.embeddingModelId
  42. target3 = """ batchTexts,
  43. userId,
  44. kb.embeddingModelId,"""
  45. replacement3 = """ batchTexts,
  46. kb.embeddingModelId,"""
  47. # Pattern 4 (Precise results): texts, userId, embeddingModelId
  48. target4 = """ texts,
  49. userId,
  50. embeddingModelId,"""
  51. replacement4 = """ texts,
  52. embeddingModelId,"""
  53. fixed_content = content.replace(target1, replacement1)
  54. fixed_content = fixed_content.replace(target2, replacement2)
  55. fixed_content = fixed_content.replace(target3, replacement3)
  56. fixed_content = fixed_content.replace(target4, replacement4)
  57. if fixed_content != content:
  58. with open(file_path, 'w', encoding='utf-8') as f:
  59. f.write(fixed_content)
  60. print("Successfully replaced patterns.")
  61. else:
  62. print("No patterns found to replace.")