Docstrings और Type Hints जोड़ना
अब अपने convert_currency() टूल को LLMs के लिए और आसान बनाएँ — docstrings और type hints जोड़कर। इनके बिना LLM यह तय नहीं कर पाएगा कि किस टूल को कॉल करना है, या फिर आर्ग्युमेंट्स में गलत मान पास कर सकता है — और दोनों ही स्थितियों में एप्लिकेशन का प्रदर्शन अविश्वसनीय हो जाता है!
एक MCP सर्वर पहले से FastMCP के जरिए इंस्टैंशिएट किया जा चुका है और उसे mcp में असाइन किया गया है।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Model Context Protocol (MCP) परिचय
अभ्यास निर्देश
- फंक्शन के आर्ग्युमेंट्स और रिटर्न ऑब्जेक्ट के लिए उपयुक्त टाइप्स जोड़ें।
- docstring पूरी करें ताकि तीनों फंक्शन आर्ग्युमेंट्स अपनी-अपनी परिभाषाओं से मेल खाएँ।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Adding typing to the function arguments and return object
@mcp.tool()
def convert_currency(amount: ____, from_currency: ____, to_currency: ____) -> ____:
# Complete the docstring with the function arguments
"""
Convert an amount from one currency to another using current exchange rates.
Args:
____: The amount to convert
____: Source currency code (e.g., 'USD', 'EUR', 'GBP')
____: Target currency code (e.g., 'USD', 'EUR', 'GBP')
Returns:
A string with the conversion result and exchange rate
"""
url = f"https://api.frankfurter.dev/v1/latest?base={from_currency}&symbols={to_currency}"
response = requests.get(url)
data = response.json()
rate = data['rates'].get(to_currency)
if rate is None:
return f"Could not find exchange rate for {from_currency} to {to_currency}"
converted_amount = amount * rate
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: {rate})"
print(convert_currency(amount=100, from_currency="EUR", to_currency="USD"))