Skip to content

Commit 1b31862

Browse files
Shonda WitherspoonShonda Witherspoon
authored andcommitted
latest iteration of react agent
Signed-off-by: Shonda Witherspoon <[email protected]>
1 parent 3c0a169 commit 1b31862

File tree

2 files changed

+399
-41
lines changed

2 files changed

+399
-41
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "25961553-040c-41d1-84b2-5e56c680e8c2",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"! echo \"::group::Install Dependencies\"\n",
11+
"%pip install uv\n",
12+
"! uv pip install \"git+https://github.com/ibm-granite-community/utils.git\" \\\n",
13+
" langgraph \\\n",
14+
" langchain \\\n",
15+
" langchain_ibm\n",
16+
"! echo \"::endgroup::\"\n"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 20,
22+
"id": "f31ae7a6-1288-41d0-a54b-82311b1f2a40",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"from ibm_granite_community.notebook_utils import get_env_var\n",
27+
"from langchain_core.utils.utils import convert_to_secret_str\n",
28+
"from langchain.chat_models import init_chat_model\n",
29+
"\n",
30+
"model = \"ibm/granite-4-h-small\"\n",
31+
"\n",
32+
"model_parameters = {\n",
33+
" \"temperature\": 0,\n",
34+
" \"max_completion_tokens\": 500,\n",
35+
" \"repetition_penalty\": 1.05,\n",
36+
"}\n",
37+
"\n",
38+
"llm = init_chat_model(\n",
39+
" model=model,\n",
40+
" model_provider=\"ibm\",\n",
41+
" url=convert_to_secret_str(get_env_var(\"WATSONX_URL\")),\n",
42+
" apikey=convert_to_secret_str(get_env_var(\"WATSONX_APIKEY\")),\n",
43+
" project_id=get_env_var(\"WATSONX_PROJECT_ID\"),\n",
44+
" params=model_parameters,\n",
45+
")"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 22,
51+
"id": "00e90492-ff94-4d25-9484-72d594b93c79",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"#definte mock tools for now as test\n",
56+
"from langchain.tools import tool\n",
57+
"\n",
58+
"@tool\n",
59+
"def get_weather(destination: str, month: str) -> str:\n",
60+
" \"\"\"Get weather for a destination and month.\"\"\"\n",
61+
" return f\"The weather in {destination} in {month} is cool and dry, highs ~15°C.\"\n",
62+
"\n",
63+
"@tool\n",
64+
"def get_flight_info(destination: str, month: str) -> str:\n",
65+
" \"\"\"Get flight cost for a destination and month.\"\"\"\n",
66+
" return f\"Flights to {destination} in {month} average around $1200 USD round-trip.\"\n",
67+
"\n",
68+
"@tool\n",
69+
"def get_hotel_prices(destination: str, month: str) -> str:\n",
70+
" \"\"\"Get hotel prices for a destination and month.\"\"\"\n",
71+
" return f\"Hotels in {destination} range from $100 to $300 per night in {month}.\"\n",
72+
"\n",
73+
"tools = [get_weather, get_flight_info, get_hotel_prices]\n"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 28,
79+
"id": "9ea8d4e2-e6f1-461a-8096-ac44ae9166b9",
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stderr",
84+
"output_type": "stream",
85+
"text": [
86+
"/var/folders/tt/jz8hl4bx1qs6c7ljdflw7z9r0000gn/T/ipykernel_29986/773479898.py:3: LangGraphDeprecatedSinceV10: create_react_agent has been moved to `langchain.agents`. Please update your import to `from langchain.agents import create_agent`. Deprecated in LangGraph V1.0 to be removed in V2.0.\n",
87+
" agent = create_react_agent(\n"
88+
]
89+
}
90+
],
91+
"source": [
92+
"#we now want to go back to the from langchain.agents import create_agent\n",
93+
"\n",
94+
"from langgraph.prebuilt import create_react_agent \n",
95+
"\n",
96+
"agent = create_react_agent(\n",
97+
" model=llm,\n",
98+
" tools=tools\n",
99+
")\n"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 24,
105+
"id": "60f85f04-44cf-4979-ba1e-1f7a8130ab5d",
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"response = agent.invoke({\n",
110+
" \"messages\": [{\"role\": \"user\", \"content\": \"I want to visit Tokyo next month, which will be April. What should I expect in terms of weather and cost?\"}]\n",
111+
"})\n"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 27,
117+
"id": "97887505-6192-4d90-a3d7-75ffba3880cc",
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"name": "stdout",
122+
"output_type": "stream",
123+
"text": [
124+
"**Tokyo in April – What to Expect**\n",
125+
"\n",
126+
"| Category | Details |\n",
127+
"|----------|---------|\n",
128+
"| **Weather** | • **Temperature:** Cool and mild. Average highs around **15 °C (59 °F)**, lows near **8 °C (46 °F)**.<br>• **Precipitation:** Generally dry; only a few light showers expected. <br>• **What to Pack:** Light jacket or sweater for evenings, comfortable layers for daytime, and an umbrella just in case. |\n",
129+
"| **Flights** | • **Average Round‑Trip Cost:** **≈ $1,200 USD** (prices can vary by airline, departure city, and how far in advance you book). |\n",
130+
"| **Hotels** | • **Price Range:** **$100 – $300 USD per night** for most mid‑range hotels. <br>• **Budget Options:** Capsule hotels or budget guesthouses can be found for **$50 – $100 USD** per night.<br>• **Luxury Options:** High‑end hotels and ryokans start around **$250 USD** and can go well above **$500 USD** per night. |\n",
131+
"\n",
132+
"### Quick Tips for Your April Trip\n",
133+
"\n",
134+
"1. **Book Flights Early** – April is a popular time (cherry‑blossom season), so securing tickets a few months ahead can save you $100–$300 on airfare.\n",
135+
"2. **Consider Off‑Peak Days** – Flying on a Tuesday or Wednesday often yields the best rates.\n",
136+
"3. **Hotel Booking Strategy** – If you want to stay near major attractions (e.g., Shinjuku, Ginza, Asakusa), expect to pay toward the higher end of the range. For more budget‑friendly stays, look a few stops away from the city center or consider neighborhoods like Ueno or Ikebukuro.\n",
137+
"4. **Cherry Blossom Season** – Late March to early April is famous for sakura (cherry blossoms). If you can, plan a day trip to Ueno Park, Shinjuku Gyoen, or the Philosopher’s Path for stunning views.\n",
138+
"5. **Transportation** – A **Suica** or **Pasmo** prepaid card makes getting around the city seamless and often cheaper than buying single tickets.\n",
139+
"\n",
140+
"### Sample Budget Snapshot\n"
141+
]
142+
}
143+
],
144+
"source": [
145+
"final_message = response[\"messages\"][-1].content\n",
146+
"print(final_message)\n"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"id": "ec7a9e0a-e01a-4a27-bb46-55c97239bc13",
153+
"metadata": {},
154+
"outputs": [],
155+
"source": []
156+
}
157+
],
158+
"metadata": {
159+
"kernelspec": {
160+
"display_name": "watsonx_api",
161+
"language": "python",
162+
"name": "watsonx_api"
163+
},
164+
"language_info": {
165+
"codemirror_mode": {
166+
"name": "ipython",
167+
"version": 3
168+
},
169+
"file_extension": ".py",
170+
"mimetype": "text/x-python",
171+
"name": "python",
172+
"nbconvert_exporter": "python",
173+
"pygments_lexer": "ipython3",
174+
"version": "3.11.13"
175+
}
176+
},
177+
"nbformat": 4,
178+
"nbformat_minor": 5
179+
}

0 commit comments

Comments
 (0)