From 586e9826db27e2c2e24780bf3fdcc7a1da2440b8 Mon Sep 17 00:00:00 2001 From: Qingyun Wu Date: Sun, 8 Sep 2024 00:09:18 -0700 Subject: [PATCH] add the "run" api --- autogen/agentchat/conversable_agent.py | 14 +++++++++++ website/docs/tutorial/introduction.ipynb | 32 ++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 1c18d4f4ded8..7b80ab830a4f 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -2132,6 +2132,20 @@ async def a_generate_reply( return reply return self._default_auto_reply + def run( + self, + message: str, + sender: Optional["Agent"] = None, + ): + if sender is not None: + all_message = self._oai_messages[sender] + all_message.append({"content": message, "role": "user"}) + self._oai_messages[sender] = all_message + else: + all_message = [{"content": message, "role": "user"}] + reply = self.generate_reply(messages=all_message) + return reply + def _match_trigger(self, trigger: Union[None, str, type, Agent, Callable, List], sender: Optional[Agent]) -> bool: """Check if the sender matches the trigger. diff --git a/website/docs/tutorial/introduction.ipynb b/website/docs/tutorial/introduction.ipynb index 92876d255443..6130f2f70f6f 100644 --- a/website/docs/tutorial/introduction.ipynb +++ b/website/docs/tutorial/introduction.ipynb @@ -79,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -108,19 +108,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can ask this agent to generate a response to a question using the `generate_reply` method:" + "You can ask this agent to generate a response to a question using the `run` (recommended) method or `generate_reply` method:" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sure, here's one for you:\n", + "\n", + "Why don't scientists trust atoms?\n", + "\n", + "Because they make up everything!\n" + ] + } + ], + "source": [ + "reply = agent.run(message=\"Tell me a joke.\")\n", + "print(reply)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Sure, here's a light-hearted joke for you:\n", + "Sure, here's one for you:\n", "\n", "Why don't scientists trust atoms?\n", "\n", @@ -257,7 +279,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.13" } }, "nbformat": 4,