Skip to content

Commit

Permalink
refactor: based on example from go-openaai
Browse files Browse the repository at this point in the history
  • Loading branch information
barakplasma committed Apr 27, 2023
1 parent 526af06 commit b6ee3af
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"strings"
"time"

readability "github.com/go-shiori/go-readability"
Expand Down Expand Up @@ -31,23 +30,29 @@ func summarize(text string) string {
if len(text) < 200 {
return ""
}
c := openai.NewClient(openaiApiKey)
ctx := context.Background()

req := openai.CompletionRequest{
Model: openai.GPT3Dot5Turbo,
MaxTokens: 60,
Prompt: text + " \n\nTl;dr",
}
resp, err := c.CreateCompletion(ctx, req)
client := openai.NewClient(openaiApiKey)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleAssistant,
Content: "Summarize the following text:",
},
{
Role: openai.ChatMessageRoleUser,
Content: text,
},
},
},
)

if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return ""
}

// append ... if text does not end with .
if !strings.HasSuffix(resp.Choices[0].Text, ".") {
resp.Choices[0].Text = resp.Choices[0].Text + "..."
}

return resp.Choices[0].Text
return resp.Choices[0].Message.Content
}

0 comments on commit b6ee3af

Please sign in to comment.