Strukturierte Ausgabe erforderlich - Amazon Nova

Strukturierte Ausgabe erforderlich

Um konsistente und strukturierte Ausgabeformate zu gewährleisten, können Sie strukturierte Ausgaben verwenden, einschließlich Formaten wie XML, JSON oder Markdown. Dieser Ansatz ermöglicht es nachgelagerten Anwendungsfällen, die vom Modell generierten Ausgaben effektiver zu nutzen und zu verarbeiten. Durch die Bereitstellung expliziter Anweisungen für das Modell werden die Antworten auf eine Weise generiert, die einem vordefinierten Schema entspricht. Wir empfehlen, dass Sie einen output schema angeben, dem das Modell folgt.

Wenn der Downstream-Parser beispielsweise bestimmte Namenskonventionen für Schlüssel in einem JSON-Objekt erwartet, sollten Sie dies in einem Ausgabeschema-Feld der Abfrage angeben. Wenn Sie es vorziehen, Antworten im JSON-Format ohne Präambeltext zu erhalten, weisen Sie das Modell außerdem entsprechend an. Geben Sie also explizit an: „Bitte generiere nur JSON-Ausgabe. Geben Sie KEINE Präambel an.“.

Verwendung von Vorausfüllungen, um das Modell zu unterstützen

Eine effiziente Alternative besteht darin, die Reaktion des Modells durch Vorausfüllen des assistant-Inhalts zu beeinflussen. Mit dieser Technik können Sie die Aktionen des Modells steuern, Präambeln umgehen und bestimmte Ausgabeformate wie JSON und XML erzwingen. Wenn Sie den Inhalt des Assistenten beispielsweise mit "{" oder "```json" vorausfüllen, kann diese Eingabe das Modell dazu veranlassen, das JSON-Objekt zu generieren, ohne zusätzliche Informationen bereitzustellen.

Tipp

Wenn Sie explizit nach dem Extrahieren von JSON suchen, besteht ein häufig beobachtetes Muster darin, es vorab mit ```json auszufüllen und eine Stoppsequenz bei ``` hinzuzufügen. Dadurch wird sichergestellt, dass das Modell ein JSON-Objekt ausgibt, welchess programmgesteuert analysiert werden kann.

Im Folgenden finden Sie Beispiele für einige gängige Formatierungsschemas.

JSON
JSON_schema = """Make sure your final response is a valid JSON schema follow the below Response Schema: ##Response Schema: ```json { "key1": "value1", "key2": "value2", key3: [{ "key3_1": "value_3_1, "key3_2": "value_3_2, ...}``` """
XML
XML_format = """Make sure your final response is a valid XML schema follow the below Response Schema: ##Response Schema: <thinking> ( your thoughts go hee ) </thinking> <output> <task>"task1"</task> <subtask> <task1_result> ( task 1 result )</task1_result> <task2_result> ( task 2 result )</task2_result> <task3_result> ( task 3 result )</task3_result> </subtask> <task>"task2"</task> <subtask> <task1_result> ( task 1 result )</task1_result> <task2_result> ( task 2 result )</task2_result> <task3_result> ( task 3 result )</task3_result> </subtask> </output> """
Markdown
markdown_schema = """Make sure your final response is a valid Markdown schema follow the below Response Schema: ##Response Schema: ## Introduction ( 2-3 line intro) ## Design Guidance (Bulleted list of design guidance) ## Step by Step Instructions on Execution ( Bulleted list of instructions with each with bold title. ## Conclusion ( conclusion ) """

Der folgende Code veranschaulicht, wie man mit der API vorab ausfüllt:

import boto3 import json # Create a Bedrock Runtime client. client = boto3.client( "bedrock-runtime", region_name="us-east-1" ) request_body = { "system": [ {"text": "You write JSON objects based on the given instructions"} ], "messages": [ { "role": "user", "content": [{"text": "Provide details about the best selling full-frame cameras in past three years. Answer in JSON format with keys like name, brand, price and a summary."}] }, { "role": "assistant", "content": [{"text": " Here is the JSON response: ```json"}] }, ], "inferenceConfig": { "maxTokens": 300, "temperature": 0, } } # Invoke the model and extract the response body. response = client.invoke_model( modelId="amazon.nova-micro-v1:0", body=json.dumps(request_body) ) model_response = json.loads(response["body"].read())
Anmerkung

Für strukturierte Ausgaben empfehlen wir die Verwendung von Greedy-Dekodierungsparametern, unabhängig davon, ob Sie die Verwendung von Tools nutzen. Das heißt, temperature=0.

Durch Hinzufügen des Abschnitts „Ausgabeschema“ mit dem entsprechenden Schema wird das Modell dazu veranlasst, dieses Schema vorrangig anzupassen. Die Modellausgabe ist jedoch nicht deterministisch und kann vom Ausgabeschema abweichen.

Rolle

Prompt

Benutzer

Gib Einzelheiten zu den meistverkauften Vollformatkameras der letzten drei Jahre an. Antworte im JSON-Format mit Schlüsseln wie Name, Marke, Preis und einer Zusammenfassung.

Gib Einzelheiten zu den meistverkauften Vollformatkameras der letzten drei Jahre an. Du darfst NUR im JSON-Format antworten. Bitte folge dem untenstehenden Ausgabeschema.

Ausgabeschema:

[{ "name": "name goes here", "brand": "brand goes here", "price": "price goes here", "summary": "summary goes here" }]

Assistent

[ { "name": "Sony Alpha A7R IV", "brand": "Sony", "price": "Approximately $3,500", "summary": "The Sony Alpha A7R IV is renowned for its exceptional image quality, thanks to a 61-megapixel full-frame sensor. It also features 4K video recording and a robust build, making it a favorite among professional photographers and videographers." }, { "name": "Canon EOS R5", "brand": "Canon", "price": "Approximately $3,900", "summary": "The Canon EOS R5 combines high-resolution imaging with advanced video capabilities. It houses a 45-megapixel sensor, 8K video recording, and impressive autofocus performance, appealing to both photography and filmmaking professionals." }, { "name": "Nikon Z7 II", "brand": "Nikon", "price": "Approximately $2,800", "summary": "The Nikon Z7 II offers a 45.7-megapixel full-frame sensor and is praised for its high resolution and excellent dynamic range. It also supports 4K video recording and features a versatile Z-mount lens system, making it a strong contender in the full-frame market." }, { "name": "Fujifilm GFX 100S", "brand": "Fujifilm", "price": "Approximately $7,000", "summary": "While not a traditional full-frame camera, the Fujifilm GFX 100S stands out with its 102-megapixel medium format sensor. It is lauded for its stunning detail and color reproduction, though it comes at a higher price point and is aimed at specialized users." } ]
[ { "name": "Nikon D850", "brand": "Nikon", "price": "$3,296.95", "summary": "The Nikon D850 is a professional full-frame DSLR camera known for its exceptional image quality, 45.7-megapixel resolution, 7 fps continuous shooting, and 4K video capabilities." }, { "name": "Canon EOS 5D Mark IV", "brand": "Canon", "price": "$3,499.00", "summary": "The Canon EOS 5D Mark IV is a popular full-frame DSLR appreciated for its 30.4-megapixel sensor, 4K video recording, robust build quality, and reliable autofocus system." }, { "name": "Sony A7R III", "brand": "Sony", "price": "$3,198.00", "summary": "The Sony A7R III is a high-resolution full-frame mirrorless camera renowned for its 42.4-megapixel sensor, excellent low-light performance, and versatile lens compatibility." }, { "name": "Fujifilm GFX 100S", "brand": "Fujifilm", "price": "$5,999.99", "summary": "The Fujifilm GFX 100S is a medium format mirrorless camera that stands out for its 102-megapixel sensor, high dynamic range, and exceptional color reproduction." }, { "name": "Sony A7 III", "brand": "Sony", "price": "$1,998.00", "summary": "The Sony A7 III is a full-frame mirrorless camera popular among enthusiasts and professionals for its 24.2-megapixel sensor, fast autofocus, and 4K video capabilities." } ]

Ein weiterer Ansatz besteht darin, die Antwort des Modells vorauszufüllen, indem der gewünschte Anfangstext in die Nachricht des Assistenten aufgenommen wird. In diesem Fall wird die Antwort des Amazon-Nova-Modells dort fortgesetzt, wo die Nachricht des Assistenten aufhört.

Rolle

Prompt

Benutzer

Gib Einzelheiten zu den meistverkauften Vollformatkameras der letzten drei Jahre an. Deine Antwort sollte im JSON-Format mit den folgenden Schlüsseln erfolgen: Name, Marke, Jahr, Preis, Zusammenfassung.

Assistent (Vorausfüllung)

```json

Assistent

{ "cameras": [ { "name": "Sony Alpha A7 III", "brand": "Sony", "year": 2018, "price": "$1999", "summary": "The Sony Alpha A7 III is a highly acclaimed full-frame mirrorless camera known for its excellent image quality, advanced autofocus system, and 4K video capabilities." }, { "name": "Canon EOS R5", "brand": "Canon", "year": 2020, "price": "$3899", "summary": "The Canon EOS R5 is a top-tier full-frame mirrorless camera that offers stunning 45MP images, 8K video recording, and exceptional performance in various lighting conditions." }, { "name": "Nikon Z7 II", "brand": "Nikon", "year": 2020, "price": "$2999", "summary": "The Nikon Z7 II is a flagship full-frame mirrorless camera with a 45.7MP sensor, superb image quality, and advanced video recording features." }, { "name": "Fujifilm GFX 100S", "brand": "Fujifilm", "year": 2020, "price": "$7999", "summary": "The Fujifilm GFX 100S is a medium format mirrorless camera known for its impressive 102MP sensor, beautiful film simulations, and versatile lens options." }, { "name": "Sony Alpha A7R IV", "brand": "Sony", "year": 2019, "price": "$3499", "summary": "The Sony Alpha A7R IV features a 61MP sensor, excellent low-light performance, and advanced video capabilities, making it a favorite among professional photographers." } ] } ```

Ein dritter Ansatz besteht darin, mithilfe von Tools ein bestimmtes Schema für die Antwort des Modells zu erzwingen, indem das pydantische JSON-Schema in den Abschnitt zum Toolverwendungsschema aufgenommen wird. Sie können die Tool-Auswahl anhand des bereitgestellten Schemas angeben und die Antwort von Amazon Nova wird auf der Grundlage des ausgewählten Tools strukturiert. Weitere Informationen zur Nutzung von Tools finden Sie unter Verwendung von Tools (Funktionsaufruf) mit Amazon Nova.

Benutzer

Extrahiere aus der unten angegebenen Abfrage die relevanten Entitäten

Query: John works in BUILDING-0987 and has been in charge of product id 23564#. His performance has been excellent in past year and he is up for a raise. Use the print_entities tool.

ToolConfig

tool_config = { "tools": [ { "toolSpec": { "name": "print_entities", "description": "Extract the named entity based on provided input", "inputSchema": { "type": "object", "properties": { "name": { "type": "string", "description": "The extracted entity name. This should be a name of a person, place, animal or thing" }, "location": { "type": "string", "description": "The extracted location name. This is a site name or a building name like SITE-001 or BUILDING-003" }, "product": { "type": "string", "description": "The extracted product code, this is generally a 6 digit alphanumeric code such as 45623#, 234567" } }, "required": ["name", "location", "product"] } } } ], "toolChoice": { "tool": { "name": "print_entities" } } }