Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛Fix: ensure runnability of samples/agents #313

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions samples/agents/bedrock_agent_langgraph.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,23 @@
"metadata": {},
"outputs": [],
"source": [
"import boto3\n",
"import json\n",
"import time\n",
"import uuid\n",
"\n",
"import boto3\n",
"\n",
"\n",
"def _create_agent_role(\n",
" agent_region,\n",
" foundation_model\n",
" agent_region,\n",
" foundation_model,\n",
") -> str:\n",
" \"\"\"\n",
" Create agent resource role prior to creation of agent, at this point we do not have agentId, keep it as wildcard\n",
"\n",
" Args:\n",
" agent_region: AWS region in which is the Agent if available\n",
" foundational_model: The model used for inference in AWS BedrockAgents\n",
" foundation_model: The model used for inference in AWS BedrockAgents\n",
" Returns:\n",
" Agent execution role arn\n",
" \"\"\"\n",
Expand Down Expand Up @@ -188,7 +190,9 @@
" ]\n",
" }\n",
" role_name = f'bedrock_agent_{uuid.uuid4()}'\n",
"\n",
" iam_client = boto3.client('iam')\n",
"\n",
" response = iam_client.create_role(\n",
" RoleName=role_name,\n",
" AssumeRolePolicyDocument=assume_role_policy_document,\n",
Expand All @@ -205,9 +209,13 @@
" except Exception as exception:\n",
" raise exception\n",
"\n",
"session = boto3.session.Session()\n",
"region_name = session.region_name\n",
"\n",
"agent_resource_role_arn = _create_agent_role(\n",
" agent_region='us-west-2',\n",
" foundation_model=foundation_model)\n",
" agent_region=region_name,\n",
" foundation_model=foundation_model,\n",
")\n",
"\n",
"agent_resource_role_arn"
]
Expand All @@ -229,14 +237,15 @@
"source": [
"from langchain_aws.agents import BedrockAgentsRunnable\n",
"\n",
"\n",
"agent = BedrockAgentsRunnable.create_agent(\n",
" agent_name=\"langgraph_interest_rate_agent\",\n",
" agent_resource_role_arn=agent_resource_role_arn,\n",
" foundation_model=foundational_model,\n",
" instruction=\"\"\"\n",
" You are an agent who helps with getting the mortgage rate based on the current asset valuation\"\"\",\n",
" tools=tools,\n",
" )\n",
" agent_name=\"langgraph_interest_rate_agent\",\n",
" agent_resource_role_arn=agent_resource_role_arn,\n",
" foundation_model=foundation_model,\n",
" instruction=\"\"\"\n",
" You are an agent who helps with getting the mortgage rate based on the current asset valuation\"\"\",\n",
" tools=tools,\n",
")\n",
"agent"
]
},
Expand All @@ -256,10 +265,12 @@
"outputs": [],
"source": [
"import operator\n",
"from typing import TypedDict, Annotated, Tuple\n",
"from typing import TypedDict, Annotated\n",
"from typing import Union\n",
"\n",
"from langchain_aws.agents import BedrockAgentAction, BedrockAgentFinish\n",
"\n",
"\n",
"class AgentState(TypedDict):\n",
" \"\"\"\n",
" Defines the state for the Graph\n",
Expand Down Expand Up @@ -287,20 +298,21 @@
"metadata": {},
"outputs": [],
"source": [
"from langgraph.prebuilt.tool_executor import ToolExecutor\n",
"from langgraph.prebuilt import ToolNode\n",
"\n",
"\n",
"# the agent node\n",
"def run_agent(data):\n",
" agent_outcome = agent.invoke(data)\n",
" return {\"output\": agent_outcome}\n",
"\n",
"# the tools node\n",
"tool_executor = ToolExecutor(tools)\n",
"tool_node = ToolNode(tools)\n",
"\n",
"def execute_tools(data):\n",
" # Get the most recent output - this is the key added in the `agent` above\n",
" agent_action = data[\"output\"]\n",
" output = tool_executor.invoke(agent_action[0])\n",
" output = tool_node.invoke(agent_action[0])\n",
" tuple_output = agent_action[0], output\n",
" print(f\"Tuple output is {tuple_output}\")\n",
" return {\"intermediate_steps\": [tuple_output]}"
Expand Down
Loading