-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from NetSepio/vaibhavvvvv/dev
api: added summary api for terms of use and privacy policy
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters