GPT Builder Demo#

Open In Colab

Inspired by GPTs interface, presented at OpenAI Dev Day 2023. Construct an agent with natural language.

Here you can build your own agent…with another agent!

%pip install llama-index-agent-openai
%pip install llama-index-embeddings-openai
%pip install llama-index-llms-openai
import os

os.environ["OPENAI_API_KEY"] = "sk-..."
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings

Settings.llm = OpenAI(model="gpt-4")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

Define Candidate Tools#

We also define a tool retriever to retrieve candidate tools.

In this setting we define tools as different Wikipedia pages.

from llama_index.core import SimpleDirectoryReader
wiki_titles = ["Toronto", "Seattle", "Chicago", "Boston", "Houston"]
from pathlib import Path

import requests

for title in wiki_titles:
    response = requests.get(
        "https://en.wikipedia.org/w/api.php",
        params={
            "action": "query",
            "format": "json",
            "titles": title,
            "prop": "extracts",
            # 'exintro': True,
            "explaintext": True,
        },
    ).json()
    page = next(iter(response["query"]["pages"].values()))
    wiki_text = page["extract"]

    data_path = Path("data")
    if not data_path.exists():
        Path.mkdir(data_path)

    with open(data_path / f"{title}.txt", "w") as fp:
        fp.write(wiki_text)
# Load all wiki documents
city_docs = {}
for wiki_title in wiki_titles:
    city_docs[wiki_title] = SimpleDirectoryReader(
        input_files=[f"data/{wiki_title}.txt"]
    ).load_data()

Build Query Tool for Each Document#

from llama_index.core import VectorStoreIndex
from llama_index.agent.openai import OpenAIAgent
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core import VectorStoreIndex

# Build tool dictionary
tool_dict = {}

for wiki_title in wiki_titles:
    # build vector index
    vector_index = VectorStoreIndex.from_documents(
        city_docs[wiki_title],
    )
    # define query engines
    vector_query_engine = vector_index.as_query_engine(llm=llm)

    # define tools
    vector_tool = QueryEngineTool(
        query_engine=vector_query_engine,
        metadata=ToolMetadata(
            name=wiki_title,
            description=("Useful for questions related to" f" {wiki_title}"),
        ),
    )
    tool_dict[wiki_title] = vector_tool

Define Tool Retriever#

# define an "object" index and retriever over these tools
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex, SimpleToolNodeMapping

tool_mapping = SimpleToolNodeMapping.from_objects(list(tool_dict.values()))
tool_index = ObjectIndex.from_objects(
    list(tool_dict.values()),
    tool_mapping,
    VectorStoreIndex,
)
tool_retriever = tool_index.as_retriever(similarity_top_k=1)

Load Data#

Here we load wikipedia pages from different cities.

Define Meta-Tools for GPT Builder#

from llama_index.core.llms import ChatMessage
from llama_index.core import ChatPromptTemplate
from typing import List

GEN_SYS_PROMPT_STR = """\
Task information is given below. 

Given the task, please generate a system prompt for an OpenAI-powered bot to solve this task: 
{task} \
"""

gen_sys_prompt_messages = [
    ChatMessage(
        role="system",
        content="You are helping to build a system prompt for another bot.",
    ),
    ChatMessage(role="user", content=GEN_SYS_PROMPT_STR),
]

GEN_SYS_PROMPT_TMPL = ChatPromptTemplate(gen_sys_prompt_messages)


agent_cache = {}


def create_system_prompt(task: str):
    """Create system prompt for another agent given an input task."""
    llm = OpenAI(llm="gpt-4")
    fmt_messages = GEN_SYS_PROMPT_TMPL.format_messages(task=task)
    response = llm.chat(fmt_messages)
    return response.message.content


def get_tools(task: str):
    """Get the set of relevant tools to use given an input task."""
    subset_tools = tool_retriever.retrieve(task)
    return [t.metadata.name for t in subset_tools]


def create_agent(system_prompt: str, tool_names: List[str]):
    """Create an agent given a system prompt and an input set of tools."""
    llm = OpenAI(model="gpt-4")
    try:
        # get the list of tools
        input_tools = [tool_dict[tn] for tn in tool_names]

        agent = OpenAIAgent.from_tools(input_tools, llm=llm, verbose=True)
        agent_cache["agent"] = agent
        return_msg = "Agent created successfully."
    except Exception as e:
        return_msg = f"An error occurred when building an agent. Here is the error: {repr(e)}"
    return return_msg
from llama_index.core.tools import FunctionTool

system_prompt_tool = FunctionTool.from_defaults(fn=create_system_prompt)
get_tools_tool = FunctionTool.from_defaults(fn=get_tools)
create_agent_tool = FunctionTool.from_defaults(fn=create_agent)
GPT_BUILDER_SYS_STR = """\
You are helping to construct an agent given a user-specified task. You should generally use the tools in this order to build the agent.

1) Create system prompt tool: to create the system prompt for the agent.
2) Get tools tool: to fetch the candidate set of tools to use.
3) Create agent tool: to create the final agent.
"""

prefix_msgs = [ChatMessage(role="system", content=GPT_BUILDER_SYS_STR)]


builder_agent = OpenAIAgent.from_tools(
    tools=[system_prompt_tool, get_tools_tool, create_agent_tool],
    prefix_messages=prefix_msgs,
    verbose=True,
)
builder_agent.query("Build an agent that can tell me about Toronto.")
Added user message to memory: Build an agent that can tell me about Toronto.
=== Calling Function ===
Calling function: create_system_prompt with args: {
  "task": "tell me about Toronto"
}
Got output: System Prompt: 

