Repository navigation
LangChain Agents 1 · From a bare model call to an agent with hands #9
akash-coded
started this conversation in
Exercises
Replies: 1 comment
from typing import Any, List
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatResult, ChatGeneration
from langchain.agents import create_agent
from langchain.tools import tool
class ScriptedChatModel(BaseChatModel):
'''Deterministic stand-in. Real framework, scripted replies, no credentials.'''
responses: List[Any]
idx: int = 0
@property
def _llm_type(self) -> str:
return "scripted"
def _generate(self, messages, stop=None, run_manager=None, **kwargs):
reply = self.responses[min(self.idx, len(self.responses) - 1)]
object.__setattr__(self, "idx", self.idx + 1)
return ChatResult(generations=[ChatGeneration(message=reply)])
def bind_tools(self, tools, **kwargs):
return self
@tool
def lookup_pnr(pnr: str) -> str:
'''Return the booking status for a PNR.'''
return {"JX48Q2": "BLR-DEL cancelled, Rao, Gold tier"}.get(pnr, "PNR not found")
# Bare agent: no tools, the loop turns once.
bare = create_agent(
ScriptedChatModel(responses=[AIMessage(content="How can I help?")]),
tools=[],
system_prompt="You are TravelMind.",
)
print("bare:", bare.invoke({"messages": [{"role": "user", "content": "Hi"}]})["messages"][-1].content)
# Tool agent: the model asks for lookup_pnr, the tool answers, the model replies.
tool_agent = create_agent(
ScriptedChatModel(responses=[
AIMessage(content="", tool_calls=[{"name": "lookup_pnr", "args": {"pnr": "JX48Q2"}, "id": "t", "type": "tool_call"}]),
AIMessage(content="JX48Q2 is cancelled. Gold tier, free rebooking."),
]),
tools=[lookup_pnr],
system_prompt="You are TravelMind.",
)
result = tool_agent.invoke({"messages": [{"role": "user", "content": "is JX48Q2 cancelled?"}]})
print("messages:", len(result["messages"])) # 4: human, AI(tool call), tool, AI(answer)
print("final:", result["messages"][-1].content)
# Production swap:
# from langchain_aws import ChatBedrockConverse
# model = ChatBedrockConverse(model="us.anthropic.claude-haiku-4-5-20251001-v1:0", region_name="us-east-1") |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
LangChain Agents · Exercise 1
Language: Python Topics: the agent loop, message types, bare vs tool agents, reading LangChain and Strands syntax Level: Foundational
Answers are letters, pairs like
1-C,T/F, or a sequence. You read code here, you do not write any.Q1 · (2 pts) A language model, stripped to its core, is:
Q2 · Trace the loop (3 pts) Edges are tagged
abcd. Start at the user message.flowchart TD U[user message] -->|a| M[model] M -->|b tool needed| T[run tool] T -->|c| M M -->|d no tool| F[final answer]Right after the tool runs, which edge fires?
aB)bC)cD)dQ3 · One arrow is flipped (3 pts)
flowchart TD U[user message] -->|a| M[model] M -->|b tool needed| T[run tool] T -->|c| U M -->|d no tool| F[final answer]Which tagged edge is wrong?
aB)bC)cD)dQ4 · Read the code (3 pts)
How many messages are in
result["messages"]?Q5 · Read the code (3 pts)
What is the model doing here?
lookup_pnrwithpnr="JX48Q2"Q6 · Match the message to its job (4 pts)
SystemMessageHumanMessageAIMessageToolMessageQ7 · Pick the correct bare agent (2 pts) A bare agent has no tools.
Option A
flowchart LR U[user] --> M[model] --> A[answer]Option B
flowchart LR U[user] --> M[model] --> T[tools node] --> A[answer]Q8 · Order the turn (3 pts) One tool-using turn runs these. Put them in order.
Q9 · Spot the line that does not belong (3 pts) In LangChain 1.0:
Q10 · Same idea, two frameworks (4 pts) You know the left from Strands. Match each to its LangChain twin.
agent("...")ChatBedrockConverse(model=..., region_name=...)str(result)agent.invoke({"messages": [...]})BedrockModel(model_id=..., region_name=...)result["messages"][-1].contentcreate_agent(model, tools, system_prompt=)Q11 · Pick all that apply (3 pts) Which are real LangChain message types?
SystemMessageHumanMessagePromptMessageAIMessageToolMessageUserBlockQ12 · True or false (3 pts) Mark each
TorF.Q13 · Read the model output (2 pts) The
us.prefix onus.anthropic.claude-haiku-4-5-20251001-v1:0is there because:Case study · TravelMind day one (5 pts)
TravelMind launches with a bare agent, no tools. Rao asks: "is JX48Q2 cancelled?"
flowchart LR U[is JX48Q2 cancelled?] --> M[model, no tools] --> A[answer]Q14a (2 pts) With no tool to check the booking, the agent is forced to:
Q14b (3 pts) The single change that grounds the answer:
lookup_pnrtool wired into the agentAll reactions