Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for http client #2579

Merged
merged 13 commits into from
May 4, 2024
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#class-values-in-llm_config"
) 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_class_values():

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
ekzhu marked this conversation as resolved.
Show resolved Hide resolved
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": [
"### Class values in llm_config\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 class values 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."
]
},
{
"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(),\n",
" }\n",
"]\n",
"\n",
"llm_config = {\n",
" \"config_list\": config_list,\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading