From a87b5056d707c4efde11104768ff9c0702dbed9f Mon Sep 17 00:00:00 2001 From: xiaofenggang Date: Mon, 25 Dec 2023 16:04:58 +0000 Subject: [PATCH] [Bugfix] Set openai proxy for class ZhiPuAPTAPI When using ZHIPUAI as the large model provider, it is not possible to access ZHIPUAI in an HTTP proxy environment, and the following error will be reported: openai.error.APIConnectionError: Error communicating with OpenAI So we need set proxy for class ZhiPuAPTAPI. --- metagpt/provider/zhipuai_api.py | 2 ++ tests/metagpt/provider/test_zhipuai_api.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/metagpt/provider/zhipuai_api.py b/metagpt/provider/zhipuai_api.py index 650720d6f..b258d2883 100644 --- a/metagpt/provider/zhipuai_api.py +++ b/metagpt/provider/zhipuai_api.py @@ -50,6 +50,8 @@ class ZhiPuAIGPTAPI(BaseGPTAPI): assert config.zhipuai_api_key zhipuai.api_key = config.zhipuai_api_key openai.api_key = zhipuai.api_key # due to use openai sdk, set the api_key but it will't be used. + if config.openai_proxy: + openai.proxy = config.openai_proxy def _const_kwargs(self, messages: list[dict]) -> dict: kwargs = {"model": self.model, "prompt": messages, "temperature": 0.3} diff --git a/tests/metagpt/provider/test_zhipuai_api.py b/tests/metagpt/provider/test_zhipuai_api.py index 4684e8887..dc8b63cc3 100644 --- a/tests/metagpt/provider/test_zhipuai_api.py +++ b/tests/metagpt/provider/test_zhipuai_api.py @@ -35,3 +35,10 @@ async def test_zhipuai_acompletion(mocker): assert resp["code"] == 200 assert "chatglm-turbo" in resp["data"]["choices"][0]["content"] + +def test_zhipuai_proxy(mocker): + import openai + from metagpt.config import CONFIG + CONFIG.openai_proxy = 'http://127.0.0.1:8080' + _ = ZhiPuAIGPTAPI() + assert openai.proxy == CONFIG.openai_proxy