Skip to content

Commit

Permalink
add check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Oct 19, 2022
1 parent ccae619 commit 1f807e0
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 50 deletions.
3 changes: 2 additions & 1 deletion ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ AI as a service

# Ai Service

Provides easy use of OpenAI endpoints for text completion and hate speech moderation
Provides easy use of OpenAI endpoints for text completion, hate speech moderation, content generation
and much more. Limits to 1000 tokens per request.
10 changes: 10 additions & 0 deletions ai/config/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"text": "\n\nLeonardo da Vinci was an Italian Renaissance architect, musician, inventor, engineer, and painter."
}
}],
"check": [{
"title": "Check text",
"run_check": false,
"request": {
"text": "What day of the wek is it?"
},
"response": {
"text": "What is the day of the week?\n"
}
}],
"moderate": [{
"title": "Moderate hate speech",
"run_check": false,
Expand Down
1 change: 1 addition & 0 deletions ai/config/publicapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"display_name": "AI",
"pricing": {
"Ai.Call": 50000,
"Ai.Check": 50000,
"Ai.Moderate": 50000
}
}
35 changes: 35 additions & 0 deletions ai/handler/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ func (e *Ai) Call(ctx context.Context, req *pb.CallRequest, rsp *pb.CallResponse
return nil
}

func (e *Ai) Check(ctx context.Context, req *pb.CheckRequest, rsp *pb.CheckResponse) error {
if len(req.Text) == 0 {
return errors.BadRequest("ai.check", "missing text")
}

uri := "https://api.openai.com/v1/edits"

if len(req.Instruction) == 0 {
req.Instruction = "Check the spelling and grammar"
}

var resp map[string]interface{}
if err := api.Post(uri, map[string]interface{}{
"model": "text-davinci-edit-001",
"input": req.Text,
"instruction": req.Instruction,
}, &resp); err != nil {
log.Errorf("Failed AI call: %v\n", err)
return errors.InternalServerError("ai.check", "Failed to make request")
}

v := resp["choices"]
if v == nil {
return nil
}

// get first choice
choice := v.([]interface{})[0].(map[string]interface{})

// set response text
rsp.Text = choice["text"].(string)

return nil
}

func (e *Ai) Moderate(ctx context.Context, req *pb.ModerateRequest, rsp *pb.ModerateResponse) error {
if len(req.Text) == 0 {
return errors.BadRequest("ai.moderate", "missing text")
Expand Down

0 comments on commit 1f807e0

Please sign in to comment.