1
1
package handlers
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"fmt"
8
+ "io"
7
9
"log"
8
10
"net/http"
9
11
"os"
12
+ "time"
10
13
11
14
"github.com/gofor-little/env"
12
15
"github.com/google/generative-ai-go/genai"
13
16
"google.golang.org/api/option"
14
17
)
15
18
19
+ type Snippet struct {
20
+ Title string `json:"title"`
21
+ Category string `json:"category"`
22
+ Content string `json:"content"`
23
+ DateTime time.Time `json:"dateTime"`
24
+ Notes string `json:"notes"`
25
+ }
26
+
16
27
func GeminiHandler (w http.ResponseWriter , r * http.Request ) {
17
28
29
+ if r .Method != http .MethodPost {
30
+ http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
31
+ return
32
+ }
33
+
34
+ var bodyBytes bytes.Buffer
35
+ if _ , err := io .Copy (& bodyBytes , r .Body ); err != nil {
36
+ http .Error (w , "Error reading request body" , http .StatusBadRequest )
37
+ return
38
+ }
39
+ r .Body .Close ()
40
+
41
+ requestBody := bodyBytes .String ()
42
+
43
+ var snippets []Snippet
44
+ err := json .Unmarshal ([]byte (requestBody ), & snippets )
45
+ if err != nil {
46
+ fmt .Println ("Error unmarshalling JSON:" , err )
47
+ return
48
+ }
49
+
50
+ var mergedContent string
51
+ for _ , snippet := range snippets {
52
+ mergedContent += snippet .Content
53
+ }
54
+
18
55
// Assuming getResponseFromGemini returns the generated text as a string
19
- generatedText , err := getResponseFromGemini (w )
56
+ generatedText , err := getResponseFromGemini (w , mergedContent )
20
57
21
58
if err != nil {
22
59
// Handle error appropriately (e.g., log the error and return a generic error message)
@@ -42,7 +79,7 @@ func GeminiHandler(w http.ResponseWriter, r *http.Request) {
42
79
}
43
80
}
44
81
45
- func getResponseFromGemini (w http.ResponseWriter ) (string , error ) {
82
+ func getResponseFromGemini (w http.ResponseWriter , mergedContent string ) (string , error ) {
46
83
ctx := context .Background ()
47
84
48
85
// Replace with your actual API key
@@ -53,13 +90,13 @@ func getResponseFromGemini(w http.ResponseWriter) (string, error) {
53
90
defer client .Close ()
54
91
55
92
model := client .GenerativeModel ("gemini-1.5-flash" )
56
- resp , err := model .GenerateContent (ctx , genai .Text ("Write a story about a magic backpack." ))
93
+ resp , err := model .GenerateContent (ctx , genai .Text ("create a summary from the following content: " + mergedContent ))
57
94
if err != nil {
58
95
return "" , err
59
96
}
60
97
encodeJSON (w , resp )
61
98
62
- return "hellooo from gljgldg " , nil
99
+ return "" , nil
63
100
}
64
101
65
102
func goDotEnvVariable (key string ) string {
0 commit comments