smart_rag 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +252 -0
  3. data/.rspec +2 -0
  4. data/AGENTS.md +33 -0
  5. data/API_DOCUMENTATION.md +828 -0
  6. data/CHANGELOG.md +16 -1
  7. data/ER-diagram.mmd +144 -0
  8. data/Gemfile +50 -0
  9. data/Gemfile.lock +398 -0
  10. data/Hybrid_Reranking.md +171 -0
  11. data/README.en.md +420 -28
  12. data/README.md +534 -63
  13. data/Rakefile +268 -0
  14. data/SETUP_GUIDE.md +650 -0
  15. data/SmartChunking.md +180 -0
  16. data/USAGE_EXAMPLES.md +1002 -0
  17. data/config/llm_config.yml +4 -2
  18. data/config/smart_rag.yml +45 -1
  19. data/config.ru +15 -0
  20. data/db/migrations/006_create_text_search_configs.rb +3 -2
  21. data/db/migrations/008_create_embeddings.rb +5 -4
  22. data/db/migrations/012_add_metadata_to_source_sections.rb +11 -0
  23. data/db/migrations/013_create_media_jobs.rb +25 -0
  24. data/db/migrations/014_add_media_job_operations_indexes.rb +11 -0
  25. data/db/migrations/015_add_media_leases_and_objects.rb +80 -0
  26. data/db/migrations/016_add_document_principals_and_staging_references.rb +38 -0
  27. data/db/migrations/017_add_media_job_request_fingerprint.rb +48 -0
  28. data/db/seeds/text_search_configs.sql +3 -3
  29. data/design.md +1057 -0
  30. data/docs/API_DOCUMENTATION.md +838 -0
  31. data/docs/DOCUMENTATION_INDEX.en.md +60 -0
  32. data/docs/DOCUMENTATION_INDEX.md +65 -0
  33. data/docs/FIX_SUMMARY.md +256 -0
  34. data/docs/FIX_SUMMARY_COMPLETE.md +273 -0
  35. data/docs/Hybrid_Reranking.md +171 -0
  36. data/docs/MIGRATION_GUIDE.md +151 -0
  37. data/docs/PERFORMANCE_GUIDE.md +58 -0
  38. data/docs/SETUP_GUIDE.md +659 -0
  39. data/docs/SmartChunking.md +180 -0
  40. data/docs/USAGE_EXAMPLES.md +1008 -0
  41. data/docs/design.md +1057 -0
  42. data/docs/evidence_pack.md +211 -0
  43. data/docs/requirements.md +376 -0
  44. data/docs/retrieval_plan.md +251 -0
  45. data/docs/smartrag_improvement_plan.md +201 -0
  46. data/docs/smartrag_refactor.md +216 -0
  47. data/docs/todo.md +931 -0
  48. data/examples/common.rb +1 -1
  49. data/exe/smart-rag-db +163 -0
  50. data/exe/smart-rag-media-worker +34 -0
  51. data/lib/smart_rag/config.rb +12 -0
  52. data/lib/smart_rag/core/document_processor.rb +80 -16
  53. data/lib/smart_rag/core/local_content_store.rb +51 -0
  54. data/lib/smart_rag/core/media_extractors.rb +140 -0
  55. data/lib/smart_rag/core/media_job_queue.rb +353 -0
  56. data/lib/smart_rag/core/media_metadata_extractor.rb +188 -0
  57. data/lib/smart_rag/core/media_object_registry.rb +79 -0
  58. data/lib/smart_rag/core/media_processor.rb +228 -0
  59. data/lib/smart_rag/core/media_safety_policy.rb +61 -0
  60. data/lib/smart_rag/core/s3_content_store.rb +78 -0
  61. data/lib/smart_rag/core/transcript_normalizer.rb +44 -0
  62. data/lib/smart_rag/core/video_semantic_extractor.rb +130 -0
  63. data/lib/smart_rag/http_access_policy.rb +86 -0
  64. data/lib/smart_rag/http_app.rb +188 -0
  65. data/lib/smart_rag/models/embedding.rb +1 -1
  66. data/lib/smart_rag/models/research_topic.rb +1 -1
  67. data/lib/smart_rag/models/research_topic_section.rb +5 -0
  68. data/lib/smart_rag/models/research_topic_tag.rb +5 -0
  69. data/lib/smart_rag/models/search_log.rb +1 -1
  70. data/lib/smart_rag/models/section_fts.rb +5 -0
  71. data/lib/smart_rag/models/section_tag.rb +5 -0
  72. data/lib/smart_rag/models/source_document.rb +1 -1
  73. data/lib/smart_rag/models/source_section.rb +1 -1
  74. data/lib/smart_rag/models/tag.rb +1 -1
  75. data/lib/smart_rag/models/text_search_config.rb +5 -0
  76. data/lib/smart_rag/retrieve.rb +72 -1
  77. data/lib/smart_rag/services/embedding_service.rb +1 -1
  78. data/lib/smart_rag/services/fulltext_search_service.rb +11 -13
  79. data/lib/smart_rag/services/hybrid_search_service.rb +15 -11
  80. data/lib/smart_rag/services/summarization_service.rb +1 -1
  81. data/lib/smart_rag/services/tag_service.rb +1 -1
  82. data/lib/smart_rag/version.rb +1 -1
  83. data/lib/smart_rag.rb +264 -30
  84. data/patch_language.rb +27 -0
  85. data/requirements.md +376 -0
  86. data/source_documents_export.json +11072 -0
  87. data/todo.md +931 -0
  88. data/workers/analyze_content.rb +6 -2
  89. data/workers/get_embedding.rb +1 -1
  90. metadata +151 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19e9abcf3817e98fad15499879a25db6fbb563bc189f5a8f0c58fbea2145cbf8
