Skip to content

Commit

Permalink
adds OpenAI project authentication. (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbettag authored Aug 18, 2024
1 parent dfc8c6e commit 69943f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/chat_models/chat_open_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ defmodule LangChain.ChatModels.ChatOpenAI do
Config.resolve(:openai_org_id)
end

@spec get_proj_id() :: String.t() | nil
defp get_proj_id() do
Config.resolve(:openai_proj_id)
end

@doc """
Setup a ChatOpenAI client configuration.
"""
Expand Down Expand Up @@ -497,6 +502,7 @@ defmodule LangChain.ChatModels.ChatOpenAI do

req
|> maybe_add_org_id_header()
|> maybe_add_proj_id_header()
|> Req.post()
# parse the body and return it as parsed structs
|> case do
Expand Down Expand Up @@ -552,6 +558,7 @@ defmodule LangChain.ChatModels.ChatOpenAI do
receive_timeout: openai.receive_timeout
)
|> maybe_add_org_id_header()
|> maybe_add_proj_id_header()
|> Req.post(
into: Utils.handle_stream_fn(openai, &decode_stream/1, &do_process_response(openai, &1))
)
Expand Down Expand Up @@ -844,6 +851,16 @@ defmodule LangChain.ChatModels.ChatOpenAI do
end
end

defp maybe_add_proj_id_header(%Req.Request{} = req) do
proj_id = get_proj_id()

if proj_id do
Req.Request.put_header(req, "OpenAI-Project", proj_id)
else
req
end
end

defp get_ratelimit_info(response_headers) do
# extract out all the ratelimit response headers
#
Expand Down
16 changes: 16 additions & 0 deletions lib/images/open_ai_image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ defmodule LangChain.Images.OpenAIImage do
Config.resolve(:openai_org_id)
end

@spec get_proj_id() :: String.t() | nil
defp get_proj_id() do
Config.resolve(:openai_proj_id)
end

@doc """
Setup a OpenAIImage client configuration.
"""
Expand Down Expand Up @@ -235,6 +240,7 @@ defmodule LangChain.Images.OpenAIImage do

req
|> maybe_add_org_id_header()
|> maybe_add_proj_id_header()
|> Req.post()
# parse the body and return it as parsed structs
|> case do
Expand Down Expand Up @@ -324,4 +330,14 @@ defmodule LangChain.Images.OpenAIImage do
req
end
end

defp maybe_add_proj_id_header(%Req.Request{} = req) do
proj_id = get_proj_id()

if proj_id do
Req.Request.put_header(req, "OpenAI-Project", proj_id)
else
req
end
end
end

0 comments on commit 69943f3

Please sign in to comment.