跳到主要内容

使用AutoGen中的函数调用与OpenAI助手聊天:OSS Insights用于高级GitHub数据分析

Open In Colab Open on GitHub

本Jupyter Notebook展示了如何通过在使用OpenAI助手的AutoGen中定义函数调用来利用OSS Insight(开源软件洞察)进行高级的GitHub数据分析。

笔记本分为四个主要部分:

  1. 函数模式与实现
  2. 在AutoGen中定义一个OpenAI助手代理
  3. 使用函数调用获取GitHub Insight数据

要求

AutoGen 需要 Python>=3.8。要运行此笔记本示例,请安装:

Requirements

安装 autogen-agentchat:

pip install autogen-agentchat~=0.2

如需更多信息,请参考安装指南

%%capture --no-stderr
# %pip install "autogen-agentchat~=0.2"

功能模式和实现

本节提供了函数模式定义及其实现细节。这些函数专门用于从GitHub获取并处理数据,利用OSS Insight的功能。

import logging
import os

from autogen import UserProxyAgent, config_list_from_json
from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent

logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)

ossinsight_api_schema = {
"name": "ossinsight_data_api",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": (
"Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. "
"For optimal results, specify the desired format for the data table in your request."
),
}
},
"required": ["question"],
},
"description": "This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data.",
}


def get_ossinsight(question):
"""
[Mock] Retrieve the top 10 developers with the most followers on GitHub.
"""
report_components = [
f"Question: {question}",
"SQL: SELECT `login` AS `user_login`, `followers` AS `followers` FROM `github_users` ORDER BY `followers` DESC LIMIT 10",
"""Results:
{'followers': 166730, 'user_login': 'torvalds'}
{'followers': 86239, 'user_login': 'yyx990803'}
{'followers': 77611, 'user_login': 'gaearon'}
{'followers': 72668, 'user_login': 'ruanyf'}
{'followers': 65415, 'user_login': 'JakeWharton'}
{'followers': 60972, 'user_login': 'peng-zhihui'}
{'followers': 58172, 'user_login': 'bradtraversy'}
{'followers': 52143, 'user_login': 'gustavoguanabara'}
{'followers': 51542, 'user_login': 'sindresorhus'}
{'followers': 49621, 'user_login': 'tj'}""",
]
return "\n" + "\n\n".join(report_components) + "\n"

在AutoGen中定义OpenAI助手代理

在这里,我们探讨如何在 AutoGen 中定义一个 OpenAI 助手代理。这包括设置代理以利用先前定义的函数调用来进行数据检索和分析。

assistant_id = os.environ.get("ASSISTANT_ID", None)
config_list = config_list_from_json("OAI_CONFIG_LIST")
llm_config = {
"config_list": config_list,
}
assistant_config = {
"assistant_id": assistant_id,
"tools": [
{
"type": "function",
"function": ossinsight_api_schema,
}
],
}

oss_analyst = GPTAssistantAgent(
name="OSS Analyst",
instructions=(
"Hello, Open Source Project Analyst. You'll conduct comprehensive evaluations of open source projects or organizations on the GitHub platform, "
"analyzing project trajectories, contributor engagements, open source trends, and other vital parameters. "
"Please carefully read the context of the conversation to identify the current analysis question or problem that needs addressing."
),
llm_config=llm_config,
assistant_config=assistant_config,
verbose=True,
)
oss_analyst.register_function(
function_map={
"ossinsight_data_api": get_ossinsight,
}
)
OpenAI client config of GPTAssistantAgent(OSS Analyst) - model: gpt-4-turbo-preview
GPT Assistant only supports one OpenAI client. Using the first client in the list.
No matching assistant found, creating a new assistant
tip

了解更多关于为agent配置LLM的信息在这里.

使用函数调用获取GitHub Insight数据

本部分演示了定义函数和OpenAI助手代理在获取和解释GitHub Insight数据中的实际应用。

user_proxy = UserProxyAgent(
name="user_proxy",
code_execution_config={
"work_dir": "coding",
"use_docker": False,
}, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
is_termination_msg=lambda msg: "TERMINATE" in msg["content"],
human_input_mode="NEVER",
max_consecutive_auto_reply=1,
)

user_proxy.initiate_chat(oss_analyst, message="Top 10 developers with the most followers")
oss_analyst.delete_assistant()
user_proxy (to OSS Analyst):

Top 10 developers with the most followers

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION ossinsight_data_api...

Input arguments: {'question': 'Top 10 developers with the most followers'}
Output:

Question: Top 10 developers with the most followers

SQL: SELECT `login` AS `user_login`, `followers` AS `followers` FROM `github_users` ORDER BY `followers` DESC LIMIT 10

Results:
{'followers': 166730, 'user_login': 'torvalds'}
{'followers': 86239, 'user_login': 'yyx990803'}
{'followers': 77611, 'user_login': 'gaearon'}
{'followers': 72668, 'user_login': 'ruanyf'}
{'followers': 65415, 'user_login': 'JakeWharton'}
{'followers': 60972, 'user_login': 'peng-zhihui'}
{'followers': 58172, 'user_login': 'bradtraversy'}
{'followers': 52143, 'user_login': 'gustavoguanabara'}
{'followers': 51542, 'user_login': 'sindresorhus'}
{'followers': 49621, 'user_login': 'tj'}

OSS Analyst (to user_proxy):

The top 10 developers with the most followers on GitHub are:

1. **Linus Torvalds** (`torvalds`) with 166,730 followers
2. **Evan You** (`yyx990803`) with 86,239 followers
3. **Dan Abramov** (`gaearon`) with 77,611 followers
4. **Ruan YiFeng** (`ruanyf`) with 72,668 followers
5. **Jake Wharton** (`JakeWharton`) with 65,415 followers
6. **Peng Zhihui** (`peng-zhihui`) with 60,972 followers
7. **Brad Traversy** (`bradtraversy`) with 58,172 followers
8. **Gustavo Guanabara** (`gustavoguanabara`) with 52,143 followers
9. **Sindre Sorhus** (`sindresorhus`) with 51,542 followers
10. **TJ Holowaychuk** (`tj`) with 49,621 followers


--------------------------------------------------------------------------------
user_proxy (to OSS Analyst):



--------------------------------------------------------------------------------
OSS Analyst (to user_proxy):

It looks like there is no question or prompt for me to respond to. Could you please provide more details or ask a question that you would like assistance with?


--------------------------------------------------------------------------------
Permanently deleting assistant...