Skip to content

Set custom header per request #494

@colinjc

Description

@colinjc

Is your feature request related to a problem? Please describe.
Very similar to #333

I'm using a proxy for requests to OpenAI and want to attach arbitrary dynamic metadata to each request as headers. This enables us to log extra information on who's sending the request through our services.

Basically - user sends request to internal service, service builds the prompt, adds their user id as header, and sends request to proxy using ruby-openai.

Current implementation only allows us to set a proxy on Client creation, but we cache it for multiple requests.

Describe the solution you'd like

Extra headers should be passed in with the rest of the request and merge into the extra_headers set in the Client.

def intitialize
  @client = OpenAI::Client.new(...)
end

def request
  @client.chat(
      parameters: {
        ...
      },
      extra_headers: {
        'x-something-userid': '123'
      }
    )
end

Describe alternatives you've considered
Creating a client per request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions