शुरू करेंमुफ़्त में शुरू करें

रेट लिमिट हैंडलिंग लागू करना

TrailBlaze, एक AI-पावर्ड एडवेंचर-ट्रैवल बुकिंग प्लेटफ़ॉर्म, ने हाल ही में एक चैट असिस्टेंट विकसित किया है जिसे नए फ़्लाइट डील्स आते ही इटिनरेरी से जुड़े सवालों की बाढ़ मिल रही है. आप एक हेल्पर फंक्शन बनाएँगे जो एक्सपोनेंशियल बैकऑफ जोड़कर Bedrock की throttling से निपट सके.

json, time और boto3 लाइब्रेरी पहले से लोड हैं. bedrock क्लायंट और model_id भी प्रीलोडेड हैं.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Amazon Bedrock परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • API रिक्वेस्ट स्ट्रिंग को prompt से पूरा करें.
  • एक्सपोनेंशियल बैकऑफ लागू करें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

def smart_retry(prompt, max_attempts=3):
  base_delay = 0.5
  
  for attempt in range(max_attempts):
    try:
      # Complete the API request string with the prompt
      response = bedrock.invoke_model(
        modelId=model_id,
        body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
                         "messages": [{"role": "user", "content": [{"type": "text", "text": ____}]}]}))
      return json.loads(response["body"].read().decode())["content"][0]["text"]
    
    except Exception as e:
      if "ThrottlingException" in str(e):
        # Implement exponential backoff
        time.sleep(____)
      else: raise e
  return "Max retries exceeded"

print(smart_retry("Tell me about hiking in Patagonia."))
कोड संपादित करें और चलाएँ