Current section
Files
Jump to
Current section
Files
src/glopenai@moderation.erl
-module(glopenai@moderation).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glopenai/moderation.gleam").
-export([new_create_request/1, with_model/2, mod_input_type_to_json/1, moderation_content_part_to_json/1, moderation_input_to_json/1, create_moderation_request_to_json/1, categories_to_json/1, category_score_to_json/1, category_applied_input_types_to_json/1, mod_input_type_decoder/0, moderation_content_part_decoder/0, moderation_input_decoder/0, categories_decoder/0, category_score_decoder/0, category_applied_input_types_decoder/0, content_moderation_result_decoder/0, create_request/2, create_response/1]).
-export_type([moderation_input/0, moderation_content_part/0, mod_input_type/0, create_moderation_request/0, categories/0, category_score/0, category_applied_input_types/0, content_moderation_result/0, create_moderation_response/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type moderation_input() :: {string_input, binary()} |
{string_array_input, list(binary())} |
{multi_modal_input, list(moderation_content_part())}.
-type moderation_content_part() :: {text_part, binary()} |
{image_url_part, binary()}.
-type mod_input_type() :: text_input | image_input.
-type create_moderation_request() :: {create_moderation_request,
moderation_input(),
gleam@option:option(binary())}.
-type categories() :: {categories,
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean(),
boolean()}.
-type category_score() :: {category_score,
float(),
float(),
float(),
float(),
float(),
float(),
float(),
float(),
float(),
float(),
float(),
float(),
float()}.
-type category_applied_input_types() :: {category_applied_input_types,
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type()),
list(mod_input_type())}.
-type content_moderation_result() :: {content_moderation_result,
boolean(),
categories(),
category_score(),
category_applied_input_types()}.
-type create_moderation_response() :: {create_moderation_response,
binary(),
binary(),
list(content_moderation_result())}.
-file("src/glopenai/moderation.gleam", 119).
?DOC(" Create a new moderation request with the required input.\n").
-spec new_create_request(moderation_input()) -> create_moderation_request().
new_create_request(Input) ->
{create_moderation_request, Input, none}.
-file("src/glopenai/moderation.gleam", 126).
?DOC(" Set the moderation model.\n").
-spec with_model(create_moderation_request(), binary()) -> create_moderation_request().
with_model(Request, Model) ->
{create_moderation_request, erlang:element(2, Request), {some, Model}}.
-file("src/glopenai/moderation.gleam", 135).
-spec mod_input_type_to_json(mod_input_type()) -> gleam@json:json().
mod_input_type_to_json(Input_type) ->
gleam@json:string(case Input_type of
text_input ->
<<"text"/utf8>>;
image_input ->
<<"image"/utf8>>
end).
-file("src/glopenai/moderation.gleam", 142).
-spec moderation_content_part_to_json(moderation_content_part()) -> gleam@json:json().
moderation_content_part_to_json(Part) ->
case Part of
{text_part, Text} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"text"/utf8>>)},
{<<"text"/utf8>>, gleam@json:string(Text)}]
);
{image_url_part, Url} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"image_url"/utf8>>)},
{<<"image_url"/utf8>>, gleam@json:string(Url)}]
)
end.
-file("src/glopenai/moderation.gleam", 157).
-spec moderation_input_to_json(moderation_input()) -> gleam@json:json().
moderation_input_to_json(Input) ->
case Input of
{string_input, S} ->
gleam@json:string(S);
{string_array_input, Arr} ->
gleam@json:array(Arr, fun gleam@json:string/1);
{multi_modal_input, Parts} ->
gleam@json:array(Parts, fun moderation_content_part_to_json/1)
end.
-file("src/glopenai/moderation.gleam", 165).
-spec create_moderation_request_to_json(create_moderation_request()) -> gleam@json:json().
create_moderation_request_to_json(Request) ->
glopenai@internal@codec:object_with_optional(
[{<<"input"/utf8>>,
moderation_input_to_json(erlang:element(2, Request))}],
[glopenai@internal@codec:optional_field(
<<"model"/utf8>>,
erlang:element(3, Request),
fun gleam@json:string/1
)]
).
-file("src/glopenai/moderation.gleam", 174).
-spec categories_to_json(categories()) -> gleam@json:json().
categories_to_json(Categories) ->
gleam@json:object(
[{<<"hate"/utf8>>, gleam@json:bool(erlang:element(2, Categories))},
{<<"hate/threatening"/utf8>>,
gleam@json:bool(erlang:element(3, Categories))},
{<<"harassment"/utf8>>,
gleam@json:bool(erlang:element(4, Categories))},
{<<"harassment/threatening"/utf8>>,
gleam@json:bool(erlang:element(5, Categories))},
{<<"illicit"/utf8>>, gleam@json:bool(erlang:element(6, Categories))},
{<<"illicit/violent"/utf8>>,
gleam@json:bool(erlang:element(7, Categories))},
{<<"self-harm"/utf8>>,
gleam@json:bool(erlang:element(8, Categories))},
{<<"self-harm/intent"/utf8>>,
gleam@json:bool(erlang:element(9, Categories))},
{<<"self-harm/instructions"/utf8>>,
gleam@json:bool(erlang:element(10, Categories))},
{<<"sexual"/utf8>>, gleam@json:bool(erlang:element(11, Categories))},
{<<"sexual/minors"/utf8>>,
gleam@json:bool(erlang:element(12, Categories))},
{<<"violence"/utf8>>,
gleam@json:bool(erlang:element(13, Categories))},
{<<"violence/graphic"/utf8>>,
gleam@json:bool(erlang:element(14, Categories))}]
).
-file("src/glopenai/moderation.gleam", 192).
-spec category_score_to_json(category_score()) -> gleam@json:json().
category_score_to_json(Scores) ->
gleam@json:object(
[{<<"hate"/utf8>>, gleam@json:float(erlang:element(2, Scores))},
{<<"hate/threatening"/utf8>>,
gleam@json:float(erlang:element(3, Scores))},
{<<"harassment"/utf8>>, gleam@json:float(erlang:element(4, Scores))},
{<<"harassment/threatening"/utf8>>,
gleam@json:float(erlang:element(5, Scores))},
{<<"illicit"/utf8>>, gleam@json:float(erlang:element(6, Scores))},
{<<"illicit/violent"/utf8>>,
gleam@json:float(erlang:element(7, Scores))},
{<<"self-harm"/utf8>>, gleam@json:float(erlang:element(8, Scores))},
{<<"self-harm/intent"/utf8>>,
gleam@json:float(erlang:element(9, Scores))},
{<<"self-harm/instructions"/utf8>>,
gleam@json:float(erlang:element(10, Scores))},
{<<"sexual"/utf8>>, gleam@json:float(erlang:element(11, Scores))},
{<<"sexual/minors"/utf8>>,
gleam@json:float(erlang:element(12, Scores))},
{<<"violence"/utf8>>, gleam@json:float(erlang:element(13, Scores))},
{<<"violence/graphic"/utf8>>,
gleam@json:float(erlang:element(14, Scores))}]
).
-file("src/glopenai/moderation.gleam", 210).
-spec category_applied_input_types_to_json(category_applied_input_types()) -> gleam@json:json().
category_applied_input_types_to_json(Types) ->
gleam@json:object(
[{<<"hate"/utf8>>,
gleam@json:array(
erlang:element(2, Types),
fun mod_input_type_to_json/1
)},
{<<"hate/threatening"/utf8>>,
gleam@json:array(
erlang:element(3, Types),
fun mod_input_type_to_json/1
)},
{<<"harassment"/utf8>>,
gleam@json:array(
erlang:element(4, Types),
fun mod_input_type_to_json/1
)},
{<<"harassment/threatening"/utf8>>,
gleam@json:array(
erlang:element(5, Types),
fun mod_input_type_to_json/1
)},
{<<"illicit"/utf8>>,
gleam@json:array(
erlang:element(6, Types),
fun mod_input_type_to_json/1
)},
{<<"illicit/violent"/utf8>>,
gleam@json:array(
erlang:element(7, Types),
fun mod_input_type_to_json/1
)},
{<<"self-harm"/utf8>>,
gleam@json:array(
erlang:element(8, Types),
fun mod_input_type_to_json/1
)},
{<<"self-harm/intent"/utf8>>,
gleam@json:array(
erlang:element(9, Types),
fun mod_input_type_to_json/1
)},
{<<"self-harm/instructions"/utf8>>,
gleam@json:array(
erlang:element(10, Types),
fun mod_input_type_to_json/1
)},
{<<"sexual"/utf8>>,
gleam@json:array(
erlang:element(11, Types),
fun mod_input_type_to_json/1
)},
{<<"sexual/minors"/utf8>>,
gleam@json:array(
erlang:element(12, Types),
fun mod_input_type_to_json/1
)},
{<<"violence"/utf8>>,
gleam@json:array(
erlang:element(13, Types),
fun mod_input_type_to_json/1
)},
{<<"violence/graphic"/utf8>>,
gleam@json:array(
erlang:element(14, Types),
fun mod_input_type_to_json/1
)}]
).
-file("src/glopenai/moderation.gleam", 250).
-spec mod_input_type_decoder() -> gleam@dynamic@decode:decoder(mod_input_type()).
mod_input_type_decoder() ->
gleam@dynamic@decode:then(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Value) -> case Value of
<<"text"/utf8>> ->
gleam@dynamic@decode:success(text_input);
<<"image"/utf8>> ->
gleam@dynamic@decode:success(image_input);
_ ->
gleam@dynamic@decode:failure(
text_input,
<<"ModInputType"/utf8>>
)
end end
).
-file("src/glopenai/moderation.gleam", 259).
-spec moderation_content_part_decoder() -> gleam@dynamic@decode:decoder(moderation_content_part()).
moderation_content_part_decoder() ->
gleam@dynamic@decode:field(
<<"type"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Tag) -> case Tag of
<<"text"/utf8>> ->
gleam@dynamic@decode:field(
<<"text"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Text) ->
gleam@dynamic@decode:success({text_part, Text})
end
);
<<"image_url"/utf8>> ->
gleam@dynamic@decode:field(
<<"image_url"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:success({image_url_part, Url})
end
);
_ ->
gleam@dynamic@decode:failure(
{text_part, <<""/utf8>>},
<<"ModerationContentPart"/utf8>>
)
end end
).
-file("src/glopenai/moderation.gleam", 276).
-spec moderation_input_decoder() -> gleam@dynamic@decode:decoder(moderation_input()).
moderation_input_decoder() ->
gleam@dynamic@decode:one_of(
begin
_pipe = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:map(
_pipe,
fun(Field@0) -> {string_input, Field@0} end
)
end,
[begin
_pipe@1 = gleam@dynamic@decode:list(
moderation_content_part_decoder()
),
gleam@dynamic@decode:map(
_pipe@1,
fun(Field@0) -> {multi_modal_input, Field@0} end
)
end,
begin
_pipe@2 = gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
gleam@dynamic@decode:map(
_pipe@2,
fun(Field@0) -> {string_array_input, Field@0} end
)
end]
).
-file("src/glopenai/moderation.gleam", 284).
-spec categories_decoder() -> gleam@dynamic@decode:decoder(categories()).
categories_decoder() ->
gleam@dynamic@decode:field(
<<"hate"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Hate) ->
gleam@dynamic@decode:field(
<<"hate/threatening"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Hate_threatening) ->
gleam@dynamic@decode:field(
<<"harassment"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Harassment) ->
gleam@dynamic@decode:field(
<<"harassment/threatening"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(Harassment_threatening) ->
gleam@dynamic@decode:field(
<<"illicit"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(Illicit) ->
gleam@dynamic@decode:field(
<<"illicit/violent"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(Illicit_violent) ->
gleam@dynamic@decode:field(
<<"self-harm"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(Self_harm) ->
gleam@dynamic@decode:field(
<<"self-harm/intent"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Self_harm_intent
) ->
gleam@dynamic@decode:field(
<<"self-harm/instructions"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Self_harm_instructions
) ->
gleam@dynamic@decode:field(
<<"sexual"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Sexual
) ->
gleam@dynamic@decode:field(
<<"sexual/minors"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Sexual_minors
) ->
gleam@dynamic@decode:field(
<<"violence"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Violence
) ->
gleam@dynamic@decode:field(
<<"violence/graphic"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_bool/1},
fun(
Violence_graphic
) ->
gleam@dynamic@decode:success(
{categories,
Hate,
Hate_threatening,
Harassment,
Harassment_threatening,
Illicit,
Illicit_violent,
Self_harm,
Self_harm_intent,
Self_harm_instructions,
Sexual,
Sexual_minors,
Violence,
Violence_graphic}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/moderation.gleam", 321).
-spec category_score_decoder() -> gleam@dynamic@decode:decoder(category_score()).
category_score_decoder() ->
gleam@dynamic@decode:field(
<<"hate"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Hate) ->
gleam@dynamic@decode:field(
<<"hate/threatening"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Hate_threatening) ->
gleam@dynamic@decode:field(
<<"harassment"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Harassment) ->
gleam@dynamic@decode:field(
<<"harassment/threatening"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(Harassment_threatening) ->
gleam@dynamic@decode:field(
<<"illicit"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(Illicit) ->
gleam@dynamic@decode:field(
<<"illicit/violent"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(Illicit_violent) ->
gleam@dynamic@decode:field(
<<"self-harm"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(Self_harm) ->
gleam@dynamic@decode:field(
<<"self-harm/intent"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(
Self_harm_intent
) ->
gleam@dynamic@decode:field(
<<"self-harm/instructions"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(
Self_harm_instructions
) ->
gleam@dynamic@decode:field(
<<"sexual"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(
Sexual
) ->
gleam@dynamic@decode:field(
<<"sexual/minors"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(
Sexual_minors
) ->
gleam@dynamic@decode:field(
<<"violence"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(
Violence
) ->
gleam@dynamic@decode:field(
<<"violence/graphic"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_float/1},
fun(
Violence_graphic
) ->
gleam@dynamic@decode:success(
{category_score,
Hate,
Hate_threatening,
Harassment,
Harassment_threatening,
Illicit,
Illicit_violent,
Self_harm,
Self_harm_intent,
Self_harm_instructions,
Sexual,
Sexual_minors,
Violence,
Violence_graphic}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/moderation.gleam", 358).
-spec category_applied_input_types_decoder() -> gleam@dynamic@decode:decoder(category_applied_input_types()).
category_applied_input_types_decoder() ->
gleam@dynamic@decode:field(
<<"hate"/utf8>>,
gleam@dynamic@decode:list(mod_input_type_decoder()),
fun(Hate) ->
gleam@dynamic@decode:field(
<<"hate/threatening"/utf8>>,
gleam@dynamic@decode:list(mod_input_type_decoder()),
fun(Hate_threatening) ->
gleam@dynamic@decode:field(
<<"harassment"/utf8>>,
gleam@dynamic@decode:list(mod_input_type_decoder()),
fun(Harassment) ->
gleam@dynamic@decode:field(
<<"harassment/threatening"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder()
),
fun(Harassment_threatening) ->
gleam@dynamic@decode:field(
<<"illicit"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder()
),
fun(Illicit) ->
gleam@dynamic@decode:field(
<<"illicit/violent"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder()
),
fun(Illicit_violent) ->
gleam@dynamic@decode:field(
<<"self-harm"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(Self_harm) ->
gleam@dynamic@decode:field(
<<"self-harm/intent"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(
Self_harm_intent
) ->
gleam@dynamic@decode:field(
<<"self-harm/instructions"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(
Self_harm_instructions
) ->
gleam@dynamic@decode:field(
<<"sexual"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(
Sexual
) ->
gleam@dynamic@decode:field(
<<"sexual/minors"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(
Sexual_minors
) ->
gleam@dynamic@decode:field(
<<"violence"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(
Violence
) ->
gleam@dynamic@decode:field(
<<"violence/graphic"/utf8>>,
gleam@dynamic@decode:list(
mod_input_type_decoder(
)
),
fun(
Violence_graphic
) ->
gleam@dynamic@decode:success(
{category_applied_input_types,
Hate,
Hate_threatening,
Harassment,
Harassment_threatening,
Illicit,
Illicit_violent,
Self_harm,
Self_harm_intent,
Self_harm_instructions,
Sexual,
Sexual_minors,
Violence,
Violence_graphic}
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/glopenai/moderation.gleam", 421).
-spec content_moderation_result_decoder() -> gleam@dynamic@decode:decoder(content_moderation_result()).
content_moderation_result_decoder() ->
gleam@dynamic@decode:field(
<<"flagged"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Flagged) ->
gleam@dynamic@decode:field(
<<"categories"/utf8>>,
categories_decoder(),
fun(Categories) ->
gleam@dynamic@decode:field(
<<"category_scores"/utf8>>,
category_score_decoder(),
fun(Category_scores) ->
gleam@dynamic@decode:field(
<<"category_applied_input_types"/utf8>>,
category_applied_input_types_decoder(),
fun(Category_applied_input_types) ->
gleam@dynamic@decode:success(
{content_moderation_result,
Flagged,
Categories,
Category_scores,
Category_applied_input_types}
)
end
)
end
)
end
)
end
).
-file("src/glopenai/moderation.gleam", 442).
-spec create_moderation_response_decoder() -> gleam@dynamic@decode:decoder(create_moderation_response()).
create_moderation_response_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"model"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Model) ->
gleam@dynamic@decode:field(
<<"results"/utf8>>,
gleam@dynamic@decode:list(
content_moderation_result_decoder()
),
fun(Results) ->
gleam@dynamic@decode:success(
{create_moderation_response, Id, Model, Results}
)
end
)
end
)
end
).
-file("src/glopenai/moderation.gleam", 461).
?DOC(" Build a request to create a moderation.\n").
-spec create_request(glopenai@config:config(), create_moderation_request()) -> gleam@http@request:request(binary()).
create_request(Config, Params) ->
glopenai@internal:post_request(
Config,
<<"/moderations"/utf8>>,
create_moderation_request_to_json(Params)
).
-file("src/glopenai/moderation.gleam", 473).
?DOC(" Parse the response from creating a moderation.\n").
-spec create_response(gleam@http@response:response(binary())) -> {ok,
create_moderation_response()} |
{error, glopenai@error:glopenai_error()}.
create_response(Response) ->
glopenai@internal:parse_response(
Response,
create_moderation_response_decoder()
).