Clouade did up to this point
This commit is contained in:
32
openai.go
32
openai.go
@@ -7,6 +7,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -57,19 +60,38 @@ type Client struct {
|
||||
}
|
||||
|
||||
func NewClient(baseURL, apiKey, model string) *Client {
|
||||
timeout := 600 * time.Second
|
||||
if v := os.Getenv("OPENAI_TIMEOUT_SECONDS"); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||
timeout = time.Duration(n) * time.Second
|
||||
}
|
||||
}
|
||||
return &Client{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Model: model,
|
||||
HTTP: &http.Client{Timeout: 120 * time.Second},
|
||||
HTTP: &http.Client{Timeout: timeout},
|
||||
}
|
||||
}
|
||||
|
||||
var aiCallCounter uint64
|
||||
|
||||
func logAIIO() bool { return os.Getenv("LOG_AI_IO") == "1" }
|
||||
|
||||
func (c *Client) Chat(ctx context.Context, messages []Message, tools []Tool) (Message, error) {
|
||||
body, err := json.Marshal(chatRequest{Model: c.Model, Messages: messages, Tools: tools})
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
callID := atomic.AddUint64(&aiCallCounter, 1)
|
||||
debug := logAIIO()
|
||||
if debug {
|
||||
fmt.Fprintf(os.Stderr, "\n===== AI REQUEST #%d model=%s msgs=%d tools=%d =====\n%s\n",
|
||||
callID, c.Model, len(messages), len(tools), string(body))
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL+"/chat/completions", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
@@ -80,6 +102,10 @@ func (c *Client) Chat(ctx context.Context, messages []Message, tools []Tool) (Me
|
||||
}
|
||||
resp, err := c.HTTP.Do(req)
|
||||
if err != nil {
|
||||
if debug {
|
||||
fmt.Fprintf(os.Stderr, "===== AI REQUEST #%d FAILED after %s: %v =====\n",
|
||||
callID, time.Since(start).Round(time.Millisecond), err)
|
||||
}
|
||||
return Message{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
@@ -88,6 +114,10 @@ func (c *Client) Chat(ctx context.Context, messages []Message, tools []Tool) (Me
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if debug {
|
||||
fmt.Fprintf(os.Stderr, "===== AI RESPONSE #%d status=%d elapsed=%s bytes=%d =====\n%s\n",
|
||||
callID, resp.StatusCode, time.Since(start).Round(time.Millisecond), len(raw), string(raw))
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
return Message{}, fmt.Errorf("api error %d: %s", resp.StatusCode, string(raw))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user