langchain-ai / langchain-aws

Build LangChain Applications on AWS
MIT License
63 stars 46 forks source link

[WIP] Bedrock Agents Runnable #91

Open 3coins opened 1 week ago

3coins commented 1 week ago

Description

This PR introduces a new Bedrock Agents Runnable that allows using Bedrock Agents with return of control functions as tools.

Usage

Define tools

from langchain_core.tools import tool

@tool("AssetDetail::getAssetValue")
def getAssetValue(asset_holder_id: str = " ") -> str:
    """Get the asset value for an owner id"""
    return f"The total asset value for {asset_holder_id} is 100K"

@tool
def getMortgageRate(asset_holder_id: str = " ", asset_value: str = " ") -> str:
    """Get the mortgage rate based on asset value"""
    return (
        f"The mortgage rate for {asset_holder_id} "
        f"with asset value of {asset_value} is 8.87%"
    )

Create the agent and use with AgentExecutor

from langchain.agents import AgentExecutor

agent = BedrockAgentsRunnable.create_agent(
    {"agent_id": "UKYYJIV1O1", "enable_trace": True}
)
tools = [getAssetValue, getMortgageRate]
agent_executor = AgentExecutor(agent=agent, tools=tools)  # type: ignore[arg-type]

output = agent_executor.invoke(
    {"input": "what is my mortgage rate for id AVC-1234"}
)

assert output["output"] == "The mortgage rate for id AVC-1234 is 8.87%"

Notes

This change follows a strategy similar to the OpenAIAssistantRunnable to create an agent. However, it's still missing the callback related code that should be plugged in before merging. This code also assumes that the agent and the corresponding actions have been setup and prepared to call before calling; this setup could be automated in the agent creation or validation function, but missing here.