diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index 434ff276a702f67cafc9fb55214791cf0d678e76..db151aefb80512862f3cc7c4583c83f3777a1b70 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -50,7 +50,7 @@ type provider struct { type Option = func(*options) // New creates a new Anthropic provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { providerOptions := options{ headers: map[string]string{}, } @@ -60,7 +60,7 @@ func New(opts ...Option) fantasy.Provider { providerOptions.baseURL = cmp.Or(providerOptions.baseURL, DefaultURL) providerOptions.name = cmp.Or(providerOptions.name, Name) - return &provider{options: providerOptions} + return &provider{options: providerOptions}, nil } // WithBaseURL sets the base URL for the Anthropic provider. diff --git a/providers/azure/azure.go b/providers/azure/azure.go index b70ead06ab459e4093b485389b9d6bd1c186d382..7e2659b934a8dd5d50272cd2fdfbe1b7b3e2dc81 100644 --- a/providers/azure/azure.go +++ b/providers/azure/azure.go @@ -27,7 +27,7 @@ const ( type Option = func(*options) // New creates a new Azure provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { o := options{ apiVersion: defaultAPIVersion, } diff --git a/providers/bedrock/bedrock.go b/providers/bedrock/bedrock.go index 68f310029a241ba3d18564d39e89ef9eb37ce876..215021c1834ad0267f30f894b752f48f7fbafdfa 100644 --- a/providers/bedrock/bedrock.go +++ b/providers/bedrock/bedrock.go @@ -21,7 +21,7 @@ const ( type Option = func(*options) // New creates a new Bedrock provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { var o options for _, opt := range opts { opt(&o) diff --git a/providers/google/google.go b/providers/google/google.go index 7e8311ee447dad9c06381c40e6b442847e4d0cee..00d72ec8808699502da443f703b1ee45f78e201b 100644 --- a/providers/google/google.go +++ b/providers/google/google.go @@ -41,7 +41,7 @@ type options struct { type Option = func(*options) // New creates a new Google provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { options := options{ headers: map[string]string{}, } @@ -53,7 +53,7 @@ func New(opts ...Option) fantasy.Provider { return &provider{ options: options, - } + }, nil } // WithBaseURL sets the base URL for the Google provider. @@ -128,11 +128,15 @@ type languageModel struct { // LanguageModel implements fantasy.Provider. func (a *provider) LanguageModel(modelID string) (fantasy.LanguageModel, error) { if strings.Contains(modelID, "anthropic") || strings.Contains(modelID, "claude") { - return anthropic.New( + p, err := anthropic.New( anthropic.WithVertex(a.options.project, a.options.location), anthropic.WithHTTPClient(a.options.client), anthropic.WithSkipAuth(a.options.skipAuth), - ).LanguageModel(modelID) + ) + if err != nil { + return nil, err + } + return p.LanguageModel(modelID) } cc := &genai.ClientConfig{ diff --git a/providers/openai/openai.go b/providers/openai/openai.go index 2af39fc7ebcd77f295bfcf6efc3b6acfd366d14c..fdb43cc5897b01cf41e5fb89e0456690a5796776 100644 --- a/providers/openai/openai.go +++ b/providers/openai/openai.go @@ -38,7 +38,7 @@ type options struct { type Option = func(*options) // New creates a new OpenAI provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { providerOptions := options{ headers: map[string]string{}, languageModelOptions: make([]LanguageModelOption, 0), @@ -57,7 +57,7 @@ func New(opts ...Option) fantasy.Provider { providerOptions.headers["OpenAi-Project"] = providerOptions.project } - return &provider{options: providerOptions} + return &provider{options: providerOptions}, nil } // WithBaseURL sets the base URL for the OpenAI provider. diff --git a/providers/openai/openai_test.go b/providers/openai/openai_test.go index f08d417ac441865c3a925a0844c2777c723879e9..74beb7c9f3a28d4fc1aa72a33791762de9074e9c 100644 --- a/providers/openai/openai_test.go +++ b/providers/openai/openai_test.go @@ -809,10 +809,11 @@ func TestDoGenerate(t *testing.T) { "content": "Hello, World!", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -841,10 +842,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -865,13 +867,14 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, }) @@ -905,10 +908,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -931,10 +935,11 @@ func TestDoGenerate(t *testing.T) { "logprobs": testLogprobs, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -965,10 +970,11 @@ func TestDoGenerate(t *testing.T) { "finish_reason": "stop", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -989,10 +995,11 @@ func TestDoGenerate(t *testing.T) { "finish_reason": "eos", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1013,13 +1020,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, }) @@ -1045,13 +1053,14 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ LogitBias: map[string]int64{ @@ -1087,13 +1096,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-mini") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions( &ProviderOptions{ @@ -1127,13 +1137,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ TextVerbosity: fantasy.Opt("low"), @@ -1165,13 +1176,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, Tools: []fantasy.Tool{ fantasy.FunctionTool{ @@ -1237,10 +1249,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1295,10 +1308,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1337,10 +1351,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o-mini") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1372,10 +1387,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o-mini") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1400,10 +1416,11 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-preview") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1448,13 +1465,14 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-preview") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, MaxOutputTokens: &[]int64{1000}[0], }) @@ -1492,10 +1510,11 @@ func TestDoGenerate(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-preview") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1519,13 +1538,14 @@ func TestDoGenerate(t *testing.T) { "model": "o1-preview", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-preview") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ MaxCompletionTokens: fantasy.Opt(int64(255)), @@ -1557,13 +1577,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ Prediction: map[string]any{ @@ -1601,13 +1622,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ Store: fantasy.Opt(true), @@ -1639,13 +1661,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ Metadata: map[string]any{ @@ -1681,13 +1704,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ PromptCacheKey: fantasy.Opt("test-cache-key-123"), @@ -1719,13 +1743,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ SafetyIdentifier: fantasy.Opt("test-safety-identifier-123"), @@ -1755,10 +1780,11 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o-search-preview") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1789,13 +1815,14 @@ func TestDoGenerate(t *testing.T) { "content": "", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o3-mini") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ ServiceTier: fantasy.Opt("flex"), @@ -1825,10 +1852,11 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o-mini") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -1858,13 +1886,14 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o-mini") - _, err := model.Generate(context.Background(), fantasy.Call{ + _, err = model.Generate(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ ServiceTier: fantasy.Opt("priority"), @@ -1894,10 +1923,11 @@ func TestDoGenerate(t *testing.T) { server.prepareJSONResponse(map[string]any{}) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") result, err := model.Generate(context.Background(), fantasy.Call{ @@ -2201,10 +2231,11 @@ func TestDoStream(t *testing.T) { "logprobs": testLogprobs, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2257,10 +2288,11 @@ func TestDoStream(t *testing.T) { server.prepareToolStreamResponse() - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2343,10 +2375,11 @@ func TestDoStream(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2382,10 +2415,11 @@ func TestDoStream(t *testing.T) { server.prepareErrorStreamResponse() - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2423,13 +2457,14 @@ func TestDoStream(t *testing.T) { "content": []string{}, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Stream(context.Background(), fantasy.Call{ + _, err = model.Stream(context.Background(), fantasy.Call{ Prompt: testPrompt, }) @@ -2471,10 +2506,11 @@ func TestDoStream(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2521,10 +2557,11 @@ func TestDoStream(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2564,13 +2601,14 @@ func TestDoStream(t *testing.T) { "content": []string{}, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Stream(context.Background(), fantasy.Call{ + _, err = model.Stream(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ Store: fantasy.Opt(true), @@ -2606,13 +2644,14 @@ func TestDoStream(t *testing.T) { "content": []string{}, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-3.5-turbo") - _, err := model.Stream(context.Background(), fantasy.Call{ + _, err = model.Stream(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ Metadata: map[string]any{ @@ -2652,13 +2691,14 @@ func TestDoStream(t *testing.T) { "content": []string{}, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o3-mini") - _, err := model.Stream(context.Background(), fantasy.Call{ + _, err = model.Stream(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ ServiceTier: fantasy.Opt("flex"), @@ -2694,13 +2734,14 @@ func TestDoStream(t *testing.T) { "content": []string{}, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("gpt-4o-mini") - _, err := model.Stream(context.Background(), fantasy.Call{ + _, err = model.Stream(context.Background(), fantasy.Call{ Prompt: testPrompt, ProviderOptions: NewProviderOptions(&ProviderOptions{ ServiceTier: fantasy.Opt("priority"), @@ -2737,10 +2778,11 @@ func TestDoStream(t *testing.T) { "model": "o1-preview", }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-preview") stream, err := model.Stream(context.Background(), fantasy.Call{ @@ -2783,10 +2825,11 @@ func TestDoStream(t *testing.T) { }, }) - provider := New( + provider, err := New( WithAPIKey("test-api-key"), WithBaseURL(server.server.URL), ) + require.NoError(t, err) model, _ := provider.LanguageModel("o1-preview") stream, err := model.Stream(context.Background(), fantasy.Call{ diff --git a/providers/openaicompat/openaicompat.go b/providers/openaicompat/openaicompat.go index 8af033fcb372269425470cf1d13b9eb1f121cc6f..ed7045c8c5bf07f3d68d448a7e6a3b6da6c63a57 100644 --- a/providers/openaicompat/openaicompat.go +++ b/providers/openaicompat/openaicompat.go @@ -22,7 +22,7 @@ const ( type Option = func(*options) // New creates a new OpenAI-compatible provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { providerOptions := options{ openaiOptions: []openai.Option{ openai.WithName(Name), diff --git a/providers/openrouter/openrouter.go b/providers/openrouter/openrouter.go index 211a0522d1dfb17f46ed35d6f17ebf5307fc1588..7551c14b1bbca5a23f7c8be14df08ba7ee6a0a3c 100644 --- a/providers/openrouter/openrouter.go +++ b/providers/openrouter/openrouter.go @@ -25,7 +25,7 @@ const ( type Option = func(*options) // New creates a new OpenRouter provider with the given options. -func New(opts ...Option) fantasy.Provider { +func New(opts ...Option) (fantasy.Provider, error) { providerOptions := options{ openaiOptions: []openai.Option{ openai.WithName(Name), diff --git a/providertests/anthropic_test.go b/providertests/anthropic_test.go index 72f30a388d8c48bf00c6bd96f0752064b15f0070..85d18d2ac5351a353e8fb079f8ec537fbee8dd43 100644 --- a/providertests/anthropic_test.go +++ b/providertests/anthropic_test.go @@ -137,10 +137,13 @@ func testAnthropicThinking(t *testing.T, result *fantasy.AgentResult) { func anthropicBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := anthropic.New( + provider, err := anthropic.New( anthropic.WithAPIKey(os.Getenv("FANTASY_ANTHROPIC_API_KEY")), anthropic.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } diff --git a/providertests/azure_test.go b/providertests/azure_test.go index 8a7aaeb04aa3519c694f6c3ee83a6feab4c58da2..13ca6d0276c34bd06693e01b6267a3dd979d5a4a 100644 --- a/providertests/azure_test.go +++ b/providertests/azure_test.go @@ -40,28 +40,37 @@ func testAzureThinking(t *testing.T, result *fantasy.AgentResult) { } func builderAzureO4Mini(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := azure.New( + provider, err := azure.New( azure.WithBaseURL(cmp.Or(os.Getenv("FANTASY_AZURE_BASE_URL"), defaultBaseURL)), azure.WithAPIKey(cmp.Or(os.Getenv("FANTASY_AZURE_API_KEY"), "(missing)")), azure.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("o4-mini") } func builderAzureGpt5Mini(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := azure.New( + provider, err := azure.New( azure.WithBaseURL(cmp.Or(os.Getenv("FANTASY_AZURE_BASE_URL"), defaultBaseURL)), azure.WithAPIKey(cmp.Or(os.Getenv("FANTASY_AZURE_API_KEY"), "(missing)")), azure.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("gpt-5-mini") } func builderAzureGrok3Mini(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := azure.New( + provider, err := azure.New( azure.WithBaseURL(cmp.Or(os.Getenv("FANTASY_AZURE_BASE_URL"), defaultBaseURL)), azure.WithAPIKey(cmp.Or(os.Getenv("FANTASY_AZURE_API_KEY"), "(missing)")), azure.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("grok-3-mini") } diff --git a/providertests/bedrock_test.go b/providertests/bedrock_test.go index edb12454dd3403aa40e152ce8149e8c396a03584..81689dbf088b19066e2590d65a4a99c3bee3680e 100644 --- a/providertests/bedrock_test.go +++ b/providertests/bedrock_test.go @@ -23,34 +23,46 @@ func TestBedrockBasicAuth(t *testing.T) { } func builderBedrockClaude3Sonnet(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := bedrock.New( + provider, err := bedrock.New( bedrock.WithHTTPClient(&http.Client{Transport: r}), bedrock.WithSkipAuth(!r.IsRecording()), ) + if err != nil { + return nil, err + } return provider.LanguageModel("us.anthropic.claude-3-sonnet-20240229-v1:0") } func builderBedrockClaude3Opus(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := bedrock.New( + provider, err := bedrock.New( bedrock.WithHTTPClient(&http.Client{Transport: r}), bedrock.WithSkipAuth(!r.IsRecording()), ) + if err != nil { + return nil, err + } return provider.LanguageModel("us.anthropic.claude-3-opus-20240229-v1:0") } func builderBedrockClaude3Haiku(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := bedrock.New( + provider, err := bedrock.New( bedrock.WithHTTPClient(&http.Client{Transport: r}), bedrock.WithSkipAuth(!r.IsRecording()), ) + if err != nil { + return nil, err + } return provider.LanguageModel("us.anthropic.claude-3-haiku-20240307-v1:0") } func buildersBedrockBasicAuth(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := bedrock.New( + provider, err := bedrock.New( bedrock.WithHTTPClient(&http.Client{Transport: r}), bedrock.WithAPIKey(os.Getenv("FANTASY_BEDROCK_API_KEY")), bedrock.WithSkipAuth(true), ) + if err != nil { + return nil, err + } return provider.LanguageModel("us.anthropic.claude-3-sonnet-20240229-v1:0") } diff --git a/providertests/google_test.go b/providertests/google_test.go index e2d7fcd28f34b7855e6e73de83061457765ed99b..6109c3ed7b3d57e3ed524ee34aa0878c04b74544 100644 --- a/providertests/google_test.go +++ b/providertests/google_test.go @@ -71,21 +71,27 @@ func testGoogleThinking(t *testing.T, result *fantasy.AgentResult) { func geminiBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := google.New( + provider, err := google.New( google.WithGeminiAPIKey(cmp.Or(os.Getenv("FANTASY_GEMINI_API_KEY"), "(missing)")), google.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } func vertexBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := google.New( + provider, err := google.New( google.WithVertex(os.Getenv("FANTASY_VERTEX_PROJECT"), os.Getenv("FANTASY_VERTEX_LOCATION")), google.WithHTTPClient(&http.Client{Transport: r}), google.WithSkipAuth(!r.IsRecording()), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } diff --git a/providertests/image_upload_test.go b/providertests/image_upload_test.go index 7b5b2ba02f57a9c64c588a35cdbc2a4a4a9feca4..3579c13ba669519337d0ba82ed8e41fe8f4e96cf 100644 --- a/providertests/image_upload_test.go +++ b/providertests/image_upload_test.go @@ -16,30 +16,39 @@ import ( func anthropicImageBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := anthropic.New( + provider, err := anthropic.New( anthropic.WithAPIKey(cmp.Or(os.Getenv("FANTASY_ANTHROPIC_API_KEY"), "(missing)")), anthropic.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } func openAIImageBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openai.New( + provider, err := openai.New( openai.WithAPIKey(cmp.Or(os.Getenv("FANTASY_OPENAI_API_KEY"), "(missing)")), openai.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } func geminiImageBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := google.New( + provider, err := google.New( google.WithGeminiAPIKey(cmp.Or(os.Getenv("FANTASY_GEMINI_API_KEY"), "(missing)")), google.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } diff --git a/providertests/openai_responses_test.go b/providertests/openai_responses_test.go index 251ae4a41fccffa2688174fd72d4c5961e40c591..dce825a86806d302550498b0b8e9e6b2a604dd81 100644 --- a/providertests/openai_responses_test.go +++ b/providertests/openai_responses_test.go @@ -21,11 +21,14 @@ func TestOpenAIResponsesCommon(t *testing.T) { func openAIReasoningBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openai.New( + provider, err := openai.New( openai.WithAPIKey(os.Getenv("FANTASY_OPENAI_API_KEY")), openai.WithHTTPClient(&http.Client{Transport: r}), openai.WithUseResponsesAPI(), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } diff --git a/providertests/openai_test.go b/providertests/openai_test.go index 371c5fc7400f2c0e84d6e78bbcd69a98f640deee..3a868fe1b05c028c4dad883cbd2494973e7f8226 100644 --- a/providertests/openai_test.go +++ b/providertests/openai_test.go @@ -27,10 +27,13 @@ func TestOpenAICommon(t *testing.T) { func openAIBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openai.New( + provider, err := openai.New( openai.WithAPIKey(os.Getenv("FANTASY_OPENAI_API_KEY")), openai.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } } diff --git a/providertests/openaicompat_test.go b/providertests/openaicompat_test.go index b967ef7a34f623b2e340e4e671a3294900d8d958..bc6dc0097b1639917281100319bb099c222e8fcd 100644 --- a/providertests/openaicompat_test.go +++ b/providertests/openaicompat_test.go @@ -49,55 +49,73 @@ func testOpenAICompatThinking(t *testing.T, result *fantasy.AgentResult) { } func builderXAIGrokCodeFast(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openaicompat.New( + provider, err := openaicompat.New( openaicompat.WithBaseURL("https://api.x.ai/v1"), openaicompat.WithAPIKey(os.Getenv("FANTASY_XAI_API_KEY")), openaicompat.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("grok-code-fast-1") } func builderXAIGrok4Fast(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openaicompat.New( + provider, err := openaicompat.New( openaicompat.WithBaseURL("https://api.x.ai/v1"), openaicompat.WithAPIKey(os.Getenv("FANTASY_XAI_API_KEY")), openaicompat.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("grok-4-fast") } func builderXAIGrok3Mini(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openaicompat.New( + provider, err := openaicompat.New( openaicompat.WithBaseURL("https://api.x.ai/v1"), openaicompat.WithAPIKey(os.Getenv("FANTASY_XAI_API_KEY")), openaicompat.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("grok-3-mini") } func builderZAIGLM45(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openaicompat.New( + provider, err := openaicompat.New( openaicompat.WithBaseURL("https://api.z.ai/api/coding/paas/v4"), openaicompat.WithAPIKey(os.Getenv("FANTASY_ZAI_API_KEY")), openaicompat.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("glm-4.5") } func builderGroq(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openaicompat.New( + provider, err := openaicompat.New( openaicompat.WithBaseURL("https://api.groq.com/openai/v1"), openaicompat.WithAPIKey(os.Getenv("FANTASY_GROQ_API_KEY")), openaicompat.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("moonshotai/kimi-k2-instruct-0905") } func builderHuggingFace(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openaicompat.New( + provider, err := openaicompat.New( openaicompat.WithBaseURL("https://router.huggingface.co/v1"), openaicompat.WithAPIKey(os.Getenv("FANTASY_HUGGINGFACE_API_KEY")), openaicompat.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel("Qwen/Qwen3-Coder-480B-A35B-Instruct:cerebras") } diff --git a/providertests/openrouter_test.go b/providertests/openrouter_test.go index 589ade1f024e23a8dfa3b99e2f54e6947df82f09..32a2023d3a0f55fc2ed32fd6781d617279bc3460 100644 --- a/providertests/openrouter_test.go +++ b/providertests/openrouter_test.go @@ -116,10 +116,13 @@ func testOpenrouterThinking(t *testing.T, result *fantasy.AgentResult) { func openrouterBuilder(model string) builderFunc { return func(r *recorder.Recorder) (fantasy.LanguageModel, error) { - provider := openrouter.New( + provider, err := openrouter.New( openrouter.WithAPIKey(os.Getenv("FANTASY_OPENROUTER_API_KEY")), openrouter.WithHTTPClient(&http.Client{Transport: r}), ) + if err != nil { + return nil, err + } return provider.LanguageModel(model) } }