2023-12-08 08:42:56 +11:00
|
|
|
import { module, test } from "qunit";
|
|
|
|
import AiPersona from "discourse/plugins/discourse-ai/discourse/admin/models/ai-persona";
|
|
|
|
|
|
|
|
module("Discourse AI | Unit | Model | ai-persona", function () {
|
2025-03-21 14:46:33 -03:00
|
|
|
test("toPOJO", function (assert) {
|
2023-12-08 08:42:56 +11:00
|
|
|
const properties = {
|
2024-06-11 18:14:14 +10:00
|
|
|
tools: [
|
2025-03-21 14:46:33 -03:00
|
|
|
["ToolName", { option1: "value1", option2: "value2" }, false],
|
2024-06-11 18:14:14 +10:00
|
|
|
"ToolName2",
|
|
|
|
"ToolName3",
|
2023-12-08 08:42:56 +11:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
const aiPersonaPOJO = AiPersona.create(properties).toPOJO();
|
2023-12-08 08:42:56 +11:00
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
assert.deepEqual(aiPersonaPOJO.tools, [
|
|
|
|
"ToolName",
|
|
|
|
"ToolName2",
|
|
|
|
"ToolName3",
|
|
|
|
]);
|
|
|
|
assert.equal(aiPersonaPOJO.toolOptions["ToolName"].option1, "value1");
|
|
|
|
assert.equal(aiPersonaPOJO.toolOptions["ToolName"].option2, "value2");
|
2023-12-08 08:42:56 +11:00
|
|
|
});
|
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
test("fromPOJO", function (assert) {
|
2023-12-08 08:42:56 +11:00
|
|
|
const properties = {
|
|
|
|
id: 1,
|
|
|
|
name: "Test",
|
2025-03-21 14:46:33 -03:00
|
|
|
tools: [["ToolName", { option1: "value1" }, false]],
|
2023-12-08 08:42:56 +11:00
|
|
|
allowed_group_ids: [12],
|
|
|
|
system: false,
|
|
|
|
enabled: true,
|
|
|
|
system_prompt: "System Prompt",
|
|
|
|
priority: false,
|
|
|
|
description: "Description",
|
2024-02-03 07:09:34 +11:00
|
|
|
top_p: 0.8,
|
|
|
|
temperature: 0.7,
|
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
|
|
|
default_llm_id: 1,
|
2024-10-16 07:20:31 +11:00
|
|
|
force_default_llm: false,
|
2024-02-15 16:37:59 +11:00
|
|
|
user: null,
|
|
|
|
user_id: null,
|
|
|
|
max_context_posts: 5,
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-27 14:30:11 +11:00
|
|
|
vision_enabled: true,
|
|
|
|
vision_max_pixels: 100,
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 13:43:34 -03:00
|
|
|
rag_uploads: [],
|
2024-04-12 23:32:46 +10:00
|
|
|
rag_chunk_tokens: 374,
|
|
|
|
rag_chunk_overlap_tokens: 10,
|
|
|
|
rag_conversation_chunks: 10,
|
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
|
|
|
rag_llm_model_id: 1,
|
|
|
|
question_consolidator_llm_id: 2,
|
2024-05-06 09:49:02 +10:00
|
|
|
allow_chat: false,
|
2024-06-11 18:14:14 +10:00
|
|
|
tool_details: true,
|
2024-10-11 07:23:42 +11:00
|
|
|
forced_tool_count: -1,
|
2024-10-16 07:20:31 +11:00
|
|
|
allow_personal_messages: true,
|
|
|
|
allow_topic_mentions: true,
|
|
|
|
allow_chat_channel_mentions: true,
|
|
|
|
allow_chat_direct_messages: true,
|
2023-12-08 08:42:56 +11:00
|
|
|
};
|
2025-03-21 14:46:33 -03:00
|
|
|
const updatedValue = "updated";
|
2023-12-08 08:42:56 +11:00
|
|
|
|
|
|
|
const aiPersona = AiPersona.create({ ...properties });
|
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
const personaPOJO = aiPersona.toPOJO();
|
2023-12-08 08:42:56 +11:00
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
personaPOJO.toolOptions["ToolName"].option1 = updatedValue;
|
|
|
|
personaPOJO.forcedTools = "ToolName";
|
2023-12-08 08:42:56 +11:00
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
const updatedPersona = aiPersona.fromPOJO(personaPOJO);
|
2023-12-08 08:42:56 +11:00
|
|
|
|
2025-03-21 14:46:33 -03:00
|
|
|
assert.deepEqual(updatedPersona.tools, [
|
|
|
|
["ToolName", { option1: updatedValue }, true],
|
|
|
|
]);
|
2023-12-08 08:42:56 +11:00
|
|
|
});
|
|
|
|
});
|