From e214e9398de75ae5046af8936bbc1add22569e71 Mon Sep 17 00:00:00 2001 From: IANTHEREAL Date: Sun, 12 Nov 2023 19:13:36 +0800 Subject: [PATCH 1/5] add oai assistant function call --- ...gentchat_oai_assistant_function_call.ipynb | 216 ++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 notebook/agentchat_oai_assistant_function_call.ipynb diff --git a/notebook/agentchat_oai_assistant_function_call.ipynb b/notebook/agentchat_oai_assistant_function_call.ipynb new file mode 100644 index 000000000000..ee0f450563af --- /dev/null +++ b/notebook/agentchat_oai_assistant_function_call.ipynb @@ -0,0 +1,216 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This Jupyter Notebook demonstrates how to leverage OSS Insight (Open Source Software Insight) for advanced GitHub data analysis by defining `Function calls` in AutoGen for the OpenAI Assistant. \n", + "\n", + "The notebook is structured into four main sections:\n", + "\n", + "1. Function Schema and Implementation\n", + "2. Defining an OpenAI Assistant Agent in AutoGen\n", + "3. Fetching GitHub Insight Data using Function Call\n", + "4. Exploring Code Interpreter" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This section provides the function schema definition and their implementation details. These functions are tailored to fetch and process data from GitHub, utilizing OSS Insight's capabilities." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "import logging\n", + "import os\n", + "import requests\n", + "\n", + "logger = logging.getLogger(__name__)\n", + "logger.setLevel(logging.WARNING)\n", + "\n", + "ossinsight_api_schema = {\n", + " \"name\": \"ossinsight_data_api\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"question\": {\n", + " \"type\": \"string\",\n", + " \"description\": (\n", + " \"Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. \"\n", + " \"For optimal results, specify the desired format for the data table in your request.\"\n", + " ),\n", + " }\n", + " },\n", + " \"required\": [\n", + " \"question\"\n", + " ]\n", + " },\n", + " \"description\": \"This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the realted and structured data.\"\n", + "}\n", + "\n", + "def get_ossinsight(question):\n", + " \"\"\"\n", + " Retrieve the top 10 developers with the most followers on GitHub.\n", + " \"\"\"\n", + " url = \"https://api.ossinsight.io/explorer/answer\"\n", + " headers = {\"Content-Type\": \"application/json\"}\n", + " data = {\n", + " \"question\": question,\n", + " \"ignoreCache\": True\n", + " }\n", + "\n", + " response = requests.post(url, headers=headers, json=data)\n", + " if response.status_code == 200:\n", + " answer = response.json()\n", + " else:\n", + " return f\"Request to {url} failed with status code: {response.status_code}\"\n", + "\n", + " report_components = []\n", + " report_components.append(f\"Question: {answer['question']['title']}\")\n", + " if answer['query']['sql'] != \"\":\n", + " report_components.append(f\"querySQL: {answer['query']['sql']}\")\n", + "\n", + " if answer.get('result', None) is None or len(answer['result']['rows']) == 0:\n", + " result = \"Result: N/A\"\n", + " else:\n", + " result = \"Result:\\n \" + \"\\n \".join([str(row) for row in answer['result']['rows']])\n", + " report_components.append(result)\n", + "\n", + " if answer.get('error', None) is not None:\n", + " report_components.append(f\"Error: {answer['error']}\")\n", + " return \"\\n\\n\".join(report_components)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here, we explore how to define an OpenAI Assistant Agent within the AutoGen. This includes setting up the agent to make use of the previously defined function calls for data retrieval and analysis." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "from autogen import config_list_from_json\n", + "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n", + "from autogen import UserProxyAgent\n", + "\n", + "assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n", + "config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n", + "llm_config = {\n", + " \"config_list\": config_list,\n", + " \"assistant_id\": assistant_id,\n", + " \"tools\": [\n", + " {\n", + " \"type\": \"function\",\n", + " \"function\": ossinsight_api_schema,\n", + " }\n", + " ]\n", + "}\n", + "\n", + "oss_analyst = GPTAssistantAgent(\n", + " name=\"OSS Analyst\", \n", + " instructions=(\n", + " \"Hello, Open Source Project Analyst. You'll conduct comprehensive evaluations of open source projects or organizations on the GitHub platform, \"\n", + " \"analyzing project trajectories, contributor engagements, open source trends, and other vital parameters. \"\n", + " \"Please carefully read the context of the conversation to identify the current analysis question or problem that needs addressing.\"\n", + " ),\n", + " llm_config=llm_config,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This part of the notebook demonstrates the practical application of the defined functions and the OpenAI Assistant Agent in fetching and interpreting GitHub Insight data." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", + "\n", + "Top 10 developers with the most followers\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33massistant\u001b[0m (to user_proxy):\n", + "\n", + "It appears that there has been an error in accessing the ossinsight_data_api. Let me try again.\n", + "\n", + "\n", + "I apologize, but it seems there is an issue with accessing the OSS Insight data API. Currently, I'm unable to retrieve the data regarding the top 10 developers with the most followers on GitHub. If you require this information, you might consider exploring the GitHub platform directly or using external tools that analyze GitHub user profiles.\n", + "\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", + "\n", + "\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33massistant\u001b[0m (to user_proxy):\n", + "\n", + "It seems there was a break in our conversation. If you have any other inquiries or need assistance with an open source project analysis on GitHub, please feel free to let me know and I would be happy to help.\n", + "\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + } + ], + "source": [ + "user_proxy = UserProxyAgent(name=\"user_proxy\",\n", + " code_execution_config={\n", + " \"work_dir\": \"coding\"\n", + " },\n", + " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n", + " human_input_mode=\"NEVER\",\n", + " max_consecutive_auto_reply=1)\n", + "\n", + "user_proxy.initiate_chat(oss_analyst, message=\"Top 10 developers with the most followers\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The final section focuses on testing and showcasing the `code interpretation` capabilities of OpenAI Assistant." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "autogen", + "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.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From d65695cab2c211d4168de778d04df33e869947cd Mon Sep 17 00:00:00 2001 From: IANTHEREAL Date: Sun, 12 Nov 2023 19:16:50 +0800 Subject: [PATCH 2/5] polish --- ...gentchat_oai_assistant_function_call.ipynb | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/notebook/agentchat_oai_assistant_function_call.ipynb b/notebook/agentchat_oai_assistant_function_call.ipynb index ee0f450563af..5160576eb6e0 100644 --- a/notebook/agentchat_oai_assistant_function_call.ipynb +++ b/notebook/agentchat_oai_assistant_function_call.ipynb @@ -10,8 +10,7 @@ "\n", "1. Function Schema and Implementation\n", "2. Defining an OpenAI Assistant Agent in AutoGen\n", - "3. Fetching GitHub Insight Data using Function Call\n", - "4. Exploring Code Interpreter" + "3. Fetching GitHub Insight Data using Function Call" ] }, { @@ -23,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -96,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -125,6 +124,11 @@ " \"Please carefully read the context of the conversation to identify the current analysis question or problem that needs addressing.\"\n", " ),\n", " llm_config=llm_config,\n", + ")\n", + "oss_analyst.register_function(\n", + " function_map={\n", + " \"ossinsight_data_api\": get_ossinsight,\n", + " }\n", ")" ] }, @@ -137,35 +141,45 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\u001b[33muser_proxy\u001b[0m (to assistant):\n", + "\u001b[33muser_proxy\u001b[0m (to OSS Analyst):\n", "\n", "Top 10 developers with the most followers\n", "\n", "--------------------------------------------------------------------------------\n", - "\u001b[33massistant\u001b[0m (to user_proxy):\n", - "\n", - "It appears that there has been an error in accessing the ossinsight_data_api. Let me try again.\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION ossinsight_data_api...\u001b[0m\n", + "\u001b[33mOSS Analyst\u001b[0m (to user_proxy):\n", "\n", + "The top 10 developers on GitHub with the most followers are:\n", "\n", - "I apologize, but it seems there is an issue with accessing the OSS Insight data API. Currently, I'm unable to retrieve the data regarding the top 10 developers with the most followers on GitHub. If you require this information, you might consider exploring the GitHub platform directly or using external tools that analyze GitHub user profiles.\n", + "1. Linus Torvalds (`torvalds`) with 166,730 followers.\n", + "2. Evan You (`yyx990803`) with 86,239 followers.\n", + "3. Dan Abramov (`gaearon`) with 77,611 followers.\n", + "4. Ruan YiFeng (`ruanyf`) with 72,668 followers.\n", + "5. Jake Wharton (`JakeWharton`) with 65,415 followers.\n", + "6. Peng Zhihui (`peng-zhihui`) with 60,972 followers.\n", + "7. Brad Traversy (`bradtraversy`) with 58,172 followers.\n", + "8. Gustavo Guanabara (`gustavoguanabara`) with 52,143 followers.\n", + "9. Sindre Sorhus (`sindresorhus`) with 51,542 followers.\n", + "10. TJ Holowaychuk (`tj`) with 49,621 followers.\n", "\n", "\n", "--------------------------------------------------------------------------------\n", - "\u001b[33muser_proxy\u001b[0m (to assistant):\n", + "\u001b[33muser_proxy\u001b[0m (to OSS Analyst):\n", "\n", "\n", "\n", "--------------------------------------------------------------------------------\n", - "\u001b[33massistant\u001b[0m (to user_proxy):\n", + "\u001b[33mOSS Analyst\u001b[0m (to user_proxy):\n", "\n", - "It seems there was a break in our conversation. If you have any other inquiries or need assistance with an open source project analysis on GitHub, please feel free to let me know and I would be happy to help.\n", + "It seems like there is no question or further instructions provided in your last response. How may I assist you further with open source project analysis or any other inquiries you might have?\n", "\n", "\n", "--------------------------------------------------------------------------------\n" @@ -183,13 +197,6 @@ "\n", "user_proxy.initiate_chat(oss_analyst, message=\"Top 10 developers with the most followers\")" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The final section focuses on testing and showcasing the `code interpretation` capabilities of OpenAI Assistant." - ] } ], "metadata": { From acf70697a2b80b0276e5019b6ae3ea3ae928355d Mon Sep 17 00:00:00 2001 From: IANTHEREAL Date: Sun, 12 Nov 2023 22:56:13 +0800 Subject: [PATCH 3/5] add titles --- ...gentchat_oai_assistant_function_call.ipynb | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/notebook/agentchat_oai_assistant_function_call.ipynb b/notebook/agentchat_oai_assistant_function_call.ipynb index 5160576eb6e0..8cf6cc136900 100644 --- a/notebook/agentchat_oai_assistant_function_call.ipynb +++ b/notebook/agentchat_oai_assistant_function_call.ipynb @@ -4,19 +4,40 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "## Auto Generated Agent Chat with OpenAI Assistants using function call in AutoGen: OSS Insights for Advanced GitHub Data Analysis\n", + "\n", "This Jupyter Notebook demonstrates how to leverage OSS Insight (Open Source Software Insight) for advanced GitHub data analysis by defining `Function calls` in AutoGen for the OpenAI Assistant. \n", "\n", "The notebook is structured into four main sections:\n", "\n", "1. Function Schema and Implementation\n", "2. Defining an OpenAI Assistant Agent in AutoGen\n", - "3. Fetching GitHub Insight Data using Function Call" + "3. Fetching GitHub Insight Data using Function Call\n", + "\n", + "### Requirements\n", + "\n", + "AutoGen requires `Python>=3.8`. To run this notebook example, please install:\n", + "```bash\n", + "pip install pyautogen\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%capture --no-stderr\n", + "# %pip install \"pyautogen[teachable]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "### Function Schema and Implementation\n", + "\n", "This section provides the function schema definition and their implementation details. These functions are tailored to fetch and process data from GitHub, utilizing OSS Insight's capabilities." ] }, @@ -90,6 +111,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### Defining an OpenAI Assistant Agent in AutoGen\n", + "\n", "Here, we explore how to define an OpenAI Assistant Agent within the AutoGen. This includes setting up the agent to make use of the previously defined function calls for data retrieval and analysis." ] }, @@ -136,6 +159,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### Fetching GitHub Insight Data using Function Call\n", + "\n", "This part of the notebook demonstrates the practical application of the defined functions and the OpenAI Assistant Agent in fetching and interpreting GitHub Insight data." ] }, From 5071caada575810844aad85d3711303ef39f1767 Mon Sep 17 00:00:00 2001 From: IANTHEREAL Date: Sun, 12 Nov 2023 22:58:04 +0800 Subject: [PATCH 4/5] polish --- notebook/agentchat_oai_assistant_function_call.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/agentchat_oai_assistant_function_call.ipynb b/notebook/agentchat_oai_assistant_function_call.ipynb index 8cf6cc136900..8d4ac2460f8b 100644 --- a/notebook/agentchat_oai_assistant_function_call.ipynb +++ b/notebook/agentchat_oai_assistant_function_call.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Auto Generated Agent Chat with OpenAI Assistants using function call in AutoGen: OSS Insights for Advanced GitHub Data Analysis\n", + "## Chat with OpenAI Assistant using function call in AutoGen: OSS Insights for Advanced GitHub Data Analysis\n", "\n", "This Jupyter Notebook demonstrates how to leverage OSS Insight (Open Source Software Insight) for advanced GitHub data analysis by defining `Function calls` in AutoGen for the OpenAI Assistant. \n", "\n", From 4957c8a42d18f31f73caaf0269eccf8f007bfb87 Mon Sep 17 00:00:00 2001 From: Qingyun Wu Date: Sun, 12 Nov 2023 12:53:35 -0500 Subject: [PATCH 5/5] Update notebook/agentchat_oai_assistant_function_call.ipynb Co-authored-by: Chi Wang --- notebook/agentchat_oai_assistant_function_call.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebook/agentchat_oai_assistant_function_call.ipynb b/notebook/agentchat_oai_assistant_function_call.ipynb index 8d4ac2460f8b..b9d230a59905 100644 --- a/notebook/agentchat_oai_assistant_function_call.ipynb +++ b/notebook/agentchat_oai_assistant_function_call.ipynb @@ -29,7 +29,7 @@ "outputs": [], "source": [ "%%capture --no-stderr\n", - "# %pip install \"pyautogen[teachable]" + "# %pip install \"pyautogen~=0.2.0b5" ] }, {