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
|
package main
import (
"bytes"
"context"
"encoding/json"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/alecthomas/chroma/v2/quick"
"github.com/lrstanley/girc"
"golang.org/x/net/proxy"
)
func DoORRequest(
appConfig *TomlConfig,
memory *[]MemoryElement,
prompt string,
) (string, error) {
var jsonPayload []byte
var err error
memoryElement := MemoryElement{
Role: "user",
Content: prompt,
}
if len(*memory) > appConfig.MemoryLimit {
*memory = []MemoryElement{}
for _, context := range appConfig.Context {
*memory = append(*memory, MemoryElement{
Role: "assistant",
Content: context,
})
}
}
*memory = append(*memory, memoryElement)
ollamaRequest := OllamaChatRequest{
Model: appConfig.Model,
Messages: *memory,
}
jsonPayload, err = json.Marshal(ollamaRequest)
if err != nil {
return "", err
}
log.Printf("json payload: %s", string(jsonPayload))
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(appConfig.RequestTimeout)*time.Second)
defer cancel()
request, err := http.NewRequest(http.MethodPost, appConfig.Endpoint, bytes.NewBuffer(jsonPayload))
if err != nil {
return "", err
}
request = request.WithContext(ctx)
request.Header.Set("content-type", "application/json")
request.Header.Set("Authorization", "Bearer "+appConfig.Apikey)
var httpClient http.Client
var dialer proxy.Dialer
if appConfig.LLMProxy != "" {
proxyURL, err := url.Parse(appConfig.IRCProxy)
if err != nil {
cancel()
log.Fatal(err.Error())
}
dialer, err = proxy.FromURL(proxyURL, &net.Dialer{Timeout: time.Duration(appConfig.RequestTimeout) * time.Second})
if err != nil {
cancel()
log.Fatal(err.Error())
}
httpClient = http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
}
}
response, err := httpClient.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
log.Println("response body:", response.Body)
var orresponse ORResponse
err = json.NewDecoder(response.Body).Decode(&orresponse)
if err != nil {
return "", err
}
var result string
for _, choice := range orresponse.Choices {
result += choice.Message.Content + "\n"
}
return result, nil
}
func ORRequestProcessor(
appConfig *TomlConfig,
client *girc.Client,
event girc.Event,
memory *[]MemoryElement,
prompt string,
) string {
response, err := DoORRequest(appConfig, memory, prompt)
if err != nil {
client.Cmd.ReplyTo(event, "error: "+err.Error())
return ""
}
assistantElement := MemoryElement{
Role: "assistant",
Content: response,
}
*memory = append(*memory, assistantElement)
log.Println(response)
var writer bytes.Buffer
err = quick.Highlight(&writer,
response,
"markdown",
appConfig.ChromaFormatter,
appConfig.ChromaStyle)
if err != nil {
client.Cmd.ReplyTo(event, "error: "+err.Error())
return ""
}
return writer.String()
}
func ORHandler(
irc *girc.Client,
appConfig *TomlConfig,
memory *[]MemoryElement) {
irc.Handlers.AddBg(girc.PRIVMSG, func(client *girc.Client, event girc.Event) {
if !strings.HasPrefix(event.Last(), appConfig.IrcNick+": ") {
return
}
if appConfig.AdminOnly {
byAdmin := false
for _, admin := range appConfig.Admins {
if event.Source.Name == admin {
byAdmin = true
}
}
if !byAdmin {
return
}
}
prompt := strings.TrimPrefix(event.Last(), appConfig.IrcNick+": ")
log.Println(prompt)
if string(prompt[0]) == "/" {
runCommand(client, event, appConfig)
return
}
result := ORRequestProcessor(appConfig, client, event, memory, prompt)
if result != "" {
sendToIRC(client, event, result, appConfig.ChromaFormatter)
}
})
}
|