Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI moderations implemented 🚀 #13

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions example/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"encoding/json"
"fmt"
"log"
"strings"

"github.com/Simplou/openai"
)
Expand Down Expand Up @@ -73,7 +75,7 @@ func chatByEmbedding(largeText string, query string) {
Model: "text-embedding-ada-002",
Input: chunks,
})
if err != nil{
if err != nil {
return &openai.EmbeddingResponse[[]float64]{}, nil
}
return emb, nil
Expand Down Expand Up @@ -103,8 +105,48 @@ func chatByEmbedding(largeText string, query string) {
relevantChunks = append(relevantChunks, chunks[i])
}
summary, err := openai.ChunksSummary(client, httpClient, relevantChunks, query)
if err != nil{
if err != nil {
log.Println(err)
}
log.Println(summary)
}

func chatModerator(customerMessage string) {
moderation, err := openai.Moderator(client, httpClient, &openai.ModerationRequest[string]{
Input: customerMessage,
})
if err != nil {
log.Println(err)
}
categories := make([]string, 0)
for _, v := range moderation.Results {
if v.Flagged {
for category, value := range v.Categories {
if value {
categories = append(categories, category)
}
}
}
}
if len(categories) == 0 {
res, err := openai.ChatCompletion[openai.DefaultMessages](
client,
httpClient,
&openai.CompletionRequest[openai.DefaultMessages]{
Model: "gpt-3.5-turbo",
Messages: openai.DefaultMessages{
{Role: "user", Content: customerMessage},
},
},
)
if err != nil {
log.Println(err)
}
log.Println(res.Choices[0].Message.Content)
} else {
s := strings.Join(categories, ", ")
moderatorMessage := fmt.Sprintf("Your statement contains several disrespectful things: (%s)", s)
log.Println(moderatorMessage)
}

}
4 changes: 4 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func main() {
`,
"oi, o que é o actor paradigm?",
)
badSentence := "I want to kill them."
query := "Hi, could you please explain what the actor paradigm is?"
chatModerator(badSentence)
chatModerator(query)
//functionCall()
//tts()
//whisper()
Expand Down
46 changes: 46 additions & 0 deletions moderation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package openai

import (
"encoding/json"

"github.com/Simplou/goxios"
)

type ModerationRequest[Input string | []string] struct {
Input string `json:"input"`
Model string `json:"model,omitempty"`
}

type ModerationResponse struct {
Id string `json:"id"`
Model string `json:"model"`
Results []struct {
Flagged bool `json:"flagged"`
Categories goxios.GenericJSON[bool] `json:"categories"`
} `json:"results"`
CategoryScores goxios.GenericJSON[float64] `json:"category_scores"`
}

func Moderator[Input string | []string](api OpenAIClient, httpClient HTTPClient, body *ModerationRequest[Input]) (*ModerationResponse, *OpenAIErr) {
api.AddHeader(contentTypeJSON)
b, err := json.Marshal(body)
if err != nil {
return nil, errCannotMarshalJSON(err)
}
options := goxios.RequestOpts{
Body: ioReader(b),
Headers: Headers(),
}
res, err := httpClient.Post(api.BaseURL()+"/moderations", &options)
if err != nil {
return nil, errCannotSendRequest(err)
}
response := new(ModerationResponse)
if err := goxios.DecodeJSON(res.Body, response); err != nil {
return nil, errCannotDecodeJSON(err)
}
if err := res.Body.Close(); err != nil {
return nil, errCloseBody(err)
}
return response, nil
}
Loading