फ़ंक्शन -> tool schema (introspection)
Tool calling (यानी function calling) मॉडल को आपके प्रोग्राम से एक फ़ंक्शन चलवाने देता है: आप tools का एक set बताते हैं, मॉडल एक tool नाम और आर्ग्युमेंट के साथ जवाब देता है, और आप उसे चलाते हैं। पर मॉडल आपका Python नहीं देख सकता -> वह सिर्फ़ एक schema देखता है: हर tool के नाम और parameters का JSON विवरण। हर फ़ंक्शन के लिए वे schemas हाथ से लिखना थकाऊ है और signature बदलते ही बेमेल हो जाता है। इलाज introspection है: schema सीधे फ़ंक्शन से बनाएँ।
Python का inspect मॉड्यूल चलते समय फ़ंक्शन का signature पढ़ता है। tool-calling APIs जिस schema की उम्मीद करती हैं वह ऐसा दिखता है:
{
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}, "days": {"type": "integer"}},
"required": ["city"]
}
}आप tool_schema(func) बना रहे हैं जो ठीक वही बनाता है। दो विचार सारा काम करते हैं:
- Python प्रकारों को JSON-schema प्रकारों से जोड़ें। हर parameter का annotation पढ़ें और बदलें:
int->"integer",float->"number",str->"string",bool->"boolean"। बाक़ी किसी भी चीज़ का डिफ़ॉल्ट"string"रखें। - defaults से required निकालें। बिना default वाला parameter ज़रूरी है; default वाला parameter वैकल्पिक है।
inspect.Parameter.empty"कोई default नहीं" का संकेत है।
import inspect
sig = inspect.signature(func)
for name, param in sig.parameters.items():
is_required = param.default is inspect.Parameter.empty
json_type = ... # from param.annotationtool नाम के लिए func.__name__ इस्तेमाल करें। इसे ऐसे बनाएँ कि दो अलग annotations वाले फ़ंक्शन दो साफ़ अलग schemas बनाएँ, int param "integer" और str "string" के रूप में दिखे, default वाला parameter required से बाहर रहे, और बिना default वाला उसमें हो। हर tool-calling integration इसी अनुबंध पर टिकी है।
tool_schema(func) लिखें जो inspect.signature से फ़ंक्शन की जाँच करे और {"name": func.__name__, "parameters": {"type": "object", "properties": {param: {"type": json_type}}, "required": [...]}} लौटाए। int->"integer", float->"number", str->"string", bool->"boolean" जोड़ें (डिफ़ॉल्ट "string"), और parameter को required में सिर्फ़ तब रखें जब उसका कोई default न हो।
इस पाठ में कोई समस्या दिखी? बताएँ · इस पाठ के बारे में पूछें
यह अभ्यास जल्द खुलेगा
इससे पहले वाला पाठ पूरा करें और यह खुल जाएगा। पूरा कोर्स मुफ़्त है, कोई खाता नहीं चाहिए, और आप यह पाठ अभी पढ़ सकते हैं।
यह ब्राउज़र learn.floati.life के लिए कुकी रोक रहा है, इसलिए यहाँ पूरे किए गए पाठ क्रम से नहीं खुल सकते। इस साइट के लिए कुकी की अनुमति दें और फिर से लोड करें।