diff --git a/main.go b/main.go index ec807ed..5ff4d62 100644 --- a/main.go +++ b/main.go @@ -147,10 +147,6 @@ func llm(cfg *Cfg, msgs []Message, tools []map[string]any) (Message, error) { p := map[string]any{"model": cfg.Model, "temperature": cfg.Temperature, "messages": msgs, "stream": cfg.Stream} if tools != nil { p["tools"] = tools } body, _ := json.Marshal(p) - req, _ := http.NewRequest("POST", strings.TrimRight(cfg.Endpoint, "/")+"/chat/completions", bytes.NewReader(body)) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Bantam/1.0)") - if cfg.APIKey != "" && cfg.APIKey != "-" { req.Header.Set("Authorization", "Bearer "+cfg.APIKey) } client := &http.Client{Transport: &http.Transport{ DialContext: (&net.Dialer{Timeout: time.Duration(cfg.Timeout) * time.Second}).DialContext, ResponseHeaderTimeout: time.Duration(cfg.Timeout) * time.Second, @@ -161,6 +157,10 @@ func llm(cfg *Cfg, msgs []Message, tools []map[string]any) (Message, error) { var err error for i := 0; i <= len(fib); i++ { if COL { fmt.Print("\r" + pend) } else { fmt.Println(pend) } + req, _ := http.NewRequest("POST", strings.TrimRight(cfg.Endpoint, "/")+"/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Bantam/1.0)") + if cfg.APIKey != "" && cfg.APIKey != "-" { req.Header.Set("Authorization", "Bearer "+cfg.APIKey) } resp, err = client.Do(req) if err == nil && resp.StatusCode >= 400 { b, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) @@ -184,12 +184,17 @@ func llm(cfg *Cfg, msgs []Message, tools []map[string]any) (Message, error) { if !cfg.Stream { var cr struct { Choices []struct { - Message Message `json:"message"` + Message struct { + Message + Reasoning string `json:"reasoning"` + } `json:"message"` } `json:"choices"` } if err := json.NewDecoder(resp.Body).Decode(&cr); err != nil { return Message{}, err } if len(cr.Choices) == 0 { return Message{}, errors.New("empty choices in LLM response") } - return cr.Choices[0].Message, nil + m := cr.Choices[0].Message.Message + if m.ReasoningContent == "" { m.ReasoningContent = cr.Choices[0].Message.Reasoning } + return m, nil } return parseStream(resp.Body) } @@ -201,12 +206,12 @@ func parseStream(r io.Reader) (Message, error) { var order []int sc := bufio.NewScanner(r) sc.Buffer(make([]byte, 0, 64*1024), 4*1024*1024) - var d streamDelta for sc.Scan() { ln := strings.TrimSpace(sc.Text()) if !strings.HasPrefix(ln, "data:") { continue } data := strings.TrimSpace(ln[5:]) if data == "[DONE]" { break } + var d streamDelta if json.Unmarshal([]byte(data), &d) != nil || len(d.Choices) == 0 { continue } dl := d.Choices[0].Delta rc := dl.ReasoningContent