Skip to content

feat: add passing http_client to components using OpenAI clients #9170

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

alexengrig
Copy link

@alexengrig alexengrig commented Apr 3, 2025

Related Issues

Proposed Changes:

When using components with an OpenAI client, we can configure the HTTP client by passing http_client.

How did you test it?

I don’t have any ideas for suitable tests, could you suggest some tests that could be added?

I added unit tests for the new parameter http_client.

Notes for the reviewer

I would like to add documentation on using proxies, authentication, and HTTP_PROXY support, could you tell me where I should add this documentation?

I added HTTP_PROXY as an example to the docstring.

Checklist

  • I have read the contributors guidelines and the code of conduct
  • I have updated the related issue with new insights and changes
  • I added unit tests
  • I updated the docstrings
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I documented my code
  • I ran pre-commit hooks and fixed any issue

@CLAassistant
Copy link

CLAassistant commented Apr 3, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the type:documentation Improvements on the docs label Apr 3, 2025
@alexengrig
Copy link
Author

I would like to add tests of using HTTP_PROXY/HTTPS_PROXY and what do you think – should I write it as a unit test (see below) or as an integration test (with a running mock server)?:

    def test_init_with_proxy_in_http_client_by_env(self, monkeypatch):
        monkeypatch.setenv('HTTP_PROXY', 'http://user:password@my-proxy.com:8080')
        monkeypatch.setenv('HTTPS_PROXY', 'https://user:password@my-proxy.com:4433')
        embedder = OpenAITextEmbedder(
            api_key=Secret.from_token('fake-api-key'),
        )
        proxies_with_auth_headers = {
            (str(mount._pool._proxy_url.origin), mount._pool._proxy_headers[0][1])
            for mount in embedder.client._client._mounts.values()
        }
        assert proxies_with_auth_headers == {
            ('http://my-proxy.com:8080', b'Basic dXNlcjpwYXNzd29yZA=='),
            ('https://my-proxy.com:4433', b'Basic dXNlcjpwYXNzd29yZA=='),
        }

@alexengrig alexengrig marked this pull request as ready for review April 8, 2025 17:40
@alexengrig alexengrig requested a review from a team as a code owner April 8, 2025 17:40
@alexengrig alexengrig requested review from vblagoje and removed request for a team April 8, 2025 17:40
…o-openai

# Conflicts:
#	haystack/components/embedders/openai_document_embedder.py
#	haystack/components/embedders/openai_text_embedder.py
#	haystack/components/generators/chat/openai.py
@alexengrig alexengrig requested a review from a team as a code owner April 8, 2025 18:43
@alexengrig alexengrig requested review from dfokina and removed request for a team April 8, 2025 18:43
@vblagoje
Copy link
Member

vblagoje commented Apr 9, 2025

Hey @alexengrig thanks for this effort. Let's split it in a few PRs. Perhaps we can start with OpenAIChatGenerator only as a test pilot PR. Note that you seem to be using 3.10 syntax, we still need to support 3.9 so let's use that syntax still. Perhaps open a new PR for OpenAIChatGenerator only?

@alexengrig
Copy link
Author

Hi @vblagoje!

Perhaps open a new PR for OpenAIChatGenerator only?

I agree, and I'll open it.

Note that you seem to be using 3.10 syntax, we still need to support 3.9 so let's use that syntax still.

OK, I'll fix it.

@anakin87
Copy link
Member

Hello!

For serialization/deserialization purposes, I think we should allow passing http_client_kwargs instead of the client itself.
You can find an example implementation in the AzureOpenAITextEmbedder.

@Amnah199
Copy link
Contributor

Hi @alexengrig,
Thank you for picking this up! I’ve opened a PR #9215 that adds client config support to both OpenAIChatGenerator and AzureChatGenerator. Feel free to use it as a reference for adding similar support to other components. Additionally, we’ve introduced a new utility method for initializing the HTTP client, which might be helpful to integrate as well.

@alexengrig
Copy link
Author

alexengrig commented Apr 12, 2025

Perhaps open a new PR for OpenAIChatGenerator only?

@vblagoje
This PR (#9215) enables OpenAI client configuration in OpenAIChatGenerator and all Azure* components. The only remaining task is to add the configuration to the following components:

  • OpenAIDocumentEmbedder and OpenAITextEmbedder
  • OpenAIGenerator
  • RemoteWhisperTranscriber
  • DALLEImageGenerator

What should I do with the current PR, and how should I split the changes for the components into additional PRs?
Embedders, generators and transcriber (3 PRs)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add passing http_client to OpenAI clients
5 participants