feat: claude computer use

Signed-off-by: wozulong <>
This commit is contained in:
wozulong
2024-10-24 16:53:28 +08:00
parent e0f780185a
commit d1ea2d2d0a
3 changed files with 52 additions and 19 deletions

View File

@@ -50,11 +50,18 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
channel.SetupApiRequestHeader(info, c, req)
req.Header.Set("x-api-key", info.ApiKey)
anthropicVersion := c.Request.Header.Get("anthropic-version")
if anthropicVersion == "" {
anthropicVersion = "2023-06-01"
}
req.Header.Set("anthropic-version", anthropicVersion)
anthropicBeta := c.Request.Header.Get("anthropic-beta")
if "" != anthropicBeta {
req.Header.Set("anthropic-beta", anthropicBeta)
}
return nil
}

View File

@@ -31,9 +31,13 @@ type ClaudeMessage struct {
}
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema map[string]interface{} `json:"input_schema"`
Name string `json:"name"`
Description any `json:"description,omitempty"`
InputSchema map[string]interface{} `json:"input_schema,omitempty"`
Type any `json:"type,omitempty"`
DisplayHeightPx int `json:"display_height_px,omitempty"`
DisplayWidthPx int `json:"display_width_px,omitempty"`
DisplayNumber int `json:"display_number,omitempty"`
}
type InputSchema struct {

View File

@@ -63,23 +63,44 @@ func RequestOpenAI2ClaudeMessage(textRequest dto.GeneralOpenAIRequest) (*ClaudeR
claudeTools := make([]Tool, 0, len(textRequest.Tools))
for _, tool := range textRequest.Tools {
if params, ok := tool.Function.Parameters.(map[string]any); ok {
claudeTool := Tool{
Name: tool.Function.Name,
Description: tool.Function.Description,
}
claudeTool.InputSchema = make(map[string]interface{})
claudeTool.InputSchema["type"] = params["type"].(string)
claudeTool.InputSchema["properties"] = params["properties"]
claudeTool.InputSchema["required"] = params["required"]
for s, a := range params {
if s == "type" || s == "properties" || s == "required" {
continue
}
claudeTool.InputSchema[s] = a
}
claudeTools = append(claudeTools, claudeTool)
claudeTool := Tool{
Name: tool.Function.Name,
Description: tool.Function.Description,
Type: tool.Type,
}
if "function" != tool.Type {
claudeTool.Description = nil
}
if params, ok := tool.Function.Parameters.(map[string]any); ok {
if "function" == tool.Type {
claudeTool.Type = nil
claudeTool.InputSchema = make(map[string]interface{})
claudeTool.InputSchema["type"] = params["type"].(string)
claudeTool.InputSchema["properties"] = params["properties"]
claudeTool.InputSchema["required"] = params["required"]
for s, a := range params {
if s == "type" || s == "properties" || s == "required" {
continue
}
claudeTool.InputSchema[s] = a
}
} else {
if val, ok := params["display_height_px"]; ok {
claudeTool.DisplayHeightPx = int(val.(float64))
}
if val, ok := params["display_width_px"]; ok {
claudeTool.DisplayWidthPx = int(val.(float64))
}
if val, ok := params["display_number"]; ok {
claudeTool.DisplayNumber = int(val.(float64))
}
}
}
claudeTools = append(claudeTools, claudeTool)
}
claudeRequest := ClaudeRequest{
@@ -91,6 +112,7 @@ func RequestOpenAI2ClaudeMessage(textRequest dto.GeneralOpenAIRequest) (*ClaudeR
TopK: textRequest.TopK,
Stream: textRequest.Stream,
Tools: claudeTools,
ToolChoice: textRequest.ToolChoice,
}
if claudeRequest.MaxTokens == 0 {
claudeRequest.MaxTokens = 4096