Panggil model AI Mistral di Amazon Bedrock menggunakan API Model Invoke dengan aliran respons - Amazon Bedrock

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Panggil model AI Mistral di Amazon Bedrock menggunakan API Model Invoke dengan aliran respons

Contoh kode berikut menunjukkan cara mengirim pesan teks ke model AI Mistral, menggunakan API Model Invoke, dan mencetak aliran respons.

Python
SDK untuk Python (Boto3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

Gunakan API Invoke Model untuk mengirim pesan teks dan mencetak aliran respons.

# Use the native inference API to send a text message to Mistral AI # and print the response stream. import boto3 import json # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Mistral Large. model_id = "mistral.mistral-large-2402-v1:0" # Define the message to send. user_message = "Describe the purpose of a 'hello world' program in one line." # Embed the message in Mistral's prompt format. prompt = f"<s>[INST] {user_message} [/INST]" # Format the request payload using the model's native structure. native_request = { "prompt": prompt, "max_tokens": 512, "temperature": 0.5, } # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. streaming_response = client.invoke_model_with_response_stream( modelId=model_id, body=request ) # Extract and print the response text in real-time. for event in streaming_response["body"]: chunk = json.loads(event["chunk"]["bytes"]) if "outputs" in chunk: print(chunk["outputs"][0]["text"], end="")

Untuk daftar lengkap panduan pengembang AWS SDK dan contoh kode, lihatMenggunakan layanan ini dengan AWS SDK. Topik ini juga mencakup informasi tentang memulai dan detail tentang versi SDK sebelumnya.