diff --git a/anthropic.go b/anthropic.go index 4d579cf..784cc32 100644 --- a/anthropic.go +++ b/anthropic.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "log" + "log/slog" "net/http" anth "github.com/liushuangls/go-anthropic/v2" @@ -25,6 +26,13 @@ func (a anthropic) ModelVersion(modelVersion string) (ChatCompletion, error) { return a, nil } +func deferClose(c io.Closer) { + err := c.Close() + if err != nil { + slog.Error("error closing", "error", err) + } +} + func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { res := anth.MessagesRequest{ Model: anth.Model(a.model), @@ -85,7 +93,7 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { continue } - defer resp.Body.Close() + defer deferClose(resp.Body) img.ContentType = resp.Header.Get("Content-Type") @@ -121,12 +129,14 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { } } - for _, tool := range req.Toolbox.funcs { - res.Tools = append(res.Tools, anth.ToolDefinition{ - Name: tool.Name, - Description: tool.Description, - InputSchema: tool.Parameters, - }) + if req.Toolbox != nil { + for _, tool := range req.Toolbox.funcs { + res.Tools = append(res.Tools, anth.ToolDefinition{ + Name: tool.Name, + Description: tool.Description, + InputSchema: tool.Parameters, + }) + } } res.Messages = msgs