From 43a11d75b7e75b3eccf4a36cf788055180c3aa8e Mon Sep 17 00:00:00 2001 From: vaibhavvvvv Date: Sat, 25 May 2024 14:59:31 +0530 Subject: [PATCH] api: added summary api for terms of use and privacy policy --- api/v1/summary/summary.go | 75 +++++++++++++++++++++++++++++++++++++++ api/v1/v1.go | 2 ++ go.mod | 2 ++ go.sum | 2 ++ 4 files changed, 81 insertions(+) create mode 100644 api/v1/summary/summary.go diff --git a/api/v1/summary/summary.go b/api/v1/summary/summary.go new file mode 100644 index 0000000..8d9a549 --- /dev/null +++ b/api/v1/summary/summary.go @@ -0,0 +1,75 @@ +package summary + +import ( + "context" + "net/http" + + "github.com/NetSepio/gateway/config/envconfig" + "github.com/gin-gonic/gin" + openai "github.com/sashabaranov/go-openai" +) + +type RequestData struct { + Terms string `json:"terms" binding:"required"` + Privacy string `json:"privacy" binding:"required"` +} + +type SummaryResponse struct { + TermsSummary string `json:"terms_summary"` + PrivacySummary string `json:"privacy_summary"` +} + +func ApplyRoutes(r *gin.RouterGroup) { + g := r.Group("/summary") + { + g.POST("", summary) + } +} + +func summary(c *gin.Context) { + var requestData RequestData + if err := c.ShouldBindJSON(&requestData); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + termsSummary, err := generateSummary(requestData.Terms) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + privacySummary, err := generateSummary(requestData.Privacy) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, SummaryResponse{ + TermsSummary: termsSummary, + PrivacySummary: privacySummary, + }) +} + +func generateSummary(content string) (string, error) { + open_ai_key := envconfig.EnvVars.OPENAI_API_KEY + + client := openai.NewClient(open_ai_key) + + req := openai.CompletionRequest{ + Model: "gpt-3.5-turbo-instruct", + Prompt: "Summarize the following text in key points:\n\n" + content, + MaxTokens: 150, + } + + resp, err := client.CreateCompletion(context.Background(), req) + if err != nil { + return "", err + } + + if len(resp.Choices) == 0 { + return "", nil + } + + return resp.Choices[0].Text, nil +} diff --git a/api/v1/v1.go b/api/v1/v1.go index 2ddc203..86f9f9f 100644 --- a/api/v1/v1.go +++ b/api/v1/v1.go @@ -18,6 +18,7 @@ import ( "github.com/NetSepio/gateway/api/v1/sotreus" "github.com/NetSepio/gateway/api/v1/stats" "github.com/NetSepio/gateway/api/v1/status" + "github.com/NetSepio/gateway/api/v1/summary" "github.com/NetSepio/gateway/api/v1/waitlist" "github.com/gin-gonic/gin" @@ -45,5 +46,6 @@ func ApplyRoutes(r *gin.RouterGroup) { account.ApplyRoutes(v1) siteinsights.ApplyRoutes(v1) subscription.ApplyRoutes(v1) + summary.ApplyRoutes(v1) } } diff --git a/go.mod b/go.mod index 960b5d6..91972ea 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,8 @@ require ( gorm.io/gorm v1.23.7 ) +require github.com/sashabaranov/go-openai v1.24.1 // indirect + require ( cloud.google.com/go/compute v1.23.3 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect diff --git a/go.sum b/go.sum index 2ea740c..b7af2a0 100644 --- a/go.sum +++ b/go.sum @@ -509,6 +509,8 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sashabaranov/go-openai v1.24.1 h1:DWK95XViNb+agQtuzsn+FyHhn3HQJ7Va8z04DQDJ1MI= +github.com/sashabaranov/go-openai v1.24.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=