LoslegenKostenlos loslegen

Modell-Fallbacks implementieren

Um einen 24/7-Service zu garantieren, muss TrailBlaze auf ein schlankeres Modell zurückgreifen, wenn Claude nicht da ist. Wenn es Probleme gibt, schalte von Claude auf Nova Text Lite um, damit die Reise-App immer läuft.

Die Bibliotheken „ json “ und „ boto3 “ sowie der Client „ bedrock “ sind schon installiert.

Diese Übung ist Teil des Kurses

Einführung in Amazon Bedrock

Kurs anzeigen

Anleitung zur Übung

  • Wickle den ersten Claude-Modellaufruf in einen „ try “-Block ein.
  • Im Block „ except “ (Fehler behandeln) kannst du Fehler mit „ ClientError “ (Fehler behandeln) abfangen und das Nova Text Lite-Modell aufrufen.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

def process_with_fallback(prompt):
    # Handle errors with a try-except block
    ____:
        response = bedrock.invoke_model(
          modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",
          body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
                           "messages": [{"role": "user", "content": [{"type": "text", "text": prompt}]}]}))
        return json.loads(response["body"].read().decode())["content"][0]["text"]
    except ____:
        fallback = bedrock.invoke_model(
            modelId="amazon.nova-lite-v1:0",
            body=json.dumps({"messages": [{"role": "user", "content": [{"text": prompt}]}]}))
        return json.loads(fallback["body"].read().decode())["output"]["message"]["content"][0]["text"]

print(process_with_fallback("What are the best destinations in Australia for kayaking?"))
Code bearbeiten und ausführen