AI एजेंट · पाठ

शुरू से ReAct लागू करना

LangChain के बिना Python की 100 पंक्तियों का कार्यान्वयन लिखें: Action पंक्तियाँ पार्स करें, उपकरण चलाएँ और Final Answer तक लूप चलाएँ।

पाठ 2, कुल 4 में से15 चरण

शुरू से ReAct लागू करना, CoddyKit पर AI एजेंट का एक निःशुल्क पाठ है। यह 4 में से 2वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह AI एजेंट सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। AI एजेंट पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

इस पाठ के कुछ हिस्सों का अभी तक अनुवाद नहीं हुआ है और वे अंग्रेज़ी में दिखाए गए हैं।

चक्र स्वयं बनाएँ

आप लगभग 100 पंक्तियों के पाइथन कोड में ReAct लिख सकते हैं — किसी फ़्रेमवर्क की आवश्यकता नहीं है। इसे एक बार स्वयं लिखने से आपको समझ आता है कि फ़्रेमवर्क किन चीज़ों को अमूर्त बनाते हैं।

Step 1: Define Tools

import json, math

def calculator(expression: str) -> str:
    try:
        return str(eval(expression, {'__builtins__': {}}, vars(math)))
    except Exception as e:
        return f'Error: {e}'

def search_wikipedia(query: str) -> str:
    # placeholder — call a real search API
    return f'Wikipedia summary for: {query}'

TOOLS = {
    'calculator': calculator,
    'search_wikipedia': search_wikipedia,
}

print(calculator('2 + 2 * sqrt(16)'))
print(search_wikipedia('agents'))

Step 2: Tool Schemas

schemas = [
    {'type': 'function', 'function': {
        'name': 'calculator',
        'description': 'Evaluate a math expression like 2 + 2 * sqrt(16)',
        'parameters': {'type': 'object', 'properties': {'expression': {'type': 'string'}}, 'required': ['expression']}
    }},
    {'type': 'function', 'function': {
        'name': 'search_wikipedia',
        'description': 'Search Wikipedia and return a summary',
        'parameters': {'type': 'object', 'properties': {'query': {'type': 'string'}}, 'required': ['query']}
    }}
]
import json
print(json.dumps(schemas, indent=2))

Step 3: System Prompt

system = '''
You are a research assistant. Use the available tools when needed.
Think step by step. If you have enough information, give a final answer.
'''
print(system.strip())

Step 4: The Loop

from openai import OpenAI
client = OpenAI()

def react(question, max_steps=10):
    messages = [
        {'role': 'system', 'content': system},
        {'role': 'user', 'content': question}
    ]
    for step in range(max_steps):
        r = client.chat.completions.create(
            model='gpt-4o-mini',
            messages=messages,
            tools=schemas,
        )
        msg = r.choices[0].message
        messages.append(msg)
        if not msg.tool_calls:
            return msg.content
        for tc in msg.tool_calls:
            args = json.loads(tc.function.arguments)
            try:
                result = TOOLS[tc.function.name](**args)
            except Exception as e:
                result = f'Tool error: {e}'
            messages.append({'role': 'tool', 'tool_call_id': tc.id, 'content': str(result)})
    return 'Step limit reached.'

Step 5: Run It

answer = react('What is the population of Tokyo times the square root of pi?')
print(answer)

चरण 6: लॉग रिकॉर्ड करें

डीबगिंग के लिए प्रत्येक चरण को कैप्चर करें:

def react_verbose(question, max_steps=10):
    messages = [...]
    for step in range(max_steps):
        r = client.chat.completions.create(model='gpt-4o-mini', messages=messages, tools=schemas)
        msg = r.choices[0].message
        print(f'[step {step}] thought: {msg.content}')
        if msg.tool_calls:
            for tc in msg.tool_calls:
                print(f'[step {step}] action: {tc.function.name}({tc.function.arguments})')
        ...

चरण 7: टूल के रूप में चरण गणक

कुछ एजेंट सिस्टम प्रॉम्प्ट में चरणों की संख्या शामिल करते हैं, ताकि मॉडल जान सके कि उसे कब अभिसरित होना है:

system = f'You have at most {MAX_STEPS} steps. Step {current_step}/{MAX_STEPS}. ...'

