-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoryService.go
177 lines (154 loc) · 4.7 KB
/
storyService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package services
import (
"context"
"database/sql"
"fmt"
"io"
"log"
"mime/multipart"
"os"
"path/filepath"
"strings"
"cloud.google.com/go/vertexai/genai"
"github.com/nhat8002nguyen/story-of-media-be/story-service/src/models"
"google.golang.org/api/option"
)
const (
projectID = "analyzing-media-files-web-app"
location = "asia-southeast1"
ModelName = "gemini-1.5-flash-001"
)
var GenaiClient *genai.Client
func CreateGenAIClient() error {
var ctx = context.Background()
// Ensure the environment variable is set
credsFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credsFile == "" {
log.Fatalf("GOOGLE_APPLICATION_CREDENTIALS environment variable not set")
}
var err error
GenaiClient, err = genai.NewClient(ctx, projectID, location, option.WithCredentialsFile(credsFile))
return err
}
func GenerateContentFromFile(
c context.Context,
file *multipart.FileHeader,
) (*genai.GenerateContentResponse, error) {
f, err := file.Open()
if err != nil {
return nil, fmt.Errorf("could not open file")
}
defer f.Close()
gemini := GenaiClient.GenerativeModel(ModelName)
gemini.SetTemperature(1)
fileBytes, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("unable to read file")
}
ext := strings.ToLower(filepath.Ext(file.Filename))
prompt := "Generate a details story to describe this file"
switch ext {
case ".jpg", ".jpeg":
return gemini.GenerateContent(c, genai.Text(prompt), genai.ImageData("jpeg", fileBytes))
case ".png":
return gemini.GenerateContent(c, genai.Text(prompt), genai.ImageData("png", fileBytes))
case ".pdf":
return gemini.GenerateContent(c, genai.Text(prompt), genai.Blob{MIMEType: "application/pdf", Data: fileBytes})
default:
return nil, fmt.Errorf("unknown or unsupported file format")
}
}
// SaveMessage save message to PostgreSQL database
func SaveMessage(userID, sessionID string, message models.Message) error {
stmt := "INSERT INTO chat_sessions(user_id, session_id, message, sender) VALUES ($1, $2, $3, $4)"
_, err := models.Db.Exec(stmt, userID, sessionID, message.Content, message.Sender)
return err
}
// LoadMessage load messages from PostgreSQL database
func LoadMessages(sessionID string) ([]models.Message, error) {
stmt := "SELECT message, sender FROM chat_sessions WHERE session_id = $1 ORDER BY timestamp"
rows, err := models.Db.Query(stmt, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var messages []models.Message
for rows.Next() {
var message models.Message
if err := rows.Scan(&message.Content, &message.Sender); err != nil {
return nil, err
}
messages = append(messages, message)
}
return messages, nil
}
// LoadChatHistory loads chat history from PostgreSQL database
func LoadChatHistory(userID, sessionID string) ([]*genai.Content, error) {
contentType := ""
fileData := []byte{}
stmt := "SELECT file_data, content_type FROM session_files WHERE user_id=$1 AND session_id=$2"
if err := models.Db.QueryRow(stmt, userID, sessionID).Scan(&fileData, &contentType); err != nil {
switch err {
case sql.ErrNoRows:
log.Printf("there is no file of user %s, and session %s", userID, sessionID)
default:
return nil, err
}
}
stmt = "SELECT sender, message FROM chat_sessions WHERE user_id=$1 AND session_id=$2 ORDER BY timestamp"
rows, err := models.Db.Query(stmt, userID, sessionID)
if err != nil {
return nil, err
}
// add media data first
contents := []*genai.Content{
{
Role: "user",
Parts: []genai.Part{genai.Blob{MIMEType: contentType, Data: fileData}},
},
}
for rows.Next() {
var message models.Message
if err := rows.Scan(&message.Sender, &message.Content); err != nil {
return nil, err
}
contents = append(contents, &genai.Content{
Role: message.Sender,
Parts: []genai.Part{genai.Text(message.Content)},
})
}
return contents, nil
}
func SaveFileData(userID, sessionID string, file *multipart.FileHeader) error {
filename := filepath.Base(file.Filename)
contentType := file.Header.Get("content-type")
f, err := file.Open()
if err != nil {
return err
}
defer f.Close()
fileData, err := io.ReadAll(f)
if err != nil {
return err
}
stmt := `INSERT INTO session_files(user_id, session_id, filename, content_type, file_data)
VALUES ($1, $2, $3, $4, $5) RETURNING id
`
return models.Db.QueryRow(stmt, userID, sessionID, filename, contentType, fileData).Err()
}
func GetStories(userID string) ([]string, error) {
stmt := "SELECT session_id FROM chat_sessions WHERE user_id = $1"
rows, err := models.Db.Query(stmt, userID)
if err != nil {
return nil, err
}
sessionIDs := []string{}
sessionID := ""
for rows.Next() {
if err = rows.Scan(&sessionID); err != nil {
return nil, err
}
sessionIDs = append(sessionIDs, sessionID)
}
return sessionIDs, nil
}