

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

# Amazon Titan Textmodelle
<a name="model-parameters-titan-text"></a>

Die Modelle von Amazon Titan Text unterstützen die folgenden Inferenzparameter.

Weitere Informationen zu den Prompt-Engineering-Richtlinien für Titan Text finden Sie unter [Prompt-Engineering-Richtlinien für Titan Text](https://d2eo22ngex1n9g.cloudfront.net/Documentation/User+Guides/Titan/Amazon+Titan+Text+Prompt+Engineering+Guidelines.pdf). 

Weitere Informationen zu Titan-Modellen finden Sie unter [Überblick über Amazon Titan -Modelle von](titan-models.md).

**Topics**
+ [Anforderung und Antwort](#model-parameters-titan-request-response)
+ [Codebeispiele](#inference-titan-code)

## Anforderung und Antwort
<a name="model-parameters-titan-request-response"></a>

Der Text der Anfrage wird im `body` Feld einer [InvokeModel[InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html)Oder-Anfrage übergeben.

------
#### [ Request ]

```
{
    "inputText": string,
    "textGenerationConfig": {
        "temperature": float,  
        "topP": float,
        "maxTokenCount": int,
        "stopSequences": [string]
    }
}
```

Die folgenden Parameter sind erforderlich:
+ **inputText** – Der Prompt zum Bereitstellen des Models, damit es eine Antwort generiert. Wenn Sie Antworten in einem Konversationsstil generieren möchten, übermitteln Sie den Prompt in folgendem Format:

  ```
  "inputText": "User: {{<theUserPrompt>}}\nBot:"
  ```

  Dieses Format vermittelt dem Modell, dass es in einer neuen Zeile antworten soll, nachdem der Benutzer einen Prompt angegeben hat.

Die `textGenerationConfig` ist optional. Sie können sie zum Konfigurieren der folgenden [Inferenzparameter](inference-parameters.md) verwenden:
+ **Temperatur** – Verwenden Sie einen niedrigeren Wert, um die Zufälligkeit der Antwort zu verringern.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/de_de/bedrock/latest/userguide/model-parameters-titan-text.html)
+ **topP** – Verwenden Sie einen niedrigeren Wert, um weniger wahrscheinliche Optionen zu ignorieren und die Vielfalt der Antworten zu verringern. Der Mindestwert schließt 0 aus. Verwenden Sie stattdessen einen sehr kleinen Wert wie 0,0001.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/de_de/bedrock/latest/userguide/model-parameters-titan-text.html)
+ **max TokenCount** — Geben Sie die maximale Anzahl von Token an, die in der Antwort generiert werden sollen. Die maximalen Token-Limits werden strikt durchgesetzt.  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/de_de/bedrock/latest/userguide/model-parameters-titan-text.html)
+ **stopSequences** – Geben Sie eine Zeichenfolge an, um festzulegen, wo das Modell anhalten soll.

------
#### [ InvokeModel Response ]

```
{
    "inputTextTokenCount": int,
    "results": [{
        "tokenCount": int,
        "outputText": "\n{{<response>}}\n",
        "completionReason": "string"
    }]
}
```

Der Antworttext enthält die folgenden Felder:
+ **input TextTokenCount** — Die Anzahl der Token in der Eingabeaufforderung.
+ **results** – Ein Array aus einem Element, ein Objekt, das die folgenden Felder enthält:
  + **tokenCount** – Die Anzahl der Token in der Antwort
  + **outputText** – Der Text in der Antwort
  + **completionReason** – Der Grund, warum die Antwort nicht mehr generiert wurde. Die folgenden Gründe sind möglich:
    + FINISHED – Die Antwort wurde vollständig generiert.
    + LENGTH – Die Antwort wurde aufgrund der von Ihnen festgelegten Antwortlänge gekürzt.
    + STOP\_CRITERIA\_MET – Die Antwort wurde gekürzt, weil die Stopp-Kriterien erreicht wurden
    + RAG\_QUERY\_WHEN\_RAG\_DISABLED – Das Feature ist deaktiviert und kann die Abfrage nicht abschließen.
    + CONTENT\_FILTERED – Der Inhalt wurde durch den angewendeten Inhaltsfilter gefiltert oder entfernt.

------
#### [ InvokeModelWithResponseStream Response ]

Jeder Textblock im Text des Antwortstreams weist das folgende Format auf. Sie müssen das `bytes`-Feld dekodieren (ein Beispiel finden Sie unter [Senden Sie eine einzelne Aufforderung mit InvokeModelSenden Sie eine einzelne Aufforderung mit InvokeModel (verschoben)](inference-invoke.md)).

```
{
    "chunk": {
        "bytes": b'{
            "index": int,
            "inputTextTokenCount": int,
            "totalOutputTextTokenCount": int,
            "outputText": "{{<response-chunk>}}",
            "completionReason": "string"
        }'
    }
}
```
+ **index** – Der Index des Blocks in der Streaming-Antwort
+ **input TextTokenCount** — Die Anzahl der Token in der Eingabeaufforderung.
+ **total OutputTextTokenCount** — Die Anzahl der Token in der Antwort.
+ **outputText** – Der Text in der Antwort
+ **completionReason** – Der Grund, warum die Antwort nicht mehr generiert wurde. Die folgenden Gründe sind möglich.
  + FINISHED – Die Antwort wurde vollständig generiert.
  + LENGTH – Die Antwort wurde aufgrund der von Ihnen festgelegten Antwortlänge gekürzt.
  + STOP\_CRITERIA\_MET – Die Antwort wurde gekürzt, weil die Stopp-Kriterien erreicht wurden
  + RAG\_QUERY\_WHEN\_RAG\_DISABLED – Das Feature ist deaktiviert und kann die Abfrage nicht abschließen.
  + CONTENT\_FILTERED – Der Inhalt wurde durch den angewendeten Filter gefiltert oder entfernt.

------

## Codebeispiele
<a name="inference-titan-code"></a>

Das folgende Beispiel zeigt, wie Inferenzen mit dem Modell Amazon Titan Text Premier mit dem Python SDK ausgeführt werden.

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to create a list of action items from a meeting transcript
with the Amazon Titan Text model (on demand).
"""
import json
import logging
import boto3

from botocore.exceptions import ClientError


class ImageError(Exception):
    "Custom exception for errors returned by Amazon Titan Text models"

    def __init__(self, message):
        self.message = message


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_text(model_id, body):
    """
    Generate text using Amazon Titan Text models on demand.
    Args:
        model_id (str): The model ID to use.
        body (str) : The request body to use.
    Returns:
        response (json): The response from the model.
    """

    logger.info(
        "Generating text with Amazon Titan Text model %s", model_id)

    bedrock = boto3.client(service_name='bedrock-runtime')

    accept = "application/json"
    content_type = "application/json"

    response = bedrock.invoke_model(
        body=body, modelId=model_id, accept=accept, contentType=content_type
    )
    response_body = json.loads(response.get("body").read())

    finish_reason = response_body.get("error")

    if finish_reason is not None:
        raise ImageError(f"Text generation error. Error is {finish_reason}")

    logger.info(
        "Successfully generated text with Amazon Titan Text model %s", model_id)

    return response_body


def main():
    """
    Entrypoint for Amazon Titan Text model example.
    """
    try:
        logging.basicConfig(level=logging.INFO,
                            format="%(levelname)s: %(message)s")

        # You can replace the model_id with any other Titan Text Models
        # Titan Text Model family model_id is as mentioned below:
        # amazon.titan-text-premier-v1:0, amazon.titan-text-express-v1, amazon.titan-text-lite-v1
        model_id = 'amazon.titan-text-premier-v1:0'

        prompt = """Meeting transcript: Miguel: Hi Brant, I want to discuss the workstream  
            for our new product launch Brant: Sure Miguel, is there anything in particular you want
            to discuss? Miguel: Yes, I want to talk about how users enter into the product.
            Brant: Ok, in that case let me add in Namita. Namita: Hey everyone 
            Brant: Hi Namita, Miguel wants to discuss how users enter into the product.
            Miguel: its too complicated and we should remove friction.  
            for example, why do I need to fill out additional forms?  
            I also find it difficult to find where to access the product
            when I first land on the landing page. Brant: I would also add that
            I think there are too many steps. Namita: Ok, I can work on the
            landing page to make the product more discoverable but brant
            can you work on the additonal forms? Brant: Yes but I would need 
            to work with James from another team as he needs to unblock the sign up workflow.
            Miguel can you document any other concerns so that I can discuss with James only once?
            Miguel: Sure.
            From the meeting transcript above, Create a list of action items for each person. """

        body = json.dumps({
            "inputText": prompt,
            "textGenerationConfig": {
                "maxTokenCount": 3072,
                "stopSequences": [],
                "temperature": 0.7,
                "topP": 0.9
            }
        })

        response_body = generate_text(model_id, body)
        print(f"Input token count: {response_body['inputTextTokenCount']}")

        for result in response_body['results']:
            print(f"Token count: {result['tokenCount']}")
            print(f"Output text: {result['outputText']}")
            print(f"Completion reason: {result['completionReason']}")

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))
    except ImageError as err:
        logger.error(err.message)
        print(err.message)

    else:
        print(
            f"Finished generating text with the Amazon Titan Text Premier model {model_id}.")


if __name__ == "__main__":
    main()
```

Das folgende Beispiel zeigt, wie Inferenzen mit dem Modell Amazon Titan Text G1 - Express mit dem Python SDK ausgeführt werden.

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to create a list of action items from a meeting transcript
with the Amazon &titan-text-express; model (on demand).
"""
import json
import logging
import boto3

from botocore.exceptions import ClientError


class ImageError(Exception):
    "Custom exception for errors returned by Amazon &titan-text-express; model"

    def __init__(self, message):
        self.message = message


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def generate_text(model_id, body):
    """
    Generate text using Amazon &titan-text-express; model on demand.
    Args:
        model_id (str): The model ID to use.
        body (str) : The request body to use.
    Returns:
        response (json): The response from the model.
    """

    logger.info(
        "Generating text with Amazon &titan-text-express; model %s", model_id)

    bedrock = boto3.client(service_name='bedrock-runtime')

    accept = "application/json"
    content_type = "application/json"

    response = bedrock.invoke_model(
        body=body, modelId=model_id, accept=accept, contentType=content_type
    )
    response_body = json.loads(response.get("body").read())

    finish_reason = response_body.get("error")

    if finish_reason is not None:
        raise ImageError(f"Text generation error. Error is {finish_reason}")

    logger.info(
        "Successfully generated text with Amazon &titan-text-express; model %s", model_id)

    return response_body


def main():
    """
    Entrypoint for Amazon &titan-text-express; example.
    """
    try:
        logging.basicConfig(level=logging.INFO,
                            format="%(levelname)s: %(message)s")

        model_id = 'amazon.titan-text-express-v1'

        prompt = """Meeting transcript: Miguel: Hi Brant, I want to discuss the workstream  
            for our new product launch Brant: Sure Miguel, is there anything in particular you want
            to discuss? Miguel: Yes, I want to talk about how users enter into the product.
            Brant: Ok, in that case let me add in Namita. Namita: Hey everyone 
            Brant: Hi Namita, Miguel wants to discuss how users enter into the product.
            Miguel: its too complicated and we should remove friction.  
            for example, why do I need to fill out additional forms?  
            I also find it difficult to find where to access the product
            when I first land on the landing page. Brant: I would also add that
            I think there are too many steps. Namita: Ok, I can work on the
            landing page to make the product more discoverable but brant
            can you work on the additonal forms? Brant: Yes but I would need 
            to work with James from another team as he needs to unblock the sign up workflow.
            Miguel can you document any other concerns so that I can discuss with James only once?
            Miguel: Sure.
            From the meeting transcript above, Create a list of action items for each person. """

        body = json.dumps({
            "inputText": prompt,
            "textGenerationConfig": {
                "maxTokenCount": 4096,
                "stopSequences": [],
                "temperature": 0,
                "topP": 1
            }
        })

        response_body = generate_text(model_id, body)
        print(f"Input token count: {response_body['inputTextTokenCount']}")

        for result in response_body['results']:
            print(f"Token count: {result['tokenCount']}")
            print(f"Output text: {result['outputText']}")
            print(f"Completion reason: {result['completionReason']}")

    except ClientError as err:
        message = err.response["Error"]["Message"]
        logger.error("A client error occurred: %s", message)
        print("A client error occured: " +
              format(message))
    except ImageError as err:
        logger.error(err.message)
        print(err.message)

    else:
        print(
            f"Finished generating text with the Amazon &titan-text-express; model {model_id}.")


if __name__ == "__main__":
    main()
```