Skip to content

Commit

Permalink
Merge pull request #173 from NetSepio/vaibhavvvvv/dev
Browse files Browse the repository at this point in the history
api: update summary api to stream response
  • Loading branch information
p-shubh authored May 29, 2024
2 parents 2b7e032 + 051b22d commit c845c60
Showing 1 changed file with 52 additions and 16 deletions.
68 changes: 52 additions & 16 deletions api/v1/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package summary

import (
"context"
"fmt"
"io"
"net/http"

"github.com/NetSepio/gateway/config/envconfig"
Expand All @@ -14,11 +16,6 @@ type RequestData struct {
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")
{
Expand All @@ -33,25 +30,23 @@ func summary(c *gin.Context) {
return
}

termsSummary, err := generateSummary(requestData.Terms)
writer := c.Writer
writer.WriteHeader(http.StatusOK)

_, err := generateAndStreamSummary(requestData.Terms, writer, "Terms Summary")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
fmt.Fprintf(writer, "\nerror: %v\n", err)
return
}

privacySummary, err := generateSummary(requestData.Privacy)
_, err = generateAndStreamSummary(requestData.Privacy, writer, "Privacy Summary")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
fmt.Fprintf(writer, "\nerror: %v\n", err)
return
}

c.JSON(http.StatusOK, SummaryResponse{
TermsSummary: termsSummary,
PrivacySummary: privacySummary,
})
}

func generateSummary(content string) (string, error) {
func generateAndStreamSummary(content string, writer io.Writer, title string) (string, error) {
open_ai_key := envconfig.EnvVars.OPENAI_API_KEY

client := openai.NewClient(open_ai_key)
Expand All @@ -71,5 +66,46 @@ func generateSummary(content string) (string, error) {
return "", nil
}

return resp.Choices[0].Text, nil
summary := resp.Choices[0].Text

_, err = writer.Write([]byte(fmt.Sprintf("\n--- %s ---\n", title)))
if err != nil {
return "", err
}

words := splitIntoWords(summary)
for _, word := range words {
_, err := writer.Write([]byte(word + " "))
if err != nil {
return "", err
}
writer.(http.Flusher).Flush()
}

return summary, nil
}

func splitIntoWords(text string) []string {
words := make([]string, 0)
currentWord := ""

for _, char := range text {
if char == ' ' || char == '\n' || char == '\t' {
if currentWord != "" {
words = append(words, currentWord)
currentWord = ""
}
if char != ' ' {
words = append(words, string(char))
}
} else {
currentWord += string(char)
}
}

if currentWord != "" {
words = append(words, currentWord)
}

return words
}

0 comments on commit c845c60

Please sign in to comment.