Skip to content

Commit 318ad18

Browse files
committed
"Update HTTPProvider to require stream in payload for /api/generate endpoint"
1 parent c6a077d commit 318ad18

2 files changed

Lines changed: 47 additions & 39 deletions

File tree

llm/http.go

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,60 @@ import (
1313
)
1414

1515
type HTTPProvider struct {
16-
cfg *config.Config
16+
cfg *config.Config
1717
}
1818

1919
func NewHTTPProvider(cfg *config.Config) (*HTTPProvider, error) {
20-
if cfg.BaseURL == "" || cfg.Model == "" {
21-
return nil, errors.New("base_url and model required")
22-
}
23-
return &HTTPProvider{cfg}, nil
20+
if cfg.BaseURL == "" || cfg.Model == "" {
21+
return nil, errors.New("base_url and model required")
22+
}
23+
return &HTTPProvider{cfg}, nil
2424
}
2525

2626
func (h *HTTPProvider) Generate(prompt string) (string, error) {
27-
payload := map[string]interface{}{
28-
"model": h.cfg.Model,
29-
"prompt": prompt,
30-
}
31-
body, _ := json.Marshal(payload)
27+
payload := map[string]interface{}{
28+
"model": h.cfg.Model,
29+
"prompt": prompt,
30+
"stream": false, // required for /api/generate
31+
}
3232

33-
req, err := http.NewRequest("POST", h.cfg.BaseURL, bytes.NewReader(body))
34-
if err != nil {
35-
return "", err
36-
}
37-
req.Header.Set("Content-Type", "application/json")
38-
if h.cfg.APIKey != "" {
39-
req.Header.Set("Authorization", "Bearer "+h.cfg.APIKey)
40-
}
41-
for k, v := range h.cfg.Headers {
42-
req.Header.Set(k, v)
43-
}
33+
body, _ := json.Marshal(payload)
4434

45-
resp, err := http.DefaultClient.Do(req)
46-
if err != nil {
47-
return "", err
48-
}
49-
defer resp.Body.Close()
50-
out, _ := io.ReadAll(resp.Body)
35+
req, err := http.NewRequest("POST", h.cfg.BaseURL, bytes.NewReader(body))
36+
if err != nil {
37+
return "", err
38+
}
39+
req.Header.Set("Content-Type", "application/json")
40+
if h.cfg.APIKey != "" {
41+
req.Header.Set("Authorization", "Bearer "+h.cfg.APIKey)
42+
}
43+
for k, v := range h.cfg.Headers {
44+
req.Header.Set(k, v)
45+
}
5146

52-
var js map[string]interface{}
53-
if err := json.Unmarshal(out, &js); err != nil {
54-
return "", err
55-
}
56-
// Try common fields
57-
if v, ok := js["response"]; ok {
58-
return strings.TrimSpace(v.(string)), nil
59-
}
60-
if v, ok := js["content"]; ok {
61-
return strings.TrimSpace(v.(string)), nil
62-
}
63-
return "", fmt.Errorf("unexpected response: %s", string(out))
47+
resp, err := http.DefaultClient.Do(req)
48+
if err != nil {
49+
return "", err
50+
}
51+
defer resp.Body.Close()
52+
53+
out, _ := io.ReadAll(resp.Body)
54+
55+
var res struct {
56+
Response string `json:"response"`
57+
Content string `json:"content"`
58+
}
59+
60+
if err := json.Unmarshal(out, &res); err != nil {
61+
return "", fmt.Errorf("failed to parse response: %w", err)
62+
}
63+
64+
switch {
65+
case res.Response != "":
66+
return strings.TrimSpace(res.Response), nil
67+
case res.Content != "":
68+
return strings.TrimSpace(res.Content), nil
69+
default:
70+
return "", fmt.Errorf("no usable content in response: %s", string(out))
71+
}
6472
}

smartcommit

1.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)