mirror of https://github.com/microsoft/autogen.git
271 lines
10 KiB
Plaintext
271 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 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",
|
|
"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",
|
|
"\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>=0.2.3\""
|
|
]
|
|
},
|
|
{
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import logging\n",
|
|
"import os\n",
|
|
"\n",
|
|
"import requests\n",
|
|
"\n",
|
|
"from autogen import UserProxyAgent, config_list_from_json\n",
|
|
"from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\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\": [\"question\"],\n",
|
|
" },\n",
|
|
" \"description\": \"This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data.\",\n",
|
|
"}\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 = {\"question\": question, \"ignoreCache\": True}\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) + \"\\n\\n\""
|
|
]
|
|
},
|
|
{
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"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",
|
|
" verbose=True,\n",
|
|
")\n",
|
|
"oss_analyst.register_function(\n",
|
|
" function_map={\n",
|
|
" \"ossinsight_data_api\": get_ossinsight,\n",
|
|
" }\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\u001b[33muser_proxy\u001b[0m (to OSS Analyst):\n",
|
|
"\n",
|
|
"Top 10 developers with the most followers\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n",
|
|
"\u001b[35m\n",
|
|
">>>>>>>> EXECUTING FUNCTION ossinsight_data_api...\u001b[0m\n",
|
|
"\u001b[35m\n",
|
|
"Input arguments: {'question': 'Who are the top 10 developers with the most followers on GitHub?'}\n",
|
|
"Output:\n",
|
|
"Question: Who are the top 10 developers with the most followers on GitHub?\n",
|
|
"\n",
|
|
"querySQL: SELECT `login` AS `user_login`, `followers` AS `followers` FROM `github_users` ORDER BY `followers` DESC LIMIT 10\n",
|
|
"\n",
|
|
"Result:\n",
|
|
" {'followers': 166730, 'user_login': 'torvalds'}\n",
|
|
" {'followers': 86239, 'user_login': 'yyx990803'}\n",
|
|
" {'followers': 77611, 'user_login': 'gaearon'}\n",
|
|
" {'followers': 72668, 'user_login': 'ruanyf'}\n",
|
|
" {'followers': 65415, 'user_login': 'JakeWharton'}\n",
|
|
" {'followers': 60972, 'user_login': 'peng-zhihui'}\n",
|
|
" {'followers': 58172, 'user_login': 'bradtraversy'}\n",
|
|
" {'followers': 52143, 'user_login': 'gustavoguanabara'}\n",
|
|
" {'followers': 51542, 'user_login': 'sindresorhus'}\n",
|
|
" {'followers': 49621, 'user_login': 'tj'}\n",
|
|
"\n",
|
|
"\u001b[0m\n",
|
|
"\u001b[33mOSS Analyst\u001b[0m (to user_proxy):\n",
|
|
"\n",
|
|
"The top 10 developers with the most followers on GitHub are as follows:\n",
|
|
"\n",
|
|
"1. `torvalds` with 166,730 followers\n",
|
|
"2. `yyx990803` with 86,239 followers\n",
|
|
"3. `gaearon` with 77,611 followers\n",
|
|
"4. `ruanyf` with 72,668 followers\n",
|
|
"5. `JakeWharton` with 65,415 followers\n",
|
|
"6. `peng-zhihui` with 60,972 followers\n",
|
|
"7. `bradtraversy` with 58,172 followers\n",
|
|
"8. `gustavoguanabara` with 52,143 followers\n",
|
|
"9. `sindresorhus` with 51,542 followers\n",
|
|
"10. `tj` with 49,621 followers\n",
|
|
"\n",
|
|
"These figures indicate the number of followers these developers had at the time of the analysis.\n",
|
|
"\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n",
|
|
"\u001b[33muser_proxy\u001b[0m (to OSS Analyst):\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n",
|
|
"\u001b[33mOSS Analyst\u001b[0m (to user_proxy):\n",
|
|
"\n",
|
|
"It seems you haven't entered a question or a request. Could you please provide more details or specify how I can assist you further?\n",
|
|
"\n",
|
|
"\n",
|
|
"--------------------------------------------------------------------------------\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"user_proxy = UserProxyAgent(\n",
|
|
" name=\"user_proxy\",\n",
|
|
" code_execution_config={\n",
|
|
" \"work_dir\": \"coding\",\n",
|
|
" \"use_docker\": False,\n",
|
|
" }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.\n",
|
|
" is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n",
|
|
" human_input_mode=\"NEVER\",\n",
|
|
" max_consecutive_auto_reply=1,\n",
|
|
")\n",
|
|
"\n",
|
|
"user_proxy.initiate_chat(oss_analyst, message=\"Top 10 developers with the most followers\")"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|