{"cells": [{"cell_type": "markdown", "id": "b7973eef-7e5e-4ca4-844a-1d016f5a638b", "metadata": {}, "source": ["# 构建自定义Agent\n", "\n", "在本手册中,我们将向您展示如何使用LlamaIndex构建自定义Agent。\n", "\n", "构建自定义Agent的最简单方法是简单地对`CustomSimpleAgentWorker`进行子类化,并实现一些必需的函数。您可以完全灵活地定义Agent的逐步逻辑。\n", "\n", "这使您能够在RAG管道的基础上添加任意复杂的推理逻辑。\n", "\n", "我们将向您展示如何构建一个简单的Agent,它在RouterQueryEngine的基础上添加了一个重试层,使其可以重试查询直到任务完成。我们将其构建在SQL工具和向量索引查询工具的基础之上。即使工具出现错误或只回答了问题的一部分,Agent也可以继续重试问题直到任务完成。\n", "\n", "**注意:** 任何文本到SQL应用程序都应意识到执行任意SQL查询可能存在安全风险。建议采取必要的预防措施,例如使用受限角色、只读数据库、沙盒等。\n"]}, {"cell_type": "markdown", "id": "0fcf8f1d-9e41-434e-82a1-75471eb85275", "metadata": {}, "source": ["## 设置自定义Agent\n", "\n", "这里我们设置自定义Agent。\n", "\n", "### 复习\n", "\n", "在LlamaIndex中,一个Agent包括Agent运行器和Agent工作者。Agent运行器是一个编排者,负责存储像内存这样的状态,而Agent工作者控制任务的逐步执行。Agent运行器包括顺序执行和并行执行。更多细节可以在我们的[低级API指南](https://docs.llamaindex.ai/en/latest/module_guides/deploying/agents/agent_runner.html)中找到。\n", "\n", "大多数核心Agent逻辑(例如ReAct,函数调用循环)可以在Agent工作者中执行。因此,我们已经很容易地将Agent工作者设置为子类,让您可以将其插入到任何Agent运行器中。\n", "\n", "### 创建自定义Agent工作者子类\n", "\n", "如上所述,我们将`CustomSimpleAgentWorker`设置为子类。这是一个已经为您设置了一些脚手架的类。这包括能够接收工具、回调、LLM,并确保状态/步骤被正确格式化。与此同时,您主要需要实现以下函数:\n", "\n", "- `_initialize_state`\n", "- `_run_step`\n", "- `_finalize_task`\n", "\n", "一些额外的注意事项:\n", "- 如果您希望支持Agent中的异步聊天,也可以实现`_arun_step`。\n", "- 只要将所有剩余的args、kwargs传递给`super()`,您可以选择重写`__init__`。\n", "- `CustomSimpleAgentWorker`被实现为Pydantic的`BaseModel`,这意味着您也可以定义自己的自定义属性。\n", "\n", "以下是每个`CustomSimpleAgentWorker`上的完整基本属性集(在构建自定义Agent时需要/可以传递的):\n", "- `tools: Sequence[BaseTool]`\n", "- `tool_retriever: Optional[ObjectRetriever[BaseTool]]`\n", "- `llm: LLM`\n", "- `callback_manager: CallbackManager`\n", "- `verbose: bool`\n", "\n", "请注意,`tools`和`tool_retriever`是互斥的,您只能传递其中一个(例如,定义一个静态工具列表或定义一个可调用函数,在给定用户消息时返回相关工具)。您可以调用`get_tools(message: str)`来返回给定消息的相关工具。\n", "\n", "在定义自定义Agent时,所有这些属性都可以通过`self`访问。\n"]}, {"cell_type": "code", "execution_count": null, "id": "3b26364e", "metadata": {}, "outputs": [], "source": ["%pip install llama-index-readers-wikipedia\n", "%pip install llama-index-llms-openai"]}, {"cell_type": "code", "execution_count": null, "id": "0aacdcad-f0c1-40f4-b319-6c4cf3b309c7", "metadata": {}, "outputs": [], "source": ["from llama_index.core.agent import (\n", " CustomSimpleAgentWorker,\n", " Task,\n", " AgentChatResponse,\n", ")\n", "from typing import Dict, Any, List, Tuple, Optional\n", "from llama_index.core.tools import BaseTool, QueryEngineTool\n", "from llama_index.core.program import LLMTextCompletionProgram\n", "from llama_index.core.output_parsers import PydanticOutputParser\n", "from llama_index.core.query_engine import RouterQueryEngine\n", "from llama_index.core import ChatPromptTemplate, PromptTemplate\n", "from llama_index.core.selectors import PydanticSingleSelector\n", "from llama_index.core.bridge.pydantic import Field, BaseModel"]}, {"cell_type": "markdown", "id": "8b5280cc-1a53-4aba-9745-03b9f9fcd864", "metadata": {}, "source": ["在这里,我们定义了一些辅助变量和方法。例如,用于检测错误的提示模板以及在Pydantic中使用的响应格式。\n"]}, {"cell_type": "code", "execution_count": null, "id": "91db5f4f-06d8-46a0-af3e-cfa9b7c096f8", "metadata": {}, "outputs": [], "source": ["DEFAULT_PROMPT_STR = \"\"\"", "给定先前的问题/响应对,请确定响应中是否发生了错误,并建议一个修改后的问题,不会触发错误。", "", "修改后的问题示例:", "- 修改问题本身以引出非错误响应", "- 用上下文补充问题,以帮助下游系统更好地回答问题。", "- 用负面响应的示例或其他负面问题来补充问题。", "", "错误意味着要么触发了异常,要么响应与问题完全不相关。", "", "请以以下JSON格式返回对响应的评估。", "", "\"\"\"", "", "", "def get_chat_prompt_template(", " system_prompt: str, current_reasoning: Tuple[str, str]", ") -> ChatPromptTemplate:", " system_msg = ChatMessage(role=MessageRole.SYSTEM, content=system_prompt)", " messages = [system_msg]", " for raw_msg in current_reasoning:", " if raw_msg[0] == \"user\":", " messages.append(", " ChatMessage(role=MessageRole.USER, content=raw_msg[1])", " )", " else:", " messages.append(", " ChatMessage(role=MessageRole.ASSISTANT, content=raw_msg[1])", " )", " return ChatPromptTemplate(message_templates=messages)", "", "", "class ResponseEval(BaseModel):", " \"\"\"评估响应是否存在错误。\"\"\"", "", " has_error: bool = Field(", " ..., description=\"响应是否存在错误。\"", " )", " new_question: str = Field(..., description=\"建议的新问题。\")", " explanation: str = Field(", " ...,", " description=(", " \"错误的解释以及新问题的解释。\"", " \"可以包括直接的堆栈跟踪。\"", " ),", " )"]}, {"cell_type": "code", "execution_count": null, "id": "1421133e-5091-425b-822c-c2c0b8f084c0", "metadata": {}, "outputs": [], "source": ["from llama_index.core.bridge.pydantic import PrivateAttr", "", "", "class RetryAgentWorker(CustomSimpleAgentWorker):", " \"\"\"在路由器顶部添加重试层的代理工作器。", "", " 继续迭代直到没有错误/任务完成。", "", " \"\"\"", "", " prompt_str: str = Field(default=DEFAULT_PROMPT_STR)", " max_iterations: int = Field(default=10)", "", " _router_query_engine: RouterQueryEngine = PrivateAttr()", "", " def __init__(self, tools: List[BaseTool], **kwargs: Any) -> None:", " \"\"\"初始化参数。\"\"\"", " # 验证所有工具是否为查询引擎工具", " for tool in tools:", " if not isinstance(tool, QueryEngineTool):", " raise ValueError(", " f\"工具 {tool.metadata.name} 不是查询引擎工具。\"", " )", " self._router_query_engine = RouterQueryEngine(", " selector=PydanticSingleSelector.from_defaults(),", " query_engine_tools=tools,", " verbose=kwargs.get(\"verbose\", False),", " )", " super().__init__(", " tools=tools,", " **kwargs,", " )", "", " def _initialize_state(self, task: Task, **kwargs: Any) -> Dict[str, Any]:", " \"\"\"初始化状态。\"\"\"", " return {\"count\": 0, \"current_reasoning\": []}", "", " def _run_step(", " self, state: Dict[str, Any], task: Task, input: Optional[str] = None", " ) -> Tuple[AgentChatResponse, bool]:", " \"\"\"运行步骤。", "", " 返回:", " 代理响应和是否完成的元组", "", " \"\"\"", " if \"new_input\" not in state:", " new_input = task.input", " else:", " new_input = state[\"new_input\"]", "", " # 首先运行路由器查询引擎", " response = self._router_query_engine.query(new_input)", "", " # 追加到当前推理", " state[\"current_reasoning\"].extend(", " [(\"user\", new_input), (\"assistant\", str(response))]", " )", "", " # 然后,检查错误", " # 根据模板动态创建用于结构化输出提取的pydantic程序", " chat_prompt_tmpl = get_chat_prompt_template(", " self.prompt_str, state[\"current_reasoning\"]", " )", " llm_program = LLMTextCompletionProgram.from_defaults(", " output_parser=PydanticOutputParser(output_cls=ResponseEval),", " prompt=chat_prompt_tmpl,", " llm=self.llm,", " )", " # 运行程序,查看结果", " response_eval = llm_program(", " query_str=new_input, response_str=str(response)", " )", " if not response_eval.has_error:", " is_done = True", " else:", " is_done = False", " state[\"new_input\"] = response_eval.new_question", "", " if self.verbose:", " print(f\"> 问题:{new_input}\")", " print(f\"> 响应:{response}\")", " print(f\"> 响应评估:{response_eval.dict()}\")", "", " # 返回响应", " return AgentChatResponse(response=str(response)), is_done", "", " def _finalize_task(self, state: Dict[str, Any], **kwargs) -> None:", " \"\"\"完成任务。\"\"\"", " # 这里没有需要完成的内容", " # 通常用于修改任何内部状态,超出`_initialize_state`中设置的内容", " pass"]}, {"cell_type": "markdown", "id": "60740ffe-8791-4723-b0e8-2d67487a2e84", "metadata": {}, "source": ["## 设置数据和工具\n", "\n", "我们为每个城市设置了SQL工具和向量索引工具。\n"]}, {"cell_type": "code", "execution_count": null, "id": "0146889a-0c15-48a0-8fe0-e4434c32f17d", "metadata": {}, "outputs": [], "source": ["from llama_index.core.tools import QueryEngineTool"]}, {"cell_type": "markdown", "id": "ff905ae1-a068-4d2e-8ad3-44d625aa0947", "metadata": {}, "source": ["### 设置SQL数据库 + 工具\n"]}, {"cell_type": "code", "execution_count": null, "id": "b866150c-deab-4ba6-b735-a77f662d33ff", "metadata": {}, "outputs": [], "source": ["from sqlalchemy import (", " create_engine, # 创建引擎", " MetaData, # 元数据", " Table, # 表", " Column, # 列", " String, # 字符串", " Integer, # 整数", " select, # 查询", " column, # 列", ")", "from llama_index.core import SQLDatabase # 导入SQLDatabase类", "", "engine = create_engine(\"sqlite:///:memory:\", future=True) # 创建引擎", "metadata_obj = MetaData() # 创建元数据对象", "# 创建城市SQL表", "table_name = \"city_stats\"", "city_stats_table = Table(", " table_name,", " metadata_obj,", " Column(\"city_name\", String(16), primary_key=True), # 城市名称", " Column(\"population\", Integer), # 人口", " Column(\"country\", String(16), nullable=False), # 国家", ")", "", "metadata_obj.create_all(engine) # 在引擎上创建所有表"]}, {"cell_type": "code", "execution_count": null, "id": "fbba0868-198a-417d-82cd-0d36911b7172", "metadata": {}, "outputs": [], "source": ["from sqlalchemy import insert\n", "\n", "rows = [\n", " {\"city_name\": \"Toronto\", \"population\": 2930000, \"country\": \"Canada\"},\n", " {\"city_name\": \"Tokyo\", \"population\": 13960000, \"country\": \"Japan\"},\n", " {\"city_name\": \"Berlin\", \"population\": 3645000, \"country\": \"Germany\"},\n", "]\n", "for row in rows:\n", " stmt = insert(city_stats_table).values(**row)\n", " with engine.begin() as connection:\n", " cursor = connection.execute(stmt)"]}, {"cell_type": "code", "execution_count": null, "id": "1639ad45-7c4b-4028-a192-e5d2fd64afcf", "metadata": {}, "outputs": [], "source": ["from llama_index.core.query_engine import NLSQLTableQueryEngine\n", "\n", "sql_database = SQLDatabase(engine, include_tables=[\"city_stats\"])\n", "sql_query_engine = NLSQLTableQueryEngine(\n", " sql_database=sql_database, tables=[\"city_stats\"], verbose=True\n", ")\n", "sql_tool = QueryEngineTool.from_defaults(\n", " query_engine=sql_query_engine,\n", " description=(\n", " \"Useful for translating a natural language query into a SQL query over\"\n", " \" a table containing: city_stats, containing the population/country of\"\n", " \" each city\"\n", " ),\n", ")"]}, {"cell_type": "markdown", "id": "fab3526c-f73a-4403-b1a4-5893e85a88af", "metadata": {}, "source": ["### 设置矢量工具\n", "\n", "\n"]}, {"cell_type": "code", "execution_count": null, "id": "48e32ccc-2aca-4884-9da2-bf20a07351b7", "metadata": {}, "outputs": [], "source": ["from llama_index.readers.wikipedia import WikipediaReader\n", "from llama_index.core import VectorStoreIndex"]}, {"cell_type": "code", "execution_count": null, "id": "86e2ff7c-a891-48df-a153-3dcaead671d0", "metadata": {}, "outputs": [], "source": ["cities = [\"Toronto\", \"Berlin\", \"Tokyo\"]\n", "wiki_docs = WikipediaReader().load_data(pages=cities)"]}, {"cell_type": "code", "execution_count": null, "id": "a9377221-dab4-4a8e-b5d1-3d76d5bb976d", "metadata": {}, "outputs": [], "source": ["# 为每个城市构建一个单独的向量索引", "# 您也可以选择定义一个跨所有文档的单个向量索引,并通过元数据为每个块进行注释", "vector_tools = []", "for city, wiki_doc in zip(cities, wiki_docs):", " vector_index = VectorStoreIndex.from_documents([wiki_doc])", " vector_query_engine = vector_index.as_query_engine()", " vector_tool = QueryEngineTool.from_defaults(", " query_engine=vector_query_engine,", " description=f\"用于回答关于{city}的语义问题\",", " )", " vector_tools.append(vector_tool)"]}, {"cell_type": "markdown", "id": "62b3286f-ef5c-4c6c-8764-0b42e6181345", "metadata": {}, "source": ["```python\n", "import random\n", "\n", "class CustomAgent:\n", " def __init__(self, actions):\n", " self.actions = actions\n", "\n", " def get_action(self, state):\n", " return random.choice(self.actions)\n", "```\n"]}, {"cell_type": "code", "execution_count": null, "id": "db3992b5-f433-44a5-8d97-fb0502143c80", "metadata": {}, "outputs": [], "source": ["from llama_index.llms.openai import OpenAI"]}, {"cell_type": "code", "execution_count": null, "id": "7824b9d2-185c-4d79-bdaa-c227b4a004f3", "metadata": {}, "outputs": [], "source": ["llm = OpenAI(model=\"gpt-4\")\n", "callback_manager = llm.callback_manager\n", "\n", "query_engine_tools = [sql_tool] + vector_tools\n", "agent_worker = RetryAgentWorker.from_tools(\n", " query_engine_tools,\n", " llm=llm,\n", " verbose=True,\n", " callback_manager=callback_manager,\n", ")\n", "agent = agent_worker.as_agent(callback_manager=callback_manager)"]}, {"cell_type": "markdown", "id": "c0d6798c-a33c-455a-b4e4-fb0bb4208435", "metadata": {}, "source": ["## 尝试一些查询\n"]}, {"cell_type": "code", "execution_count": null, "id": "28ae5efa-378b-4e0c-9b0b-72a460ada9fb", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[1;3;38;5;200mSelecting query engine 0: The choice is about translating a natural language query into a SQL query over a table containing city_stats, which likely includes information about the country of each city..\n", "\u001b[0m> Table desc str: Table 'city_stats' has columns: city_name (VARCHAR(16)), population (INTEGER), country (VARCHAR(16)), and foreign keys: .\n", "> Predicted SQL query: SELECT city_name, country FROM city_stats\n", "> Question: Which countries are each city from?\n", "> Response: The city of Toronto is from Canada, Tokyo is from Japan, and Berlin is from Germany.\n", "> Response eval: {'has_error': True, 'new_question': 'Which country is each of the following cities from: Toronto, Tokyo, Berlin?', 'explanation': 'The original question was too vague as it did not specify which cities the question was referring to. The new question provides specific cities for which the country of origin is being asked.'}\n", "\u001b[1;3;38;5;200mSelecting query engine 0: This choice is relevant because it mentions a table containing city_stats, which likely includes information about the country of each city..\n", "\u001b[0m> Table desc str: Table 'city_stats' has columns: city_name (VARCHAR(16)), population (INTEGER), country (VARCHAR(16)), and foreign keys: .\n", "> Predicted SQL query: SELECT city_name, country\n", "FROM city_stats\n", "WHERE city_name IN ('Toronto', 'Tokyo', 'Berlin')\n", "> Question: Which country is each of the following cities from: Toronto, Tokyo, Berlin?\n", "> Response: Toronto is from Canada, Tokyo is from Japan, and Berlin is from Germany.\n", "> Response eval: {'has_error': False, 'new_question': '', 'explanation': ''}\n", "Toronto is from Canada, Tokyo is from Japan, and Berlin is from Germany.\n"]}], "source": ["response = agent.chat(\"Which countries are each city from?\")\n", "print(str(response))"]}, {"cell_type": "code", "execution_count": null, "id": "7300fe97-deb3-44ca-a375-2c14b173d17e", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[1;3;38;5;200mSelecting query engine 0: The question is asking about the top modes of transportation for the city with the highest population. Choice (1) is the most relevant because it mentions a table containing city_stats, which likely includes information about the population of each city..\n", "\u001b[0m> Table desc str: Table 'city_stats' has columns: city_name (VARCHAR(16)), population (INTEGER), country (VARCHAR(16)), and foreign keys: .\n", "> Predicted SQL query: SELECT city_name, population, mode_of_transportation\n", "FROM city_stats\n", "WHERE population = (SELECT MAX(population) FROM city_stats)\n", "ORDER BY mode_of_transportation ASC\n", "LIMIT 5;\n", "> Question: What are the top modes of transporation fo the city with the higehest population?\n", "> Response: I'm sorry, but there was an error in retrieving the information. Please try again later.\n", "> Response eval: {'has_error': True, 'new_question': 'What are the top modes of transportation for the city with the highest population?', 'explanation': 'The original question had spelling errors which might have caused the system to not understand the question correctly. The corrected question should now be clear and understandable for the system.'}\n", "\u001b[1;3;38;5;200mSelecting query engine 0: The first choice is the most relevant because it mentions translating a natural language query into a SQL query over a table containing city_stats, which likely includes information about the population of each city..\n", "\u001b[0m> Table desc str: Table 'city_stats' has columns: city_name (VARCHAR(16)), population (INTEGER), country (VARCHAR(16)), and foreign keys: .\n", "> Predicted SQL query: SELECT city_name, population, country\n", "FROM city_stats\n", "WHERE population = (SELECT MAX(population) FROM city_stats)\n", "> Question: What are the top modes of transportation for the city with the highest population?\n", "> Response: The city with the highest population is Tokyo, Japan with a population of 13,960,000.\n", "> Response eval: {'has_error': True, 'new_question': 'What are the top modes of transportation for Tokyo, Japan?', 'explanation': 'The assistant failed to answer the original question correctly. The response was about the city with the highest population, but it did not mention anything about the top modes of transportation in that city. The new question directly asks about the top modes of transportation in Tokyo, Japan, which is the city with the highest population.'}\n", "\u001b[1;3;38;5;200mSelecting query engine 3: The question specifically asks about Tokyo, and choice (4) is about answering semantic questions about Tokyo..\n", "\u001b[0m> Question: What are the top modes of transportation for Tokyo, Japan?\n", "> Response: The top modes of transportation for Tokyo, Japan are trains and subways, which are considered clean and efficient. Tokyo has an extensive network of electric train lines and over 900 train stations. Buses, monorails, and trams also play a secondary role in the public transportation system. Additionally, expressways connect Tokyo to other points in the Greater Tokyo Area and beyond. Taxis and long-distance ferries are also available for transportation within the city and to the surrounding islands.\n", "> Response eval: {'has_error': True, 'new_question': 'What are the top modes of transportation for Tokyo, Japan?', 'explanation': 'The original question was not answered correctly because the assistant did not provide information on the top modes of transportation for the city with the highest population. The new question directly asks for the top modes of transportation for Tokyo, Japan, which is the city with the highest population.'}\n", "\u001b[1;3;38;5;200mSelecting query engine 3: Tokyo is mentioned in choice 4.\n", "\u001b[0m> Question: What are the top modes of transportation for Tokyo, Japan?\n", "> Response: The top modes of transportation for Tokyo, Japan are trains and subways, which are considered clean and efficient. Tokyo has an extensive network of electric train lines and over 900 train stations. Buses, monorails, and trams also play a secondary role in public transportation within the city. Additionally, Tokyo has two major airports, Narita International Airport and Haneda Airport, which offer domestic and international flights. Expressways and taxis are also available for transportation within the city.\n", "> Response eval: {'has_error': True, 'new_question': 'What are the top modes of transportation for Tokyo, Japan?', 'explanation': 'The response is erroneous because it does not answer the question asked. The question asks for the top modes of transportation in the city with the highest population, but the response only provides the population of the city. The new question directly asks for the top modes of transportation in Tokyo, Japan, which is the city with the highest population.'}\n", "\u001b[1;3;38;5;200mSelecting query engine 3: The question specifically asks about Tokyo, and choice 4 is about answering semantic questions about Tokyo..\n", "\u001b[0m> Question: What are the top modes of transportation for Tokyo, Japan?\n", "> Response: The top modes of transportation for Tokyo, Japan are trains and subways, which are considered clean and efficient. Tokyo has an extensive network of electric train lines and over 900 train stations. Buses, monorails, and trams also play a secondary role in public transportation within the city. Additionally, Tokyo has two major airports, Narita International Airport and Haneda Airport, which offer domestic and international flights. Expressways and taxis are also available for transportation within the city.\n", "> Response eval: {'has_error': False, 'new_question': '', 'explanation': ''}\n", "The top modes of transportation for Tokyo, Japan are trains and subways, which are considered clean and efficient. Tokyo has an extensive network of electric train lines and over 900 train stations. Buses, monorails, and trams also play a secondary role in public transportation within the city. Additionally, Tokyo has two major airports, Narita International Airport and Haneda Airport, which offer domestic and international flights. Expressways and taxis are also available for transportation within the city.\n"]}], "source": ["response = agent.chat(\n", " \"What are the top modes of transporation fo the city with the higehest population?\"\n", ")\n", "print(str(response))"]}, {"cell_type": "code", "execution_count": null, "id": "6b98fa9c-123d-4347-a696-81148d48bc4c", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["The top modes of transportation for Tokyo, Japan are trains and subways, which are considered clean and efficient. Tokyo has an extensive network of electric train lines and over 900 train stations. Buses, monorails, and trams also play a secondary role in public transportation within the city. Additionally, Tokyo has two major airports, Narita International Airport and Haneda Airport, which offer domestic and international flights. Expressways and taxis are also available for transportation within the city.\n"]}], "source": ["print(str(response))"]}, {"cell_type": "code", "execution_count": null, "id": "926b79ba-1868-46ca-bb7e-2bd3c907773c", "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["\u001b[1;3;38;5;200mSelecting query engine 3: The question is asking about sports teams in Asia, and Tokyo is located in Asia..\n", "\u001b[0m> Question: What are the sports teams of each city in Asia?\n", "> Response: I'm sorry, but the context information does not provide a comprehensive list of sports teams in each city in Asia. It only mentions some sports teams in Tokyo, Japan. To get a complete list of sports teams in each city in Asia, you would need to consult a reliable source or conduct further research.\n", "> Response eval: {'has_error': True, 'new_question': 'What are some popular sports teams in Tokyo, Japan?', 'explanation': 'The original question is too broad and requires extensive data that the system may not possess. The new question is more specific and focuses on a single city, making it more likely to receive a correct and comprehensive answer.'}\n", "\u001b[1;3;38;5;200mSelecting query engine 3: The question specifically asks about Tokyo, and choice 4 is about answering semantic questions about Tokyo..\n", "\u001b[0m> Question: What are some popular sports teams in Tokyo, Japan?\n", "> Response: Some popular sports teams in Tokyo, Japan include the Yomiuri Giants and Tokyo Yakult Swallows in baseball, F.C. Tokyo and Tokyo Verdy 1969 in soccer, and Hitachi SunRockers, Toyota Alvark Tokyo, and Tokyo Excellence in basketball. Tokyo is also known for its sumo wrestling tournaments held at the Ryōgoku Kokugikan sumo arena.\n", "> Response eval: {'has_error': False, 'new_question': '', 'explanation': ''}\n", "Some popular sports teams in Tokyo, Japan include the Yomiuri Giants and Tokyo Yakult Swallows in baseball, F.C. Tokyo and Tokyo Verdy 1969 in soccer, and Hitachi SunRockers, Toyota Alvark Tokyo, and Tokyo Excellence in basketball. Tokyo is also known for its sumo wrestling tournaments held at the Ryōgoku Kokugikan sumo arena.\n"]}], "source": ["response = agent.chat(\"What are the sports teams of each city in Asia?\")\n", "print(str(response))"]}], "metadata": {"kernelspec": {"display_name": "llama_index_v2", "language": "python", "name": "llama_index_v2"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3"}}, "nbformat": 4, "nbformat_minor": 5}