Using AutoGen with motleycrew
Microsoft AutoGen is one of the most popular multi-agent frameworks. Motleycrew supports interaction with autogen in both directions: firstly, you can wrap an AutoGen chat as a motleycrew tool, which you can then give to any supported agent; secondly, you can give any motleycrew tool (which includes agents used as tools) to an AutoGen agent.
The two options are described in detail below
[1]:
from dotenv import load_dotenv
load_dotenv()
[1]:
True
Integrating an AutoGen chat into motleycrew
If you want to use an AutoGen chat (say one from their many wonderful examples, or one you have already) as part of a motleycrew setup, you can wrap it as a tool, which you can then give to any other motleycrew-supported agent.
Let’s create an AutoGen chat for automatic problem solving. The code is taken from the example here: https://microsoft.github.io/autogen/docs/notebooks/agentchat_groupchat
[1]:
!pip install pyautogen
import autogen
import os
llm_config = {
"config_list": [{"model": "gpt-4-turbo", "api_key": os.environ["OPENAI_API_KEY"]}],
"cache_seed": None,
}
[3]:
user_proxy = autogen.UserProxyAgent(
name="User_proxy",
system_message="A human admin.",
code_execution_config={
"last_n_messages": 2,
"work_dir": "examples/data/groupchat",
"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.
human_input_mode="TERMINATE",
)
coder = autogen.AssistantAgent(
name="Coder",
llm_config=llm_config,
)
pm = autogen.AssistantAgent(
name="Product_manager",
system_message="Creative in software product ideas.",
llm_config=llm_config,
)
groupchat = autogen.GroupChat(agents=[user_proxy, coder, pm], messages=[], max_round=12)
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
If we were using plain AutoGen, we’d start the chat with something like this:
user_proxy.initiate_chat(
manager, message="Find a latest paper about gpt-4 on arxiv and find its potential applications in software."
)
[4]:
from motleycrew.tools.autogen_chat_tool import AutoGenChatTool
knowledge_retrieval_tool = AutoGenChatTool(
name="retrieve_knowledge_by_topic",
description="Search arxiv for the latest paper on a given topic "
"and find its potential applications in software.", # will be used in the prompt of the future agent that will use the tool
prompt="Find a latest paper about {topic} on arxiv "
"and find its potential applications in software.", # this is the initial prompt for the AutoGen chat itself
initiator=user_proxy,
recipient=manager,
)
We can now give the tool to any agent and solve tasks with it.
[5]:
from motleycrew import MotleyCrew
from motleycrew.agents.langchain.tool_calling_react import ReActToolCallingAgent
crew = MotleyCrew()
writer = ReActToolCallingAgent(tools=[knowledge_retrieval_tool])
[6]:
from motleycrew.tasks import SimpleTask
blog_post_task = SimpleTask(
crew = crew,
name="Produce blog post on the applications of latest advancements related to GPT-4",
description="Using the insights provided by searching research papers, develop an engaging blog "
"post that highlights the most significant advancements on GPT-4 ant their applications.\n"
"Your post should be informative yet accessible, catering to a tech-savvy audience.\n"
"Make it sound cool, avoid complex words so it doesn't sound like AI. "
"Create a blog post of at least 4 paragraphs.",
agent=writer,
)
[7]:
crew.run()
WARNING:root:No known Cypher type matching annotation typing.Optional[typing.Any], will use JSON string
WARNING:root:No known Cypher type matching annotation typing.List[str], will use JSON string
WARNING:root:No known Cypher type matching annotation typing.List[str], will use JSON string
User_proxy (to chat_manager):
Find a latest paper about GPT-4 on arxiv and find its potential applications in software.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
Coder (to chat_manager):
First, we'll search the ArXiv repository for the latest papers on GPT-4. We can achieve this using the `arxiv` Python package, which allows querying and fetching papers from ArXiv.
To begin, we need to search for the most recent papers related to "GPT-4". Once we find the papers, I will look into the one that is most recent and check its content to find mentions of potential applications in software.
Let's start by finding the latest paper about GPT-4 on ArXiv:
```python
# Install the arXiv package if needed
# pip install arxiv
import arxiv
# Query for the latest papers on GPT-4
def find_latest_gpt4_paper():
search = arxiv.Search(
query="GPT-4",
max_results=1,
sort_by=arxiv.SortCriterion.SubmittedDate
)
# Fetch the most recent paper
for result in search.results():
print("Title:", result.title)
print("Authors:", ", ".join(author.name for author in result.authors))
print("Abstract:", result.summary.replace('\n', ' '))
print("Published:", result.published.date())
print("URL:", result.entry_id)
break
find_latest_gpt4_paper()
```
Please run the above Python script. It will output the title, authors, abstract, publication date, and URL of the most recent paper about GPT-4 on ArXiv. We can then analyze the abstract to identify its potential applications in software.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
User_proxy (to chat_manager):
exitcode: 1 (execution failed)
Code output:
Traceback (most recent call last):
File "", line 4, in <module>
import arxiv
ModuleNotFoundError: No module named 'arxiv'
--------------------------------------------------------------------------------
Coder (to chat_manager):
It looks like the Python package `arxiv` is not installed, which caused the error. Instead of using the `arxiv` package, we can employ web scraping techniques to obtain the information from the ArXiv website. We'll use `requests` to fetch the page content and `BeautifulSoup` to parse it.
Please ensure that you have the `requests` and `beautifulsoup4` libraries installed. If not, you can install them using pip.
Here is the modified code to search for the latest GPT-4 paper on ArXiv using web scraping:
```python
# filename: fetch_latest_gpt4_paper.py
# Install the necessary packages if they are not installed
# pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
def fetch_latest_gpt4_paper():
# URL to search for GPT-4 related papers sorted by newest
url = "https://arxiv.org/search/?query=GPT-4&searchtype=all&abstracts=show&order=-announced_date_first&size=1"
# Issue a GET request to fetch the page content
response = requests.get(url)
if response.status_code != 200:
print("Failed to fetch data from arXiv")
return
# Use BeautifulSoup to parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Locate the title, authors, abstract, published date, and link to the paper
paper = soup.select_one('li.arxiv-result')
title = paper.select_one('p.title').text.strip()
authors = paper.select_one('p.authors').text.replace('\n', ' ').strip()
abstract = paper.select_one('span.abstract-full').text.strip()
published_date = paper.select_one('p.is-size-7').text.strip()
link = "https://arxiv.org" + paper.select_one('p.title').a['href']
# Print the results
print("Title:", title)
print("Authors:", authors)
print("Abstract:", abstract)
print("Published:", published_date)
print("URL:", link)
fetch_latest_gpt4_paper()
```
Please save this script to a file, install the required packages if needed, and run the script. It will print the details of the most recent paper on GPT-4 from ArXiv. We can then proceed to analyze the abstract for potential applications in software.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
User_proxy (to chat_manager):
exitcode: 0 (execution succeeded)
Code output:
Failed to fetch data from arXiv
--------------------------------------------------------------------------------
Product_manager (to chat_manager):
It seems there was an issue fetching the data from the ArXiv website, possibly due to network issues or restrictions on scraping from their site. As an alternative approach, here are some methods you can consider to manually find the latest paper about GPT-4 on ArXiv:
### Manual Search on ArXiv:
1. **Go to the ArXiv website**: Visit [arxiv.org](https://arxiv.org/).
2. **Use the Search Bar**: Type "GPT-4" into the search bar.
3. **Adjust Search Settings**: Ensure the search is set to sort by the most recent submissions first. You can adjust this in the search settings.
4. **Review Results**: Look through the titles and abstracts for the most recent entries to find relevant papers.
### Google Scholar:
1. **Visit Google Scholar**: Go to [Google Scholar](https://scholar.google.com/).
2. **Search for "GPT-4"**: Ensure to add filters for recent years to get the most up-to-date research.
3. **Review and Filter Results**: Google Scholar often includes links to ArXiv preprints, helping you find the latest studies.
### Using APIs:
If you are still interested in automating this process, using a more robust method such as the CrossRef API or the ArXiv API with proper handling of user-agent and headers might yield better results.
If you can manually find a paper, you can share its abstract here, and we can analyze potential software applications together. Alternatively, if you have any other tasks or need further assistance, feel free to let me know!
--------------------------------------------------------------------------------
User_proxy (to chat_manager):
Find a latest paper about GPT-4 on arxiv and find its potential applications in software.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
User_proxy (to chat_manager):
--------------------------------------------------------------------------------
Coder (to chat_manager):
First, let's write a script to search for the latest papers about GPT-4 on arXiv, a well-known repository for scientific papers. The script will use the arXiv API to fetch this information. Let's retrieve the details of the most recent paper related to GPT-4.
```python
# filename: fetch_gpt4_papers.py
import urllib.request
import urllib.parse
import xml.etree.ElementTree as ET
# Define the base URL for the ArXiv API
base_url = 'http://export.arxiv.org/api/query?'
# Specify search parameters: search for GPT-4, max result is 1, sorted by the latest submissions
search_params = {
"search_query": "all:GPT-4",
"sortBy": "submittedDate",
"sortOrder": "descending",
"max_results": 1
}
# Encode the search parameters, make the request, and read the response
query_string = urllib.parse.urlencode(search_params)
search_url = base_url + query_string
response = urllib.request.urlopen(search_url)
response_content = response.read()
# Parse the XML response content
root = ET.fromstring(response_content)
# Extract and print the title, summary, and published date from the entry
for entry in root.findall('{http://www.w3.org/2005/Atom}entry'):
title = entry.find('{http://www.w3.org/2005/Atom}title').text
summary = entry.find('{http://www.w3.org/2005/Atom}summary').text
published_date = entry.find('{http://www.w3.org/2005/Atom}published').text
# Output the results
print('Title:', title)
print('Published Date:', published_date)
print('Summary:', summary)
```
**Instructions**:
1. Save the above script to a file named `fetch_gpt4_papers.py`.
2. Run the script using a Python interpreter. This will retrieve the latest paper about GPT-4 from arXiv.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
User_proxy (to chat_manager):
exitcode: 0 (execution succeeded)
Code output:
Title: Observational Scaling Laws and the Predictability of Language Model
Performance
Published Date: 2024-05-17T17:49:44Z
Summary: Understanding how language model performance varies with scale is critical to
benchmark and algorithm development. Scaling laws are one approach to building
this understanding, but the requirement of training models across many
different scales has limited their use. We propose an alternative,
observational approach that bypasses model training and instead builds scaling
laws from ~80 publically available models. Building a single scaling law from
multiple model families is challenging due to large variations in their
training compute efficiencies and capabilities. However, we show that these
variations are consistent with a simple, generalized scaling law where language
model performance is a function of a low-dimensional capability space, and
model families only vary in their efficiency in converting training compute to
capabilities. Using this approach, we show the surprising predictability of
complex scaling phenomena: we show that several emergent phenomena follow a
smooth, sigmoidal behavior and are predictable from small models; we show that
the agent performance of models such as GPT-4 can be precisely predicted from
simpler non-agentic benchmarks; and we show how to predict the impact of
post-training interventions like Chain-of-Thought and Self-Consistency as
language model capabilities continue to improve.
--------------------------------------------------------------------------------
Product_manager (to chat_manager):
Based on the summary of the latest paper about GPT-4 titled "Observational Scaling Laws and the Predictability of Language Model Performance," here are several potential applications in software development and related fields:
1. **Predictive Benchmarking Tools**:
- The paper discusses the predictability of language model performance using scaling laws. This could lead to the development of new software tools that predict the performance of language models like GPT-4 based on various scaling inputs. Such tools would be valuable in optimizing model configurations for specific applications without needing extensive empirical testing.
2. **Resource Optimization Algorithms**:
- The paper's framework for predicting performance as a function of a "capability space" can be translated into algorithms that optimize the training compute resources for developing AI models. This could significantly reduce costs and increase the efficiency of AI training cycles in commercial software development.
3. **Automated Model Scaling**:
- Software that automatically scales AI models' capabilities based on available compute resources could be developed. This would make AI more accessible, especially for organizations with fluctuating resource availability, by providing them with tools to dynamically adjust their model's complexity and performance.
4. **Advanced Development Frameworks**:
- Integrating the predictive scaling laws into AI development frameworks could enable developers to anticipate the behavior and limitations of their models, facilitating more accurate and robust AI systems design.
5. **Enhanced Debugging and Analysis Tools**:
- Using the generalized scaling law proposed in the paper, new debugging and performance analysis tools can be created, which would help in identifying bottlenecks and inefficiencies in language model training and operation.
6. **Optimization of Intervention Techniques**:
- The ability to predict the impact of post-training interventions like the Chain-of-Thought and Self-Consistency could be encapsulated in software solutions that optimize these interventions. This would improve the effectiveness and reliability of language models in practical applications.
These applications demonstrate how theoretical insights from academic research can be used to inform and enhance software product development, particularly in the rapidly evolving field of artificial intelligence.
--------------------------------------------------------------------------------
WARNING:root:No known Cypher type matching annotation typing.Optional[typing.Any], will use JSON string
[7]:
[Task(status=done)]
[8]:
from IPython.display import display, Markdown
display(Markdown(blog_post_task.output))
Final Answer: Welcome to the Future: Exploring the Frontiers of GPT-4
In the rapidly evolving world of artificial intelligence, GPT-4 stands out as a beacon of innovation and potential. Recent research, particularly a fascinating study titled “Observational Scaling Laws and the Predictability of Language Model Performance,” sheds light on groundbreaking advancements that could redefine how we interact with technology.
GPT-4 isn’t just about understanding or generating text; it’s about predicting and optimizing performance in ways previously deemed impossible. The study introduces predictive benchmarking tools that could revolutionize the development of AI models. Imagine software that can anticipate the performance of language models like GPT-4 based on various inputs, streamlining the development process and enhancing efficiency without the need for extensive testing.
But the implications extend beyond mere prediction. The research discusses resource optimization algorithms that could significantly reduce the costs associated with AI training. These algorithms utilize a “capability space” to optimize the compute resources needed, ensuring that AI development is not only faster but also more economical.
Moreover, GPT-4 could lead to the creation of automated model scaling software. Such tools would adjust AI capabilities based on available resources, making cutting-edge technology accessible to a broader range of users and organizations, regardless of their fluctuating resource availability.
The integration of these advancements into AI development frameworks could also empower developers with better tools for debugging and performance analysis, enhancing the robustness and accuracy of AI systems. This is not just about making AI smarter; it’s about making AI development smarter, more intuitive, and infinitely more creative.
As we stand on the brink of these exciting developments, GPT-4 is not just a tool but a transformational force in the tech landscape, promising to bring about a new era of innovation and efficiency. Stay tuned, because the future of AI is here, and it’s more promising than ever!
The writer agent used the AutoGen chat tool to retrieve data for the post!
Using any motleycrew-supported tools with Autogen agents
Now let’s do it the other way around: give tools (or agents, for that matter) from motleycrew to AutoGen agents. The code is based on https://microsoft.github.io/autogen/docs/tutorial/tool-use
[24]:
from motleycrew.tools import MotleyTool
from langchain_community.tools import DuckDuckGoSearchRun
search_tool = MotleyTool.from_supported_tool(DuckDuckGoSearchRun()) # Any tools or even motleycrew's agents can be converted to MotleyTool like this!
# Let's first define the assistant agent that suggests tool calls.
assistant = autogen.ConversableAgent(
name="Assistant",
system_message="You are a helpful AI assistant. "
"You can provide useful and up-to-date data using web search. "
"Return 'TERMINATE' when the task is done.",
llm_config=llm_config,
)
# The user proxy agent is used for interacting with the assistant agent
# and executes tool calls.
user_proxy = autogen.ConversableAgent(
name="User",
llm_config=False,
is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
human_input_mode="NEVER",
)
# Make an autogen-compatible tool from the MotleyTool.
autogen_tool = search_tool.to_autogen_tool()
# Register the tool signature with the assistant agent.
assistant.register_for_llm(name="search_tool", description="Web search tool")(autogen_tool)
# Register the tool function with the user proxy agent.
user_proxy.register_for_execution(name="search_tool")(autogen_tool)
[24]:
<function motleycrew.tools.tool.MotleyTool.to_autogen_tool.<locals>.autogen_tool_fn(input: str) -> str>
[25]:
chat_result = user_proxy.initiate_chat(assistant, message="What was the first computer?")
User (to Assistant):
What was the first computer?
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Assistant (to User):
***** Suggested tool call (call_Na0NhkbZVtS7cnme7XRqxqUS): search_tool *****
Arguments:
{"input":"first computer"}
****************************************************************************
--------------------------------------------------------------------------------
>>>>>>>> EXECUTING FUNCTION search_tool...
User (to Assistant):
User (to Assistant):
***** Response from calling tool (call_Na0NhkbZVtS7cnme7XRqxqUS) *****
ENIAC, the first programmable general-purpose electronic digital computer, built during World War II by the United States and completed in 1946. The project was led by John Mauchly, J. Presper Eckert, Jr., and their colleagues. ENIAC was the most powerful calculating device built to that time. Learn about the origins and evolution of computers, from the first mechanical and electric machines to the modern devices we use today. Explore the inventions, concepts, and events that shaped the computer industry and culture. Learn about the history of computing, from Charles Babbage's mechanical difference and analytical engines to the first electronic computer, the Atanasoff-Berry Computer. Discover how World War II and personal computers shaped the evolution of computing. The first commercially available personal computer was the Altair 8800, released in 1975. The Altair was a rudimentary device sold as a $439 kit that users had to assemble themselves, but it quickly gained a cult following among technology enthusiasts. Ada Lovelace, English mathematician, an associate of Charles Babbage, for whose digital computer prototype, the Analytical Engine, she created a program in 1843. She has been called the first computer programmer. Ada Lovelace Day, the second Tuesday in October, honors women's contributions to science and technology.
**********************************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Assistant (to User):
The first programmable general-purpose electronic digital computer was the ENIAC, completed in 1946. It was built during World War II by the United States and led by John Mauchly, J. Presper Eckert, Jr., and their colleagues. ENIAC stood as the most powerful calculating device of its time.
--------------------------------------------------------------------------------
User (to Assistant):
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Assistant (to User):
TERMINATE
--------------------------------------------------------------------------------
[ ]:
final_result = chat_result.summary
The agent used the search tool!