Skip to content

Commit

Permalink
Add http_headers config option to allow beta headers. E.g.:
Browse files Browse the repository at this point in the history
```
config :ex_openai,
  http_headers: [
    {"OpenAI-Beta", "assistants=v1"}
  ]
```
  • Loading branch information
emcmanus committed Dec 8, 2023
1 parent 77673ac commit 4636d51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/ex_openai/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ defmodule ExOpenAI.Client do

def request_options(), do: Config.http_options()

def default_headers(), do: Config.http_headers()

def stream_options(request_options, convert_response) do
with {:ok, stream_val} <- Keyword.fetch(request_options, :stream),
{:ok, stream_to} when is_pid(stream_to) or is_function(stream_to) <-
Expand All @@ -82,7 +84,7 @@ defmodule ExOpenAI.Client do
request_options_map = Enum.into(request_options, %{})

headers =
[]
default_headers()
|> add_json_request_headers()
|> add_organization_header(Map.get(request_options_map, :openai_organization_key, nil))
|> add_bearer_header(Map.get(request_options_map, :openai_api_key, nil))
Expand Down Expand Up @@ -114,7 +116,7 @@ defmodule ExOpenAI.Client do
request_options_map = Enum.into(request_options, %{})

headers =
[]
default_headers()
|> add_json_request_headers()
|> add_organization_header(Map.get(request_options_map, :openai_organization_key, nil))
|> add_bearer_header(Map.get(request_options_map, :openai_api_key, nil))
Expand All @@ -136,7 +138,7 @@ defmodule ExOpenAI.Client do
request_options_map = Enum.into(request_options, %{})

headers =
[]
default_headers()
|> add_json_request_headers()
|> add_organization_header(Map.get(request_options_map, :openai_organization_key, nil))
|> add_bearer_header(Map.get(request_options_map, :openai_api_key, nil))
Expand Down Expand Up @@ -181,7 +183,7 @@ defmodule ExOpenAI.Client do
|> Enum.map(&multipart_param/1)}

headers =
[]
default_headers()
|> add_multipart_request_headers()
|> add_organization_header(Map.get(request_options_map, :openai_organization_key, nil))
|> add_bearer_header(Map.get(request_options_map, :openai_api_key, nil))
Expand Down
5 changes: 4 additions & 1 deletion lib/ex_openai/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule ExOpenAI.Config do
@config_keys [
:api_key,
:organization_key,
:http_options
:http_options,
:http_headers
]

def start_link(opts), do: GenServer.start_link(__MODULE__, opts, name: __MODULE__)
Expand All @@ -36,6 +37,8 @@ defmodule ExOpenAI.Config do
# HTTP Options
def http_options, do: get_config_value(:http_options, [])

def http_headers, do: get_config_value(:http_headers, [])

defp get_config_value(key, default \\ nil) do
value =
:ex_openai
Expand Down

0 comments on commit 4636d51

Please sign in to comment.