Skip to content

Commit d5ef763

Browse files
committed
Huge update, everything added.
1 parent 6619de6 commit d5ef763

File tree

19 files changed

+1346
-929
lines changed

19 files changed

+1346
-929
lines changed

autogen/agentchat/contrib/captain_user_proxy_agent.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
import autogen
77
from autogen.agentchat.conversable_agent import ConversableAgent
8-
from autogen.tool_utils import get_full_tool_description
98

109
from .agent_builder import AgentBuilder
11-
from .tool_retriever import ToolBuilder
10+
from .tool_retriever import ToolBuilder, get_full_tool_description
1211

1312

1413
def check_nested_mode_config(nested_mode_config: Dict):
@@ -20,7 +19,6 @@ def check_nested_mode_config(nested_mode_config: Dict):
2019
"group_chat_llm_config" in nested_mode_config.keys()
2120
), "group_chat_llm_config is required when using autobuild as nested mode."
2221
elif "meta_prompting_llm_config" in nested_mode_config.keys():
23-
# TODO: check meta_prompting_config
2422
pass
2523
else:
2624
raise ValueError("nested_mode_config should contain either autobuild_init_config or meta_prompting_llm_config.")
@@ -94,7 +92,6 @@ def __init__(
9492
name (str): name of the agent.
9593
nested_mode_config (dict): the configuration for the nested chat mode.
9694
For autobuild, please refer to: autogen.agentchat.contrib.agent_builder[AgentBuilder]
97-
TODO: Add meta_prompting description
9895
is_termination_msg (function): a function that takes a message in the form of a dictionary
9996
and returns a boolean value indicating if this received message is a termination message.
10097
The dict can contain the following keys: "content", "role", "name", "function_call".
@@ -179,7 +176,7 @@ def _run_autobuild(self, group_name: str, execution_task: str, building_task: st
179176
if group_name in self.build_history.keys():
180177
agent_list, agent_configs = builder.load(config_json=json.dumps(self.build_history[group_name]))
181178
if self._nested_mode_config.get("autobuild_tool_config", None) and agent_configs["coding"] is True:
182-
# tool library enabled, load tools and bind to the agents
179+
# tool library enabled, reload tools and bind to the agents
183180
tool_root_dir = self.tool_root_dir
184181
tool_builder = ToolBuilder(
185182
corpus_path=os.path.join(tool_root_dir, "tool_description.tsv"),
@@ -204,7 +201,7 @@ def _run_autobuild(self, group_name: str, execution_task: str, building_task: st
204201
if len(skills) == 0:
205202
skills = [building_task]
206203

207-
if self._nested_mode_config["autobuild_tool_config"]["tool_root"] == "default":
204+
if self._nested_mode_config["autobuild_tool_config"].get("tool_root", "default") == "default":
208205
cur_path = os.path.dirname(os.path.abspath(__file__))
209206
tool_root_dir = os.path.join(cur_path, "captainagent", "tools")
210207
else:

autogen/agentchat/contrib/captainagent/tools/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ Tools can be imported from `tools/{category}/{tool_name}.py` with exactly the sa
2424
`tool_description.tsv` contains descriptions of tools for retrieval.
2525

2626
# How to use
27-
Some tools require Bing Search API key and RapidAPI key. For Bing API, you can read more about how to get an API on the [Bing Web Search API](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api) page. For RapidAPI, you can [sign up](https://rapidapi.com/auth/sign-up) and subscribe to these two links([link1](https://rapidapi.com/illmagination/api/youtube-captions-and-transcripts/), [link2](https://rapidapi.com/420vijay47/api/youtube-mp3-downloader2/)). These apis have free billing options and there is no need to worry about extra costs.
27+
Some tools require Bing Search API key and RapidAPI key. For Bing API, you can read more about how to get an API on the [Bing Web Search API](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api) page. For RapidAPI, you can [sign up](https://rapidapi.com/auth/sign-up) and subscribe to these two links([link1](https://rapidapi.com/solid-api-solid-api-default/api/youtube-transcript3), [link2](https://rapidapi.com/420vijay47/api/youtube-mp3-downloader2)). These apis have free billing options and there is no need to worry about extra costs.
2828

2929
To install the requirements for running tools, use pip install.
3030
```bash
31-
pip install -r tools/requirements.txt
31+
pip install -r autogen/agentchat/contrib/captainagent/tools/README.md
3232
```
3333

3434
Whenever you run the tool-related code, remember to export the api keys to system variables.

autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_youtube_caption.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ def get_youtube_caption(videoId):
1919
import requests
2020

2121
RAPID_API_KEY = os.environ["RAPID_API_KEY"]
22-
url = "https://youtube-captions-and-transcripts.p.rapidapi.com/getCaptions"
22+
video_url = f"https://www.youtube.com/watch?v={videoId}"
23+
url = "https://youtube-transcript3.p.rapidapi.com/api/transcript-with-url"
2324

24-
querystring = {"videoId": videoId, "lang": "en", "format": "text"}
25+
querystring = {"url": video_url, "lang": "en", "flat_text": "true"}
2526

26-
headers = {"X-RapidAPI-Key": RAPID_API_KEY, "X-RapidAPI-Host": "youtube-captions-and-transcripts.p.rapidapi.com"}
27+
headers = {"X-RapidAPI-Key": RAPID_API_KEY, "X-RapidAPI-Host": "youtube-transcript3.p.rapidapi.com"}
2728

2829
response = requests.get(url, headers=headers, params=querystring)
2930
response = response.json()
30-
return response["data"]
31+
print(response)
32+
return response["transcript"]

autogen/agentchat/contrib/tool_retriever.py

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import importlib.util
2+
import inspect
3+
import os
4+
from textwrap import dedent, indent
5+
16
import pandas as pd
27
from sentence_transformers import SentenceTransformer, util
38

49
from autogen import AssistantAgent, UserProxyAgent
510
from autogen.coding import LocalCommandLineCodeExecutor
6-
from autogen.tool_utils import find_callables
711

812

913
class ToolBuilder:
@@ -13,7 +17,7 @@ class ToolBuilder:
1317
{functions}
1418
"""
1519

16-
def __init__(self, corpus_path, retriever):
20+
def __init__(self, corpus_path, retriever="all-mpnet-base-v2"):
1721

1822
self.df = pd.read_csv(corpus_path, sep="\t")
1923
document_list = self.df["document_content"].tolist()
@@ -65,3 +69,46 @@ def bind_user_proxy(self, agent: UserProxyAgent, tool_root: str):
6569
default_auto_reply=agent._default_auto_reply,
6670
)
6771
return updated_user_proxy
72+
73+
74+
def get_full_tool_description(py_file):
75+
"""
76+
Retrieves the function signature for a given Python file.
77+
"""
78+
with open(py_file, "r") as f:
79+
code = f.read()
80+
exec(code)
81+
function_name = os.path.splitext(os.path.basename(py_file))[0]
82+
if function_name in locals():
83+
func = locals()[function_name]
84+
content = f"def {func.__name__}{inspect.signature(func)}:\n"
85+
docstring = func.__doc__
86+
87+
if docstring:
88+
docstring = dedent(docstring)
89+
docstring = '"""' + docstring + '"""'
90+
docstring = indent(docstring, " ")
91+
content += docstring + "\n"
92+
return content
93+
else:
94+
raise ValueError(f"Function {function_name} not found in {py_file}")
95+
96+
97+
def find_callables(directory):
98+
"""
99+
Find all callable objects defined in Python files within the specified directory.
100+
"""
101+
callables = []
102+
for root, dirs, files in os.walk(directory):
103+
for file in files:
104+
if file.endswith(".py"):
105+
module_name = os.path.splitext(file)[0]
106+
module_path = os.path.join(root, file)
107+
spec = importlib.util.spec_from_file_location(module_name, module_path)
108+
module = importlib.util.module_from_spec(spec)
109+
spec.loader.exec_module(module)
110+
for name, value in module.__dict__.items():
111+
if callable(value) and name == module_name:
112+
callables.append(value)
113+
break
114+
return callables

autogen/tool_utils.py

-47
This file was deleted.

0 commit comments

Comments
 (0)