huggingface / huggingface_hub

The official Python client for the Huggingface Hub.
https://huggingface.co/docs/huggingface_hub
Apache License 2.0
1.82k stars 470 forks source link

Handle proxy with AsyncInferenceClient post request #2349

Closed noech373 closed 6 days ago

noech373 commented 1 week ago

There is a problem when you try to do a post request with an AsyncInferenceClient object : It doesn't handle the case where you're using a proxy, so the post request never reaches the given URL. I wanted to add the ability to configure the proxy to perform this post request.

Here is a proposal of what I did to add this feature :

class AsyncInferenceClient : 
   def __init__(
      self,
      ....
      proxy: Optional[str] = None
   ) -> None:
      ...
      self.proxy = proxy

   async def post(...):
      ...
      try:
         response = await client.post(url, json=json, data=data_as_binary, proxy=self.proxy)
      ...

class InferenceClient():
   def __init__(
      self,
      ...
      proxy: Optional[str] = None
   ) -> None: 
      ...
      self.proxy = proxy

   def post(...):
      ...
      try: 
         response = get_session().post(url, ..., proxies=self.proxy)
      ...

I did for both synchronous and asynchronous InferenceClient because of the following code in HuggingFaceEndpoint :

class HuggingFaceEndpoint(LLM):
   def validate_environment(...) -> Dict:
      ...
      values["client"] = InferenceClient(
            model=values["model"],
            timeout=values["timeout"],
            token=huggingfacehub_api_token,
            **values["server_kwargs"],
        )
        values["async_client"] = AsyncInferenceClient(
            model=values["model"],
            timeout=values["timeout"],
            token=huggingfacehub_api_token,
            **values["server_kwargs"],
        )

To finish, here is an example of how to build a HuggingFaceEndpoint object with a specified proxy for our AsyncInferenceClient :

HuggingFaceEndpoint(endpoint_url=... , ... , server_kwargs={"proxy": "http://localhost:3128"})

Wauplin commented 1 week ago

Thanks @noech373 for opening this issue! I reviewed and commented directly on your PR :)