From 3130075693048811f5f0f7ad13fd80450dc7cc37 Mon Sep 17 00:00:00 2001 From: qbc Date: Mon, 15 Jan 2024 16:33:45 +0800 Subject: [PATCH 1/4] add jupyter for conversation --- notebook/conversation.ipynb | 182 ++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 notebook/conversation.ipynb diff --git a/notebook/conversation.ipynb b/notebook/conversation.ipynb new file mode 100644 index 000000000..1080a2bb7 --- /dev/null +++ b/notebook/conversation.ipynb @@ -0,0 +1,182 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b75c96fc-e399-4f81-bcf8-8a5d15bcb79b", + "metadata": {}, + "source": [ + "# Conversation with Agent" + ] + }, + { + "cell_type": "markdown", + "id": "25d30c19de76e93f", + "metadata": { + "collapsed": false + }, + "source": [ + "In this notebook, we will show a demo of how to program a multi-agent conversation in AgentScope.Complete code is in `example/conversation/conversation.py`, which sets up a user agent and an assistant agent to have a conversation. When user input \"exit\", the conversation ends. You can modify the sys_prompt to change the role of assistant agent." + ] + }, + { + "cell_type": "markdown", + "id": "28c4c4dc-7107-45e5-9ef4-f6506a0d55e2", + "metadata": {}, + "source": [ + "To install AgentScope, you need to have Python 3.9 or higher installed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6dbd5990-ed46-4727-84b1-d4131c5a6245", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install AgentScope" + ] + }, + { + "cell_type": "markdown", + "id": "4ccebee3-9a27-47d9-9133-40126b888593", + "metadata": {}, + "source": [ + "First, set the configs of the models you use." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c60227f7-7380-4f72-b653-bc7394cfaace", + "metadata": {}, + "outputs": [], + "source": [ + "import agentscope\n", + "from agentscope.agents import DialogAgent\n", + "from agentscope.agents.user_agent import UserAgent\n", + "from agentscope.pipelines.functional import sequentialpipeline\n", + "\n", + "agentscope.init(\n", + " model_configs=[\n", + " {\n", + " \"type\": \"openai\",\n", + " \"name\": \"gpt-3.5-turbo\",\n", + " \"api_key\": \"xxx\", # Load from env if not provided\n", + " \"organization\": \"xxx\", # Load from env if not provided\n", + " \"generate_args\": {\n", + " \"temperature\": 0.5,\n", + " },\n", + " },\n", + " {\n", + " \"type\": \"post_api\",\n", + " \"name\": \"my_post_api\",\n", + " \"api_url\": \"https://xxx\",\n", + " \"headers\": {},\n", + " },\n", + " ],\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "503f7e7a-7a93-468d-b88b-37540f607083", + "metadata": {}, + "source": [ + "Then, initialize two agents, one is used as an assistant agent, and the other one is a user agent." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "848270c3-b73b-482d-a426-49e0ac67dc39", + "metadata": {}, + "outputs": [], + "source": [ + "dialog_agent = DialogAgent(\n", + " name=\"Assistant\",\n", + " sys_prompt=\"You're a helpful assistant.\",\n", + " model=\"gpt-3.5-turbo\", # replace by your model config name\n", + ")\n", + "user_agent = UserAgent()" + ] + }, + { + "cell_type": "markdown", + "id": "128356259909067b", + "metadata": { + "collapsed": false + }, + "source": [ + "Start the conversation between user and assistant. Input \"exit\" to quit." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6057d6c5-a332-4949-84a7-6fbeda021561", + "metadata": {}, + "outputs": [], + "source": [ + "x = None\n", + "while x is None or x.content != \"exit\":\n", + " x = sequentialpipeline([dialog_agent, user_agent], x)" + ] + }, + { + "cell_type": "markdown", + "id": "aed5ff6a-5167-4450-be57-b42ee8ae5e92", + "metadata": {}, + "source": [ + "We show the following dialog as an example." + ] + }, + { + "cell_type": "markdown", + "id": "9cb11109-4229-4faa-a859-9e806d9c2a6f", + "metadata": {}, + "source": [ + "Assistant: Thank you! I'm here to assist you with any questions or tasks you have. How can I help you today?\n", + "User: Please help me arrange a day trip to Hangzhou.\n", + "Assistant: Certainly! I can help you with that. When are you planning to visit Hangzhou?\n", + "User: Tomorrow\n", + "Assistant: Great! Tomorrow sounds like a good day for a day trip to Hangzhou. Here's a suggested itinerary for your day trip:\n", + "\n", + "1. Depart from your current location: It's important to plan your departure time to arrive in Hangzhou early in the morning. You can consider taking a train or bus, or hiring a private driver for the trip.\n", + "\n", + "2. West Lake: Start your day by visiting West Lake, which is the heart of Hangzhou. Take a leisurely stroll along the lake and enjoy the beautiful scenery. You can also rent a boat or take a cruise to explore the lake.\n", + "\n", + "3. Lingyin Temple: After visiting West Lake, head to Lingyin Temple, one of the most famous Buddhist temples in China. Marvel at the intricate architecture and explore the peaceful surroundings.\n", + "\n", + "4. Hefang Street: Next, make your way to Hefang Street, a vibrant pedestrian street filled with shops, street vendors, and traditional food stalls. Take some time to shop for souvenirs or try some local snacks.\n", + "\n", + "5. Longjing Tea Plantations: As Hangzhou is renowned for its tea, a visit to the Longjing Tea Plantations is a must. Take a short trip to the outskirts of Hangzhou and immerse yourself in the serene tea fields. You can also participate in a tea ceremony and learn about the tea-making process.\n", + "\n", + "6. Return to your starting point: After a full day of exploring Hangzhou, it's time to head back to your starting point. Plan your return journey accordingly, whether it's by train, bus, or with the help of a hired driver.\n", + "\n", + "Remember to check the opening hours of the attractions and plan your time accordingly. Also, be prepared for the weather and make sure to bring comfortable shoes, a hat, and sunscreen. Enjoy your day trip to Hangzhou!\n", + "User: exit" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 4d94276992535fce78db4e1070cd4563d794a8fd Mon Sep 17 00:00:00 2001 From: qbc Date: Mon, 15 Jan 2024 17:03:41 +0800 Subject: [PATCH 2/4] fix typo --- notebook/conversation.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/conversation.ipynb b/notebook/conversation.ipynb index 1080a2bb7..f97f4237c 100644 --- a/notebook/conversation.ipynb +++ b/notebook/conversation.ipynb @@ -15,7 +15,7 @@ "collapsed": false }, "source": [ - "In this notebook, we will show a demo of how to program a multi-agent conversation in AgentScope.Complete code is in `example/conversation/conversation.py`, which sets up a user agent and an assistant agent to have a conversation. When user input \"exit\", the conversation ends. You can modify the sys_prompt to change the role of assistant agent." + "In this notebook, we will show a demo of how to program a multi-agent conversation in AgentScope.Complete code is in `examples/conversation/conversation.py`, which sets up a user agent and an assistant agent to have a conversation. When user input \"exit\", the conversation ends. You can modify the sys_prompt to change the role of assistant agent." ] }, { From 803a180064677735d6c1cf4bd984a61e07a964e9 Mon Sep 17 00:00:00 2001 From: qbc Date: Tue, 16 Jan 2024 11:33:43 +0800 Subject: [PATCH 3/4] minor changes --- notebook/conversation.ipynb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/notebook/conversation.ipynb b/notebook/conversation.ipynb index f97f4237c..d1d791f42 100644 --- a/notebook/conversation.ipynb +++ b/notebook/conversation.ipynb @@ -136,9 +136,13 @@ "metadata": {}, "source": [ "Assistant: Thank you! I'm here to assist you with any questions or tasks you have. How can I help you today?\n", + "\n", "User: Please help me arrange a day trip to Hangzhou.\n", + "\n", "Assistant: Certainly! I can help you with that. When are you planning to visit Hangzhou?\n", + "\n", "User: Tomorrow\n", + "\n", "Assistant: Great! Tomorrow sounds like a good day for a day trip to Hangzhou. Here's a suggested itinerary for your day trip:\n", "\n", "1. Depart from your current location: It's important to plan your departure time to arrive in Hangzhou early in the morning. You can consider taking a train or bus, or hiring a private driver for the trip.\n", @@ -154,6 +158,7 @@ "6. Return to your starting point: After a full day of exploring Hangzhou, it's time to head back to your starting point. Plan your return journey accordingly, whether it's by train, bus, or with the help of a hired driver.\n", "\n", "Remember to check the opening hours of the attractions and plan your time accordingly. Also, be prepared for the weather and make sure to bring comfortable shoes, a hat, and sunscreen. Enjoy your day trip to Hangzhou!\n", + "\n", "User: exit" ] } From c588138c2076fd3f063b21f33b671a6cbb7189f4 Mon Sep 17 00:00:00 2001 From: qbc Date: Tue, 16 Jan 2024 18:31:51 +0800 Subject: [PATCH 4/4] modify according to comments --- notebook/conversation.ipynb | 41 +++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/notebook/conversation.ipynb b/notebook/conversation.ipynb index d1d791f42..1afe43047 100644 --- a/notebook/conversation.ipynb +++ b/notebook/conversation.ipynb @@ -15,25 +15,15 @@ "collapsed": false }, "source": [ - "In this notebook, we will show a demo of how to program a multi-agent conversation in AgentScope.Complete code is in `examples/conversation/conversation.py`, which sets up a user agent and an assistant agent to have a conversation. When user input \"exit\", the conversation ends. You can modify the sys_prompt to change the role of assistant agent." + "In this notebook, we will show a demo of how to program a multi-agent conversation in AgentScope. The complete codes can be found in `examples/conversation/conversation.py`, which sets up a user agent and an assistant agent to have a conversation. When user input \"exit\", the conversation ends. You can modify the `sys_prompt` to change the role of assistant agent." ] }, { "cell_type": "markdown", - "id": "28c4c4dc-7107-45e5-9ef4-f6506a0d55e2", + "id": "5f8b1a15-cf1a-46ff-8a62-88d7476c8608", "metadata": {}, "source": [ - "To install AgentScope, you need to have Python 3.9 or higher installed." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6dbd5990-ed46-4727-84b1-d4131c5a6245", - "metadata": {}, - "outputs": [], - "source": [ - "!pip install AgentScope" + "To install AgentScope, please follow the steps in [README.md](https://github.com/alibaba/AgentScope/blob/main/README.md#installation)." ] }, { @@ -53,7 +43,7 @@ "source": [ "import agentscope\n", "from agentscope.agents import DialogAgent\n", - "from agentscope.agents.user_agent import UserAgent\n", + "from agentscope.agents import UserAgent\n", "from agentscope.pipelines.functional import sequentialpipeline\n", "\n", "agentscope.init(\n", @@ -87,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "848270c3-b73b-482d-a426-49e0ac67dc39", "metadata": {}, "outputs": [], @@ -110,6 +100,27 @@ "Start the conversation between user and assistant. Input \"exit\" to quit." ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "e46f7a9d-355b-46cd-86b5-201c91eabffb", + "metadata": {}, + "outputs": [], + "source": [ + "x = None\n", + "while x is None or x.content != \"exit\":\n", + " x = dialog_agent(x)\n", + " x = user_agent(x)" + ] + }, + { + "cell_type": "markdown", + "id": "adc601a8-144f-4e42-bc86-f0e14adb39b5", + "metadata": {}, + "source": [ + "To code it easier, you can use pipeline in [`agentscope.pipelines`](https://github.com/alibaba/AgentScope/blob/main/src/agentscope/pipelines/pipeline.py) and [`agentscope.pipelines.functional`](https://github.com/alibaba/AgentScope/blob/main/src/agentscope/pipelines/functional.py)." + ] + }, { "cell_type": "code", "execution_count": null,