Files
go-openai/moderation.go
Oleg d63df93c65 Add OpenAI Mock Server (#31)
* add constants for completions, refactor usage, add test server

Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>

* append v1 endpoint to test

Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>

* add makefile for easy targets

Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>

* lint files & add linter

Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>

* disable real API tests in short mode

Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>

Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>
2022-08-11 15:29:23 +06:00

70 lines
2.0 KiB
Go

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 bool `json:"flagged"`
}
// ResultCategories represents Categories of Result.
type ResultCategories struct {
Hate bool `json:"hate"`
HateThreatening bool `json:"hate/threatening"`
SelfHarm bool `json:"self-harm"`
Sexual bool `json:"sexual"`
SexualMinors bool `json:"sexual/minors"`
Violence bool `json:"violence"`
ViolenceGraphic bool `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
}