Tool definition¶
In [ ]:
from dotenv import load_dotenv
load_dotenv()
Out[ ]:
True
In [2]:
import os
from langchain.chat_models import init_chat_model
from langchain.tools import tool
DEEPSEEK_MODEL = "deepseek-chat"
DEEPSEEK_BASE_URL = "https://api.deepseek.com"
def deepseek_model(model: str = DEEPSEEK_MODEL, max_tokens=1000, **kwargs):
return init_chat_model(
model=model,
# DeepSeek uses LangChain's OpenAI-compatible transport.
model_provider="openai",
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url=DEEPSEEK_BASE_URL,
max_tokens=max_tokens,
**kwargs,
)
*** recommended format
In [7]:
@tool("square_root", description="Calculate the square root of a number")
def square_root(x: float) -> float:
return x ** 0.5
In [8]:
square_root.invoke({"x": 467})
Out[8]:
21.61018278497431
Adding to agents¶
In [9]:
from langchain.agents import create_agent
from langchain.messages import HumanMessage
from pprint import pprint
agent = create_agent(
model=deepseek_model(),
tools=[square_root],
system_prompt="You are an arithmetic wizard. Use your tools to calculate the square root and square of any number."
)
In [10]:
question = HumanMessage(content="What is the square root of 467?")
response = agent.invoke(
{"messages": [question]}
)
print(response['messages'][-1].content)
The square root of **467** is approximately **21.6102**. If you'd also like to know the square of any number, just let me know!
In [11]:
pprint(response)
{'messages': [HumanMessage(content='What is the square root of 467?', additional_kwargs={}, response_metadata={}, id='ce017f8d-d282-4326-9da7-9154614ab8a6'),
AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 43, 'prompt_tokens': 302, 'total_tokens': 345, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 302}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': '2be4c1c8-8094-4367-8388-7b2ea1896a18', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019e20c1-5f7f-7142-a461-e05ffce56497-0', tool_calls=[{'name': 'square_root', 'args': {'x': 467}, 'id': 'call_00_uxnyo9ULM8fgE539sDeX4636', 'type': 'tool_call'}], invalid_tool_calls=[], usage_metadata={'input_tokens': 302, 'output_tokens': 43, 'total_tokens': 345, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}),
ToolMessage(content='21.61018278497431', name='square_root', id='57e4952a-ac0d-40f1-ab56-47e27ccb7134', tool_call_id='call_00_uxnyo9ULM8fgE539sDeX4636'),
AIMessage(content="The square root of **467** is approximately **21.6102**.\n\nIf you'd also like to know the square of any number, just let me know!", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 33, 'prompt_tokens': 364, 'total_tokens': 397, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 256}, 'prompt_cache_hit_tokens': 256, 'prompt_cache_miss_tokens': 108}, 'model_provider': 'openai', 'model_name': 'deepseek-v4-flash', 'system_fingerprint': 'fp_8b330d02d0_prod0820_fp8_kvcache_20260402', 'id': 'ca43648d-bf10-42ad-8ec4-e034e5187a07', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--019e20c1-6588-7d80-b095-c7658e2533d7-0', tool_calls=[], invalid_tool_calls=[], usage_metadata={'input_tokens': 364, 'output_tokens': 33, 'total_tokens': 397, 'input_token_details': {'cache_read': 256}, 'output_token_details': {}})]}
In [12]:
print(response["messages"][1].tool_calls)
[{'name': 'square_root', 'args': {'x': 467}, 'id': 'call_00_uxnyo9ULM8fgE539sDeX4636', 'type': 'tool_call'}]
In [ ]: