Feat: http request for agent
This commit is contained in:
42
agent/request.go
Normal file
42
agent/request.go
Normal file
@@ -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)
|
||||
}
|
||||
4
bot.go
4
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())
|
||||
|
||||
Reference in New Issue
Block a user