पाठ 5 / 27
हर SDK के साथ आपकी पहली Call
बुनियादी अनुरोध करें और उत्तर, stop reason और usage पढ़ें।
बनाएँ, पढ़ें, जाँचें
SDK के साथ बुनियादी call कुछ पंक्तियों की है। Client बनाएँ (वह key environment से पढ़ता है जब तक आप न दें), model, messages और सीमाओं के साथ create method बुलाएँ, फिर response ऑब्जेक्ट से उत्तर पढ़ें। हमेशा stop reason (मॉडल ने पूरा किया, आपकी token सीमा छुई, या tool उपयोग माँगा?) और usage संख्याएँ (लागत ट्रैकिंग के लिए) भी पढ़ें। मॉडल नाम <model-name> जैसी string है: वर्तमान नाम provider के दस्तावेज़ से लें, configuration में रखें और मॉडल जारी व सेवानिवृत्त होने पर उनके बदलने की अपेक्षा रखें।
बातचीत और उत्तर को आकार दें
भूमिकाएँ, इतिहास, system prompt और कुछ parameters तय करते हैं कि मॉडल क्या देखे और कैसे उत्तर दे।
Anthropic Messages call, चलाकर
मैंने यह Python virtual environment में anthropic 1.11.0 और openai 3.22.1 SDKs के साथ छोटे स्थानीय stand-in server (testing विषय में दिखाया, mock.py नाम से सहेजा) के विरुद्ध चलाया। Server तैयार उत्तर लौटाता है, इसलिए कोई key, network या असली मॉडल शामिल नहीं: यह साबित करता है कि SDK requests कैसे बनाता और उत्तर कैसे सँभालता है, यह नहीं कि असली मॉडल क्या कहता। SDK key को x-api-key के रूप में anthropic-version header के साथ /v1/messages को भेजता है, और response ऑब्जेक्ट पाठ, stop reason (end_turn) और token usage (14 अंदर, 8 बाहर) दिखाता है।
import mock, anthropic, openai
srv = mock.start(); base = f"http://127.0.0.1:{srv.server_address[1]}" # local stand-in server, not a real API
client = anthropic.Anthropic(api_key="sk-test-123", base_url=base)
msg = client.messages.create(model="demo-model", max_tokens=100,
messages=[{"role": "user", "content": "Capital of France?"}])
print(msg.content[0].text)
print(msg.stop_reason, msg.usage.input_tokens, msg.usage.output_tokens)
sent = mock.STATE["log"][-1]
print("SDK sent:", sent["headers"]["x-api-key"], sent["headers"]["anthropic-version"], sent["path"])
Output:
Paris is the capital of France. end_turn 14 8 SDK sent: sk-test-123 2023-06-01 /v1/messages
OpenAI Chat Completions call, चलाकर
मैंने यह Python virtual environment में anthropic 1.11.0 और openai 3.22.1 SDKs के साथ छोटे स्थानीय stand-in server (testing विषय में दिखाया, mock.py नाम से सहेजा) के विरुद्ध चलाया। Server तैयार उत्तर लौटाता है, इसलिए कोई key, network या असली मॉडल शामिल नहीं: यह साबित करता है कि SDK requests कैसे बनाता और उत्तर कैसे सँभालता है, यह नहीं कि असली मॉडल क्या कहता। OpenAI SDK Authorization: Bearer ... को /v1/chat/completions भेजता है। उत्तर choices[0].message.content में है, finish reason stop है, और usage prompt, completion और कुल tokens बताता है।
import mock, anthropic, openai
srv = mock.start(); base = f"http://127.0.0.1:{srv.server_address[1]}" # local stand-in server, not a real API
client = openai.OpenAI(api_key="sk-test-456", base_url=base + "/v1")
resp = client.chat.completions.create(model="demo-model",
messages=[{"role": "user", "content": "Capital of France?"}])
print(resp.choices[0].message.content)
print(resp.choices[0].finish_reason, resp.usage.prompt_tokens, resp.usage.completion_tokens, resp.usage.total_tokens)
sent = mock.STATE["log"][-1]
print("SDK sent:", sent["headers"]["authorization"], sent["path"])
Output:
Paris is the capital of France. stop 14 8 22 SDK sent: Bearer sk-test-456 /v1/chat/completions
SDK संस्करण pin करें
SDKs जल्दी बदलते हैं। Lockfile में संस्करण pin करें और upgrade से पहले release notes पढ़ें।
त्वरित जाँच: पाठ के साथ stop reason भी क्यों पढ़ें?
- इसमें API key होती है
- यह बताता है कि उत्तर कटा या tool माँगा गया
- यह मॉडल नाम है
- यह सजावटी है
Answer
यह बताता है कि उत्तर कटा या tool माँगा गया — कटे या tool माँगने वाले उत्तर को पूरे हुए उत्तर से अलग सँभालना होता है।