YUNOVA · AI Agents
Module 9: Agents with the OpenAI and Claude SDKs
Setting up a key and the SDK
Key points
- OpenAI या Anthropic (Claude) के console पर account बनाकर API key बनाई जाती है।
- API key को code में सीधे न लिखें; environment variable या .env file में रखें और Git में commit न करें।
- Python में SDK pip से install होता है, जैसे openai या anthropic package।
- पहला test एक छोटा 'hello' message भेजकर करें कि key और setup सही हैं।
The first tool call in Python
Key points
- Tool को एक नाम, description और JSON Schema वाले parameters के साथ define करके API request में भेजा जाता है।
- Model ज़रूरत समझे तो जवाब में tool call लौटाता है, जिसमें tool का नाम और arguments होते हैं।
- आपका Python code उसी नाम का function arguments के साथ चलाता है और result model को वापस भेजता है।
The agent loop in code
Key points
- Agent loop में code बार-बार model को call करता है जब तक model tool call माँगना बंद न कर दे।
- हर बार model का जवाब और tool का result messages की list में जोड़ा जाता है।
- Claude API में stop_reason 'tool_use' बताता है कि model tool चलवाना चाहता है।
- Loop में अधिकतम iterations की सीमा और error handling ज़रूर रखें।
Streaming responses
Key points
- Streaming में model का जवाब छोटे टुकड़ों में आता है, जिससे user को पूरा जवाब बनने का इंतज़ार नहीं करना पड़ता।
- SDK में stream विकल्प चालू करके events को एक-एक करके पढ़ा और screen पर दिखाया जाता है।
- Tool calls के साथ streaming में events को जोड़कर पूरा tool call बनाना पड़ता है।
Structured outputs in the SDK
Key points
- Structured outputs में model का जवाब तय schema वाले JSON में मिलता है।
- Python में अक्सर Pydantic model से schema define किया जाता है और जवाब उसी में parse होता है।
- इससे code में जवाब पढ़ना भरोसेमंद होता है; फिर भी validation और error handling रखें।