forked from tremend-cofe/decidim-module-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add third party service #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Quentinchampenois
wants to merge
17
commits into
main
Choose a base branch
from
feat/third_party_service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5d967f0
feat(ai): Add Scaleway third-party AI service
Quentinchampenois b08c103
chore: Add README documentation
Quentinchampenois 9ee8df3
fix: error on InvalidResponse
Quentinchampenois 8ed6145
feat(ai): Add Scaleway Third Party strategy
Quentinchampenois 1bfebe6
fix(scaleway): Allow to refers reportable class
Quentinchampenois 06ce952
fix(scw): Third party service
Quentinchampenois 281e091
fix(jobs): Add third party obs
Quentinchampenois 6913064
chore: Update documentation
Quentinchampenois 4aa5328
fix: Scaleway third party service
Quentinchampenois 157161e
chore(doc): Add Third party and scaleway documentation
Quentinchampenois cdf5be9
chore(doc): Edit README
Quentinchampenois 93f74f0
fix: Multi-tenant reporting user
Quentinchampenois b94a6bd
fix: Add error handling
Quentinchampenois b5ec2e7
fix: Add organization host in third party analyzer
Quentinchampenois 145c1c0
fix: Handle Activator request timeout
Quentinchampenois 20158de
fix: Unify resource content into a single string
Quentinchampenois ec88085
fix: Remove exception rescue to let sidekiq deal with
Quentinchampenois File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/jobs/decidim/ai/spam_detection/third_party/generic_spam_analyzer_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Decidim | ||
| module Ai | ||
| module SpamDetection | ||
| module ThirdParty | ||
| class GenericSpamAnalyzerJob < Decidim::Ai::SpamDetection::GenericSpamAnalyzerJob | ||
| def perform(reportable, author, locale, fields) | ||
| @author = author | ||
| @organization = reportable.organization | ||
| klass = reportable.class.to_s | ||
| overall_score = I18n.with_locale(locale) do | ||
| contents = fields.map do |field| | ||
| content = translated_attribute(reportable.send(field)) | ||
| if content.present? | ||
| "### #{field}:\n#{content}" | ||
| else | ||
| "" | ||
| end | ||
| end | ||
|
|
||
| classifier.classify(contents.join("\n"), @organization.host, klass) | ||
| classifier.score | ||
| end | ||
|
|
||
| return unless overall_score >= Decidim::Ai::SpamDetection.resource_score_threshold | ||
|
|
||
| Decidim::CreateReport.call(form, reportable) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
41 changes: 41 additions & 0 deletions
41
app/jobs/decidim/ai/spam_detection/third_party/user_spam_analyzer_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Decidim | ||
| module Ai | ||
| module SpamDetection | ||
| module ThirdParty | ||
| class UserSpamAnalyzerJob < Decidim::Ai::SpamDetection::UserSpamAnalyzerJob | ||
| def perform(reportable) | ||
| @author = reportable | ||
| @organization = reportable.organization | ||
| klass = reportable.class.to_s | ||
| contents = [ | ||
| "### nickname:", | ||
| reportable.nickname.to_s, | ||
| "### about:", | ||
| translated_attribute(reportable.about).to_s, | ||
| "### locale:", | ||
| reportable.locale.to_s | ||
| ] | ||
|
|
||
| if reportable.personal_url.present? | ||
| contents << "### personal_url:" | ||
| contents << reportable.personal_url.to_s | ||
| end | ||
|
|
||
| classifier.classify(contents.join("\n"), @organization.host, klass) | ||
|
|
||
| return unless classifier.score >= Decidim::Ai::SpamDetection.user_score_threshold | ||
|
|
||
| if Decidim::UserModeration.find_by(user: reporting_user).present? | ||
| Rails.logger.warn("[decidim-ai] User already moderated: ##{reportable.id} #{reportable.nickname}") | ||
| return | ||
| end | ||
|
|
||
| Decidim::CreateUserReport.call(form, reportable) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Configure a Scaleway Third Party AI system to detect spam | ||
|
|
||
| ## Introduction to Scaleway Strategy | ||
|
|
||
| We've added a Scaleway strategy which inherits from the Third Party Strategy to abstract the AI system configuration to a third party service. It reduces considerably the configuration on the Decidim instance by defining only a endpoint and a secret parameters to connect to the Scaleway AI service. | ||
|
|
||
| ### How it works | ||
|
|
||
| The Scaleway strategy uses the Scaleway AI service to analyze content for spam detection. It sends the content to the Scaleway endpoint, which processes it and returns a response indicating whether the content is considered spam or not. | ||
|
|
||
| Outputs expected are JSON objects with the following structure: | ||
|
|
||
| ```json | ||
| { | ||
| "SPAM": "SPAM" | ||
| } | ||
| ``` | ||
| or | ||
| ```json | ||
| { | ||
| "SPAM": "NOT_SPAM" | ||
| } | ||
| ``` | ||
|
|
||
| Every time a contribution is made on Decidim, a POST request is sent to a serverless function endpoint. This endpoint retrieve the corresponding prompt based on the resource being analyzed (e.g., proposal, comment, etc.) and the type of analysis (resource or user). And it performs a POST request to the [Scaleway AI service](https://www.scaleway.com/en/docs/generative-apis/concepts/) with the content to be analyzed, the prompt, and the necessary parameters (temperature, top_p, etc…). | ||
|
|
||
| The whole AI specifications prompts, parameters, etc… are defined in a [Langfuse](https://github.com/langfuse/langfuse) self-hosted instance which allows to get metrics on the AI usage and to improve the prompts over time. | ||
|
|
||
| Every Decidim application connected to this system has the same prompts and parameters, which are defined in the Langfuse instance. This allows for a consistent spam detection experience across all applications using this strategy. | ||
|
|
||
| ## Infrastructure | ||
|
|
||
| ⚠️ We plan to share the Terraform (OpenTofu) project to deploy the serverless endpoint located at : https://github.com/OpenSourcePolitics/serverless/tree/main/faas_ai/infra | ||
|
|
||
|
|
||
| ## Getting started | ||
|
|
||
| To use a third-party AI system to detect spam, you need to configure the `decidim_ai` gem in your Decidim application. This guide will help you set up the necessary configurations. | ||
|
|
||
| ## Configure the AI module | ||
|
|
||
| Define a decidim-ai initializer in your application configuration : `config/initializers/decidim_ai.rb`: | ||
|
|
||
| ```ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| if Decidim.module_installed?(:ai) | ||
| analyzers = [ | ||
| { | ||
| name: :scaleway, | ||
| strategy: Decidim::Ai::SpamDetection::Strategy::Scaleway, | ||
| options: { | ||
| endpoint: Rails.application.secrets.dig(:decidim, :ai, :endpoint), | ||
| secret: Rails.application.secrets.dig(:decidim, :ai, :secret), | ||
| } | ||
| } | ||
| ] | ||
|
|
||
| Decidim::Ai::SpamDetection.resource_analyzers = analyzers | ||
| Decidim::Ai::SpamDetection.user_analyzers = analyzers | ||
| end | ||
| ``` | ||
|
|
||
| **A full example of configuration is available at examples/scaleway.rb** | ||
|
|
||
|
|
||
| Add the secrets to your `config/secrets.yml` file: | ||
|
|
||
| ```yaml | ||
| decidim: | ||
| ai: | ||
| endpoint: <%= Decidim::Env.new("DECIDIM_AI_ENDPOINT").to_s %> | ||
| secret: <%= Decidim::Env.new("DECIDIM_AI_SECRET").to_s %> | ||
| ``` | ||
|
|
||
| You can now run your server and start using the Scaleway AI service for spam detection ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Configure a Third Party AI system to detect spam | ||
|
|
||
| ## Getting started | ||
|
|
||
| To use a third-party AI system to detect spam, you need to configure the `decidim_ai` gem in your Decidim application. This guide will help you set up the necessary configurations. | ||
|
|
||
| ## Configure the AI module | ||
|
|
||
| Define a decidim-ai initializer in your application configuration : `config/initializers/decidim_ai.rb`: | ||
|
|
||
| ```ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| if Decidim.module_installed?(:ai) | ||
| analyzers = [ | ||
| { | ||
| name: :third_party, | ||
| strategy: Decidim::Ai::SpamDetection::Strategy::ThirdParty, | ||
| options: { | ||
| model: Rails.application.secrets.dig(:decidim, :ai, :model), | ||
| endpoint: Rails.application.secrets.dig(:decidim, :ai, :endpoint), | ||
| secret: Rails.application.secrets.dig(:decidim, :ai, :secret), | ||
| max_tokens: Rails.application.secrets.dig(:decidim, :ai, :max_tokens), | ||
| temperature: Rails.application.secrets.dig(:decidim, :ai, :temperature), | ||
| top_p: Rails.application.secrets.dig(:decidim, :ai, :top_p), | ||
| presence_penalty: Rails.application.secrets.dig(:decidim, :ai, :presence_penalty), | ||
| stream: Rails.application.secrets.dig(:decidim, :ai, :stream), | ||
| system_message: Rails.application.secrets.dig(:decidim, :ai, :system_message), | ||
| reporting_user_email: Rails.application.secrets.dig(:decidim, :ai, :reporting_user_email) | ||
| } | ||
| } | ||
| ] | ||
|
|
||
| Decidim::Ai::SpamDetection.resource_analyzers = analyzers | ||
| Decidim::Ai::SpamDetection.user_analyzers = analyzers | ||
| end | ||
| ``` | ||
|
|
||
| **A full example of configuration is available at examples/decidim_ai_third_party.rb** | ||
|
|
||
| Add the secrets to your `config/secrets.yml` file: | ||
|
|
||
| ```yaml | ||
| decidim: | ||
| ai: | ||
| model: <%= Decidim::Env.new("DECIDIM_AI_MODEL").to_s %> | ||
| endpoint: <%= Decidim::Env.new("DECIDIM_AI_ENDPOINT").to_s %> | ||
| secret: <%= Decidim::Env.new("DECIDIM_AI_SECRET").to_s %> | ||
| max_tokens: <%= Decidim::Env.new("DECIDIM_AI_MAX_TOKENS").to_i %> | ||
| temperature: <%= Decidim::Env.new("DECIDIM_AI_TEMPERATURE").to_f %> | ||
| top_p: <%= Decidim::Env.new("DECIDIM_AI_TOP_P").to_i %> | ||
| presence_penalty: <%= Decidim::Env.new("DECIDIM_AI_PRESENCE_PENALTY").to_i %> | ||
| stream: <%= Decidim::Env.new("DECIDIM_AI_STREAM") == "true" %> | ||
| system_message: <%= Decidim::Env.new("DECIDIM_AI_SYSTEM_MESSAGE").to_s %> | ||
| reporting_user_email: <%= Decidim::Env.new("DECIDIM_AI_REPORTING_USER_EMAIL").to_s %> | ||
| ``` | ||
|
|
||
| You can now run your server and start using the third-party AI service for spam detection ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| if Decidim.module_installed?(:ai) | ||
| if Rails.application.secrets.dig(:decidim, :ai, :endpoint).blank? || Rails.application.secrets.dig(:decidim, :ai, :secret).blank? | ||
| Rails.logger.warn "[decidim-ai] Initializer - AI endpoint or secret not configured. AI features will be disabled." | ||
| return | ||
| end | ||
|
|
||
| # Module configuration | ||
| Decidim::Ai::SpamDetection.reporting_user_email = Rails.application.secrets.dig(:decidim, :ai, :reporting_user_email) | ||
| Decidim::Ai::Language.formatter = "Decidim::Ai::Language::Formatter" | ||
| Decidim::Ai::SpamDetection.user_models = { | ||
| "Decidim::User" => "Decidim::Ai::SpamDetection::Resource::UserBaseEntity" | ||
| } | ||
| Decidim::Ai::SpamDetection.resource_models = begin | ||
| models = {} | ||
| models["Decidim::Comments::Comment"] = "Decidim::Ai::SpamDetection::Resource::Comment" if Decidim.module_installed?("comments") | ||
| models["Decidim::Debates::Debate"] = "Decidim::Ai::SpamDetection::Resource::Debate" if Decidim.module_installed?("debates") | ||
| models["Decidim::Initiative"] = "Decidim::Ai::SpamDetection::Resource::Initiative" if Decidim.module_installed?("initiatives") | ||
| models["Decidim::Meetings::Meeting"] = "Decidim::Ai::SpamDetection::Resource::Meeting" if Decidim.module_installed?("meetings") | ||
| models["Decidim::Proposals::Proposal"] = "Decidim::Ai::SpamDetection::Resource::Proposal" if Decidim.module_installed?("proposals") | ||
| if Decidim.module_installed?("proposals") | ||
| models["Decidim::Proposals::CollaborativeDraft"] = | ||
| "Decidim::Ai::SpamDetection::Resource::CollaborativeDraft" | ||
| end | ||
| models | ||
| end | ||
|
|
||
| # Configuring Scaleway strategy | ||
| analyzers = [ | ||
| { | ||
| name: :scaleway, | ||
| strategy: Decidim::Ai::SpamDetection::Strategy::Scaleway, | ||
| options: { | ||
| endpoint: Rails.application.secrets.dig(:decidim, :ai, :endpoint), | ||
| secret: Rails.application.secrets.dig(:decidim, :ai, :secret) | ||
| } | ||
| } | ||
| ] | ||
|
|
||
| Decidim::Ai::SpamDetection.resource_analyzers = analyzers | ||
| Decidim::Ai::SpamDetection.user_analyzers = analyzers | ||
|
|
||
| # Configuring Third Party services | ||
| Decidim::Ai::SpamDetection.user_detection_service = "Decidim::Ai::SpamDetection::ThirdPartyService" | ||
| Decidim::Ai::SpamDetection.resource_detection_service = "Decidim::Ai::SpamDetection::ThirdPartyService" | ||
|
|
||
| # Configuring Third Party jobs | ||
| Decidim::Ai::SpamDetection.user_spam_analyzer_job = "Decidim::Ai::SpamDetection::ThirdParty::UserSpamAnalyzerJob" | ||
| Decidim::Ai::SpamDetection.generic_spam_analyzer_job = "Decidim::Ai::SpamDetection::ThirdParty::GenericSpamAnalyzerJob" | ||
|
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we do not need to register a custom analyzer after the changes are added to main repo. |
||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure that we would really need the spam class as input data. I think a spam message is spam regardless of the place is posted. But nevertheless, i would like to find out why you need it.