openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Moderation Api : missing harassment and harassment/threatening metrics and flag

Open Aelentel opened this issue 1 year ago • 3 comments

calling the Moderation API result in the following payload :

however the java model "ModerationResult" doesn't have the following flags & metrics : harassment & harassment/threatening

{
  "id": "modr-<SOMEID>",
  "model": "text-moderation-005",
  "results": [
    {
      "flagged": true,
      "categories": {
        "sexual": false,
        "hate": false,
        "harassment": false,
        "self-harm": false,
        "sexual/minors": false,
        "hate/threatening": false,
        "violence/graphic": false,
        "self-harm/intent": false,
        "self-harm/instructions": false,
        "harassment/threatening": true,
        "violence": false
      },
      "category_scores": {
        "sexual": 0.0005017801886424422,
        "hate": 0.0000533161437488161,
        "harassment": 0.1876802295446396,
        "self-harm": 0.00004789013109984808,
        "sexual/minors": 1.1327256288495846e-6,
        "hate/threatening": 0.0005069859325885773,
        "violence/graphic": 0.00003967898010159843,
        "self-harm/intent": 0.000017482938346802257,
        "self-harm/instructions": 5.0221064640254554e-8,
        "harassment/threatening": 0.47996777296066284,
        "violence": 0.8792107701301575
      }
    }
  ]
}

Aelentel avatar Jan 18 '24 04:01 Aelentel

@Aelentel Working on this and will raise a PR soon

remyohajinwa avatar Jan 30 '24 14:01 remyohajinwa

This can be handled dynamically.

import java.util.ArrayList;
import java.util.HashMap;
import lombok.Data;
@Data
public class CategoryResults {
	
	HashMap<String, Boolean> categories = new HashMap<String, Boolean>();
	HashMap<String, Double> category_scores = new HashMap<String, Double>();
	Boolean flagged = false;
	
	public CategoryResults(HashMap<String, Boolean> categories, HashMap<String, Double> category_scores,
			Boolean flagged) {
		super();
		this.categories = categories;
		this.category_scores = category_scores;
		this.flagged = flagged;
	}
	
	public ArrayList<String> reasons() {
		ArrayList<String> rzns = new ArrayList<String>();
        for (String key : categories.keySet()) {
        	if (categories.get(key)) {
        		rzns.add(key);
        	}
        }
        return rzns;
	}

}
import java.util.ArrayList;
import java.util.Optional;
import lombok.Data;
@Data
public class Moderation {

	String id = null;
	String model = "text-moderation-latest";
	ArrayList<CategoryResults> results = new ArrayList<CategoryResults>();

	@Override
	public String toString() {
		if (results != null && results.stream().anyMatch(CategoryResults::getFlagged)) {
			Optional<CategoryResults> cr = results.stream().findFirst();
		    return String.join(", ", cr.get().reasons());
		} else {
			return "no particular reason.";
		}
	}

	public Moderation(CategoryResults violation) {
		super();
		results.add(violation);
	}
	
}
       //         HttpResponse<String> response = {API request};
	public static Gson gs = new GsonBuilder().setPrettyPrinting().create();
	Moderation mod = gs.fromJson(response.body(), Moderation.class);

cryptoapebot avatar Jan 30 '24 14:01 cryptoapebot

@Aelentel Working on this and will raise a PR soon

do you know if @TheoKanning is letting this project down ? there's a lots of PR not answered. this project was pretty alive 2 month ago, and as such i based one of the company project on it, kinda want to fork it and fix/merge some because time is running short. no pressure though, but it's strange to see those PR accumulate

Aelentel avatar Jan 30 '24 19:01 Aelentel