Skip to content

Commit

Permalink
Add ToolResultContentBlock and add TestMessageWithForcedToolIntegrati…
Browse files Browse the repository at this point in the history
…on (#27)
  • Loading branch information
kaptinlin authored May 30, 2024
1 parent 816d2a2 commit c228c3b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/anthropic/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ func NewImageContentBlock(mediaType MediaType, base64Data string) ContentBlock {
}
}

// ToolResultContentBlock represents a block of tool result content.
type ToolResultContentBlock struct {
Type string `json:"type"`
ToolUseID string `json:"tool_use_id"`
Content interface{} `json:"content"`
IsError bool `json:"is_error,omitempty"`
}

func (t ToolResultContentBlock) isContentBlock() {}

// NewToolResultContentBlock creates a new tool result content block with the given parameters.
func NewToolResultContentBlock(toolUseID string, content interface{}, isError bool) ContentBlock {
return ToolResultContentBlock{
Type: "tool_result",
ToolUseID: toolUseID,
Content: content,
IsError: isError,
}
}

// ToolChoice specifies the tool preferences for a message request.
type ToolChoice struct {
Type string `json:"type"` // Type of tool choice: "tool", "any", or "auto".
Expand Down
55 changes: 55 additions & 0 deletions pkg/internal/integration_tests/message_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@ func TestMessageWithToolsIntegration(t *testing.T) {
}
}

func TestMessageWithForcedToolIntegration(t *testing.T) {
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
t.Skip("ANTHROPIC_API_KEY environment variable is not set, skipping integration test")
}

client, err := anthropic.NewClient(apiKey)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

request := &anthropic.MessageRequest{
Model: anthropic.Claude3Opus,
MaxTokensToSample: 1024,
ToolChoice: &anthropic.ToolChoice{
Type: "tool",
Name: "get_weather",
},
Tools: []anthropic.Tool{
{
Name: "get_weather",
Description: "Get the weather",
InputSchema: anthropic.InputSchema{
Type: "object",
Properties: map[string]anthropic.Property{
"city": {Type: "string", Description: "city to get the weather for"},
"unit": {Type: "string", Enum: []string{"celsius", "fahrenheit"}, Description: "temperature unit to return"}},
Required: []string{"city"},
},
},
},
Messages: []anthropic.MessagePartRequest{
{
Role: "user",
Content: []anthropic.ContentBlock{
anthropic.NewTextContentBlock("what's the weather in Charleston?"),
},
},
},
}

response, err := client.Message(request)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if response == nil || len(response.Content) == 0 {
t.Errorf("Expected a message response, got none or empty content")
}

if response.StopReason != "tool_use" {
t.Errorf("Expected stop reason 'tool_use', got %s", response.StopReason)
}
}

func TestMessageWithImageIntegration(t *testing.T) {
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
Expand Down

0 comments on commit c228c3b

Please sign in to comment.