need help on tool running before passing to llm
hi,
i'm shan from India. I'm learning Langchain. Since I'm a total noob in python and langchain i'm not able to test my tools in jupyter notebook one by one.
Actually, i'm trying a simple get_weather agent. but i couldn't run the tool before running the llm for testing. Kindly, help me.
Here is my coe:
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
get_weather.invoke({"location":"beijing"})
import os
import json
from dotenv import load_dotenv, find_dotenv
from langchain_deepseek import ChatDeepSeek
from urllib.parse import quote
from httpx import request
def get_weather(location:str)->str:
"""
this is used to get the weather of a city anywhere in the world.
Args:
location (str): specify the location to get its weather. eg) 'New York', 'Beijing', 'New Delhi' e.tc
Returns:
return a string that explains that city's weather in celsius, fareheight and how the weather is.
"""
key=os.environ.get("WEATHER_API_KEY")
url="https://api.weatherapi.com/v1/current.json?q={location}&key={key}"
encoded_url=quote(url)
response=request.get(encoded_url).json()
if response:
return f"weather data for **{location}**: celsius: {response["current"].temp_c} farenheight: {response["current"].temp_f}, day is: {response["current"]["condition"].text}"
# data=response.json()
# return data
else:
return f"Weather API failed retry after some time"
get_weather.invoke("beijing")
for this i get the error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[28], line 1
----> 1 get_weather.invoke("beijing")
File ~/Documents/Agentic-experiments/tools-experiments/.venv/lib/python3.12/site-packages/langchain_core/tools/base.py:738, in BaseTool.invoke(self, input, config, **kwargs)
730
731 def invoke(
732 self,
(...) 735 **kwargs: Any,
736 ) -> Any:
737 tool_input, kwargs = _prep_run_args(input, config, **kwargs)
--> 738 return self.run(tool_input, **kwargs)
File ~/Documents/Agentic-experiments/tools-experiments/.venv/lib/python3.12/site-packages/langchain_core/tools/base.py:1100, in BaseTool.run(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, run_id, config, tool_call_id, **kwargs)
1098 if error_to_raise:
1099 run_manager.on_tool_error(error_to_raise, tool_call_id=tool_call_id)
-> 1100 raise error_to_raise
1101 output = _format_output(content, artifact, tool_call_id, self.name, status)
1102 run_manager.on_tool_end(output, color=color, name=self.name, **kwargs)
File ~/Documents/Agentic-experiments/tools-experiments/.venv/lib/python3.12/site-packages/langchain_core/tools/base.py:1066, in BaseTool.run(self, tool_input, verbose, start_color, color, callbacks, tags, metadata, run_name, run_id, config, tool_call_id, **kwargs)
1064 if config_param := _get_runnable_config_param(self._run):
1065 tool_kwargs |= {config_param: config}
-> 1066 response = context.run(self._run, *tool_args, **tool_kwargs)
1067 if self.response_format == "content_and_artifact":
1068 msg = (
1069 "Since response_format='content_and_artifact' "
1070 "a two-tuple of the message content and raw tool output is "
1071 f"expected. Instead, generated response is of type: "
1072 f"{type(response)}."
1073 )
File ~/Documents/Agentic-experiments/tools-experiments/.venv/lib/python3.12/site-packages/langchain_core/tools/structured.py:97, in StructuredTool._run(self, config, run_manager, *args, **kwargs)
95 if config_param := _get_runnable_config_param(self.func):
96 kwargs[config_param] = config
---> 97 return self.func(*args, **kwargs)
98 msg = "StructuredTool does not support sync invocation."
99 raise NotImplementedError(msg)
Cell In[25], line 41, in get_weather(location)
37 """
38 key=os.environ.get("WEATHER_API_KEY")
39 url="https://api.weatherapi.com/v1/current.json?q={location}&key={key}"
40 encoded_url=quote(url)
---> 41 response=request.get(encoded_url)
42 if response:
43 # return f"weather data for **{location}**: celsius: {response["current"].temp_c} farenheight: {response["current"].temp_f}, day is: {response["current"]["condition"].text}"
44 data=response.json()
AttributeError: 'function' object has no attribute 'get'
how to resolve this and get results in testing what the tool prints.
u/WinnerPristine6119 — 10 days ago