Before you click Practice โ Module 3 notebook
Tools are ordinary Python functions the model is allowed to request. You decorate them with @tool, then bind_tools so Groq can emit a structured tool_calls object instead of only chatting.
InstallโAPI keyโ@tool multiply / weatherโbind_tools + invoke
After you run bind_tools, the interesting print is not the sentence โ it is Tool Calls output by model.
Practice task 1
Install packages into this notebookโs Python
Run the first code cell. It uses sys.executable -m pip so packages go into Kodinโs Jupyter kernel, not a random Windows pip.
# Install into THIS notebook kernel (not Windows pip).
import sys
import subprocess
print("Installing into:", sys.executable)
subprocess.check_call([
sys.executable, "-m", "pip", "install", "-qU",
"langgraph", "langchain-groq", "langchain-core",
])
print("Done. Run the next cells.")
Expected: Prints the kernel path, then โDone.โ
Why this cell: A plain !pip install on Windows often installs into a different Python, then the next cell fails with ModuleNotFoundError: langgraph. If import still fails: Kernel โ Restart Kernel, then Run All.
Practice task 2
API key + MODEL + groq_text (same as Module 1)
Every practice notebook repeats this cell. Run it before any Groq call. Key stays in os.environ["GROQ_API_KEY"] for this kernel session.
import os
MODEL = "openai/gpt-oss-20b"
def groq_text(msg) -> str:
# GPT-OSS may return text, a list of parts, or reasoning_content
...
if not os.getenv("GROQ_API_KEY"):
os.environ["GROQ_API_KEY"] = input("Enter GROQ_API_KEY: ").strip()
print("API key ready. Model:", MODEL)
Expected: โAPI key ready. Model: openai/gpt-oss-20bโ
Why this cell: Without this cell, ChatGroq has no key. Without groq_text, later prints can look empty even when the model answered.
Practice task 3
Create tools with @tool
The docstring is the description the model reads. Type hints become the argument schema. These functions are fake on purpose (weather is always sunny) so the lab never needs the internet.
from langchain_core.tools import tool
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two integers."""
return a * b
@tool
def get_weather(location: str) -> str:
"""Get the current weather in a given location."""
return f"The weather in {location} is sunny and 75 degrees."
tools = [multiply, get_weather]
print("Tools created:", [t.name for t in tools])
- Write a one-line docstring โ that is the โmanualโ the LLM sees.
- Put tools in a list; later cells pass that same list to
bind_tools.
Expected: Tools created: ['multiply', 'get_weather']
Why this cell: Without @tool, LangChain cannot send a JSON schema to Groq. The name of the function becomes the tool name (multiply, get_weather).
Practice task 4
Bind tools and watch the model request them
bind_tools does not run the functions yet. It only lets the model ask to run them. You print response.tool_calls to see name + arguments.
from langchain_groq import ChatGroq
llm = ChatGroq(model=MODEL, temperature=0)
llm_with_tools = llm.bind_tools(tools)
response = llm_with_tools.invoke("What is 15 multiplied by 4?")
print("Tool Calls output by model:", getattr(response, "tool_calls", None) or [])
response2 = llm_with_tools.invoke("What is the weather like in Paris?")
print("Tool Calls output by model (Weather):", getattr(response2, "tool_calls", None) or [])
llm_with_tools = llm.bind_tools(tools) โ attach schemas to this chat model.
tool_calls โ structured request, not the number 60 yet.
- If the list is empty, re-run Task 2 (key) and check the prompt is clearly a math/weather question.
Expected: tool_calls for multiply with a=15, b=4, then get_weather with location Paris.
Why this cell: This matches the Tool-Using Workflow on this page: the model chooses a tool. A full agent (Module 5) would execute the tool and send the result back. Here you stop at โthe model requested multiply(15, 4).โ