-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (99 loc) · 2.66 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/nohe427/absurd-celery/genai"
"github.com/nohe427/absurd-celery/webprocessor"
)
func getGeminiKey() string {
return os.Getenv("GEMINI_KEY")
}
func fullText(allPassages *webprocessor.AllPassages) string {
var sb strings.Builder
cs := ""
for _, pi := range *allPassages.Passages {
if pi.Speaker != cs {
// Append the current speaker on its own line
sb.WriteString(fmt.Sprintf("%v\n", pi.Speaker))
}
sb.WriteString(fmt.Sprintf("%v\n", pi.Text))
}
return sb.String()
}
func byAct(allPassages *webprocessor.AllPassages) []string {
var sb strings.Builder
cs := ""
ca := 0
byAct := make([]string, 0)
for _, pi := range *allPassages.Passages {
if pi.Act != ca {
if ca != 0 {
byAct = append(byAct, sb.String())
sb.Reset()
}
ca = pi.Act
// reset the current speaker
cs = ""
}
if pi.Speaker != cs {
// Append the current speaker on its own line
sb.WriteString(fmt.Sprintf("%v\n", pi.Speaker))
}
sb.WriteString(fmt.Sprintf("%v\n", pi.Text))
}
byAct = append(byAct, sb.String())
return byAct
}
func main() {
fmt.Println("Hello world")
passages, err := webprocessor.LoadPage("https://shakespeare.mit.edu/hamlet/full.html")
if err != nil {
fmt.Println(err)
return
}
key := getGeminiKey()
if key == "" {
fmt.Println("Please set your GEMINI_KEY env variable to your API Key")
return
}
client := genai.NewGeminiClient(key)
tc := client.TokenCount(fullText(passages))
fmt.Printf("Full Text Token Count : %v\n", tc)
if tc > 32_700 {
fmt.Printf("Token Count Exceeds summarization Limit\n")
}
fmt.Printf("Parsing by Act\n")
acts := byAct(passages)
for i, v := range acts {
fmt.Printf("Act %v\n", i+1)
tc := client.TokenCount(v)
fmt.Printf("Token Count : %v\n", tc)
if tc > 32_600 {
fmt.Printf("Token Count Exceeds summarization Limit\n")
}
}
fmt.Printf("Summarizing by Act\n")
actSummaries := make([]string, 0)
for i, v := range acts {
prompt := fmt.Sprintf("Summarize the following Shapespeare Act in modern English:\nAct %v\n%v", i+1, v)
summary, err := client.Summarize(prompt)
if err != nil {
fmt.Printf("error : %v", err)
}
actSummaries = append(actSummaries, summary)
fmt.Printf("Act %v Summay:\n%v\n", i+1, summary)
}
var sb strings.Builder
sb.WriteString("Summarize all of the summaries of these Shakepseare acts into one narrative.\n")
for i, v := range actSummaries {
sb.WriteString(fmt.Sprintf("Act %v\nSumamry:%v\n", i+1, v))
}
finalPrompt := sb.String()
summary, err := client.Summarize(finalPrompt)
if err != nil {
fmt.Printf("ERROR : %v", err)
return
}
fmt.Printf("\n\n\nSummary of the entire play:\n\n%v\n\n", summary)
}