diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 1f1b2ff489f7..86ef3db0d295 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -136,6 +136,24 @@ def __init__( else (lambda x: content_str(x.get("content")) == "TERMINATE") ) + if llm_config is None: + raise ValueError("Please specify the value for 'llm_config'.") + + if isinstance(llm_config, dict): + config_list = None + if "config_list" in llm_config: + config_list = llm_config["config_list"] + if config_list is None or len(config_list) == 0: + raise ValueError("Please specify at least one configuration in 'llm_config'.") + + # We know that there's at least one entry in the configuration. + # Verify that model is specified as well. + model = None + if "model" in llm_config["config_list"][0]: + model = llm_config["config_list"][0]["model"] + if model is None or len(model) == 0: + raise ValueError("Please specify a value for the 'model' in 'llm_config'.") + if llm_config is False: self.llm_config = False self.client = None diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index a10b1a0e3a07..a07897a418eb 100644 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -474,7 +474,7 @@ async def test_a_generate_reply_raises_on_messages_and_sender_none(conversable_a def test_update_function_signature_and_register_functions() -> None: with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent = ConversableAgent(name="agent", llm_config={}) + agent = ConversableAgent(name="agent", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) def exec_python(cell: str) -> None: pass @@ -618,9 +618,9 @@ def get_origin(d: Dict[str, Callable[..., Any]]) -> Dict[str, Callable[..., Any] def test_register_for_llm(): with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent3 = ConversableAgent(name="agent3", llm_config={"config_list": []}) - agent2 = ConversableAgent(name="agent2", llm_config={"config_list": []}) - agent1 = ConversableAgent(name="agent1", llm_config={"config_list": []}) + agent3 = ConversableAgent(name="agent3", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) + agent2 = ConversableAgent(name="agent2", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) + agent1 = ConversableAgent(name="agent1", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) @agent3.register_for_llm() @agent2.register_for_llm(name="python") @@ -691,9 +691,9 @@ async def exec_sh(script: Annotated[str, "Valid shell script to execute."]) -> s def test_register_for_llm_api_style_function(): with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent3 = ConversableAgent(name="agent3", llm_config={"config_list": []}) - agent2 = ConversableAgent(name="agent2", llm_config={"config_list": []}) - agent1 = ConversableAgent(name="agent1", llm_config={"config_list": []}) + agent3 = ConversableAgent(name="agent3", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) + agent2 = ConversableAgent(name="agent2", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) + agent1 = ConversableAgent(name="agent1", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) @agent3.register_for_llm(api_style="function") @agent2.register_for_llm(name="python", api_style="function") @@ -762,7 +762,7 @@ async def exec_sh(script: Annotated[str, "Valid shell script to execute."]) -> s def test_register_for_llm_without_description(): with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent = ConversableAgent(name="agent", llm_config={}) + agent = ConversableAgent(name="agent", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) with pytest.raises(ValueError) as e: @@ -774,25 +774,33 @@ def exec_python(cell: Annotated[str, "Valid Python cell to execute."]) -> str: def test_register_for_llm_without_LLM(): - with pytest.MonkeyPatch.context() as mp: - mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent = ConversableAgent(name="agent", llm_config=None) - agent.llm_config = None - assert agent.llm_config is None + try: + ConversableAgent(name="agent", llm_config=None) + assert False, "Expected ConversableAgent to throw ValueError." + except ValueError as e: + assert e.args[0] == "Please specify the value for 'llm_config'." - with pytest.raises(RuntimeError) as e: - @agent.register_for_llm(description="run cell in ipython and return the execution result.") - def exec_python(cell: Annotated[str, "Valid Python cell to execute."]) -> str: - pass +def test_register_for_llm_without_configuration(): + try: + ConversableAgent(name="agent", llm_config={"config_list": []}) + assert False, "Expected ConversableAgent to throw ValueError." + except ValueError as e: + assert e.args[0] == "Please specify at least one configuration in 'llm_config'." + - assert e.value.args[0] == "LLM config must be setup before registering a function for LLM." +def test_register_for_llm_without_model_name(): + try: + ConversableAgent(name="agent", llm_config={"config_list": [{"model": "", "api_key": ""}]}) + assert False, "Expected ConversableAgent to throw ValueError." + except ValueError as e: + assert e.args[0] == "Please specify a value for the 'model' in 'llm_config'." def test_register_for_execution(): with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent = ConversableAgent(name="agent", llm_config={"config_list": []}) + agent = ConversableAgent(name="agent", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) user_proxy_1 = UserProxyAgent(name="user_proxy_1") user_proxy_2 = UserProxyAgent(name="user_proxy_2") @@ -827,7 +835,7 @@ async def exec_sh(script: Annotated[str, "Valid shell script to execute."]): def test_register_functions(): with pytest.MonkeyPatch.context() as mp: mp.setenv("OPENAI_API_KEY", MOCK_OPEN_AI_API_KEY) - agent = ConversableAgent(name="agent", llm_config={"config_list": []}) + agent = ConversableAgent(name="agent", llm_config={"config_list": [{"model": "gpt-4", "api_key": ""}]}) user_proxy = UserProxyAgent(name="user_proxy") def exec_python(cell: Annotated[str, "Valid Python cell to execute."]) -> str: