checkpoint

This commit is contained in:
Rafael dos Santos Silva 2023-11-06 08:09:30 -08:00
parent 261fe13599
commit f115d8b851
No known key found for this signature in database
5 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
module DiscourseAi
module DiscordBot
class DiscordBotController < ::ApplicationController
requires_plugin ::DiscourseAi::PLUGIN_NAME
skip_before_action :verify_authenticity_token
MY_PUBLIC_KEY = "6056aea5db8de8f0c1aa2494e45db790fd4a68c607785a9933ed186025f1c8c6".freeze
def search
# Request signature verification
begin
verify_request!
rescue Ed25519::VerifyError
return head :unauthorized
end
body = JSON.parse(request.body.read)
if body['type'] == 1
# Respond to Discord PING request
render json: { type: 1 }
else
# Respond to /commands
response = { type: 4, data: { content: 'This is a response' } }
render json: response
end
end
private
def verify_request!
signature = request.headers['X-Signature-Ed25519']
timestamp = request.headers['X-Signature-Timestamp']
verify_key.verify([signature].pack('H*'), "#{timestamp}#{request.raw_post}")
end
def verify_key
Ed25519::VerifyKey.new([MY_PUBLIC_KEY].pack('H*')).freeze
end
end
end
end

View File

@ -18,6 +18,10 @@ DiscourseAi::Engine.routes.draw do
post "post/:post_id/stop-streaming" => "bot#stop_streaming_response" post "post/:post_id/stop-streaming" => "bot#stop_streaming_response"
get "bot-username" => "bot#show_bot_username" get "bot-username" => "bot#show_bot_username"
end end
scope module: :discord_bot, path: "/discord-bot", defaults: { format: :json } do
post "search" => "discord_bot#search"
end
end end
Discourse::Application.routes.append { mount ::DiscourseAi::Engine, at: "discourse-ai" } Discourse::Application.routes.append { mount ::DiscourseAi::Engine, at: "discourse-ai" }

View File

@ -0,0 +1,62 @@
# frozen_string_literal: true
module DiscourseAi
module Embeddings
class EntryPoint
def load_files
require_relative "model"
require_relative "topic"
require_relative "jobs/regular/generate_embeddings"
require_relative "semantic_related"
require_relative "semantic_search"
end
def inject_into(plugin)
plugin.add_to_class(:topic_view, :related_topics) do
if topic.private_message? || !SiteSetting.ai_embeddings_semantic_related_topics_enabled
return nil
end
@related_topics ||=
TopicList.new(
:suggested,
nil,
DiscourseAi::Embeddings::SemanticRelated.candidates_for(topic),
).topics
end
plugin.register_modifier(
:topic_view_suggested_topics_options,
) do |suggested_options, topic_view|
related_topics = topic_view.related_topics
include_random = related_topics.nil? || related_topics.length == 0
suggested_options.merge(include_random: include_random)
end
%i[topic_view TopicViewPosts].each do |serializer|
plugin.add_to_serializer(
serializer,
:related_topics,
include_condition: -> { SiteSetting.ai_embeddings_semantic_related_topics_enabled },
) do
if object.next_page.nil? && !object.topic.private_message?
object.related_topics.map do |t|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
end
end
end
end
callback =
Proc.new do |topic|
if SiteSetting.ai_embeddings_enabled
Jobs.enqueue(:generate_embeddings, topic_id: topic.id)
end
end
plugin.on(:topic_created, &callback)
plugin.on(:topic_edited, &callback)
end
end
end
end

View File

@ -9,6 +9,7 @@
gem "tokenizers", "0.3.2", platform: RUBY_PLATFORM gem "tokenizers", "0.3.2", platform: RUBY_PLATFORM
gem "tiktoken_ruby", "0.0.5", platform: RUBY_PLATFORM gem "tiktoken_ruby", "0.0.5", platform: RUBY_PLATFORM
gem "ed25519", "1.2.4", platform: RUBY_PLATFORM
enabled_site_setting :discourse_ai_enabled enabled_site_setting :discourse_ai_enabled