OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Proxy to use OpenAI API

Open gary-young opened this issue 1 month ago • 6 comments

I try to use the OpenAI API with OpenHands Agent GUI. However, due to some network problem, I (and some people) must use proxy to get service from OpenAI. How can I set the proxy?

I install the openhands with a standard installed docker:

Specifically, I install docker in root account and then use newgrp dockerto get the authority to use docker. Then I install OpenHands by the standard instruction in the main page.

  docker pull docker.all-hands.dev/all-hands-ai/runtime:0.19-nikolaik

  docker run -it --rm --pull=always \
      -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.19-nikolaik \
      -e LOG_ALL_EVENTS=true \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v ~/.openhands-state:/.openhands-state \
      -p 3000:3000 \
      --add-host host.docker.internal:host-gateway \
      --name openhands-app \
      docker.all-hands.dev/all-hands-ai/openhands:0.19

I have tried to use HTTP_PROXY and HTTPS_PROXY enviroment variables but it seems useless. I also try this method (https://docs.docker.com/engine/cli/proxy/). Specifically I write the proxy in a ~/.docker/config.json file. However, I find this error.

04:57:34 - openhands:INFO: openhands_config.py:48 - Using config class None
INFO:     Started server process [10]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:3000 (Press CTRL+C to quit)
INFO:     ('172.17.0.1', 39662) - "WebSocket /socket.io/?latest_event_id=0&conversation_id=63bab271fd3b492f9b3aaa3e2cd44e92&EIO=4&transport=websocket" [accepted]
04:57:40 - openhands:INFO: listen_socket.py:25 - sio:connect: xqorPJ8aNPKNeQodAAAB
04:57:40 - openhands:INFO: manager.py:214 - join_conversation:63bab271fd3b492f9b3aaa3e2cd44e92:xqorPJ8aNPKNeQodAAAB
04:57:40 - openhands:INFO: manager.py:369 - _get_event_stream:63bab271fd3b492f9b3aaa3e2cd44e92
04:57:40 - openhands:INFO: manager.py:351 - maybe_start_agent_loop:63bab271fd3b492f9b3aaa3e2cd44e92
04:57:40 - openhands:INFO: manager.py:354 - start_agent_loop:63bab271fd3b492f9b3aaa3e2cd44e92
04:57:40 - openhands:INFO: manager.py:369 - _get_event_stream:63bab271fd3b492f9b3aaa3e2cd44e92
04:57:40 - openhands:INFO: manager.py:372 - found_local_agent_loop:63bab271fd3b492f9b3aaa3e2cd44e92
04:57:42 - openhands:INFO: docker_runtime.py:133 - [runtime 63bab271fd3b492f9b3aaa3e2cd44e92] Starting runtime with image: docker.all-hands.dev/all-hands-ai/runtime:0.19-nikolaik
04:57:52 - openhands:INFO: docker_runtime.py:137 - [runtime 63bab271fd3b492f9b3aaa3e2cd44e92] Container started: openhands-runtime-63bab271fd3b492f9b3aaa3e2cd44e92. VSCode URL: None
04:57:52 - openhands:INFO: docker_runtime.py:145 - [runtime 63bab271fd3b492f9b3aaa3e2cd44e92] Waiting for client to become ready at http://host.docker.internal:39668...
04:57:52 - openhands:ERROR: session.py:116 - Error creating controller: 502 Server Error: Bad Gateway for url: http://host.docker.internal:39668/alive
Traceback (most recent call last):
  File "/app/openhands/runtime/utils/request.py", line 45, in send_request
    response.raise_for_status()
  File "/app/.venv/lib/python3.12/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 502 Server Error: Bad Gateway for url: http://host.docker.internal:39668/alive

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/openhands/server/session/session.py", line 104, in initialize_agent
    await self.agent_session.start(
  File "/app/openhands/server/session/agent_session.py", line 93, in start
    await self._create_runtime(
  File "/app/openhands/server/session/agent_session.py", line 198, in _create_runtime
    await self.runtime.connect()
  File "/app/openhands/runtime/impl/docker/docker_runtime.py", line 148, in connect
    await call_sync_from_async(self._wait_until_alive)
  File "/app/openhands/utils/async_utils.py", line 18, in call_sync_from_async
    result = await coro
             ^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/openhands/utils/async_utils.py", line 17, in <lambda>
    coro = loop.run_in_executor(None, lambda: fn(*args, **kwargs))
                                              ^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 336, in wrapped_f
    return copy(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 475, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 376, in iter
    result = action(retry_state)
             ^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 398, in <lambda>
    self._add_action_func(lambda rs: rs.outcome.result())
                                     ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 478, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/app/openhands/runtime/impl/docker/docker_runtime.py", line 337, in _wait_until_alive
    self.check_if_alive()
  File "/app/openhands/runtime/impl/action_execution/action_execution_client.py", line 99, in check_if_alive
    with self._send_action_server_request(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/openhands/runtime/impl/action_execution/action_execution_client.py", line 96, in _send_action_server_request
    return send_request(self.session, method, url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 336, in wrapped_f
    return copy(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 475, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 376, in iter
    result = action(retry_state)
             ^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 398, in <lambda>
    self._add_action_func(lambda rs: rs.outcome.result())
                                     ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/app/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 478, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/app/openhands/runtime/utils/request.py", line 51, in send_request
    raise RequestHTTPError(
openhands.runtime.utils.request.RequestHTTPError: 502 Server Error: Bad Gateway for url: http://host.docker.internal:39668/alive

My config.json file is :

{
        "proxies": {
                "default": {
                        "httpProxy": "http://host.docker.internal:7890",
                        "httpsProxy": "https://host.docker.internal:7890",
                        "noProxy": "http://localhost"
                }
        }
}

I use a clash in the host machine and the clash listen to the port 7890. In the host machine, I can successfully get google by setting HTTP_PROXY=http://127.0.0.1:7890

And I remove the proxy config the error disappears but I cannot get the OpenAI API.

Does anyone know how to solve the problem?

gary-young avatar Jan 10 '25 05:01 gary-young