4
- data.tar.gz: 88f7e4a0f1929b83c9346e8261928bdd72690de4c9da3689a3ca7882a548a6b1
3
+ metadata.gz: db8be81c26762690c4b8b4d2a3bb7c9b3081fd94ce9a05e379964df92ba78b22
4
+ data.tar.gz: 47465a464dff0874022bfcfd37b7eb1115291f1af71b6d4afeb5eefc6eaf249e
5
5
  SHA512:
6
- metadata.gz: f3c7c945032e61c816f383ea6ae1ea87b0be9d4fb2a794175543489e91763825f73fbb925c2a52b37d786af2e1dd126180b4da3cb5c31ded4130e1fa5bc4ebda
7
- data.tar.gz: 6de0148938c32c3842d3d3e2a8f31641c508be193d70e9fa11d01c93dd413e3e805f2fec3afb348981455566989654436cb5e7f70c0351b2b94e1dc21978a94a
6
+ metadata.gz: 68a9f3d4fffa35954489285189d49ed73cab62acd304fd441249aeffa3b454fda816f4799cfd111e2d2a4392d18f2aba4897563201c555b91c70ba7c6d25973e
7
+ data.tar.gz: c3f5efc011510a414aaae99f956119f4f3446391c6352567502e4572e0eeb5df0ac5690c02f50c259293bb55733f59f492b34b5c2e3ee0a31421a223d683f7ff
data/.env.example ADDED
@@ -0,0 +1,252 @@
1
+ # SmartRAG Environment Configuration
2
+ # Copy this file to .env and customize for your environment
3
+
4
+ # =
5
+ # PostgreSQL Database Configuration
6
+ # =
7
+
8
+ # Development database (default)
9
+ SMARTRAG_DB_HOST=
10
+ SMARTRAG_DB_PORT=
11
+ SMARTRAG_DB_NAME=
12
+ SMARTRAG_DB_USER=
13
+ SMARTRAG_DB_PASSWORD=
14
+
15
+ # Test database (for RSpec tests)
16
+ SMARTRAG_TEST_DB_HOST=
17
+ SMARTRAG_TEST_DB_PORT=
18
+ SMARTRAG_TEST_DB_NAME=
19
+ SMARTRAG_TEST_DB_USER=
20
+ SMARTRAG_TEST_DB_PASSWORD=
21
+
22
+ # =
23
+ # API Configuration
24
+ # =
25
+
26
+ # Optional: OpenAI API key (for LLM if you use OpenAI)
27
+ OPENAI_API_KEY=
28
+
29
+ # Media ingestion and built-in semantic extraction
30
+ MEDIA_MAX_FILE_SIZE_MB=50
31
+ MEDIA_MAX_DURATION_MS=7200000
32
+ MEDIA_COMMAND_TIMEOUT=120
33
+ MEDIA_ALLOW_PRIVATE_URLS=false
34
+ MEDIA_ALLOWED_HOSTS=
35
+ MEDIA_CONTENT_STORE_ENABLED=false
36
+ MEDIA_CONTENT_STORE_PROVIDER=local
37
+ MEDIA_CONTENT_STORE_ROOT=
38
+ MEDIA_S3_BUCKET=
39
+ MEDIA_S3_REGION=us-east-1
40
+ MEDIA_S3_PREFIX=smart-rag/media
41
+ MEDIA_S3_ENDPOINT=
42
+ MEDIA_S3_FORCE_PATH_STYLE=false
43
+ MEDIA_S3_ACCESS_KEY_ID=
44
+ MEDIA_S3_SECRET_ACCESS_KEY=
45
+ MEDIA_OPENAI_BASE_URL=https://api.openai.com
46
+ MEDIA_OPENAI_API_KEY=
47
+ MEDIA_VISION_MODEL=
48
+ MEDIA_TRANSCRIPTION_MODEL=
49
+ MEDIA_TRANSCRIPTION_LANGUAGE=
50
+ MEDIA_OCR_PROVIDER=
51
+ MEDIA_OCR_LANGUAGE=
52
+ MEDIA_JOB_MAX_ATTEMPTS=3
53
+ MEDIA_JOB_STALE_AFTER_SECONDS=900
54
+ MEDIA_JOB_RETENTION_SECONDS=604800
55
+ MEDIA_JOB_HEARTBEAT_INTERVAL_SECONDS=30
56
+ SMARTRAG_MEDIA_JOB_UPLOAD_DIR=
57
+
58
+ # HTTP authentication and per-principal quotas (0 disables a quota)
59
+ SMARTRAG_HTTP_AUTH_ENABLED=false
60
+ SMARTRAG_HTTP_PRINCIPAL=default
61
+ SMARTRAG_HTTP_TOKEN=
62
+ SMARTRAG_REQUESTS_PER_MINUTE=0
63
+ SMARTRAG_UPLOAD_BYTES_PER_DAY=0
64
+
65
+ # Embedding API configuration (default: local Ollama qwen3-embedding)
66
+ EMBEDDING_PROVIDER=local
67
+ EMBEDDING_API_KEY=ollama-local
68
+ EMBEDDING_ENDPOINT=http://localhost:11434/v1/embeddings
69
+ EMBEDDING_MODEL=qwen3-embedding
70
+ EMBEDDING_DIMENSIONS=4096
71
+ OLLAMA_BASE_URL=http://localhost:11434/v1/
72
+ OLLAMA_API_KEY=ollama-local
73
+
74
+ # LLM Configuration
75
+ LLM_PROVIDER=openai
76
+ LLM_API_KEY=ollama-local
77
+ LLM_ENDPOINT=http://localhost:11434/v1/chat/completions
78
+ LLM_MODEL=qwen3
79
+ SILICONFLOW_API_KEY=
80
+
81
+ # =
82
+ # Search Configuration
83
+ # =
84
+
85
+ # Default language for search (en, zh, ja, ko)
86
+ DEFAULT_LANGUAGE=
87
+
88
+ # Maximum search results
89
+ MAX_SEARCH_RESULTS=
90
+
91
+ # Hybrid search weights (0.0 to 1.0)
92
+ FULLTEXT_WEIGHT=
93
+ VECTOR_WEIGHT=
94
+
95
+ # RRF (Reciprocal Rank Fusion) parameter
96
+ RRF_K=
97
+
98
+ # Search result limits
99
+ DEFAULT_SEARCH_LIMIT=
100
+
101
+ # Tag boost weight for vector search (0.0 to 1.0)
102
+ TAG_BOOST_WEIGHT=
103
+
104
+ # Maximum distance threshold for relevant results (0.0 to 1.0)
105
+ MAX_DISTANCE_THRESHOLD=
106
+
107
+ # =
108
+ # Chinese Text Processing (pg_jieba)
109
+ # =
110
+
111
+ # Enable Chinese text segmentation
112
+ ENABLE_JIEBA=
113
+
114
+ # Optional: Path to custom dictionary
115
+ # JIEBA_DICT_PATH=
116
+
117
+ # =
118
+ # Document Chunking Configuration
119
+ # =
120
+
121
+ # Maximum characters per chunk
122
+ CHUNK_MAX_CHARS=
123
+
124
+ # Overlap between chunks (preserve context)
125
+ CHUNK_OVERLAP=
126
+
127
+ # Split by Markdown headers first
128
+ SPLIT_BY_HEADERS=
129
+
130
+ # Minimum chunk size (discard smaller chunks)
131
+ MIN_CHUNK_SIZE=
132
+
133
+ # =
134
+ # Document Processing
135
+ # =
136
+
137
+ # Maximum file size in MB
138
+ MAX_FILE_SIZE=
139
+
140
+ # Download timeout for URLs (seconds)
141
+ DOWNLOAD_TIMEOUT=
142
+
143
+ # =
144
+ # Redis Cache Configuration (Optional)
145
+ # =
146
+
147
+ # Enable search result caching
148
+ # CACHE_ENABLED=
149
+
150
+ # Redis URL for caching
151
+ # REDIS_URL=
152
+
153
+ # Cache TTL in seconds (default: 1 hour)
154
+ # CACHE_TTL=
155
+
156
+ # =
157
+ # Performance Settings
158
+ # =
159
+
160
+ # Thread pool size for parallel processing
161
+ THREAD_POOL_SIZE=
162
+
163
+ # Batch size for database operations
164
+ DB_BATCH_SIZE=
165
+
166
+ # Enable connection pooling
167
+ CONNECTION_POOLING=
168
+
169
+ # =
170
+ # Logging Configuration
171
+ # =
172
+
173
+ # Log level: debug, info, warn, error, fatal
174
+ LOG_LEVEL=
175
+
176
+ # Log format: json, plain
177
+ LOG_FORMAT=
178
+
179
+ # Optional: Log file path (leave empty for stdout)
180
+ # LOG_FILE_PATH=
181
+
182
+ # Enable query logging
183
+ ENABLE_QUERY_LOG=
184
+
185
+ # =
186
+ # LLM Generation Parameters
187
+ # =
188
+
189
+ # Temperature for LLM (0.0 to 2.0, higher =
190
+ LLM_TEMPERATURE=
191
+
192
+ # Maximum tokens to generate
193
+ LLM_MAX_TOKENS=
194
+
195
+ # LLM request timeout (seconds)
196
+ LLM_TIMEOUT=
197
+
198
+ # =
199
+ # Monitoring and Analytics
200
+ # =
201
+
202
+ # Log slow queries (over threshold)
203
+ LOG_SLOW_QUERIES=
204
+
205
+ # Slow query threshold in milliseconds
206
+ SLOW_QUERY_THRESHOLD=
207
+
208
+ # =
209
+ # Index Maintenance
210
+ # =
211
+
212
+ # Enable partitioned indexes
213
+ ENABLE_PARTITION=
214
+
215
+ # Enable auto vacuum
216
+ AUTO_VACUUM=
217
+
218
+ # Optional: Index rebuild schedule (cron format)
219
+ # INDEX_REBUILD_SCHEDULE=
220
+
221
+ # =
222
+ # Azure OpenAI Configuration (if using Azure)
223
+ # =
224
+
225
+ # AZURE_OPENAI_API_KEY=
226
+ # AZURE_OPENAI_ENDPOINT=
227
+ # AZURE_OPENAI_DEPLOYMENT_NAME=
228
+ # AZURE_OPENAI_API_VERSION=
229
+
230
+ # =
231
+ # Local Embedding Model Configuration
232
+ # =
233
+
234
+ # If using local embeddings (e.g., via Ollama)
235
+ # EMBEDDING_PROVIDER=local
236
+ # EMBEDDING_ENDPOINT=http://localhost:11434/v1/embeddings
237
+ # EMBEDDING_MODEL=qwen3-embedding
238
+ # EMBEDDING_DIMENSIONS=4096
239
+
240
+ # =
241
+ # Application Environment
242
+ # =
243
+
244
+ # Rack environment (development, test, production)
245
+ RACK_ENV=
246
+
247
+ # =
248
+ # Migration Control
249
+ # =
250
+
251
+ # Set to false to skip seed data
252
+ # SEED_DATA=
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/AGENTS.md ADDED
@@ -0,0 +1,33 @@
1
+ # Repository Guidelines
2
+
3
+ ## Project Structure & Module Organization
4
+ - `lib/` contains the SmartRAG library code. Core logic lives under `lib/smart_rag/core`, and services under `lib/smart_rag/services`.
5
+ - `test/` holds runnable scripts (e.g., `test_rag.rb`, `import_doc.rb`) and sample markdown documents used for manual testing.
6
+ - `spec/` contains RSpec tests (when present).
7
+ - `config/` stores application configuration, including database and full-text search settings.
8
+ - `db/` contains SQL scripts and database-related artifacts.
9
+
10
+ ## Build, Test, and Development Commands
11
+ - `bundle install` installs Ruby dependencies.
12
+ - `ruby test/import_doc.rb import` imports test documents into the local database.
13
+ - `ruby test/test_rag.rb` runs the end-to-end RAG tests in `test/`.
14
+ - `bundle exec rspec` runs any RSpec tests in `spec/`.
15
+
16
+ ## Coding Style & Naming Conventions
17
+ - Ruby files use 2-space indentation.
18
+ - Class/module names use `CamelCase`; methods and files use `snake_case`.
19
+ - Keep public API methods in `lib/smart_rag.rb` small and delegate to `core/` or `services/`.
20
+
21
+ ## Testing Guidelines
22
+ - End-to-end checks are in `test/test_rag.rb`; run after reindexing or data changes.
23
+ - RSpec tests (when present) should live in `spec/` and follow `*_spec.rb` naming.
24
+ - If a test depends on data, ensure `test/import_doc.rb` has been run.
25
+
26
+ ## Commit & Pull Request Guidelines
27
+ - Git history shows short messages like `fix bug`; there is no strict convention yet.
28
+ - Use concise, imperative commit messages (e.g., `fix fulltext indexing`).
29
+ - PRs should include: a short summary, steps to verify, and any data/setup notes.
30
+
31
+ ## Security & Configuration Tips
32
+ - Database credentials live in `config/smart_rag.yml` or environment variables (see `config/`).
33
+ - Avoid committing API keys; use `.env.example` as the template.