You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" useful for when you need to answer questions about a company's income statement,\n",
" cash flow statement, or balance sheet. This tool can help you extract data points like\n",
" net income, revenue, free cash flow, and total debt, among other financial line items.\n",
"\"\"\"\n",
")"
],
"metadata": {
"id": "yh4iYLeJWEpr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Step 5. Create your custom Agent\n",
"This Agent uses your custom tool(s) to get things done.\n",
"\n",
"For our example, the Agent is given 1 tool (`annual_report_tool` from above) and answers questions about annual reports!\n",
"\n",
"The code here is heavily borrowed from [this wonderful GitHub repository](https://github.com/mpaepper/llm_agents), which is created by [Marc Päpper](https://twitter.com/mpaepper).\n",
"\n",
"Marc wrote an [excellent blog post](https://www.paepper.com/blog/posts/intelligent-agents-guided-by-llms/) that explains how Agents work.\n"
],
"metadata": {
"id": "F0PC0OPbVP7x"
}
},
{
"cell_type": "code",
"source": [
"import re\n",
"\n",
"from pydantic import BaseModel\n",
"from typing import Tuple\n",
"\n",
"class Agent(BaseModel):\n",
" # The large language model that the Agent will use to decide the action to take\n",
" llm: BaseModel\n",
" # The prompt that the language model will use and append previous responses to\n",
" prompt: str\n",
" # The list of tools that the Agent can use\n",
" tools: List[Tool]\n",
" # Adjust this so that the Agent does not loop infinitely\n",
" max_loops: int = 5\n",
" # The stop pattern is used, so the LLM does not hallucinate until the end\n",
" stop_pattern: List[str]\n",
"\n",
" @property\n",
" def tool_by_names(self) -> Dict[str, Tool]:\n",
" return {tool.name: tool for tool in self.tools}\n",
"\n",
" def run(self, question: str):\n",
" name_to_tool_map = {tool.name: tool for tool in self.tools}\n",
"This prompt will be fed into the Agent's large language model (LLM). \n",
"\n",
"As it \"reasons\" and answers your query, the Agent will update this prompt by appending the previous response (context) to the prompt to maintain context of its overall \"chain of thought\"."
],
"metadata": {
"id": "_um0nrAlYcw_"
}
},
{
"cell_type": "code",
"source": [
"FINAL_ANSWER_TOKEN = \"Final Answer:\"\n",
"OBSERVATION_TOKEN = \"Observation:\"\n",
"THOUGHT_TOKEN = \"Thought:\"\n",
"PROMPT_TEMPLATE = \"\"\"Answer the question as best as you can using the following tools: \n",
"\n",
"{tool_description}\n",
"\n",
"Use the following format:\n",
"\n",
"Question: the input question you must answer\n",
"Thought: comment on what you want to do next\n",
"Action: the action to take, exactly one element of [{tool_names}]\n",
"Action Input: the input to the action\n",
"Observation: the result of the action\n",
"... (this Thought/Action/Action Input/Observation repeats N times, use it until you are sure of the answer)\n",
"Thought: I now know the final answer\n",
"Final Answer: your final answer to the original input question\n",
"\n",
"Begin!\n",
"\n",
"Question: {question}\n",
"Thought: {previous_responses}\n",
"\"\"\""
],
"metadata": {
"id": "LXVPO69KZ46c"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Step 7. Run your custom Agent\n",
"You can update the `question` variable to ask your Agent to answer questions about the PDF that you previously loaded!\n"
],
"metadata": {
"id": "f-M4SKq7ZFC2"
}
},
{
"cell_type": "code",
"source": [
"# The tool(s) that your Agent will use\n",
"tools = [annual_report_tool]\n",
"\n",
"# The question that you will ask your Agent\n",
"question = \"What was Meta's net income in 2022? What was net income the year before that?\"\n",
"\n",
"# The prompt that your Agent will use and update as it is \"reasoning\"\n",
"prompt = PROMPT_TEMPLATE.format(\n",
" tool_description=\"\\n\".join([f\"{tool.name}: {tool.description}\" for tool in tools]),\n",
" tool_names=\", \".join([tool.name for tool in tools]),\n",