aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2023-04-07 07:51:25 -0400
committerXe Iaso <me@xeiaso.net>2023-04-07 07:51:25 -0400
commit4c15f8fa4ce26a8e43ff1a86c316d5f25f899a1c (patch)
tree15bf749161c6b100465f53c124adf08fe8c5f7a0 /web
parent500611047b6b38740283695d471354e676f1a390 (diff)
downloadx-4c15f8fa4ce26a8e43ff1a86c316d5f25f899a1c.tar.xz
x-4c15f8fa4ce26a8e43ff1a86c316d5f25f899a1c.zip
fix build
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'web')
-rw-r--r--web/cryptocompare/cryptocompare.go2
-rw-r--r--web/openai/chatgpt/chatgpt.go (renamed from web/chatgpt/chatgpt.go)0
-rw-r--r--web/openai/moderation/moderation.go77
3 files changed, 78 insertions, 1 deletions
diff --git a/web/cryptocompare/cryptocompare.go b/web/cryptocompare/cryptocompare.go
index 6886f77..8321e07 100644
--- a/web/cryptocompare/cryptocompare.go
+++ b/web/cryptocompare/cryptocompare.go
@@ -37,7 +37,7 @@ func Get(symbol string, currencies []string) (map[string]float64, error) {
result := make(map[string]float64)
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
- return nil, fmt.Errorf("cryptocompare: can't decode result: %w")
+ return nil, fmt.Errorf("cryptocompare: can't decode result: %w", err)
}
return result, nil
diff --git a/web/chatgpt/chatgpt.go b/web/openai/chatgpt/chatgpt.go
index d0b70dc..d0b70dc 100644
--- a/web/chatgpt/chatgpt.go
+++ b/web/openai/chatgpt/chatgpt.go
diff --git a/web/openai/moderation/moderation.go b/web/openai/moderation/moderation.go
new file mode 100644
index 0000000..6127d9c
--- /dev/null
+++ b/web/openai/moderation/moderation.go
@@ -0,0 +1,77 @@
+package moderation
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "net/http"
+)
+
+type Request struct {
+ Input string `json:"input"`
+}
+
+type Response struct {
+ ID string `json:"id"`
+ Model string `json:"model"`
+ Results []Results `json:"results"`
+}
+type Categories 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"`
+}
+type CategoryScores struct {
+ Hate float64 `json:"hate"`
+ HateThreatening float64 `json:"hate/threatening"`
+ SelfHarm float64 `json:"self-harm"`
+ Sexual float64 `json:"sexual"`
+ SexualMinors float64 `json:"sexual/minors"`
+ Violence float64 `json:"violence"`
+ ViolenceGraphic float64 `json:"violence/graphic"`
+}
+type Results struct {
+ Categories Categories `json:"categories"`
+ CategoryScores CategoryScores `json:"category_scores"`
+ Flagged bool `json:"flagged"`
+}
+
+type Client struct {
+ httpCli *http.Client
+ apiKey string
+}
+
+func (c Client) Check(ctx context.Context, input string) (*Response, error) {
+ data := Request{
+ Input: input,
+ }
+ payloadBytes, err := json.Marshal(data)
+ if err != nil {
+ return nil, err
+ }
+ body := bytes.NewReader(payloadBytes)
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.openai.com/v1/moderations", body)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Authorization", "Bearer "+c.apiKey)
+ req.Header.Set("Content-Type", "application/json")
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ var result Response
+ if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
+ return nil, err
+ }
+
+ return &result, nil
+}