code: get_ipython().run_line_magic('pip', 'install llama-hub-llama-packs-agents-llm-compiler-step')
  get_ipython().run_line_magic('pip', 'install llama-index-readers-wikipedia')
  get_ipython().run_line_magic('pip', 'install llama-index-llms-openai')
  
  
  import phoenix as px
  
  px.launch_app()
  
  import llama_index.core
  
  llama_index.core.set_global_handler("arize_phoenix")
  
  
  
  import nest_asyncio
  
  nest_asyncio.apply()
  
  
  
  from llama_index.packs.agents.llm_compiler.step import LLMCompilerAgentWorker
  
  from llama_index.core.llama_pack import download_llama_pack
  
  download_llama_pack(
      "LLMCompilerAgentPack",
      "./agent_pack",
      skip_load=True,
  )
  from agent_pack.step import LLMCompilerAgentWorker
  
  
  
  import json
  from typing import Sequence, List
  
  from llama_index.llms.openai import OpenAI
  from llama_index.core.llms import ChatMessage
  from llama_index.core.tools import BaseTool, FunctionTool
  
  import nest_asyncio
  
  nest_asyncio.apply()
  
  
  
  def multiply(a: int, b: int) -> int:
      """Multiple two integers and returns the result integer"""
      return a * b
  
  
  multiply_tool = FunctionTool.from_defaults(fn=multiply)
  
  
  def add(a: int, b: int) -> int:
      """Add two integers and returns the result integer"""
      return a + b
  
  
  add_tool = FunctionTool.from_defaults(fn=add)
  
  tools = [multiply_tool, add_tool]
  
  
  multiply_tool.metadata.fn_schema_str
  
  
  
  from llama_index.core.agent import AgentRunner
  
  
  llm = OpenAI(model="gpt-4")
  
  
  callback_manager = llm.callback_manager
  
  
  agent_worker = LLMCompilerAgentWorker.from_tools(
      tools, llm=llm, verbose=True, callback_manager=callback_manager
  )
  agent = AgentRunner(agent_worker, callback_manager=callback_manager)
  
  
  
  response = agent.chat("What is (121 * 3) + 42?")
  
  
  response
  
  
  agent.memory.get_all()
  
  
  
  
  get_ipython().system('pip install llama-index-readers-wikipedia')
  
  
  from llama_index.readers.wikipedia import WikipediaReader
  
  
  wiki_titles = ["Toronto", "Seattle", "Chicago", "Boston", "Miami"]
  
  
  city_docs = {}
  reader = WikipediaReader()
  for wiki_title in wiki_titles:
      docs = reader.load_data(pages=[wiki_title])
      city_docs[wiki_title] = docs
  
  
  
  from llama_index.core import ServiceContext
  from llama_index.llms.openai import OpenAI
  from llama_index.core.callbacks import CallbackManager
  
  llm = OpenAI(temperature=0, model="gpt-4")
  service_context = ServiceContext.from_defaults(llm=llm)
  callback_manager = CallbackManager([])
  
  
  
  from llama_index.core import load_index_from_storage, StorageContext
  from llama_index.core.node_parser import SentenceSplitter
  from llama_index.core.tools import QueryEngineTool, ToolMetadata
  from llama_index.core import VectorStoreIndex
  import os
  
  node_parser = SentenceSplitter()
  
  query_engine_tools = []
  
  for idx, wiki_title in enumerate(wiki_titles):
      nodes = node_parser.get_nodes_from_documents(city_docs[wiki_title])
  
      if not os.path.exists(f"./data/{wiki_title}"):
          vector_index = VectorStoreIndex(
              nodes, service_context=service_context, callback_manager=callback_manager
          )
          vector_index.storage_context.persist(persist_dir=f"./data/{wiki_title}")
      else:
          vector_index = load_index_from_storage(
              StorageContext.from_defaults(persist_dir=f"./data/{wiki_title}"),
              service_context=service_context,
              callback_manager=callback_manager,
          )
      vector_query_engine = vector_index.as_query_engine()
  
      query_engine_tools.append(
          QueryEngineTool(
              query_engine=vector_query_engine,
              metadata=ToolMetadata(
                  name=f"vector_tool_{wiki_title}",
                  description=(
                      "Useful for questions related to specific aspects of"
                      f" {wiki_title} (e.g. the history, arts and culture,"
                      " sports, demographics, or more)."
                  ),
              ),
          )
      )
  
  
  
  from llama_index.core.agent import AgentRunner
  from llama_index.llms.openai import OpenAI
  
  llm = OpenAI(model="gpt-4")
  agent_worker = LLMCompilerAgentWorker.from_tools(
      query_engine_tools,
      llm=llm,
      verbose=True,
      callback_manager=callback_manager,
  )
  agent = AgentRunner(agent_worker, callback_manager=callback_manager)
  
  
  
  response = agent.chat(
      "Tell me about the demographics of Miami, and compare that with the demographics of Chicago?"
  )
  print(str(response))
  
  
  response = agent.chat(
      "Is the climate of Chicago or Seattle better during the wintertime?"
  )
  print(str(response))
