github.com/anthropics/anthropic-sdk-go@v1.70.1/betamessagebatch_test.go (about)

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package anthropic_test
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/anthropics/anthropic-sdk-go"
    12  	"github.com/anthropics/anthropic-sdk-go/internal/testutil"
    13  	"github.com/anthropics/anthropic-sdk-go/option"
    14  	"github.com/anthropics/anthropic-sdk-go/shared/constant"
    15  )
    16  
    17  func TestBetaMessageBatchNewWithOptionalParams(t *testing.T) {
    18  	baseURL := "http://localhost:4010"
    19  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
    20  		baseURL = envURL
    21  	}
    22  	if !testutil.CheckTestServer(t, baseURL) {
    23  		return
    24  	}
    25  	client := anthropic.NewClient(
    26  		option.WithBaseURL(baseURL),
    27  		option.WithAPIKey("my-anthropic-api-key"),
    28  	)
    29  	_, err := client.Beta.Messages.Batches.New(context.TODO(), anthropic.BetaMessageBatchNewParams{
    30  		Requests: []anthropic.BetaMessageBatchNewParamsRequest{{
    31  			CustomID: "my-custom-id-1",
    32  			Params: anthropic.BetaMessageBatchNewParamsRequestParams{
    33  				MaxTokens: 1024,
    34  				Messages: []anthropic.BetaMessageParam{{
    35  					Content: []anthropic.BetaContentBlockParamUnion{{
    36  						OfText: &anthropic.BetaTextBlockParam{
    37  							Text: "x",
    38  							CacheControl: anthropic.BetaCacheControlEphemeralParam{
    39  								TTL: anthropic.BetaCacheControlEphemeralTTLTTL5m,
    40  							},
    41  							Citations: []anthropic.BetaTextCitationParamUnion{{
    42  								OfCharLocation: &anthropic.BetaCitationCharLocationParam{
    43  									CitedText:      "The grass is green. The sky is blue.",
    44  									DocumentIndex:  0,
    45  									DocumentTitle:  anthropic.String("x"),
    46  									EndCharIndex:   0,
    47  									StartCharIndex: 0,
    48  								},
    49  							}},
    50  						},
    51  					}},
    52  					Role:    anthropic.BetaMessageParamRoleUser,
    53  					ClearAt: anthropic.BetaMessageParamClearAtNextUserMessage,
    54  					OutputConfig: anthropic.BetaSystemMessageOutputConfigParam{
    55  						Effort: anthropic.BetaSystemMessageOutputConfigEffortLow,
    56  					},
    57  				}},
    58  				Model: anthropic.ModelClaudeOpus5,
    59  				CacheControl: anthropic.BetaCacheControlEphemeralParam{
    60  					TTL: anthropic.BetaCacheControlEphemeralTTLTTL5m,
    61  				},
    62  				Container: anthropic.BetaMessageBatchNewParamsRequestParamsContainerUnion{
    63  					OfContainers: &anthropic.BetaContainerParams{
    64  						ID: anthropic.String("id"),
    65  						Skills: []anthropic.BetaSkillParams{{
    66  							SkillID: "pdf",
    67  							Type:    anthropic.BetaSkillParamsTypeAnthropic,
    68  							Version: anthropic.String("latest"),
    69  						}},
    70  					},
    71  				},
    72  				ContextManagement: anthropic.BetaContextManagementConfigParam{
    73  					Edits: []anthropic.BetaContextManagementConfigEditUnionParam{{
    74  						OfClearToolUses20250919: &anthropic.BetaClearToolUses20250919EditParam{
    75  							ClearAtLeast: anthropic.BetaInputTokensClearAtLeastParam{
    76  								Value: 0,
    77  							},
    78  							ClearToolInputs: anthropic.BetaClearToolUses20250919EditClearToolInputsUnionParam{
    79  								OfBool: anthropic.Bool(true),
    80  							},
    81  							ExcludeTools: []string{"string"},
    82  							Keep: anthropic.BetaToolUsesKeepParam{
    83  								Value: 0,
    84  							},
    85  							Trigger: anthropic.BetaClearToolUses20250919EditTriggerUnionParam{
    86  								OfInputTokens: &anthropic.BetaInputTokensTriggerParam{
    87  									Value: 1,
    88  								},
    89  							},
    90  						},
    91  					}},
    92  				},
    93  				Diagnostics: anthropic.BetaDiagnosticsParam{
    94  					PreviousMessageID: anthropic.String("previous_message_id"),
    95  				},
    96  				FallbackCreditToken: anthropic.BetaMessageBatchNewParamsRequestParamsFallbackCreditTokenUnion{
    97  					OfString: anthropic.String("x"),
    98  				},
    99  				Fallbacks: anthropic.BetaFallbacksParamUnion{
   100  					OfDefault: constant.ValueOf[constant.Default](),
   101  				},
   102  				InferenceGeo: anthropic.String("inference_geo"),
   103  				MCPServers: []anthropic.BetaRequestMCPServerURLDefinitionParam{{
   104  					Name:               "name",
   105  					URL:                "url",
   106  					AuthorizationToken: anthropic.String("authorization_token"),
   107  					ToolConfiguration: anthropic.BetaRequestMCPServerToolConfigurationParam{
   108  						AllowedTools: []string{"string"},
   109  						Enabled:      anthropic.Bool(true),
   110  					},
   111  				}},
   112  				Metadata: anthropic.BetaMetadataParam{
   113  					UserID: anthropic.String("13803d75-b4b5-4c3e-b2a2-6f21399b021b"),
   114  				},
   115  				OutputConfig: anthropic.BetaOutputConfigParam{
   116  					Effort: anthropic.BetaOutputConfigEffortLow,
   117  					Format: anthropic.BetaJSONOutputFormatParam{
   118  						Schema: map[string]any{
   119  							"foo": "bar",
   120  						},
   121  					},
   122  					TaskBudget: anthropic.BetaTokenTaskBudgetParam{
   123  						Total:     1024,
   124  						Remaining: anthropic.Int(0),
   125  					},
   126  				},
   127  				OutputFormat: anthropic.BetaJSONOutputFormatParam{
   128  					Schema: map[string]any{
   129  						"foo": "bar",
   130  					},
   131  				},
   132  				ServiceTier:   "auto",
   133  				Speed:         "standard",
   134  				StopSequences: []string{"string"},
   135  				Stream:        anthropic.Bool(false),
   136  				System: []anthropic.BetaTextBlockParam{{
   137  					Text: "Today's date is 2024-06-01.",
   138  					CacheControl: anthropic.BetaCacheControlEphemeralParam{
   139  						TTL: anthropic.BetaCacheControlEphemeralTTLTTL5m,
   140  					},
   141  					Citations: []anthropic.BetaTextCitationParamUnion{{
   142  						OfCharLocation: &anthropic.BetaCitationCharLocationParam{
   143  							CitedText:      "The grass is green. The sky is blue.",
   144  							DocumentIndex:  0,
   145  							DocumentTitle:  anthropic.String("x"),
   146  							EndCharIndex:   0,
   147  							StartCharIndex: 0,
   148  						},
   149  					}},
   150  				}},
   151  				Temperature: anthropic.Float(1),
   152  				Thinking: anthropic.BetaThinkingConfigParamUnion{
   153  					OfAdaptive: &anthropic.BetaThinkingConfigAdaptiveParam{
   154  						BlockBinding: anthropic.BetaThinkingBlockBindingParam{
   155  							PrefixMismatchBehavior: anthropic.BetaThinkingPrefixMismatchBehaviorError,
   156  						},
   157  						Display: anthropic.BetaThinkingConfigAdaptiveDisplaySummarized,
   158  					},
   159  				},
   160  				ToolChoice: anthropic.BetaToolChoiceUnionParam{
   161  					OfAuto: &anthropic.BetaToolChoiceAutoParam{
   162  						DisableParallelToolUse: anthropic.Bool(true),
   163  					},
   164  				},
   165  				Tools: []anthropic.BetaToolUnionParam{{
   166  					OfTool: &anthropic.BetaToolParam{
   167  						InputSchema: anthropic.BetaToolInputSchemaParam{
   168  							Properties: map[string]any{
   169  								"location": "bar",
   170  								"unit":     "bar",
   171  							},
   172  							Required: []string{"location"},
   173  						},
   174  						Name:           "name",
   175  						AllowedCallers: []string{"direct"},
   176  						CacheControl: anthropic.BetaCacheControlEphemeralParam{
   177  							TTL: anthropic.BetaCacheControlEphemeralTTLTTL5m,
   178  						},
   179  						DeferLoading:        anthropic.Bool(true),
   180  						Description:         anthropic.String("Get the current weather in a given location"),
   181  						EagerInputStreaming: anthropic.Bool(true),
   182  						InputExamples: []map[string]any{{
   183  							"foo": "bar",
   184  						}},
   185  						Strict: anthropic.Bool(true),
   186  						Type:   anthropic.BetaToolTypeCustom,
   187  					},
   188  				}},
   189  				TopK: anthropic.Int(5),
   190  				TopP: anthropic.Float(0.7),
   191  			},
   192  		}},
   193  		Betas:         []anthropic.AnthropicBeta{anthropic.AnthropicBetaMessageBatches2024_09_24},
   194  		UserProfileID: anthropic.String("anthropic-user-profile-id"),
   195  		WorkspaceID:   anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   196  	})
   197  	if err != nil {
   198  		var apierr *anthropic.Error
   199  		if errors.As(err, &apierr) {
   200  			t.Log(string(apierr.DumpRequest(true)))
   201  		}
   202  		t.Fatalf("err should be nil: %s", err.Error())
   203  	}
   204  }
   205  
   206  func TestBetaMessageBatchGetWithOptionalParams(t *testing.T) {
   207  	baseURL := "http://localhost:4010"
   208  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
   209  		baseURL = envURL
   210  	}
   211  	if !testutil.CheckTestServer(t, baseURL) {
   212  		return
   213  	}
   214  	client := anthropic.NewClient(
   215  		option.WithBaseURL(baseURL),
   216  		option.WithAPIKey("my-anthropic-api-key"),
   217  	)
   218  	_, err := client.Beta.Messages.Batches.Get(
   219  		context.TODO(),
   220  		"message_batch_id",
   221  		anthropic.BetaMessageBatchGetParams{
   222  			Betas:       []anthropic.AnthropicBeta{anthropic.AnthropicBetaMessageBatches2024_09_24},
   223  			WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   224  		},
   225  	)
   226  	if err != nil {
   227  		var apierr *anthropic.Error
   228  		if errors.As(err, &apierr) {
   229  			t.Log(string(apierr.DumpRequest(true)))
   230  		}
   231  		t.Fatalf("err should be nil: %s", err.Error())
   232  	}
   233  }
   234  
   235  func TestBetaMessageBatchListWithOptionalParams(t *testing.T) {
   236  	baseURL := "http://localhost:4010"
   237  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
   238  		baseURL = envURL
   239  	}
   240  	if !testutil.CheckTestServer(t, baseURL) {
   241  		return
   242  	}
   243  	client := anthropic.NewClient(
   244  		option.WithBaseURL(baseURL),
   245  		option.WithAPIKey("my-anthropic-api-key"),
   246  	)
   247  	_, err := client.Beta.Messages.Batches.List(context.TODO(), anthropic.BetaMessageBatchListParams{
   248  		AfterID:     anthropic.String("after_id"),
   249  		BeforeID:    anthropic.String("before_id"),
   250  		Limit:       anthropic.Int(1),
   251  		Betas:       []anthropic.AnthropicBeta{anthropic.AnthropicBetaMessageBatches2024_09_24},
   252  		WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   253  	})
   254  	if err != nil {
   255  		var apierr *anthropic.Error
   256  		if errors.As(err, &apierr) {
   257  			t.Log(string(apierr.DumpRequest(true)))
   258  		}
   259  		t.Fatalf("err should be nil: %s", err.Error())
   260  	}
   261  }
   262  
   263  func TestBetaMessageBatchDeleteWithOptionalParams(t *testing.T) {
   264  	baseURL := "http://localhost:4010"
   265  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
   266  		baseURL = envURL
   267  	}
   268  	if !testutil.CheckTestServer(t, baseURL) {
   269  		return
   270  	}
   271  	client := anthropic.NewClient(
   272  		option.WithBaseURL(baseURL),
   273  		option.WithAPIKey("my-anthropic-api-key"),
   274  	)
   275  	_, err := client.Beta.Messages.Batches.Delete(
   276  		context.TODO(),
   277  		"message_batch_id",
   278  		anthropic.BetaMessageBatchDeleteParams{
   279  			Betas:       []anthropic.AnthropicBeta{anthropic.AnthropicBetaMessageBatches2024_09_24},
   280  			WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   281  		},
   282  	)
   283  	if err != nil {
   284  		var apierr *anthropic.Error
   285  		if errors.As(err, &apierr) {
   286  			t.Log(string(apierr.DumpRequest(true)))
   287  		}
   288  		t.Fatalf("err should be nil: %s", err.Error())
   289  	}
   290  }
   291  
   292  func TestBetaMessageBatchCancelWithOptionalParams(t *testing.T) {
   293  	baseURL := "http://localhost:4010"
   294  	if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
   295  		baseURL = envURL
   296  	}
   297  	if !testutil.CheckTestServer(t, baseURL) {
   298  		return
   299  	}
   300  	client := anthropic.NewClient(
   301  		option.WithBaseURL(baseURL),
   302  		option.WithAPIKey("my-anthropic-api-key"),
   303  	)
   304  	_, err := client.Beta.Messages.Batches.Cancel(
   305  		context.TODO(),
   306  		"message_batch_id",
   307  		anthropic.BetaMessageBatchCancelParams{
   308  			Betas:       []anthropic.AnthropicBeta{anthropic.AnthropicBetaMessageBatches2024_09_24},
   309  			WorkspaceID: anthropic.String("wrkspc_011CZkZaBF1tNoB5wlCeusgy"),
   310  		},
   311  	)
   312  	if err != nil {
   313  		var apierr *anthropic.Error
   314  		if errors.As(err, &apierr) {
   315  			t.Log(string(apierr.DumpRequest(true)))
   316  		}
   317  		t.Fatalf("err should be nil: %s", err.Error())
   318  	}
   319  }