Skip to content

Commit

Permalink
adding chat moderator example
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielluizsf committed May 24, 2024
1 parent 664d323 commit a15a9d1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
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

0 comments on commit a15a9d1

Please sign in to comment.