Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/req_llm/provider/defaults.ex
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,11 @@ defmodule ReqLLM.Provider.Defaults do
|> Req.Request.put_header("authorization", "Bearer #{api_key}")
|> Req.Request.register_options(extra_option_keys)
|> Req.Request.merge_options(
[
finch: ReqLLM.Application.finch_name(),
model: model.provider_model_id || model.id,
auth: {:bearer, api_key}
] ++
user_opts
finch_option(request) ++
[
model: model.provider_model_id || model.id,
auth: {:bearer, api_key}
] ++ user_opts
)
|> ReqLLM.Step.Retry.attach()
|> ReqLLM.Step.Error.attach()
Expand All @@ -583,6 +582,11 @@ defmodule ReqLLM.Provider.Defaults do
|> ReqLLM.Step.Fixture.maybe_attach(model, user_opts)
end

@spec finch_option(Req.Request.t()) :: keyword()
def finch_option(%Req.Request{} = request) do
[finch: request.options[:finch] || ReqLLM.Application.finch_name()]
end

@doc """
Fetches API key and extra common option keys.
"""
Expand Down
3 changes: 2 additions & 1 deletion lib/req_llm/providers/anthropic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ defmodule ReqLLM.Providers.Anthropic do
|> Req.Request.put_private(:req_llm_model, model)
|> maybe_add_beta_header(user_opts)
|> Req.Request.merge_options(
[finch: ReqLLM.Application.finch_name(), model: get_api_model_id(model)] ++ user_opts
ReqLLM.Provider.Defaults.finch_option(request) ++
[model: get_api_model_id(model)] ++ user_opts
)
|> ReqLLM.Step.Error.attach()
|> ReqLLM.Step.Retry.attach(user_opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/req_llm/providers/azure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ defmodule ReqLLM.Providers.Azure do
end)
end)
|> Req.Request.register_options(registered_option_keys)
|> Req.Request.merge_options([finch: ReqLLM.Application.finch_name()] ++ user_opts)
|> Req.Request.merge_options(ReqLLM.Provider.Defaults.finch_option(request) ++ user_opts)
|> ReqLLM.Step.Retry.attach()
|> ReqLLM.Step.Error.attach()
|> Req.Request.append_response_steps(llm_decode_response: &decode_response/1)
Expand Down
11 changes: 5 additions & 6 deletions lib/req_llm/providers/cohere.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ defmodule ReqLLM.Providers.Cohere do
|> Req.Request.put_header("authorization", "Bearer #{api_key}")
|> Req.Request.register_options(extra_option_keys)
|> Req.Request.merge_options(
[
finch: ReqLLM.Application.finch_name(),
model: model.provider_model_id || model.id,
auth: {:bearer, api_key}
] ++
user_opts
ReqLLM.Provider.Defaults.finch_option(request) ++
[
model: model.provider_model_id || model.id,
auth: {:bearer, api_key}
] ++ user_opts
)
|> ReqLLM.Step.Retry.attach()
|> ReqLLM.Step.Error.attach()
Expand Down
6 changes: 3 additions & 3 deletions lib/req_llm/providers/google_vertex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ defmodule ReqLLM.Providers.GoogleVertex do
gcp_creds = Req.Request.get_private(request, :gcp_credentials)

request
|> Req.Request.merge_options(finch: ReqLLM.Application.finch_name())
|> Req.Request.merge_options(ReqLLM.Provider.Defaults.finch_option(request))
|> ReqLLM.Step.Error.attach()
|> ReqLLM.Step.Retry.attach()
|> Req.Request.append_response_steps(llm_decode_response: &decode_response/1)
Expand Down Expand Up @@ -381,7 +381,7 @@ defmodule ReqLLM.Providers.GoogleVertex do

defp attach_ocr(request, model, gcp_creds, opts) do
request
|> Req.Request.merge_options(finch: ReqLLM.Application.finch_name())
|> Req.Request.merge_options(ReqLLM.Provider.Defaults.finch_option(request))
|> ReqLLM.Step.Error.attach()
|> ReqLLM.Step.Retry.attach()
|> ReqLLM.Step.Fixture.maybe_attach(model, opts)
Expand All @@ -390,7 +390,7 @@ defmodule ReqLLM.Providers.GoogleVertex do

