Math calculations के लिए एक tool बनाएँ
अब अपने tool को बनाने का समय है. मान लीजिए आप एक छोटी construction कंपनी चलाते हैं और आपको छत के एक किनारे की लंबाई निकालनी है. अगर आपको उन दो बीम्स की लंबाई पता है जो छत को समकोण पर सहारा देती हैं, तो आप नीचे दिए गए hypotenuse फॉर्मूला से छत की लंबाई निकाल सकते हैं. $$c = \sqrt{a^2 + b^2}$$

यह अभ्यास पाठ्यक्रम का हिस्सा है
LangChain के साथ Agentic Systems डिज़ाइन करना
अभ्यास निर्देश
- फंक्शन को tool की तरह परिभाषित करने के लिए आवश्यक डेकोरेटर का उपयोग करें.
- Hypotenuse पाने के लिए,
inputस्ट्रिंग पर.split()मेथड का उपयोग करके समकोण त्रिभुज की बाकी दो लंबाइयाँ निकालें. - त्रिभुज के हर किनारे
aऔरbको फ्लोट में कन्वर्ट करें और वैल्यू से अतिरिक्त स्पेस हटाने के लिए.strip()का उपयोग करें. - Python के
mathमॉड्यूल का उपयोग करकेaऔरbकी लंबाइयों का square करें, उन्हें जोड़ें, और उनका square root निकालकर छत की कुल लंबाई पता करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Define this math function as a tool
____
def hypotenuse_length(input: str) -> float:
"""Calculates the length of the hypotenuse of a right-angled triangle given the lengths of the other two sides."""
# Split the input string to get the lengths of the triangle
sides = ____.____(',')
# Convert the input values to floats, removing extra spaces
a = ____(____[0].____())
b = ____(____[1].____())
# Square each of the values, add them together, and find the square root
return ____.sqrt(a____2 + b____2)