-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (113 loc) · 2.9 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
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
package main
import (
"bytes"
"log"
"net/http"
"os"
"github.com/bwmarrin/discordgo"
"github.com/daulet/tokenizers"
"github.com/joho/godotenv"
)
var (
s *discordgo.Session
tk *tokenizers.Tokenizer
port string
)
func init() {
_, err := os.Stat(".env")
if !os.IsNotExist(err) {
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %v\n", err)
}
}
for _, v := range []string{"DISCORD_TOKEN", "OPENAI_API_KEY", "OPENAI_API_URL"} {
if os.Getenv(v) == "" {
log.Fatalf("No %s found in env\n", v)
}
}
apiUrl = os.Getenv("OPENAI_API_URL")
apiKey = os.Getenv("OPENAI_API_KEY")
port = os.Getenv("PORT")
if port == "" {
port = "8080"
}
s, err = discordgo.New("Bot " + os.Getenv("DISCORD_TOKEN"))
if err != nil {
log.Fatalf("Error creating Discord session: %v\n", err)
}
tk, err = tokenizers.FromFile("tokenizer.json")
if err != nil {
log.Fatalf("Error loading tokenizer: %v\n", err)
}
}
func main() {
defer tk.Close()
http.HandleFunc("GET /proposal/{space}/{id}", summarizeProposal)
http.HandleFunc("POST /proposal", summarizeProposal)
http.HandleFunc("GET /thread/{space}/{id}", summarizeThread)
http.HandleFunc("POST /thread", summarizeThread)
log.Println("Listening on port", port)
http.ListenAndServe(":"+port, nil)
}
func summarizeProposal(w http.ResponseWriter, req *http.Request) {
p, err := fetchProposal(w, req)
if err != nil {
return
}
var b bytes.Buffer
proposalUserTmpl.Execute(&b, p.Data)
userPrompt := b.String()
out := make(chan InferenceResult)
go inference(InferenceRequest{
proposalSystemPrompt,
userPrompt,
}, out)
inferenceRes := <-out
if inferenceRes.err != nil {
http.Error(w, inferenceRes.err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(inferenceRes.result))
}
func summarizeThread(w http.ResponseWriter, req *http.Request) {
p, err := fetchProposal(w, req)
if err != nil {
return
}
userPrompts, err := threadPrompts(p.Data.DiscussionThreadURL)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out := make(chan InferenceResult)
go inference(InferenceRequest{
threadSystemPrompt,
userPrompts[0],
}, out)
inferenceRes := <-out
if inferenceRes.err != nil {
http.Error(w, inferenceRes.err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(inferenceRes.result))
}
func fetchProposal(w http.ResponseWriter, req *http.Request) (*ProposalResponse, error) {
space := req.PathValue("space")
id := req.PathValue("id")
var p *ProposalResponse
var err error
if req.Method == "POST" {
p, err = processProposal(req.Body)
} else if req.Method == "GET" {
if space == "" || id == "" {
http.Error(w, "Invalid path (missing space or id)", http.StatusBadRequest)
return nil, err
}
p, err = proposal(space, id)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil, err
}
return p, nil
}