defp attach_embedding(request, model, gcp_creds, opts) do
request
|> Req.Request.merge_options(finch: ReqLLM.Application.finch_name())
|> Req.Request.merge_options(ReqLLM.Provider.Defaults.finch_option(request))
|> ReqLLM.Step.Error.attach()
|> ReqLLM.Step.Retry.attach()
|> ReqLLM.Step.Usage.attach(model)
Expand Down
10 changes: 4 additions & 6 deletions lib/req_llm/providers/openai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,10 @@ defmodule ReqLLM.Providers.OpenAI do
|> maybe_put_authorization_header(credential)
|> Req.Request.register_options(extra_option_keys)
|> Req.Request.merge_options(
[
finch: ReqLLM.Application.finch_name(),
model: model.provider_model_id || model.id
] ++
auth_req_options(credential) ++
user_opts
ReqLLM.Provider.Defaults.finch_option(request) ++
[
model: model.provider_model_id || model.id
] ++ auth_req_options(credential) ++ user_opts
)
|> ReqLLM.Step.Retry.attach()
|> ReqLLM.Step.Error.attach()
Expand Down
10 changes: 5 additions & 5 deletions lib/req_llm/providers/openai_codex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ defmodule ReqLLM.Providers.OpenAICodex do
|> Req.Request.put_header("originator", originator)
|> Req.Request.register_options(extra_option_keys)
|> Req.Request.merge_options(
[
finch: ReqLLM.Application.finch_name(),
model: model.provider_model_id || model.id,
auth: {:bearer, credential.token}
] ++ user_opts
ReqLLM.Provider.Defaults.finch_option(request) ++
[
model: model.provider_model_id || model.id,
auth: {:bearer, credential.token}
] ++ user_opts
)
|> attach_retry(user_opts)
|> ReqLLM.Step.Error.attach()
Expand Down
15 changes: 15 additions & 0 deletions test/provider/azure/azure_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ defmodule ReqLLM.Providers.AzureTest do
assert url_string =~ "api-version=2023-05-15"
end

test "preserves custom finch from req_http_options" do
{:ok, model} = ReqLLM.model("azure:gpt-4o")

{:ok, request} =
Azure.prepare_request(
:chat,
model,
"Hello",
base_url: "https://my-resource.openai.azure.com/openai",
req_http_options: [finch: :custom_finch]
)

assert request.options[:finch] == :custom_finch
end

test "embedding operation uses correct endpoint" do
model = %LLMDB.Model{
id: "text-embedding-3-small",
Expand Down
10 changes: 10 additions & 0 deletions test/providers/openai_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ defmodule ReqLLM.Providers.OpenAITest do
assert request.method == :post
end

test "prepare_request preserves custom finch from req_http_options" do
{:ok, model} = ReqLLM.model("openai:gpt-4-turbo")
context = context_fixture()

{:ok, request} =
OpenAI.prepare_request(:chat, model, context, req_http_options: [finch: :custom_finch])

assert request.options[:finch] == :custom_finch
end

test "prepare_request routes gpt-4o models to Responses API" do
{:ok, model} = ReqLLM.model("openai:gpt-4o")
context = context_fixture()
Expand Down
14 changes: 14 additions & 0 deletions test/req_llm/step/retry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ defmodule ReqLLM.Step.RetryTest do
refute request.options[:retry_delay]
end

test "default_attach preserves an existing finch option" do
{:ok, model} = ReqLLM.model("openai:gpt-4")

request =
ReqLLM.Provider.Defaults.default_attach(
ReqLLM.Providers.OpenAI,
Req.new(finch: :custom_finch),
model,
api_key: "test-key"
)

assert request.options[:finch] == :custom_finch
end

test "retry function correctly identifies retryable errors" do
{:ok, model} = ReqLLM.model("openai:gpt-4")

Expand Down