GPT Builder Demo#
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!
from llama_index.tools import BaseTool, FunctionTool
from llama_index.agent import OpenAIAgent
from llama_index.prompts import PromptTemplate
from llama_index.llms import ChatMessage, OpenAI
from llama_index import ServiceContext
llm = OpenAI(model="gpt-4")
service_context = ServiceContext.from_defaults(llm=llm)
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 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.agent import OpenAIAgent
from llama_index.tools import QueryEngineTool, ToolMetadata
# Build tool dictionary
tool_dict = {}
for wiki_title in wiki_titles:
# build vector index
vector_index = VectorStoreIndex.from_documents(
city_docs[wiki_title], service_context=service_context
)
# define query engines
vector_query_engine = vector_index.as_query_engine()
# 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 import VectorStoreIndex
from llama_index.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.prompts 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
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],
llm=llm,
prefix_messages=prefix_msgs,
verbose=True,
)
builder_agent.query("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 scene, and thriving business community, Toronto offers a wide range of attractions and experiences.
Toronto is home to iconic landmarks such as the CN Tower, which offers breathtaking views of the city, and the Royal Ontario Museum, which houses an extensive collection of art, culture, and natural history. The city also boasts beautiful waterfront areas, including the Harbourfront Centre and the Toronto Islands, where visitors can enjoy outdoor activities and scenic views.
In terms of culture, Toronto hosts numerous festivals throughout the year, including the Toronto International Film Festival, Caribana, and Nuit Blanche. The city is also known for its world-class dining scene, offering a diverse range of cuisines from around the globe.
Toronto is a major economic hub, with a strong presence in industries such as finance, technology, and healthcare. It is home to the Toronto Stock Exchange and several multinational corporations. The city's robust public transportation system, including the TTC subway and streetcar network, makes it easy to navigate and explore.
Whether you're interested in exploring its cultural attractions, enjoying its culinary delights, or experiencing its vibrant nightlife, Toronto has something to offer for everyone. How can I assist you further in discovering more about Toronto?"
========================
=== Calling Function ===
Calling function: get_tools with args: {
"task": "tell me about Toronto"
}
Got output: ['Toronto']
========================
=== Calling Function ===
Calling function: create_agent with args: {
"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 scene, and thriving business community, Toronto offers a wide range of attractions and experiences.\n\nToronto is home to iconic landmarks such as the CN Tower, which offers breathtaking views of the city, and the Royal Ontario Museum, which houses an extensive collection of art, culture, and natural history. The city also boasts beautiful waterfront areas, including the Harbourfront Centre and the Toronto Islands, where visitors can enjoy outdoor activities and scenic views.\n\nIn terms of culture, Toronto hosts numerous festivals throughout the year, including the Toronto International Film Festival, Caribana, and Nuit Blanche. The city is also known for its world-class dining scene, offering a diverse range of cuisines from around the globe.\n\nToronto is a major economic hub, with a strong presence in industries such as finance, technology, and healthcare. It is home to the Toronto Stock Exchange and several multinational corporations. The city's robust public transportation system, including the TTC subway and streetcar network, makes it easy to navigate and explore.\n\nWhether you're interested in exploring its cultural attractions, enjoying its culinary delights, or experiencing its vibrant nightlife, Toronto has something to offer for everyone. How can I assist you further in discovering more 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 landmarks, culture, economy, and transportation.', source_nodes=[], metadata=None)
city_agent = agent_cache["agent"]
response = city_agent.query("Tell me about the parks in Toronto")
print(str(response))
=== Calling Function ===
Calling function: Toronto with args: {
"input": "parks in Toronto"
}
Got output: Toronto has a wide variety of public parks and spaces. Some of the downtown parks include Allan Gardens, Christie Pits, Grange Park, Little Norway Park, Moss Park, Queen's Park, Riverdale Park and Trinity Bellwoods Park. There are also two large parks on the waterfront south of downtown: Tommy Thompson Park and the Toronto Islands. Other large parks managed by the city in the outer areas include High Park, Humber Bay Park, Centennial Park, Downsview Park, Guild Park and Gardens, Sunnybrook Park and Morningside Park. Toronto also has parts of Rouge National Urban Park, the largest urban park in North America, which is managed by Parks Canada.
========================
Toronto is home to a variety of parks, offering a mix of natural beauty, recreational activities, and cultural experiences. Here are some of the notable parks in Toronto:
1. **Allan Gardens**: Located downtown, this park features a conservatory with six greenhouses showcasing rare botanical plants.
2. **Christie Pits**: Known for its outdoor pool and artificial ice rink, this park is a popular spot for sports and leisure.
3. **Grange Park**: This park is located in the heart of the city and offers a playground, a splash pad, and a dog off-leash area.
4. **Little Norway Park**: Overlooking the waterfront, this park features a playground, a wading pool, and a baseball diamond.
5. **Moss Park**: This downtown park has a large sports field, a playground, and a splash pad.
6. **Queen's Park**: This urban park is home to the Ontario Legislative Building and several monuments.
7. **Riverdale Park**: Offering panoramic views of downtown Toronto, this park has sports fields, a swimming pool, and a large off-leash dog area.
8. **Trinity Bellwoods Park**: This popular park features a variety of recreational facilities, including sports fields, a wading pool, and a children's playground.
9. **Tommy Thompson Park**: Located on the waterfront, this park is a popular spot for bird watching and nature walks.
10. **Toronto Islands**: This group of small islands offers beaches, picnic areas, and canoe rentals.
11. **High Park**: Toronto's largest public park, featuring hiking trails, sports facilities, a beautiful lakefront, a dog park, a zoo, and several playgrounds.
12. **Humber Bay Park**: This waterfront park offers stunning views of the Toronto skyline, a butterfly habitat, and a network of trails.
13. **Centennial Park**: One of Toronto's busiest parks, featuring a conservatory, a ski hill, a golf centre, and a multipurpose sports field.
14. **Downsview Park**: Once a military base, now a dynamic urban park with sports fields, a pond, and a forested area.
15. **Guild Park and Gardens**: Known for its collection of salvaged architectural pieces, this park offers a unique blend of nature and culture.
16. **Sunnybrook Park**: This park offers a variety of sports fields, horse stables, and a dog off-leash area.
17. **Morningside Park**: One of Toronto's largest parks, featuring picnic areas, walking trails, and a creek.
18. **Rouge National Urban Park**: Managed by Parks Canada, this is the largest urban park in North America, offering a mix of wilderness, farmland, and historical sites.