चरण 8: अंतिम उत्तर का प्रारूप

यदि आपको संरचित आउटपुट चाहिए, तो अंतिम टूल कॉल को अनिवार्य करें:

tools.append({'type': 'function', 'function': {
    'name': 'final_answer',
    'description': 'Submit the final answer to the user.',
    'parameters': {'type': 'object', 'properties': {'answer': {'type': 'string'}}, 'required': ['answer']}
}})
# Loop terminates when final_answer is called.

चरण 9: टूल त्रुटियाँ

त्रुटियों को हमेशा टूल परिणाम के रूप में लौटाएँ, ताकि एजेंट उनसे उबर सके:

try:
    result = TOOLS[name](**args)
except Exception as e:
    result = f'TOOL ERROR: {type(e).__name__}: {e}'
# Model sees the error and can try a different approach.

चरण 10: समानांतर टूल कॉल

यदि msg.tool_calls में कई प्रविष्टियाँ हों, तो उन्हें समानांतर रूप से चलाएँ:

import asyncio

async def run_tools(tool_calls):
    async def run_one(tc):
        args = json.loads(tc.function.arguments)
        return tc.id, TOOLS[tc.function.name](**args)
    return await asyncio.gather(*[run_one(tc) for tc in tool_calls])

चरण 11: बातचीत की सीमा तय करना

अधिकतम चरणों के भीतर भी संदेश-सूचियाँ बहुत बड़ी हो सकती हैं। चरणों के बीच टोकन संख्या के आधार पर उन्हें छोटा करें:

if total_tokens(messages) > 6000:
    messages = trim_messages(messages)

चरण 12: उत्पादन-स्तर की मजबूती

उत्पादन के लिए जोड़ें: टूल कॉल पर समय-सीमाएँ, प्रत्येक टूल के लिए कोटा, एपीआई विफलताओं पर विलंब सहित पुनःप्रयास और पूरा ट्रेस लॉग। लेकिन ऊपर दिया गया चक्र आपका आधार ढाँचा है।

टूल त्रुटि प्रबंधन

जब कोई टूल अपवाद उत्पन्न करे, तब आपको क्या करना चाहिए?

पुनरावलोकन

लगभग 80 पंक्तियों में ReAct: संदेश-सूची + while चक्र + डिस्पैच तालिका। फ़्रेमवर्क अतिरिक्त सुविधाएँ देते हैं, लेकिन मूल ढाँचा यही है।

शुरुआत निःशुल्क

एआई शिक्षक के साथ AI एजेंट सीखें — निःशुल्क

अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।

पाठ्यक्रम
60
पाठ
239

अक्सर पूछे जाने वाले प्रश्न

क्या “शुरू से ReAct लागू करना” पाठ निःशुल्क है?

हाँ—“शुरू से ReAct लागू करना” का पूरा पाठ यहाँ वेब पर निःशुल्क पढ़ा जा सकता है। इंटरैक्टिव अभ्यास (अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर) करने और AI एजेंट पाठ्यक्रम का बाकी हिस्सा अनलॉक करने के लिए CoddyKit PRO लें। AI एजेंट पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

“शुरू से ReAct लागू करना” में मैं क्या सीखूँगा?

LangChain के बिना Python की 100 पंक्तियों का कार्यान्वयन लिखें: Action पंक्तियाँ पार्स करें, उपकरण चलाएँ और Final Answer तक लूप चलाएँ। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ AI एजेंट का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।

क्या AI एजेंट शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?

पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर AI एजेंट शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 2वाँ पाठ है।

“शुरू से ReAct लागू करना” पाठ पूरा करने में कितना समय लगता है?

CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।

क्या मैं इस AI एजेंट पाठ में कोड लिख और चला सकता हूँ?

हाँ। हर AI एजेंट पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।

इस पाठ्यक्रम के सभी पाठ

  1. ReAct: तर्क + कार्रवाई पैटर्न
  2. शुरू से ReAct लागू करना
  3. सामान्य उपकरण-समूह (वेब, कैलकुलेटर, RAG)
  4. उपकरण त्रुटियों का पता लगाना और उनसे उबरना
← AI एजेंट पर वापस जाएँ