discourse-ai/db/post_migrate
Sam 5e80f93e4c
FEATURE: PDF support for rag pipeline (#1118)
This PR introduces several enhancements and refactorings to the AI Persona and RAG (Retrieval-Augmented Generation) functionalities within the discourse-ai plugin. Here's a breakdown of the changes:

**1. LLM Model Association for RAG and Personas:**

-   **New Database Columns:** Adds `rag_llm_model_id` to both `ai_personas` and `ai_tools` tables. This allows specifying a dedicated LLM for RAG indexing, separate from the persona's primary LLM.  Adds `default_llm_id` and `question_consolidator_llm_id` to `ai_personas`.
-   **Migration:**  Includes a migration (`20250210032345_migrate_persona_to_llm_model_id.rb`) to populate the new `default_llm_id` and `question_consolidator_llm_id` columns in `ai_personas` based on the existing `default_llm` and `question_consolidator_llm` string columns, and a post migration to remove the latter.
-   **Model Changes:**  The `AiPersona` and `AiTool` models now `belong_to` an `LlmModel` via `rag_llm_model_id`. The `LlmModel.proxy` method now accepts an `LlmModel` instance instead of just an identifier.  `AiPersona` now has `default_llm_id` and `question_consolidator_llm_id` attributes.
-   **UI Updates:**  The AI Persona and AI Tool editors in the admin panel now allow selecting an LLM for RAG indexing (if PDF/image support is enabled).  The RAG options component displays an LLM selector.
-   **Serialization:** The serializers (`AiCustomToolSerializer`, `AiCustomToolListSerializer`, `LocalizedAiPersonaSerializer`) have been updated to include the new `rag_llm_model_id`, `default_llm_id` and `question_consolidator_llm_id` attributes.

**2. PDF and Image Support for RAG:**

-   **Site Setting:** Introduces a new hidden site setting, `ai_rag_pdf_images_enabled`, to control whether PDF and image files can be indexed for RAG. This defaults to `false`.
-   **File Upload Validation:** The `RagDocumentFragmentsController` now checks the `ai_rag_pdf_images_enabled` setting and allows PDF, PNG, JPG, and JPEG files if enabled.  Error handling is included for cases where PDF/image indexing is attempted with the setting disabled.
-   **PDF Processing:** Adds a new utility class, `DiscourseAi::Utils::PdfToImages`, which uses ImageMagick (`magick`) to convert PDF pages into individual PNG images. A maximum PDF size and conversion timeout are enforced.
-   **Image Processing:** A new utility class, `DiscourseAi::Utils::ImageToText`, is included to handle OCR for the images and PDFs.
-   **RAG Digestion Job:** The `DigestRagUpload` job now handles PDF and image uploads. It uses `PdfToImages` and `ImageToText` to extract text and create document fragments.
-   **UI Updates:**  The RAG uploader component now accepts PDF and image file types if `ai_rag_pdf_images_enabled` is true. The UI text is adjusted to indicate supported file types.

**3. Refactoring and Improvements:**

-   **LLM Enumeration:** The `DiscourseAi::Configuration::LlmEnumerator` now provides a `values_for_serialization` method, which returns a simplified array of LLM data (id, name, vision_enabled) suitable for use in serializers. This avoids exposing unnecessary details to the frontend.
-   **AI Helper:** The `AiHelper::Assistant` now takes optional `helper_llm` and `image_caption_llm` parameters in its constructor, allowing for greater flexibility.
-   **Bot and Persona Updates:** Several updates were made across the codebase, changing the string based association to a LLM to the new model based.
-   **Audit Logs:** The `DiscourseAi::Completions::Endpoints::Base` now formats raw request payloads as pretty JSON for easier auditing.
- **Eval Script:** An evaluation script is included.

**4. Testing:**

-    The PR introduces a new eval system for LLMs, this allows us to test how functionality works across various LLM providers. This lives in `/evals`
2025-02-14 12:15:07 +11:00
..
20240724174343_migrate_vision_llms.rb DEV: Remove old code now that features rely on LlmModels. (#729) 2024-07-30 13:44:57 -03:00
20240729202857_migrate_persona_llm_override.rb DEV: Remove old code now that features rely on LlmModels. (#729) 2024-07-30 13:44:57 -03:00
20240809162837_rename_ai_helper_enabled_setting.rb DEV: Clearly separate post/composer helper settings (#747) 2024-08-12 15:40:23 -07:00
20240809163303_rename_ai_helper_allowed_groups_setting.rb DEV: Clearly separate post/composer helper settings (#747) 2024-08-12 15:40:23 -07:00
20240912055831_drop_persona_id_from_rag_document_fragments.rb FEATURE: Make tool support polymorphic (#798) 2024-09-16 08:17:17 +10:00
20241008055831_drop_old_embeddings_indexes.rb FEATURE: Index embeddings using bit vectors (#824) 2024-10-14 13:26:03 -03:00
20241014041242_ai_persona_post_migrate_drop_cols.rb FEATURE: smarter persona tethering (#832) 2024-10-16 07:20:31 +11:00
20241031041242_migrate_sentiment_classification_result_format.rb Migrate sentiment to a TEI backend (#886) 2024-11-04 09:14:34 -03:00
20241206115958_rebake_shared_ai_conversation_oneboxes.rb SECURITY: Fix XSS on Shared AI Conversations local Onebox (#1069) 2025-01-14 18:05:37 -03:00
20250113171444_drop_old_embedding_tables.rb FIX: Split backfill into separate migrations to use independent transactions (#1063) 2025-01-14 13:30:52 -03:00
20250114184356_drop_old_embedding_tables2.rb FIX: Missing table check in post_migration (#1068) 2025-01-14 17:33:01 -03:00
20250115181147_drop_ai_summaries_content_range.rb FIX: Make summaries backfill job more resilient. (#1071) 2025-01-16 09:42:53 -03:00
20250210032351_post_migrate_persona_to_llm_model_id.rb FEATURE: PDF support for rag pipeline (#1118) 2025-02-14 12:15:07 +11:00