Skip to content

Add moderations API to OpenAI::Client

What does this MR do and why?

This MR adds moderations api to Gitlab::Llm::OpenAi::Client for users of this client to utilize.

Example response:

Gitlab::Llm::OpenAi::Client.new(User.first).moderations(input: 'hi')

=> {"id"=>"modr-7AEg8b8vwgoLno0lJKDJ6BEyjO8Pf",
 "model"=>"text-moderation-004",
 "results"=>
  [{"flagged"=>false,
    "categories"=>{"sexual"=>false, "hate"=>false, "violence"=>false, "self-harm"=>false, "sexual/minors"=>false, "hate/threatening"=>false, "violence/graphic"=>false},
    "category_scores"=>
     {"sexual"=>7.04759731888771e-05,
      "hate"=>1.410793828426904e-07,
      "violence"=>1.953629436002302e-08,
      "self-harm"=>1.1341324580860235e-11,
      "sexual/minors"=>2.1905959712853473e-08,
      "hate/threatening"=>4.442902643009283e-12,
      "violence/graphic"=>2.6168814337701463e-10}}]}

Mentions https://gitlab.com/gitlab-org/gitlab/-/issues/408171

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

  1. Open rails console rails c
  2. Set your OpenAI key Gitlab::CurrentSettings.update!(openai_api_key: "")
  3. Perform a sample request and observe results Gitlab::Llm::OpenAi::Client.new(User.first).moderations(input: 'hi')
  4. Verify response is from OpenAI moderations endpoint

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by George Koltsov

Merge request reports