Envoi et traitement d’un document avec les modèles Cohere Command sur Amazon Bedrock - Amazon Bedrock

Envoi et traitement d’un document avec les modèles Cohere Command sur Amazon Bedrock

L’exemple de code suivant montre comment envoyer et traiter un document avec des modèles Cohere Command sur Amazon Bedrock.

Python
Kit SDK for Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

Envoyez et traitez un document avec les modèles Cohere Command sur Amazon Bedrock.

# Send and process a document with Cohere Command models on Amazon Bedrock. import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client in the AWS Region you want to use. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g. Command R+. model_id = "cohere.command-r-plus-v1:0" # Load the document with open("example-data/amazon-nova-service-cards.pdf", "rb") as file: document_bytes = file.read() # Start a conversation with a user message and the document conversation = [ { "role": "user", "content": [ {"text": "Briefly compare the models described in this document"}, { "document": { # Available formats: html, md, pdf, doc/docx, xls/xlsx, csv, and txt "format": "pdf", "name": "Amazon Nova Service Cards", "source": {"bytes": document_bytes}, } }, ], } ] try: # Send the message to the model, using a basic inference configuration. response = client.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 500, "temperature": 0.3}, ) # Extract and print the response text. response_text = response["output"]["message"]["content"][0]["text"] print(response_text) except (ClientError, Exception) as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") exit(1)
  • Pour plus de détails sur l’API, consultez Converse dans la Référence des API du kit AWS SDK for Python (Boto3).

Pour obtenir la liste complète des guides de développement AWS SDK et des exemples de code, consultez Utilisation d’Amazon Bedrock avec un kit AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes du kit de développement logiciel (SDK).