Skip to content

Commit

Permalink
Fix for http client (#2579)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AbdurNawaz committed May 4, 2024
1 parent 3a4bb08 commit 4711d7b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
8 changes: 7 additions & 1 deletion autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions test/agentchat/test_conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions website/docs/topics/llm_configuration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down

0 comments on commit 4711d7b

Please sign in to comment.