Connecting to AWS Bedrock
- Refer Here for langchain aws integration
- Create a new directory
- initialize with uv and add the following packages
uv add langchain python-dotenv langchain-aws
- Open in vscode and select interpretor
- In main.py
from langchain.agents import create_agent
from langchain_aws import ChatBedrockConverse
from langchain.chat_models import init_chat_model
from dotenv import load_dotenv
import os
load_dotenv()
def main():
model = ChatBedrockConverse(
model="us.amazon.nova-lite-v1:0",
)
result = model.invoke("What is capital of France")
result.pretty_print()
if __name__ == "__main__":
main()
- Watch the class room video where we tried interacting with local llm
Creating a Basic Agent in langchain
-
Agent: llm + tools + memory + context
-
The tool in its simplest form in langchain is a function
@tool
def add(a: int|float,b: int|float)-> int | float:
"""Documentation
"""
return a + b
- Langchain gives a very simple way to create an agent
from langchain.agents import create_agent
#from langchain_aws import ChatBedrockConverse
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.chat_models import init_chat_model
from dotenv import load_dotenv
from langchain_core.tools import tool
from dotenv import load_dotenv
import os
load_dotenv()
@tool()
def add(a: int|float, b: int|float) -> int|float:
"""Adds two numbers together.
Args:
a (int | float): number
b (int | float): number
Returns:
int|float: Sum of two numbers
Examples:
>>> add(2, 3)
5
"""
return a + b
@tool()
def subtract(a: int|float, b: int|float) -> int|float:
"""Subtracts b from a.
Args:
a (int | float): number
b (int | float): number
Returns:
int|float: Difference of two numbers
Examples:
>>> subtract(5, 3)
2
"""
return a - b
@tool()
def multiply(a: int|float, b: int|float) -> int|float:
"""Multiplies two numbers together.
Args:
a (int | float): number
b (int | float): number
Returns:
int|float: Product of two numbers
Examples:
>>> multiply(4, 3)
12
"""
return a * b
@tool()
def divide(a: int|float, b: int|float) -> float:
"""Divides a by b.
Args:
a (int | float): numerator
b (int | float): denominator (must not be zero)
Returns:
float: Quotient of a divided by b
Examples:
>>> divide(10, 2)
5.0
"""
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
@tool()
def modulus(a: int|float, b: int|float) -> int|float:
"""Returns the remainder of a divided by b.
Args:
a (int | float): dividend
b (int | float): divisor (must not be zero)
Returns:
int|float: Remainder of a divided by b
Examples:
>>> modulus(10, 3)
1
"""
if b == 0:
raise ValueError("Cannot perform modulus with zero divisor")
return a % b
def main():
model = ChatGoogleGenerativeAI(
model="gemini-3.5-flash",
project=os.getenv('PROJECT_ID')
)
# result = model.invoke("What is capital of France")
# result.pretty_print()
agent = create_agent(
model=model,
tools=[add, subtract, multiply, divide, modulus],
)
result = agent.invoke({
"messages": [
{
"role": "user",
"content": "What is 5 plus 3?"
}
]
})
for message in result['messages']:
message.pretty_print()
if __name__ == "__main__":
main()
