From 6d1af7fd88bdcb2e467de2441d12263462822914 Mon Sep 17 00:00:00 2001 From: noona Date: Fri, 1 Jul 2022 03:49:55 +0000 Subject: [PATCH 1/2] Added support for Moderations API --- moderation.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 moderation.go diff --git a/moderation.go b/moderation.go new file mode 100644 index 000000000..6ecfb72f2 --- /dev/null +++ b/moderation.go @@ -0,0 +1,69 @@ +package gogpt + +import ( + "bytes" + "context" + "encoding/json" + "net/http" +) + +// ModerationRequest represents a request structure for moderation API +type ModerationRequest struct { + Input string `json:"input,omitempty"` + Model *string `json:"model,omitempty"` +} + +// Result represents one of possible moderation results +type Result struct { + Categories []ResultCategories `json:"categories"` + CategoryScores []ResultCategoryScores `json:"category_scores"` + Flagged int `json:"flagged"` +} + +// ResultCategories represents Categories of Result +type ResultCategories struct { + Hate int `json:"hate"` + HateThreatening int `json:"hate/threatening"` + SelfHarm int `json:"self-harm"` + Sexual int `json:"sexual"` + SexualMinors int `json:"sexual/minors"` + Violence int `json:"violence"` + ViolenceGraphic int `json:"violence/graphic"` +} + +// ResultCategoryScores represents CategoryScores of Result +type ResultCategoryScores struct { + Hate float32 `json:"hate"` + HateThreatening float32 `json:"hate/threatening"` + SelfHarm float32 `json:"self-harm"` + Sexual float32 `json:"sexual"` + SexualMinors float32 `json:"sexual/minors"` + Violence float32 `json:"violence"` + ViolenceGraphic float32 `json:"violence/graphic"` +} + +// ModerationResponse represents a response structure for moderation API +type ModerationResponse struct { + ID string `json:"id"` + Model string `json:"model"` + Results []Result `json:"results"` +} + +// Moderations — perform a moderation api call over a string. +// Input can be an array or slice but a string will reduce the complexity. +func (c *Client) Moderations(ctx context.Context, request ModerationRequest) (response ModerationResponse, err error) { + var reqBytes []byte + reqBytes, err = json.Marshal(request) + if err != nil { + return + } + + req, err := http.NewRequest("POST", c.fullURL("/moderations"), bytes.NewBuffer(reqBytes)) + if err != nil { + return + } + + req = req.WithContext(ctx) + err = c.sendRequest(req, &response) + return +} From 54a73c7bba4f2f4fa9eec216775f9c8e6021804b Mon Sep 17 00:00:00 2001 From: galpt Date: Fri, 1 Jul 2022 10:51:59 +0700 Subject: [PATCH 2/2] gofmt moderation.go --- moderation.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/moderation.go b/moderation.go index 6ecfb72f2..d7ef1fce2 100644 --- a/moderation.go +++ b/moderation.go @@ -9,37 +9,37 @@ import ( // ModerationRequest represents a request structure for moderation API type ModerationRequest struct { - Input string `json:"input,omitempty"` + Input string `json:"input,omitempty"` Model *string `json:"model,omitempty"` } // Result represents one of possible moderation results type Result struct { - Categories []ResultCategories `json:"categories"` + Categories []ResultCategories `json:"categories"` CategoryScores []ResultCategoryScores `json:"category_scores"` - Flagged int `json:"flagged"` + Flagged int `json:"flagged"` } // ResultCategories represents Categories of Result type ResultCategories struct { - Hate int `json:"hate"` - HateThreatening int `json:"hate/threatening"` - SelfHarm int `json:"self-harm"` - Sexual int `json:"sexual"` - SexualMinors int `json:"sexual/minors"` - Violence int `json:"violence"` - ViolenceGraphic int `json:"violence/graphic"` + Hate int `json:"hate"` + HateThreatening int `json:"hate/threatening"` + SelfHarm int `json:"self-harm"` + Sexual int `json:"sexual"` + SexualMinors int `json:"sexual/minors"` + Violence int `json:"violence"` + ViolenceGraphic int `json:"violence/graphic"` } // ResultCategoryScores represents CategoryScores of Result type ResultCategoryScores struct { - Hate float32 `json:"hate"` - HateThreatening float32 `json:"hate/threatening"` - SelfHarm float32 `json:"self-harm"` - Sexual float32 `json:"sexual"` - SexualMinors float32 `json:"sexual/minors"` - Violence float32 `json:"violence"` - ViolenceGraphic float32 `json:"violence/graphic"` + Hate float32 `json:"hate"` + HateThreatening float32 `json:"hate/threatening"` + SelfHarm float32 `json:"self-harm"` + Sexual float32 `json:"sexual"` + SexualMinors float32 `json:"sexual/minors"` + Violence float32 `json:"violence"` + ViolenceGraphic float32 `json:"violence/graphic"` } // ModerationResponse represents a response structure for moderation API