From 95692f2d5e076ae4ded920cb323e3d85b8e8b7f0 Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Thu, 27 Apr 2023 14:26:15 +0000 Subject: [PATCH 1/3] feat: switch to GPT3.5 for cost savings --- go.mod | 1 + go.sum | 2 ++ summarize.go | 8 ++++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 056bee0..691d59c 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( require ( github.com/go-shiori/dom v0.0.0-20210627111528-4e4722cd0d65 // indirect github.com/gogs/chardet v0.0.0-20191104214054-4b6791f73a28 // indirect + github.com/sashabaranov/go-openai v1.9.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect ) diff --git a/go.sum b/go.sum index 6a1664d..4cb2f48 100644 --- a/go.sum +++ b/go.sum @@ -242,6 +242,8 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sashabaranov/go-gpt3 v1.0.1 h1:KHwY4uroFlX1qI1Hui7d31ZI6uzbNGL9zAkh1FkfhuM= github.com/sashabaranov/go-gpt3 v1.0.1/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ= +github.com/sashabaranov/go-openai v1.9.0 h1:NoiO++IISxxJ1pRc0n7uZvMGMake0G+FJ1XPwXtprsA= +github.com/sashabaranov/go-openai v1.9.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/savioxavier/termlink v1.2.1 h1:O9ZQvk9BPQQK4JQeMB56ZfV8uam0Ts+f97mJme7+dq8= github.com/savioxavier/termlink v1.2.1/go.mod h1:WA7FTALNwN41NGnmQMIrnjAYTsEhIAZ4RuzgEiB0Jp8= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= diff --git a/summarize.go b/summarize.go index ed38299..ea2059d 100644 --- a/summarize.go +++ b/summarize.go @@ -7,7 +7,7 @@ import ( "time" readability "github.com/go-shiori/go-readability" - gogpt "github.com/sashabaranov/go-gpt3" + openai "github.com/sashabaranov/go-openai" ) func getSummaryFromLink(url string) string { @@ -31,11 +31,11 @@ func summarize(text string) string { if len(text) < 200 { return "" } - c := gogpt.NewClient(openaiApiKey) + c := openai.NewClient(openaiApiKey) ctx := context.Background() - req := gogpt.CompletionRequest{ - Model: gogpt.GPT3TextDavinci003, + req := openai.CompletionRequest{ + Model: openai.GPT3Dot5Turbo, MaxTokens: 60, Prompt: text + " \n\nTl;dr", } From 526af06b4941e0e7bc24a2c1409f20f8b2c6dbfe Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Thu, 27 Apr 2023 14:29:58 +0000 Subject: [PATCH 2/3] fix: remove older library --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 4cb2f48..32a4672 100644 --- a/go.sum +++ b/go.sum @@ -240,8 +240,6 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sashabaranov/go-gpt3 v1.0.1 h1:KHwY4uroFlX1qI1Hui7d31ZI6uzbNGL9zAkh1FkfhuM= -github.com/sashabaranov/go-gpt3 v1.0.1/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ= github.com/sashabaranov/go-openai v1.9.0 h1:NoiO++IISxxJ1pRc0n7uZvMGMake0G+FJ1XPwXtprsA= github.com/sashabaranov/go-openai v1.9.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/savioxavier/termlink v1.2.1 h1:O9ZQvk9BPQQK4JQeMB56ZfV8uam0Ts+f97mJme7+dq8= From b6ee3af01d471c9127f346abbe84a0aec89e56b4 Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Thu, 27 Apr 2023 14:52:54 +0000 Subject: [PATCH 3/3] refactor: based on example from go-openaai --- summarize.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/summarize.go b/summarize.go index ea2059d..6a60783 100644 --- a/summarize.go +++ b/summarize.go @@ -3,7 +3,6 @@ package main import ( "context" "fmt" - "strings" "time" readability "github.com/go-shiori/go-readability" @@ -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 }