Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set custom header per request #494

Open
colinjc opened this issue Jun 5, 2024 · 1 comment
Open

Set custom header per request #494

colinjc opened this issue Jun 5, 2024 · 1 comment

Comments

@colinjc
Copy link

colinjc commented Jun 5, 2024

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.

@alexrudall
Copy link
Owner

@colinjc thanks for the request, I'd be open to a PR on this. Out of interest, what's the benefit of caching the client - have you measured any improvement in memory etc.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants