Nova를 사용하여 구조화된 출력 생성 - Amazon Nova

Nova를 사용하여 구조화된 출력 생성

구조화된 출력은 다운스트림 사용 사례가 생성된 출력을 더 효과적으로 소비하고 처리할 수 있도록 하기 때문에 머신 간 통신 사용 사례에 매우 중요합니다. 문서에서 정보를 추출하든, API에서 데이터를 가져오는 어시스턴트를 생성하든, 조치를 취하는 에이전트를 개발하든, 이러한 작업을 수행하려면 파운데이션 모델이 구조화된 특정 형식으로 출력을 생성해야 합니다.

Nova 모델은 제한된 디코딩을 활용하여 생성된 출력에서 높은 모델 신뢰성을 보장하고 모델이 복잡한 스키마를 쉽게 처리할 수 있도록 합니다. 제한된 디코딩은 문법에 따라 모델이 각 단계에서 출력할 수 있는 가능한 토큰을 ‘제한’합니다. 이는 출력 생성 시 모델이 선택할 수 있는 실제 토큰을 변경하기 때문에 이전에 사용된 프롬프트 기법과 구별됩니다. 예를 들어 JSON 객체를 닫을 때 모델은 선택할 } 토큰으로만 제한됩니다. 도구 구성이 전달될 때마다 제한된 디코딩이 활용됩니다. 도구 사용은 이미 특정 스키마를 제공하므로 이를 사용하여 개발자가 원하는 스키마를 기반으로 문법을 동적으로 생성할 수 있습니다. 제한된 디코딩은 모델이 잘못된 키를 생성하지 않도록 방지하고 정의된 스키마를 기반으로 올바른 데이터 유형을 적용합니다.

구조화된 출력에서 도구 사용을 활용하기 위해 기본 단계에서 출력에 필요한 JSON 스키마를 정의해야 합니다. 다음은 tool_config 정의 내에 정의된 JSON 스키마의 예입니다.

tool_config = { "tools": [ { "toolSpec": { "name": "ProductAnalysis", "description": "Analyze product information from text.", "inputSchema": { "json": { "type": "object", "properties": { "name": { "type": "string", "description": "Product name" }, "rating": { "maximum": 5, "description": "Customer rating 1-5", "type": [ "number", "null" ], "minimum": 1 }, "features": { "description": "Key product features", "type": "array", "items": { "type": "string" } }, "category": { "type": "string", "description": "Product category" }, "price": { "type": "number", "description": "Price in USD" } }, "required": [ "name", "category", "price", "features" ] } } } } ], "toolChoice": { "tool": { "name": "ProductAnalysis" } } }

나중에 모델을 사용하여 도구를 직접적으로 호출하면 스키마 형식으로 응답하는 출력이 수신됩니다. 예를 들어, 다음은 Python에서 모델을 직접적으로 호출하는 예입니다.

import boto3 client = boto3.client("bedrock-runtime") model_id = "amazon.nova-lite-1-5-v1:0" user_query = """The Amazon Kindle Scribe is a state-of-the-art e-reader designed for both reading and writing, featuring a 10.2-inch paper-like display and a premium stylus. This versatile device allows users to enjoy books, take notes, annotate PDFs, and even sketch, making it ideal for readers, students, and professionals. Priced at $339.99, it falls under the electronics category and boasts features like a front light, adjustable warm light settings, and up to 12 weeks of battery life on a single charge. Customer ratings for the Kindle Scribe average around 4.5 stars, reflecting its high user satisfaction.""" messages = [{ "role": "user", "content": [{ "text": user_query }] }] system = [{"text": "Leverage the ProductAnalysis tool to extract product information"}] inference_params = {"temperature": 0} response = client.converse(modelId=model_id, system=system, messages=messages, toolConfig=tool_config, inferenceConfig=inference_params) print(next( block["toolUse"] for block in response["output"]["message"]["content"] if "toolUse" in block ))

그러면 출력이 다음과 같이 나타납니다.

{ "toolUseId": "tooluse_hke1FUeuRbKXK8DPqIptVg", "name": "ProductAnalysis", "input": { "name": "Amazon Kindle Scribe", "rating": 4.5, "features": [ "10.2-inch paper-like display", "premium stylus", "front light", "adjustable warm light settings", "up to 12 weeks of battery life" ], "category": "electronics", "price": 339.99 } }