apis: llama_index.core.StorageContext.from_defaults llama_index.core.callbacks.CallbackManager llama_index.core.ServiceContext.from_defaults llama_index.core.node_parser.SentenceSplitter llama_index.core.VectorStoreIndex llama_index.core.tools.FunctionTool.from_defaults llama_index.core.llama_pack.download_llama_pack llama_index.llms.openai.OpenAI llama_index.readers.wikipedia.WikipediaReader llama_index.core.tools.ToolMetadata llama_index.core.agent.AgentRunner
extract_api: [((266, 281), 'phoenix.launch_app', 'px.launch_app', ([], {}), '()\n', (279, 281), True, 'import phoenix as px\n'), ((385, 405), 'nest_asyncio.apply', 'nest_asyncio.apply', ([], {}), '()\n', (403, 405), False, 'import nest_asyncio\n'), ((549, 624), 'llama_index.core.llama_pack.download_llama_pack', 'download_llama_pack', (['"""LLMCompilerAgentPack"""', '"""./agent_pack"""'], {'skip_load': '(True)'}), "('LLMCompilerAgentPack', './agent_pack', skip_load=True)\n", (568, 624), False, 'from llama_index.core.llama_pack import download_llama_pack\n'), ((910, 930), 'nest_asyncio.apply', 'nest_asyncio.apply', ([], {}), '()\n', (928, 930), False, 'import nest_asyncio\n'), ((1069, 1108), 'llama_index.core.tools.FunctionTool.from_defaults', 'FunctionTool.from_defaults', ([], {'fn': 'multiply'}), '(fn=multiply)\n', (1095, 1108), False, 'from llama_index.core.tools import BaseTool, FunctionTool\n'), ((1231, 1265), 'llama_index.core.tools.FunctionTool.from_defaults', 'FunctionTool.from_defaults', ([], {'fn': 'add'}), '(fn=add)\n', (1257, 1265), False, 'from llama_index.core.tools import BaseTool, FunctionTool\n'), ((1398, 1419), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'model': '"""gpt-4"""'}), "(model='gpt-4')\n", (1404, 1419), False, 'from llama_index.llms.openai import OpenAI\n'), ((1479, 1581), 'agent_pack.step.LLMCompilerAgentWorker.from_tools', 'LLMCompilerAgentWorker.from_tools', (['tools'], {'llm': 'llm', 'verbose': '(True)', 'callback_manager': 'callback_manager'}), '(tools, llm=llm, verbose=True,\n    callback_manager=callback_manager)\n', (1512, 1581), False, 'from agent_pack.step import LLMCompilerAgentWorker\n'), ((1592, 1652), 'llama_index.core.agent.AgentRunner', 'AgentRunner', (['agent_worker'], {'callback_manager': 'callback_manager'}), '(agent_worker, callback_manager=callback_manager)\n', (1603, 1652), False, 'from llama_index.core.agent import AgentRunner\n'), ((1966, 1983), 'llama_index.readers.wikipedia.WikipediaReader', 'WikipediaReader', ([], {}), '()\n', (1981, 1983), False, 'from llama_index.readers.wikipedia import WikipediaReader\n'), ((2248, 2284), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'temperature': '(0)', 'model': '"""gpt-4"""'}), "(temperature=0, model='gpt-4')\n", (2254, 2284), False, 'from llama_index.llms.openai import OpenAI\n'), ((2303, 2340), 'llama_index.core.ServiceContext.from_defaults', 'ServiceContext.from_defaults', ([], {'llm': 'llm'}), '(llm=llm)\n', (2331, 2340), False, 'from llama_index.core import ServiceContext\n'), ((2360, 2379), 'llama_index.core.callbacks.CallbackManager', 'CallbackManager', (['[]'], {}), '([])\n', (2375, 2379), False, 'from llama_index.core.callbacks import CallbackManager\n'), ((2646, 2664), 'llama_index.core.node_parser.SentenceSplitter', 'SentenceSplitter', ([], {}), '()\n', (2662, 2664), False, 'from llama_index.core.node_parser import SentenceSplitter\n'), ((3946, 3967), 'llama_index.llms.openai.OpenAI', 'OpenAI', ([], {'model': '"""gpt-4"""'}), "(model='gpt-4')\n", (3952, 3967), False, 'from llama_index.llms.openai import OpenAI\n'), ((3983, 4098), 'agent_pack.step.LLMCompilerAgentWorker.from_tools', 'LLMCompilerAgentWorker.from_tools', (['query_engine_tools'], {'llm': 'llm', 'verbose': '(True)', 'callback_manager': 'callback_manager'}), '(query_engine_tools, llm=llm, verbose=True,\n    callback_manager=callback_manager)\n', (4016, 4098), False, 'from agent_pack.step import LLMCompilerAgentWorker\n'), ((4122, 4182), 'llama_index.core.agent.AgentRunner', 'AgentRunner', (['agent_worker'], {'callback_manager': 'callback_manager'}), '(agent_worker, callback_manager=callback_manager)\n', (4133, 4182), False, 'from llama_index.core.agent import AgentRunner\n'), ((2822, 2860), 'os.path.exists', 'os.path.exists', (['f"""./data/{wiki_title}"""'], {}), "(f'./data/{wiki_title}')\n", (2836, 2860), False, 'import os\n'), ((2885, 2981), 'llama_index.core.VectorStoreIndex', 'VectorStoreIndex', (['nodes'], {'service_context': 'service_context', 'callback_manager': 'callback_manager'}), '(nodes, service_context=service_context, callback_manager=\n    callback_manager)\n', (2901, 2981), False, 'from llama_index.core import VectorStoreIndex\n'), ((3150, 3214), 'llama_index.core.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'persist_dir': 'f"""./data/{wiki_title}"""'}), "(persist_dir=f'./data/{wiki_title}')\n", (3178, 3214), False, 'from llama_index.core import load_index_from_storage, StorageContext\n'), ((3499, 3705), 'llama_index.core.tools.ToolMetadata', 'ToolMetadata', ([], {'name': 'f"""vector_tool_{wiki_title}"""', 'description': 'f"""Useful for questions related to specific aspects of {wiki_title} (e.g. the history, arts and culture, sports, demographics, or more)."""'}), "(name=f'vector_tool_{wiki_title}', description=\n    f'Useful for questions related to specific aspects of {wiki_title} (e.g. the history, arts and culture, sports, demographics, or more).'\n    )\n", (3511, 3705), False, 'from llama_index.core.tools import QueryEngineTool, ToolMetadata\n')]