Skip to content

Commit

Permalink
Merge pull request #172 from NetSepio/vaibhavvvvv/dev
Browse files Browse the repository at this point in the history
api: added summary api for terms of use and privacy policy
  • Loading branch information
p-shubh authored May 26, 2024
2 parents 9f01514 + 43a11d7 commit 2b7e032
Showing 4 changed files with 81 additions and 0 deletions.
75 changes: 75 additions & 0 deletions api/v1/summary/summary.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions api/v1/v1.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 2b7e032

Please sign in to comment.