我在尝试使用 GPT2 从我的 AWS 端点生成内容时不断遇到错误..
import os
import boto3
from botocore.config import Config
# Set AWS credentials using environment variables
os.environ["AWS_ACCESS_KEY_ID"] = "key"
os.environ["AWS_SECRET_ACCESS_KEY"] = "secret key"
# Configure the timeout settings and retry attempts for the Boto3 client
config = Config(
read_timeout=300, # Increase the read timeout to 300 seconds (5 minutes)
connect_timeout=60, # Increase the connect timeout to 60 seconds
retries={'max_attempts': 5} # Increase the maximum number of retry attempts to 5
)
sagemaker_client = boto3.client('sagemaker')
runtime_client = boto3.client('runtime.sagemaker', config=config)
# Replace 'gpt-2-model' with the actual name of your deployed GPT-2 model
gpt2_model_name = 'gpt-2-model'
# Replace 'gpt2-endpoint' with the actual name of your SageMaker endpoint
endpoint_name = 'gpt2-endpoint'
# Get information about the deployed GPT-2 model
try:
gpt2_model_info = sagemaker_client.describe_model(ModelName=gpt2_model_name)
print("GPT-2 Model Information:")
print(gpt2_model_info)
except Exception as e:
print(f"Error retrieving GPT-2 model information: {e}")
# Get endpoint information
try:
endpoint_info = sagemaker_client.describe_endpoint(EndpointName=endpoint_name)
endpoint_config_name = endpoint_info['EndpointConfigName']
print("Endpoint Information:")
print(endpoint_info)
except Exception as e:
print(f"Error retrieving endpoint information: {e}")
def generate_content(prompt, max_length=500):
try:
response = runtime_client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType='text/plain',
Body=prompt.encode('utf-8'),
Accept='text/plain'
)
generated_content = response['Body'].read().decode('utf-8')
return generated_content[:max_length]
except Exception as e:
print(f"Error generating content: {e}")
return None
# Test the generate_content function
prompt = "Write a blog post introduction about the benefits of automated marketing platforms."
generated_content = generate_content(prompt)
if generated_content:
print("Generated Content:")
print(generated_content)
else:
print("Failed to generate content.")
我尝试增加实例数、增加连接超时、读取超时、重试尝试..但仍然遇到错误..
错误:
(env) macbook@Macbooks-MacBook-Pro content_generation % python content_generator.py
GPT-2 Model Information:
{'ModelName': 'gpt-2-model', 'Containers': [{'Image': '763104351884.dkr.ecr.eu-north-1.amazonaws.com/pytorch-inference:1.12.1-cpu-py38', 'Mode': 'SingleModel'}], 'ExecutionRoleArn': 'arn:aws:iam::471112657772:role/sagemaker', 'CreationTime': datetime.datetime(2024, 4, 19, 4, 38, 3, 872000, tzinfo=tzlocal()), 'ModelArn': 'arn:aws:sagemaker:eu-north-1:471112657772:model/gpt-2-model', 'EnableNetworkIsolation': False, 'DeploymentRecommendation': {'RecommendationStatus': 'COMPLETED', 'RealTimeInferenceRecommendations': [{'RecommendationId': 'gpt-2-model/pxjHziEz', 'InstanceType': 'ml.c5d.xlarge', 'Environment': {}}, {'RecommendationId': 'gpt-2-model/0TscHZCS', 'InstanceType': 'ml.g4dn.xlarge', 'Environment': {}}, {'RecommendationId': 'gpt-2-model/4pQJVxdM', 'InstanceType': 'ml.c5.2xlarge', 'Environment': {}}]}, 'ResponseMetadata': {'RequestId': 'bfeae9be-a458-4e89-baed-9d9954a9f1fe', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'bfeae9be-a458-4e89-baed-9d9954a9f1fe', 'content-type': 'application/x-amz-json-1.1', 'content-length': '731', 'date': 'Sun, 21 Apr 2024 07:27:19 GMT'}, 'RetryAttempts': 0}}
Endpoint Information:
{'EndpointName': 'gpt2-endpoint', 'EndpointArn': 'arn:aws:sagemaker:eu-north-1:471112657772:endpoint/gpt2-endpoint', 'EndpointConfigName': 'gpt2-config', 'ProductionVariants': [{'VariantName': 'default-variant-name', 'DeployedImages': [{'SpecifiedImage': '763104351884.dkr.ecr.eu-north-1.amazonaws.com/pytorch-inference:1.12.1-cpu-py38', 'ResolvedImage': '763104351884.dkr.ecr.eu-north-1.amazonaws.com/pytorch-inference@sha256:749b039a90e417706ed17cad5c977b5bc380d1dd2ed68b339e17a494ddc97b4c', 'ResolutionTime': datetime.datetime(2024, 4, 20, 14, 20, 57, 370000, tzinfo=tzlocal())}], 'CurrentWeight': 1.0, 'DesiredWeight': 1.0, 'CurrentInstanceCount': 2, 'DesiredInstanceCount': 2}], 'EndpointStatus': 'InService', 'CreationTime': datetime.datetime(2024, 4, 19, 10, 7, 28, 593000, tzinfo=tzlocal()), 'LastModifiedTime': datetime.datetime(2024, 4, 20, 14, 23, 20, 819000, tzinfo=tzlocal()), 'ResponseMetadata': {'RequestId': '858530d1-4e9b-458b-a298-bd1837479e25', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '858530d1-4e9b-458b-a298-bd1837479e25', 'content-type': 'application/x-amz-json-1.1', 'content-length': '704', 'date': 'Sun, 21 Apr 2024 07:27:19 GMT'}, 'RetryAttempts': 0}}
Error generating content: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (0) from primary with message "Your invocation timed out while waiting for a response from container primary. Review the latency metrics for each container in Amazon CloudWatch, resolve the issue, and try again.". See https://eu-north-1.console.aws.amazon.com/cloudwatch/home?region=eu-north-1#logEventViewer:group=/aws/sagemaker/Endpoints/gpt2-endpoint in account 471112657772 for more information.
Failed to generate content.
我对编程和Python不太熟悉,正在寻找导师..目前正在从事一个项目,我相信我需要有关它的指导..如果这里的专家可以提供帮助,我会非常高兴
错误消息表明 CloudWatch Logs 中提供了更多详细信息。您可以打开错误消息中包含的 URL 以查看详细的错误信息。另请参阅本文档 SageMaker 相关日志组名称的结构。