From 4711d7bb9c2cc3946dcef482d713684d28154797 Mon Sep 17 00:00:00 2001 From: Abdur Rahman Nawaz Date: Sat, 4 May 2024 19:42:36 +0530 Subject: [PATCH] Fix for http client (#2579) * Fix for http client * fixed constructor to only ignore the http_client while copying * fixed comment formating * removed check for http_client and added error message with docs * fix formatting * fix formatting * added test for http-fix * changed title and content of docs * changed test func name --- autogen/agentchat/conversable_agent.py | 8 ++++- test/agentchat/test_conversable_agent.py | 21 ++++++++++++ website/docs/topics/llm_configuration.ipynb | 38 +++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index b04222f514e..a997b915142 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -149,7 +149,13 @@ def __init__( ) # Take a copy to avoid modifying the given dict if isinstance(llm_config, dict): - llm_config = copy.deepcopy(llm_config) + try: + llm_config = copy.deepcopy(llm_config) + except TypeError as e: + raise TypeError( + "Please implement __deepcopy__ method for each value class in llm_config to support deepcopy." + " Refer to the docs for more details: https://microsoft.github.io/autogen/docs/topics/llm_configuration#adding-http-client-in-llm_config-for-proxy" + ) from e self._validate_llm_config(llm_config) diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index b57dcf1b597..b81a897b47c 100755 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -1382,6 +1382,27 @@ def bob_initiate_chat(agent: ConversableAgent, text: Literal["past", "future"]): assert bob.chat_messages[charlie][-2]["content"] == "This is bob from the future speaking." +def test_http_client(): + + import httpx + + with pytest.raises(TypeError): + config_list = [ + { + "model": "my-gpt-4-deployment", + "api_key": "", + "http_client": httpx.Client(), + } + ] + + autogen.ConversableAgent( + "test_agent", + human_input_mode="NEVER", + llm_config={"config_list": config_list}, + default_auto_reply="This is alice speaking.", + ) + + if __name__ == "__main__": # test_trigger() # test_context() diff --git a/website/docs/topics/llm_configuration.ipynb b/website/docs/topics/llm_configuration.ipynb index 073ec686b2c..51abf1f4622 100644 --- a/website/docs/topics/llm_configuration.ipynb +++ b/website/docs/topics/llm_configuration.ipynb @@ -254,6 +254,44 @@ "assert len(config_list) == 1" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Adding http client in llm_config for proxy\n", + "\n", + "In Autogen, a deepcopy is used on llm_config to ensure that the llm_config passed by user is not modified internally. You may get an error if the llm_config contains objects of a class that do not support deepcopy. To fix this, you need to implement a `__deepcopy__` method for the class.\n", + "\n", + "The below example shows how to implement a `__deepcopy__` method for http client and add a proxy." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#!pip install httpx\n", + "import httpx\n", + "\n", + "\n", + "class MyHttpClient(httpx.Client):\n", + " def __deepcopy__(self, memo):\n", + " return self\n", + "\n", + "config_list = [\n", + " {\n", + " \"model\": \"my-gpt-4-deployment\",\n", + " \"api_key\": \"\",\n", + " \"http_client\": MyHttpClient(proxy=\"http://localhost:8030\"),\n", + " }\n", + "]\n", + "\n", + "llm_config = {\n", + " \"config_list\": config_list,\n", + "}" + ] + }, { "cell_type": "markdown", "metadata": {},