2023-05-05 15:28:31 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
class BotController < ::ApplicationController
|
|
|
|
requires_plugin ::DiscourseAi::PLUGIN_NAME
|
|
|
|
requires_login
|
|
|
|
|
2024-11-12 08:14:30 +11:00
|
|
|
def show_debug_info_by_id
|
|
|
|
log = AiApiAuditLog.find(params[:id])
|
|
|
|
raise Discourse::NotFound if !log.topic
|
|
|
|
|
|
|
|
guardian.ensure_can_debug_ai_bot_conversation!(log.topic)
|
|
|
|
render json: AiApiAuditLogSerializer.new(log, root: false), status: 200
|
|
|
|
end
|
|
|
|
|
2024-04-15 23:22:06 +10:00
|
|
|
def show_debug_info
|
|
|
|
post = Post.find(params[:post_id])
|
|
|
|
guardian.ensure_can_debug_ai_bot_conversation!(post)
|
|
|
|
|
|
|
|
posts =
|
|
|
|
Post
|
|
|
|
.where("post_number <= ?", post.post_number)
|
|
|
|
.where(topic_id: post.topic_id)
|
|
|
|
.order("post_number DESC")
|
|
|
|
|
|
|
|
debug_info = AiApiAuditLog.where(post: posts).order(created_at: :desc).first
|
|
|
|
|
2024-06-27 10:43:00 -03:00
|
|
|
render json: AiApiAuditLogSerializer.new(debug_info, root: false), status: 200
|
2024-04-15 23:22:06 +10:00
|
|
|
end
|
|
|
|
|
2023-05-05 15:28:31 -03:00
|
|
|
def stop_streaming_response
|
|
|
|
post = Post.find(params[:post_id])
|
|
|
|
guardian.ensure_can_see!(post)
|
|
|
|
|
|
|
|
Discourse.redis.del("gpt_cancel:#{post.id}")
|
|
|
|
|
|
|
|
render json: {}, status: 200
|
|
|
|
end
|
2023-05-16 14:38:21 -03:00
|
|
|
|
|
|
|
def show_bot_username
|
2024-06-18 14:32:14 -03:00
|
|
|
bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model(params[:username])
|
|
|
|
raise Discourse::InvalidParameters.new(:username) if !bot_user
|
2023-05-16 14:38:21 -03:00
|
|
|
|
2024-06-18 14:32:14 -03:00
|
|
|
render json: { bot_username: bot_user.username_lower }, status: 200
|
2023-05-16 14:38:21 -03:00
|
|
|
end
|
2025-02-20 14:37:58 -03:00
|
|
|
|
|
|
|
def discover
|
|
|
|
ai_persona =
|
|
|
|
AiPersona.all_personas.find do |persona|
|
|
|
|
persona.id == SiteSetting.ai_bot_discover_persona.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
if ai_persona.nil? || !current_user.in_any_groups?(ai_persona.allowed_group_ids.to_a)
|
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
|
|
|
|
if ai_persona.default_llm_id.blank?
|
|
|
|
render_json_error "Discover persona is missing a default LLM model.", status: 503
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
query = params[:query]
|
|
|
|
raise Discourse::InvalidParameters.new("Missing query to discover") if query.blank?
|
|
|
|
|
|
|
|
RateLimiter.new(current_user, "ai_bot_discover_#{current_user.id}", 3, 1.minute).performed!
|
|
|
|
|
|
|
|
Jobs.enqueue(:stream_discover_reply, user_id: current_user.id, query: query)
|
|
|
|
|
|
|
|
render json: {}, status: 200
|
|
|
|
end
|
2023-05-05 15:28:31 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|