पाठ 10 / 31

Tools परिभाषित करना

Function को नाम, विवरण और schema के साथ tool बनाएँ।

ऐसा function जिसे मॉडल माँग सके

Tool ऐसा function है जिसका नाम, विवरण और argument schema है जिसे मॉडल माँग सके। @tool decorator तीनों function से बनाता है: नाम function नाम से, विवरण docstring से और schema type hints से। अच्छे docstrings मायने रखते हैं क्योंकि मॉडल उन्हें पढ़कर तय करता है कि tool कब बुलाना है। Tool-calling chat model के साथ model.bind_tools([...]) मॉडल को संरचित अनुरोध ("add को a=2, b=40 के साथ बुलाओ") लौटाने देता है; आपका कोड (या agent loop) उसे चलाकर परिणाम लौटाता है। Arguments validate करें और हर tool की क्षमता सीमित करें।

कार्रवाई, निर्णय, बहाली

Tools मॉडल को कार्रवाई करने देते हैं, routing रास्ता चुनता है, और retries व fallbacks chains चालू रखते हैं।

चार औज़ार: tools, branch, retry, trace।
चित्र 3.1 — Tools, branch, retry और trace।

Function से tool, चलाकर

मैंने यह Python virtual environment में langchain-core 1.6.6, langchain-text-splitters 1.1.2 और llama-index-core 0.14.25 के साथ ऑफ़लाइन चलाया। किसी API key या network call की ज़रूरत नहीं क्योंकि असली मॉडल की जगह fake मॉडल या खिलौना embedding है। Decorator ने tool का नाम add, docstring से विवरण, और type hints से argument schema निकाला। Dict के साथ invoke करने पर 42 मिलता है।

from langchain_core.tools import tool

@tool
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

print(add.name, "|", add.description)
print(add.args)
print(add.invoke({"a": 2, "b": 40}))

Output:

add | Add two integers.
{'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}
42

Docstring मॉडल के लिए लिखें

बताएँ tool क्या करता है, कब उपयोग करें और हर argument का क्या अर्थ है। अस्पष्ट विवरण ग़लत या छूटी tool calls पैदा करते हैं।

त्वरित जाँच: @tool decorator tool का विवरण कहाँ से लेता है?

  • Function का docstring
  • File का नाम
  • Random string
  • GPU
Answer

Function का docstring — Docstring वह विवरण बनता है जो मॉडल पढ़ता है।