"Sure! I can provide you with information about Toronto. Toronto is the capital city of the province of Ontario, Canada. It is the largest city in Canada and one of the most multicultural cities in the world. Known for its diverse population, vibrant arts and culture scene, and iconic landmarks, Toronto offers a unique blend of modernity and history.

Toronto is home to the CN Tower, which is one of the tallest freestanding structures in the world and offers breathtaking views of the city. The city also boasts beautiful waterfront areas, such as the Harbourfront Centre and the Toronto Islands, where visitors can enjoy recreational activities and scenic views.

In terms of arts and culture, Toronto is renowned for its theaters, including the Royal Alexandra Theatre and the Princess of Wales Theatre, which host a variety of Broadway shows and musicals. The city is also home to numerous museums and galleries, such as the Art Gallery of Ontario and the Royal Ontario Museum, where visitors can explore a wide range of art and historical artifacts.

Toronto is a sports-loving city, with professional sports teams in hockey, basketball, baseball, and soccer. The Toronto Maple Leafs, Toronto Raptors, Toronto Blue Jays, and Toronto FC are some of the popular teams that attract passionate fans.

The city's diverse culinary scene offers a wide range of international cuisines, reflecting the multiculturalism of its residents. From fine dining restaurants to street food vendors, Toronto has something to satisfy every palate.

Whether you're interested in exploring its vibrant neighborhoods, shopping in trendy boutiques, or attending exciting festivals and events, Toronto has something for everyone. Let me know if there's anything specific you'd like to know about Toronto!"
  "system_prompt": "Sure! I can provide you with information about Toronto. Toronto is the capital city of the province of Ontario, Canada. It is the largest city in Canada and one of the most multicultural cities in the world. Known for its diverse population, vibrant arts and culture scene, and iconic landmarks, Toronto offers a unique blend of modernity and history.\n\nToronto is home to the CN Tower, which is one of the tallest freestanding structures in the world and offers breathtaking views of the city. The city also boasts beautiful waterfront areas, such as the Harbourfront Centre and the Toronto Islands, where visitors can enjoy recreational activities and scenic views.\n\nIn terms of arts and culture, Toronto is renowned for its theaters, including the Royal Alexandra Theatre and the Princess of Wales Theatre, which host a variety of Broadway shows and musicals. The city is also home to numerous museums and galleries, such as the Art Gallery of Ontario and the Royal Ontario Museum, where visitors can explore a wide range of art and historical artifacts.\n\nToronto is a sports-loving city, with professional sports teams in hockey, basketball, baseball, and soccer. The Toronto Maple Leafs, Toronto Raptors, Toronto Blue Jays, and Toronto FC are some of the popular teams that attract passionate fans.\n\nThe city's diverse culinary scene offers a wide range of international cuisines, reflecting the multiculturalism of its residents. From fine dining restaurants to street food vendors, Toronto has something to satisfy every palate.\n\nWhether you're interested in exploring its vibrant neighborhoods, shopping in trendy boutiques, or attending exciting festivals and events, Toronto has something for everyone. Let me know if there's anything specific you'd like to know about Toronto!",
  "tool_names": ["Toronto"]
}
Got output: Agent created successfully.
========================
Response(response='The agent has been successfully created. It can provide detailed information about Toronto, including its history, culture, landmarks, sports teams, and culinary scene. If you have any specific questions or need more information about Toronto, feel free to ask the agent.', source_nodes=[], metadata=None)
city_agent = agent_cache["agent"]
response = city_agent.query("Tell me about the parks in Toronto")
print(str(response))
Added user message to memory: Tell me about the parks in Toronto
=== Calling Function ===
Calling function: Toronto with args: {
  "input": "parks in Toronto"
}
Got output: Toronto has a diverse array of public spaces and parks. Some of the notable ones include Nathan Phillips Square, Yonge–Dundas Square, Harbourfront Square, and Mel Lastman Square. There are also large downtown parks like Allan Gardens, Christie Pits, Grange Park, Little Norway Park, Moss Park, Queen's Park, Riverdale Park and Trinity Bellwoods Park. Other parks include Tommy Thompson Park and the Toronto Islands. The outer areas of the city have parks like High Park, Humber Bay Park, Centennial Park, Downsview Park, Guild Park and Gardens, Sunnybrook Park and Morningside Park. Toronto also operates several public golf courses. Morningside Park is the largest park managed by the city. Parts of Rouge National Urban Park, the largest urban park in North America, is also in Toronto.
========================

Toronto is known for its diverse array of public spaces and parks. Some of the notable ones include:

1. **Nathan Phillips Square**: This vibrant city square features the Toronto City Hall, an ice rink, and a peace garden.

2. **Yonge–Dundas Square**: Often considered Toronto's Times Square, this public square hosts many public events, performances, and art displays.

3. **Harbourfront Square**: Located by the Lake Ontario, it's a key cultural district with theatres, galleries, and dining.

4. **Mel Lastman Square**: Located in North York, it's a public square that hosts a variety of activities throughout the year including concerts, markets, and festivals.

5. **Allan Gardens**: A park and indoor botanical garden located downtown, known for its historic, cast-iron and glass domed "Palm House" built in 1910.

6. **High Park**: It's the largest park in downtown Toronto featuring many hiking trails, sports facilities, a beautiful lakefront, a dog park, a zoo, and playgrounds.

7. **Toronto Islands**: A group of small islands located offshore from the city centre, providing shelter for the Toronto Harbour and offering stunning views of the city skyline.

8. **Tommy Thompson Park**: An urban wilderness park located on the Leslie Street Spit, known for bird watching.

9. **Rouge National Urban Park**: The largest urban park in North America, it's located in the city's eastern portion.

10. **Morningside Park**: The largest park managed by the city, it's located in Scarborough and features picnic areas, walking trails, and a creek.

These parks offer a variety of recreational activities and provide a green respite from the bustling city.