Skip to content

A programming framework for agentic AI

Fix for http client (#2579)

Abdur Rahman Nawaz4711d7bb9c2c
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py
index b04222f51..a997b9151 100644
--- a/autogen/agentchat/conversable_agent.py
+++ b/autogen/agentchat/conversable_agent.py
@@ -149,7 +149,13 @@ class ConversableAgent(LLMAgent):
)
# 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 b57dcf1b5..b81a897b4 100755
--- a/test/agentchat/test_conversable_agent.py
+++ b/test/agentchat/test_conversable_agent.py
@@ -1382,6 +1382,27 @@ def test_chat_history():
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 073ec686b..51abf1f46 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": {},