Skip to content

Commit

Permalink
Huge update, everything added.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoLjl committed Nov 15, 2024
1 parent 6619de6 commit d5ef763
Show file tree
Hide file tree
Showing 19 changed files with 1,346 additions and 929 deletions.
9 changes: 3 additions & 6 deletions autogen/agentchat/contrib/captain_user_proxy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

import autogen
from autogen.agentchat.conversable_agent import ConversableAgent
from autogen.tool_utils import get_full_tool_description

from .agent_builder import AgentBuilder
from .tool_retriever import ToolBuilder
from .tool_retriever import ToolBuilder, get_full_tool_description


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

if self._nested_mode_config["autobuild_tool_config"]["tool_root"] == "default":
if self._nested_mode_config["autobuild_tool_config"].get("tool_root", "default") == "default":
cur_path = os.path.dirname(os.path.abspath(__file__))
tool_root_dir = os.path.join(cur_path, "captainagent", "tools")
else:
Expand Down
4 changes: 2 additions & 2 deletions autogen/agentchat/contrib/captainagent/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Tools can be imported from `tools/{category}/{tool_name}.py` with exactly the sa
`tool_description.tsv` contains descriptions of tools for retrieval.

# How to use
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.
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.

To install the requirements for running tools, use pip install.
```bash
pip install -r tools/requirements.txt
pip install -r autogen/agentchat/contrib/captainagent/tools/README.md
```

Whenever you run the tool-related code, remember to export the api keys to system variables.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def get_youtube_caption(videoId):
import requests

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

querystring = {"videoId": videoId, "lang": "en", "format": "text"}
querystring = {"url": video_url, "lang": "en", "flat_text": "true"}

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

response = requests.get(url, headers=headers, params=querystring)
response = response.json()
return response["data"]
print(response)
return response["transcript"]
51 changes: 49 additions & 2 deletions autogen/agentchat/contrib/tool_retriever.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import importlib.util
import inspect
import os
from textwrap import dedent, indent

import pandas as pd
from sentence_transformers import SentenceTransformer, util

from autogen import AssistantAgent, UserProxyAgent
from autogen.coding import LocalCommandLineCodeExecutor
from autogen.tool_utils import find_callables


class ToolBuilder:
Expand All @@ -13,7 +17,7 @@ class ToolBuilder:
{functions}
"""

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

self.df = pd.read_csv(corpus_path, sep="\t")
document_list = self.df["document_content"].tolist()
Expand Down Expand Up @@ -65,3 +69,46 @@ def bind_user_proxy(self, agent: UserProxyAgent, tool_root: str):
default_auto_reply=agent._default_auto_reply,
)
return updated_user_proxy


def get_full_tool_description(py_file):
"""
Retrieves the function signature for a given Python file.
"""
with open(py_file, "r") as f:
code = f.read()
exec(code)
function_name = os.path.splitext(os.path.basename(py_file))[0]
if function_name in locals():
func = locals()[function_name]
content = f"def {func.__name__}{inspect.signature(func)}:\n"
docstring = func.__doc__

if docstring:
docstring = dedent(docstring)
docstring = '"""' + docstring + '"""'
docstring = indent(docstring, " ")
content += docstring + "\n"
return content
else:
raise ValueError(f"Function {function_name} not found in {py_file}")


def find_callables(directory):
"""
Find all callable objects defined in Python files within the specified directory.
"""
callables = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".py"):
module_name = os.path.splitext(file)[0]
module_path = os.path.join(root, file)
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name, value in module.__dict__.items():
if callable(value) and name == module_name:
callables.append(value)
break
return callables
47 changes: 0 additions & 47 deletions autogen/tool_utils.py

This file was deleted.

Loading

0 comments on commit d5ef763

Please sign in to comment.