-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini_client.go
More file actions
310 lines (278 loc) · 9.78 KB
/
Copy pathgemini_client.go
File metadata and controls
310 lines (278 loc) · 9.78 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
const (
GeminiBaseURL = "https://generativelanguage.googleapis.com/v1beta/models"
GeminiModel = "gemini-2.0-flash"
)
type AIClient struct {
httpClient *http.Client
apiKey string
model string
}
type geminiRequest struct {
Contents []geminiContent `json:"contents"`
GenerationConfig *geminiGenerationConfig `json:"generationConfig,omitempty"`
}
type geminiContent struct {
Parts []geminiPart `json:"parts"`
Role string `json:"role,omitempty"`
}
type geminiPart struct {
Text string `json:"text"`
}
type geminiGenerationConfig struct {
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"topP,omitempty"`
TopK int `json:"topK,omitempty"`
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
}
type geminiResponse struct {
Candidates []geminiCandidate `json:"candidates"`
Error *geminiError `json:"error,omitempty"`
}
type geminiCandidate struct {
Content geminiContent `json:"content"`
FinishReason string `json:"finishReason"`
}
type geminiError struct {
Code int `json:"code"`
Message string `json:"message"`
Status string `json:"status"`
}
func NewAIClientFromEnv() (*AIClient, error) {
apiKey := os.Getenv("GEMINI_API_KEY")
if apiKey == "" {
return nil, fmt.Errorf("GEMINI_API_KEY environment variable is required. Get one at https://aistudio.google.com/app/apikey")
}
return &AIClient{
httpClient: &http.Client{Timeout: 120 * time.Second},
apiKey: apiKey,
model: GeminiModel,
}, nil
}
func (a *AIClient) sendRequest(ctx context.Context, systemPrompt, userPrompt string) (string, error) {
combinedPrompt := fmt.Sprintf("%s\n\n%s", systemPrompt, userPrompt)
reqBody := geminiRequest{
Contents: []geminiContent{{Parts: []geminiPart{{Text: combinedPrompt}}}},
GenerationConfig: &geminiGenerationConfig{
Temperature: 0.3, TopP: 0.95, TopK: 40, MaxOutputTokens: 4096,
},
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return "", fmt.Errorf("failed to marshal request: %w", err)
}
url := fmt.Sprintf("%s/%s:generateContent?key=%s", GeminiBaseURL, a.model, a.apiKey)
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := a.httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("failed to send request to Gemini: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Gemini returned status %d: %s", resp.StatusCode, string(body))
}
var geminiResp geminiResponse
if err := json.Unmarshal(body, &geminiResp); err != nil {
return "", fmt.Errorf("failed to decode response: %w", err)
}
if geminiResp.Error != nil {
return "", fmt.Errorf("Gemini API error: %s", geminiResp.Error.Message)
}
if len(geminiResp.Candidates) == 0 || len(geminiResp.Candidates[0].Content.Parts) == 0 {
return "", fmt.Errorf("no response from Gemini")
}
return geminiResp.Candidates[0].Content.Parts[0].Text, nil
}
func (a *AIClient) AnalyzeText(ctx context.Context, text string) (string, error) {
return a.sendRequest(ctx, "You are an AI model that analyzes call transcripts.", text)
}
// AnalyzeTranscript analyzes a transcript, optionally with seller history context
func (a *AIClient) AnalyzeTranscript(ctx context.Context, rt RawTranscript) (*AnalysisResult, error) {
return a.AnalyzeTranscriptWithContext(ctx, rt, "")
}
// AnalyzeTranscriptWithContext analyzes a transcript with seller history context
func (a *AIClient) AnalyzeTranscriptWithContext(ctx context.Context, rt RawTranscript, sellerContext string) (*AnalysisResult, error) {
prompt := buildAnalysisPrompt(rt.Transcript, sellerContext)
systemPrompt := buildSystemPrompt()
response, err := a.sendRequest(ctx, systemPrompt, prompt)
if err != nil {
return nil, fmt.Errorf("LLM request failed: %w", err)
}
analysis, err := parseAnalysisResponse(response, rt)
if err != nil {
log.Printf("WARNING: Failed to parse LLM response for call %s: %v", rt.CallID, err)
analysis = &AnalysisResult{
CallID: rt.CallID, SellerID: rt.SellerID, Timestamp: rt.Timestamp,
TranscriptEn: rt.Transcript, OriginalLang: rt.Language,
LLMRaw: map[string]interface{}{"raw": response, "parse_error": err.Error()},
AnalyzedAt: time.Now(),
}
}
return analysis, nil
}
func buildSystemPrompt() string {
return fmt.Sprintf(`You are an expert customer service analyst for IndiaMART, India's largest B2B marketplace.
%s
YOUR TASK: Analyze seller support call transcripts and extract structured business insights.
ANALYSIS GUIDELINES:
1. Identify ALL issues mentioned - even subtle ones
2. Map issues to correct buckets based on IndiaMART's product knowledge
3. Assess churn risk based on seller language, complaint severity, and competitor mentions
4. Identify upsell opportunities based on seller needs and business signals
5. Evaluate agent performance against IndiaMART standards
6. Provide actionable recommendations specific to IndiaMART's solutions
7. If seller history is provided, consider recurring patterns and unresolved issues
IMPORTANT: Respond with ONLY valid JSON. No markdown, no code blocks, no explanations.`, IndiaMARTContext)
}
func buildAnalysisPrompt(transcript string, sellerContext string) string {
bucketList := strings.Join(FeatureBuckets, ", ")
contextSection := ""
if sellerContext != "" {
contextSection = fmt.Sprintf(`
SELLER CONTEXT (Previous Interactions):
%s
Consider the seller's history when analyzing. Look for:
- Recurring issues that need systemic fixes
- Worsening sentiment trends indicating high churn risk
- Repeated escalations suggesting service failures
`, sellerContext)
}
return fmt.Sprintf(`%sANALYZE THIS CALL TRANSCRIPT:
%s
ISSUE CATEGORIES (use these exact names): %s
RESPOND WITH THIS EXACT JSON STRUCTURE:
{
"transcript_en": "English translation/cleaned version of transcript",
"call_summary": "2-3 sentence summary of what happened in the call",
"issues": [
{
"problem": "Specific issue description",
"bucket": "Category from list above",
"severity": "low|medium|high|critical",
"actionable_summary": "What IndiaMART should do to fix this"
}
],
"intent": {
"sentiment": "Positive|Neutral|Negative",
"satisfaction_score": 1-10,
"prompt_resolution": true/false,
"overall_experience": "Good|Average|Poor"
},
"churn": {
"is_likely_to_churn": "low|medium|high",
"renewal_at_risk": true/false,
"dissatisfaction_level": "low|medium|high",
"churn_reason": "Why they might leave",
"renewal_probability": 0.0-1.0
},
"upsell": {
"has_opportunity": true/false,
"score": 1-10,
"willingness_to_invest": "low|medium|high",
"is_growth_oriented": true/false,
"interested_features": ["feature1", "feature2"],
"upsell_reason": "Why this opportunity exists"
},
"agent_performance": "Good|Average|Poor",
"key_insights": ["insight1", "insight2"],
"follow_up_needed": true/false,
"escalation_required": true/false
}`, contextSection, transcript, bucketList)
}
func parseAnalysisResponse(response string, rt RawTranscript) (*AnalysisResult, error) {
jsonStr := extractJSON(response)
jsonStr = sanitizeJSONString(jsonStr)
var parsed struct {
TranscriptEn string `json:"transcript_en"`
CallSummary string `json:"call_summary"`
Issues []Issue `json:"issues"`
Intent SellerIntent `json:"intent"`
Churn ChurnPrediction `json:"churn"`
Upsell UpsellScore `json:"upsell"`
AgentPerformance string `json:"agent_performance"`
KeyInsights []string `json:"key_insights"`
FollowUpNeeded bool `json:"follow_up_needed"`
EscalationRequired bool `json:"escalation_required"`
}
if err := json.Unmarshal([]byte(jsonStr), &parsed); err != nil {
return nil, fmt.Errorf("failed to parse LLM response: %w", err)
}
result := &AnalysisResult{
CallID: rt.CallID, SellerID: rt.SellerID, Timestamp: rt.Timestamp,
TranscriptEn: parsed.TranscriptEn, OriginalLang: rt.Language,
Issues: parsed.Issues, Intent: parsed.Intent, Churn: parsed.Churn,
Upsell: parsed.Upsell, CallSummary: parsed.CallSummary,
AgentPerformance: parsed.AgentPerformance,
LLMRaw: map[string]interface{}{
"parsed": true, "key_insights": parsed.KeyInsights,
"follow_up_needed": parsed.FollowUpNeeded, "escalation_required": parsed.EscalationRequired,
},
AnalyzedAt: time.Now(),
}
if result.TranscriptEn == "" {
result.TranscriptEn = rt.Transcript
}
return result, nil
}
func extractJSON(response string) string {
response = strings.TrimSpace(response)
response = strings.TrimPrefix(response, "```json")
response = strings.TrimPrefix(response, "```")
response = strings.TrimSuffix(response, "```")
response = strings.TrimSpace(response)
start := strings.Index(response, "{")
end := strings.LastIndex(response, "}")
if start >= 0 && end > start {
return response[start : end+1]
}
return response
}
func sanitizeJSONString(jsonStr string) string {
var result strings.Builder
inString, escaped := false, false
for i := 0; i < len(jsonStr); i++ {
c := jsonStr[i]
if escaped {
result.WriteByte(c)
escaped = false
continue
}
if c == '\\' {
result.WriteByte(c)
escaped = true
continue
}
if c == '"' {
inString = !inString
result.WriteByte(c)
continue
}
if inString && (c == '\n' || c == '\r') {
result.WriteByte(' ')
continue
}
result.WriteByte(c)
}
return result.String()
}
func (a *AIClient) Close() error { return nil }