diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-02 14:34:46 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-02 14:34:46 +0200 |
| commit | 986456be623ba1974fdf06bfdfcaeffd8cd13bb7 (patch) | |
| tree | ada0248bc5dfc32a7355dc088742089262a270fd /internal/llm/util.go | |
| parent | 3e3d1cb988b457a55425a7334f8f5a91cc939666 (diff) | |
llm: extract shared doJSON and logStart helpers (task 409)
Diffstat (limited to 'internal/llm/util.go')
| -rw-r--r-- | internal/llm/util.go | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/internal/llm/util.go b/internal/llm/util.go index b99d7c8..b6e2adc 100644 --- a/internal/llm/util.go +++ b/internal/llm/util.go @@ -1,6 +1,37 @@ package llm -import "errors" +import ( + "bytes" + "context" + "errors" + "net/http" + "strings" + + "codeberg.org/snonux/hexai/internal/logging" +) // small helper to keep return type consistent func nilStringErr(msg string) (string, error) { return "", errors.New(msg) } + +func doJSONRequest(ctx context.Context, httpClient *http.Client, url string, body []byte, headers map[string]string, accept string) (*http.Response, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + if strings.TrimSpace(accept) != "" { + req.Header.Set("Accept", accept) + } + for k, v := range headers { + req.Header.Set(k, v) + } + return httpClient.Do(req) +} + +func logStartMessages(chatLogger logging.ChatLogger, stream bool, o Options, messages []Message) { + logMessages := make([]struct{ Role, Content string }, len(messages)) + for i, m := range messages { + logMessages[i] = struct{ Role, Content string }{m.Role, m.Content} + } + chatLogger.LogStart(stream, o.Model, o.Temperature, o.MaxTokens, o.Stop, logMessages) +} |
