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

Library doesn't support setting custom headers #122

Open
funCapital opened this issue Nov 9, 2024 · 2 comments
Open

Library doesn't support setting custom headers #122

funCapital opened this issue Nov 9, 2024 · 2 comments

Comments

@funCapital
Copy link

Describe the feature or improvement you're requesting

Description

Some integrations with LLM providers require setting custom headers in API requests. For example, I am using Helicone alongside PostHog, which requires additional headers (Helicone-Auth, Helicone-Posthog-Key, etc.) to track usage and analytics effectively. However, I could not find an option to set custom headers in the OpenAI library.

Expected Behavior

The client initialization could allow a default_headers parameter to define headers that apply to all requests, e.g.:

client = OpenAI(
    api_key="<your-api-key>",
    default_headers={
        "Helicone-Auth": f"Bearer {HELICONE_API_KEY}",
        "Helicone-Posthog-Key": "<ph_project_api_key>",
        "Helicone-Posthog-Host": "<ph_client_api_host>",  # Optional
    }
)

This would provide flexibility for monitoring, analytics, and other third-party integrations.

Proposed Implementation

If this feature is not currently supported, I am happy to contribute by submitting a PR to add this functionality. Please let me know if this aligns with the library's goals.

Use Cases

Tracking requests with Helicone and PostHog.
Supporting other custom integrations that require headers.

Steps Taken

I reviewed the library documentation and source code but could not find support for setting default headers. If this feature is already available, I'd appreciate guidance on how to use it.

Additional context

No response

@dongri
Copy link
Owner

dongri commented Nov 10, 2024

Got it; I understand what you're aiming to do. Let me think about how to implement it. I’d like to tidy up new_with_xxxx.

@dongri
Copy link
Owner

dongri commented Nov 10, 2024

https://github.com/dongri/openai-api-rs/pull/123/files
I responded in this way.

@funCapital Please use v5.2.0.

let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder()
    .with_api_key(api_key)
    .with_endpoint("https://oai.helicone.ai/v1")
    .with_header("Helicone-Auth", "sk-helicone-xxxxxx")
    .with_header("Helicone-Posthog-Key", "<ph_project_api_key>")
    .build()?;

I think we can use the Helicone API if we write it this way.

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