2026-02-19 20:50:36 +00:00
|
|
|
package llamacpp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-02-19 20:54:52 +00:00
|
|
|
"io"
|
2026-02-19 20:50:36 +00:00
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
2026-03-16 21:08:52 +00:00
|
|
|
|
|
|
|
|
coreerr "forge.lthn.ai/core/go-log"
|
2026-02-19 20:50:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Client communicates with a llama-server instance.
|
|
|
|
|
type Client struct {
|
|
|
|
|
baseURL string
|
|
|
|
|
httpClient *http.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewClient creates a client for the llama-server at the given base URL.
|
|
|
|
|
func NewClient(baseURL string) *Client {
|
|
|
|
|
return &Client{
|
|
|
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
|
|
|
httpClient: &http.Client{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type healthResponse struct {
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Health checks whether the llama-server is ready to accept requests.
|
|
|
|
|
func (c *Client) Health(ctx context.Context) error {
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/health", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2026-02-19 20:54:52 +00:00
|
|
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
|
2026-03-16 21:08:52 +00:00
|
|
|
return coreerr.E("llamacpp.Health", fmt.Sprintf("health returned %d: %s", resp.StatusCode, string(body)), nil)
|
2026-02-19 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
var h healthResponse
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&h); err != nil {
|
2026-03-16 21:08:52 +00:00
|
|
|
return coreerr.E("llamacpp.Health", "health decode", err)
|
2026-02-19 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
if h.Status != "ok" {
|
2026-03-16 21:08:52 +00:00
|
|
|
return coreerr.E("llamacpp.Health", fmt.Sprintf("server not ready (status: %s)", h.Status), nil)
|
2026-02-19 20:50:36 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|