Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
0d70ec46de | |||
6e2b5a33c0 | |||
dfb768d966 | |||
46a526fd5a | |||
0b06fd965e |
29
anthropic.go
29
anthropic.go
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
anth "github.com/liushuangls/go-anthropic/v2"
|
anth "github.com/liushuangls/go-anthropic/v2"
|
||||||
@ -25,6 +26,13 @@ func (a anthropic) ModelVersion(modelVersion string) (ChatCompletion, error) {
|
|||||||
return a, nil
|
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 {
|
func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
|
||||||
res := anth.MessagesRequest{
|
res := anth.MessagesRequest{
|
||||||
Model: anth.Model(a.model),
|
Model: anth.Model(a.model),
|
||||||
@ -63,6 +71,11 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, img := range msg.Images {
|
for _, img := range msg.Images {
|
||||||
|
// anthropic doesn't allow the assistant to send images, so we need to say it's from the user
|
||||||
|
if m.Role == anth.RoleAssistant {
|
||||||
|
m.Role = anth.RoleUser
|
||||||
|
}
|
||||||
|
|
||||||
if img.Base64 != "" {
|
if img.Base64 != "" {
|
||||||
m.Content = append(m.Content, anth.NewImageMessageContent(
|
m.Content = append(m.Content, anth.NewImageMessageContent(
|
||||||
anth.NewMessageContentSource(
|
anth.NewMessageContentSource(
|
||||||
@ -85,7 +98,7 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer deferClose(resp.Body)
|
||||||
|
|
||||||
img.ContentType = resp.Header.Get("Content-Type")
|
img.ContentType = resp.Header.Get("Content-Type")
|
||||||
|
|
||||||
@ -121,12 +134,14 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tool := range req.Toolbox.funcs {
|
if req.Toolbox != nil {
|
||||||
res.Tools = append(res.Tools, anth.ToolDefinition{
|
for _, tool := range req.Toolbox.funcs {
|
||||||
Name: tool.Name,
|
res.Tools = append(res.Tools, anth.ToolDefinition{
|
||||||
Description: tool.Description,
|
Name: tool.Name,
|
||||||
InputSchema: tool.Parameters,
|
Description: tool.Description,
|
||||||
})
|
InputSchema: tool.Parameters,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Messages = msgs
|
res.Messages = msgs
|
||||||
|
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.23.1
|
|||||||
require (
|
require (
|
||||||
github.com/google/generative-ai-go v0.19.0
|
github.com/google/generative-ai-go v0.19.0
|
||||||
github.com/liushuangls/go-anthropic/v2 v2.13.0
|
github.com/liushuangls/go-anthropic/v2 v2.13.0
|
||||||
github.com/sashabaranov/go-openai v1.36.0
|
github.com/sashabaranov/go-openai v1.36.1
|
||||||
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
|
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
|
||||||
google.golang.org/api v0.214.0
|
google.golang.org/api v0.214.0
|
||||||
)
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -39,6 +39,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
|||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=
|
github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=
|
||||||
github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
|
github.com/sashabaranov/go-openai v1.36.1 h1:EVfRXwIlW2rUzpx6vR+aeIKCK/xylSrVYAx1TMTSX3g=
|
||||||
|
github.com/sashabaranov/go-openai v1.36.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
||||||
|
27
openai.go
27
openai.go
@ -3,8 +3,9 @@ package go_llm
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
oai "github.com/sashabaranov/go-openai"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
oai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
type openaiImpl struct {
|
type openaiImpl struct {
|
||||||
@ -57,18 +58,20 @@ func (o openaiImpl) requestToOpenAIRequest(request Request) oai.ChatCompletionRe
|
|||||||
res.Messages = append(res.Messages, m)
|
res.Messages = append(res.Messages, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tool := range request.Toolbox.funcs {
|
if request.Toolbox != nil {
|
||||||
res.Tools = append(res.Tools, oai.Tool{
|
for _, tool := range request.Toolbox.funcs {
|
||||||
Type: "function",
|
res.Tools = append(res.Tools, oai.Tool{
|
||||||
Function: &oai.FunctionDefinition{
|
Type: "function",
|
||||||
Name: tool.Name,
|
Function: &oai.FunctionDefinition{
|
||||||
Description: tool.Description,
|
Name: tool.Name,
|
||||||
Strict: tool.Strict,
|
Description: tool.Description,
|
||||||
Parameters: tool.Parameters.Definition(),
|
Strict: tool.Strict,
|
||||||
},
|
Parameters: tool.Parameters.Definition(),
|
||||||
})
|
},
|
||||||
|
})
|
||||||
|
|
||||||
fmt.Println("tool:", tool.Name, tool.Description, tool.Strict, tool.Parameters.Definition())
|
fmt.Println("tool:", tool.Name, tool.Description, tool.Strict, tool.Parameters.Definition())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.Temperature != nil {
|
if request.Temperature != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user