기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
AWS SDK와 CreatePrompt
함께 사용
다음 코드 예시는 CreatePrompt
의 사용 방법을 보여 줍니다.
작업 예제는 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 다음 코드 예제에서는 컨텍스트 내에서 이 작업을 확인할 수 있습니다.
- Python
-
- SDK for Python (Boto3)
-
Amazon Bedrock 관리형 프롬프트를 생성합니다.
def create_prompt(client, prompt_name, prompt_description, prompt_template, model_id=None):
"""
Creates an Amazon Bedrock managed prompt.
Args:
client: Amazon Bedrock Agent boto3 client.
prompt_name (str): The name for the new prompt.
prompt_description (str): The description for the new prompt.
prompt_template (str): The template for the prompt.
model_id (str, optional): The model ID to associate with the prompt.
Returns:
dict: The response from CreatePrompt.
"""
try:
logger.info("Creating prompt: %s.", prompt_name)
# Create a variant with the template
variant = {
"name": "default",
"templateType": "TEXT",
"templateConfiguration": {
"text": {
"text": prompt_template,
"inputVariables": []
}
}
}
# Extract input variables from the template
# Look for patterns like {{variable_name}}
variables = re.findall(r'{{(.*?)}}', prompt_template)
for var in variables:
variant["templateConfiguration"]["text"]["inputVariables"].append({"name": var.strip()})
# Add model ID if provided
if model_id:
variant["modelId"] = model_id
# Create the prompt with the variant
create_params = {
'name': prompt_name,
'description': prompt_description,
'variants': [variant]
}
response = client.create_prompt(**create_params)
logger.info("Successfully created prompt: %s. ID: %s",
prompt_name,
response['id'])
return response
except ClientError as e:
logger.exception("Client error creating prompt: %s", str(e))
raise
except Exception as e:
logger.exception("Unexpected error creating prompt: %s", str(e))
raise
AWS SDK 개발자 가이드 및 코드 예제의 전체 목록은 섹션을 참조하세요AWS SDK에서 Amazon Bedrock 사용. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.