summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--agent/request.go42
-rw-r--r--bot.go4
2 files changed, 43 insertions, 3 deletions
diff --git a/agent/request.go b/agent/request.go
new file mode 100644
index 0000000..3b8d083
--- /dev/null
+++ b/agent/request.go
@@ -0,0 +1,42 @@
+package agent
+
+import (
+ "gf-lt/config"
+ "io"
+ "log/slog"
+ "net/http"
+)
+
+var httpClient = &http.Client{}
+
+type AgentClient struct {
+ cfg *config.Config
+ getToken func() string
+ log slog.Logger
+}
+
+func NewAgentClient(cfg *config.Config, log slog.Logger, gt func() string) *AgentClient {
+ return &AgentClient{
+ cfg: cfg,
+ getToken: gt,
+ log: log,
+ }
+}
+
+func (ag *AgentClient) LLMRequest(body io.Reader) ([]byte, error) {
+ req, err := http.NewRequest("POST", ag.cfg.CurrentAPI, body)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Add("Accept", "application/json")
+ req.Header.Add("Content-Type", "application/json")
+ req.Header.Add("Authorization", "Bearer "+ag.getToken())
+ req.Header.Set("Accept-Encoding", "gzip")
+ resp, err := httpClient.Do(req)
+ if err != nil {
+ ag.log.Error("llamacpp api", "error", err)
+ return nil, err
+ }
+ defer resp.Body.Close()
+ return io.ReadAll(resp.Body)
+}
diff --git a/bot.go b/bot.go
index ddc6d2a..d6418ff 100644
--- a/bot.go
+++ b/bot.go
@@ -327,12 +327,11 @@ func fetchLCPModels() ([]string, error) {
return localModels, nil
}
+// sendMsgToLLM expects streaming resp
func sendMsgToLLM(body io.Reader) {
choseChunkParser()
-
var req *http.Request
var err error
-
// Capture and log the request body for debugging
if _, ok := body.(*io.LimitedReader); ok {
// If it's a LimitedReader, we need to handle it differently
@@ -379,7 +378,6 @@ func sendMsgToLLM(body io.Reader) {
return
}
}
-
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+chunkParser.GetToken())