ALLM.Providers.Support.OpenAIHeaders (allm v0.3.0)

Copy Markdown View Source

Shared HTTP-header builder for OpenAI provider adapters. See spec §35.7 Decision #11.

Layer B helper. Both the chat adapter (ALLM.Providers.OpenAI) and the image adapter (ALLM.Providers.OpenAI.Images) inject the same Authorization and (optional) openai-organization headers; the only difference is whether content-type: application/json is set explicitly (json_headers/2) or elided so Req's :form_multipart step can stamp the multipart boundary itself (multipart_headers/2).

Both functions honor opts[:adapter_opts][:organization] per the chat-adapter precedent at lib/allm/providers/openai.ex:443-446 — callers wishing to scope a request to an OpenAI org pass adapter_opts: [organization: "org-..."] in their opts keyword list.

Examples

iex> ALLM.Providers.Support.OpenAIHeaders.json_headers("sk-test", [])
[{"authorization", "Bearer sk-test"}, {"content-type", "application/json"}]

iex> ALLM.Providers.Support.OpenAIHeaders.multipart_headers("sk-test", [])
[{"authorization", "Bearer sk-test"}]

iex> opts = [adapter_opts: [organization: "org-abc"]]
iex> ALLM.Providers.Support.OpenAIHeaders.json_headers("sk-test", opts)
[
  {"openai-organization", "org-abc"},
  {"authorization", "Bearer sk-test"},
  {"content-type", "application/json"}
]

iex> opts = [adapter_opts: [organization: "org-abc"]]
iex> ALLM.Providers.Support.OpenAIHeaders.multipart_headers("sk-test", opts)
[{"openai-organization", "org-abc"}, {"authorization", "Bearer sk-test"}]

Summary

Functions

Build headers for a JSON-bodied request. Returns [{"authorization", "Bearer <api_key>"}, {"content-type", "application/json"}, ...] optionally prefixed with {"openai-organization", org} when opts[:adapter_opts][:organization] is a binary.

Build headers for a multipart/form-data request. Returns [{"authorization", "Bearer <api_key>"}] only — content-type is intentionally elided so Req's :form_multipart step stamps it with the boundary string. Optionally prefixed with {"openai-organization", org} when set.

Functions

json_headers(api_key, opts)

@spec json_headers(api_key :: String.t(), opts :: keyword()) :: [
  {String.t(), String.t()}
]

Build headers for a JSON-bodied request. Returns [{"authorization", "Bearer <api_key>"}, {"content-type", "application/json"}, ...] optionally prefixed with {"openai-organization", org} when opts[:adapter_opts][:organization] is a binary.

multipart_headers(api_key, opts)

@spec multipart_headers(api_key :: String.t(), opts :: keyword()) :: [
  {String.t(), String.t()}
]

Build headers for a multipart/form-data request. Returns [{"authorization", "Bearer <api_key>"}] only — content-type is intentionally elided so Req's :form_multipart step stamps it with the boundary string. Optionally prefixed with {"openai-organization", org} when set.