Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

116 lines
4.9KB

  1. <%
  2. ' Semantic index maintenance (see core/lib.Embeddings.asp). Run through scripts\embed.cmd, which sets EMBED_ACTION:
  3. ' status | run | reindex | jobs | worker
  4. ' jobs: process due ingestion jobs once (queued, interrupted, retryable failures; see core/lib.Jobs.asp)
  5. ' worker: one quiet pass for the embed-worker service in docker-compose.yml: due ingestion jobs, then pending
  6. ' embedding rows and failed rows that still have attempts left (prints a line only when something happened)
  7. ' The framework folder must be the web root: -e SERVER_WEB_ROOT=/opt/axonasp/site
  8. %>
  9. <!--#include file="../core/autoload_core.asp" -->
  10. <%
  11. Dim sh : Set sh = CreateObject("WScript.Shell")
  12. Emb_TimeoutMs = 60000 ' the worker may wait for a slow provider (web requests use EmbeddingTimeoutMs)
  13. Dim action : action = LCase(Trim(sh.ExpandEnvironmentStrings("%EMBED_ACTION%")))
  14. Dim rs, total, n, rounds, ts
  15. Sub ShowStatus()
  16. Dim r
  17. Response.Write "provider: " & Emb_Setting("EmbeddingProvider", "none") & vbCrLf
  18. Response.Write "model: " & Emb_Model() & " (" & Emb_Dims() & " dimensions)" & vbCrLf
  19. Response.Write "collection: " & Emb_Collection() & vbCrLf
  20. Set r = DAL().Query("SELECT status, COUNT(*) FROM embedding_index GROUP BY status ORDER BY status", Empty)
  21. Do While Not r.EOF
  22. Response.Write " " & r(0) & ": " & r(1) & vbCrLf
  23. r.MoveNext
  24. Loop
  25. r.Close
  26. Set r = DAL().Query("SELECT COUNT(*) FROM embedding_index WHERE status = 'indexed' AND (model <> ? OR dimensions <> ?)", Array(Emb_Model(), Emb_Dims()))
  27. If Not r.EOF Then
  28. If CLng(r(0)) > 0 Then
  29. Response.Write " stale (other model): " & r(0) & " -> run: scripts\embed.cmd reindex" & vbCrLf
  30. End If
  31. End If
  32. r.Close
  33. Set r = DAL().Query("SELECT error FROM embedding_index WHERE status = 'failed' ORDER BY updated_at DESC LIMIT 1", Empty)
  34. If Not r.EOF Then
  35. Response.Write " last error: " & r(0) & vbCrLf
  36. End If
  37. r.Close
  38. End Sub
  39. Sub RunAll()
  40. total = 0
  41. rounds = 0
  42. Do
  43. Emb_LastError = ""
  44. n = Emb_ProcessPending("", 64)
  45. total = total + n
  46. rounds = rounds + 1
  47. If n = 0 Or rounds > 10000 Then
  48. Exit Do
  49. End If
  50. Loop
  51. Response.Write "indexed: " & total & vbCrLf
  52. If Len(Emb_LastError) > 0 Then
  53. Response.Write "ERROR: " & Emb_LastError & vbCrLf
  54. End If
  55. End Sub
  56. Select Case action
  57. Case "status"
  58. ShowStatus
  59. Case "run"
  60. If Not Emb_Enabled() Then
  61. Response.Write "EmbeddingProvider is not ollama in public/web.config; nothing to do." & vbCrLf
  62. Else
  63. DAL().Execute "UPDATE embedding_index SET status = 'pending', attempts = 0 WHERE status = 'failed'", Empty
  64. RunAll
  65. ShowStatus
  66. End If
  67. Case "reindex"
  68. If Not Emb_Enabled() Then
  69. Response.Write "EmbeddingProvider is not ollama in public/web.config; nothing to do." & vbCrLf
  70. Else
  71. ts = Api_Now()
  72. ' rows for everything that should be indexed (missing ones are added), all set to pending
  73. DAL().Execute "INSERT OR IGNORE INTO embedding_index (target_kind, target_id, tenant_id, namespace_id, revision, status, attempts, updated_at) " & _
  74. "SELECT 'memory', m.id, m.tenant_id, m.namespace_id, m.current_revision, 'pending', 0, ? FROM memories m WHERE m.lifecycle_status = 'active'", ts
  75. DAL().Execute "INSERT OR IGNORE INTO embedding_index (target_kind, target_id, tenant_id, namespace_id, revision, status, attempts, updated_at) " & _
  76. "SELECT 'chunk', c.id, d.tenant_id, d.namespace_id, c.document_revision, 'pending', 0, ? FROM chunks c JOIN documents d ON d.id = c.document_id " & _
  77. "WHERE c.document_revision = d.current_revision", ts
  78. DAL().Execute "UPDATE embedding_index SET status = 'pending', attempts = 0, error = NULL, updated_at = ? WHERE status <> 'delete'", ts
  79. RunAll
  80. ShowStatus
  81. End If
  82. Case "jobs"
  83. Response.Write "jobs (done failed): " & Job_ProcessDue(100) & vbCrLf
  84. Case "worker"
  85. Dim jr
  86. jr = Job_ProcessDue(20)
  87. If jr <> "0 0" Then
  88. Response.Write Api_Now() & " jobs done/failed " & jr & vbCrLf
  89. End If
  90. If Emb_Enabled() Then
  91. Emb_LastError = ""
  92. total = 0
  93. rounds = 0
  94. Do
  95. n = Emb_ProcessPending("", 64)
  96. total = total + n
  97. rounds = rounds + 1
  98. If n = 0 Or rounds >= 20 Then
  99. Exit Do
  100. End If
  101. Loop
  102. If total > 0 Then
  103. Response.Write Api_Now() & " indexed " & total & vbCrLf
  104. End If
  105. If Len(Emb_LastError) > 0 Then
  106. Response.Write Api_Now() & " ERROR " & Emb_LastError & vbCrLf
  107. End If
  108. End If
  109. Case Else
  110. Response.Write "Usage: scripts\embed.cmd status|run|reindex|jobs|worker" & vbCrLf
  111. End Select
  112. %>

Powered by TurnKey Linux.