Utiliser l'application

Pour interroger un moteur de raisonnement, vous devez d'abord disposer d'une instance de moteur de raisonnement. Vous pouvez créer une instance ou obtenir une instance existante d'un moteur de raisonnement. Le reste de cette section part du principe que vous disposez d'une instance remote_app.

La commande suivante fournit une liste de schémas au format JSON correspondant aux opérations de l'objet remote_app :

remote_app.operation_schemas()

Voici un exemple de liste de schémas :

[
    {
        'description': 'Retrieves the exchange rate between two currencies on a specified date.\n'
        '\n'
        'Uses the Frankfurter API (https://api.frankfurter.app/) to obtain exchange rate data.\n'
        '\n'
        'Args:\n'
        '    currency_from: The base currency (3-letter currency code).\n'
        '        Defaults to "USD" (US Dollar).\n'
        '    currency_to: The target currency (3-letter currency code).\n'
        '        Defaults to "EUR" (Euro).\n'
        '    currency_date: The date for which to retrieve the exchange rate.\n'
        '        Defaults to "latest" for the most recent exchange rate data.\n'
        '        Can be specified in YYYY-MM-DD format for historical rates.\n'
        '\n'
        'Returns:\n'
        '    dict: A dictionary containing the exchange rate information.\n'
        '        Example: {"amount": 1.0, "base": "USD", "date": "2023-11-24",\n'
        '            "rates": {"EUR": 0.95534}}',
        'name': 'query',
        'parameters': {
            'type': 'object',
            'properties': {
                'currency_from': {'type': 'string'},
                'currency_to': {'type': 'string'},
                'currency_date': {'type': 'string'},
            },
            'required': [],
        },
        'api_mode': '',
    },
    {
        'description': 'Retrieves the exchange rate between two currencies on a specified date.\n'
        '\n'
        'Uses the Frankfurter API (https://api.frankfurter.app/) to obtain exchange rate data.\n'
        '\n'
        'Args:\n'
        '    currency_from: The base currency (3-letter currency code).\n'
        '        Defaults to "USD" (US Dollar).\n'
        '    currency_to: The target currency (3-letter currency code).\n'
        '        Defaults to "EUR" (Euro).\n'
        '    currency_date: The date for which the exchange rate is retrieved.\n'
        '        Defaults to "latest" for the most recent exchange rate data.\n'
        '        Can be specified in YYYY-MM-DD format for historical rates.\n'
        '\n'
        'Returns:\n'
        '    generator: A generator yielding dictionaries representing intermediate steps and the final result.\n'
        '        Intermediate steps examples:\n'
        '            {'
        '                "actions": ['
        '                    {'
        '                        "tool": "get_exchange_rate",'
        '                        "tool_input": {'
        '                            "currency_from": "USD",'
        '                            "currency_to": "SEK"'
        '                        },'
        '                        "tool_output": ...'
        '                    }'
        '                ]'
        '            }\n'
        '            {'
        '                "steps": ['
        '                    {'
        '                        "action": {'
        '                            "tool": "get_exchange_rate",'
        '                            "tool_input": {'
        '                                "currency_from": "USD",'
        '                                "currency_to": "SEK"'
        '                            },'
        '                            "tool_output": ...'
        '                        },'
        '                        "observation": ...'
        '                    }'
        '                ]'
        '            }\n'
        '            {'
        '                "output": "The exchange rate from US dollars to Swedish currency (SEK) is ...",'
        '                "messages": [...]'
        '            }\n',
        'name': 'stream_query',
        'parameters': {
            'type': 'object',
            'properties': {
                'currency_from': {'type': 'string'},
                'currency_to': {'type': 'string'},
                'currency_date': {'type': 'string'},
            },
            'required': [],
        },
        'api_mode': 'stream',
    }
]

Pour interroger Reasoning Engine, utilisez la méthode .query(). Pour éviter toute ambiguïté, spécifiez le nom de chaque argument.

SDK Vertex AI pour Python

La commande suivante est un exemple de requête du moteur de raisonnement :

remote_app = reasoning_engines.ReasoningEngine("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID")

response = remote_app.query(input="What is the exchange rate from US dollars to Swedish currency?")

REST

La commande suivante est un exemple de requête du moteur de raisonnement :

curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID:query -d '{
  "input": {
    "input": "What is the exchange rate from US dollars to Swedish currency?"
  }
}'

La réponse à la requête est une chaîne semblable à la sortie d'un test d'application locale :

{"input": "What is the exchange rate from US dollars to Swedish currency?",
 # ...
 "output": "For 1 US dollar you will get 10.7345 Swedish Krona."}

Pour diffuser des réponses à partir de Reasoning Engine, utilisez la méthode .stream_query(). Pour éviter toute ambiguïté, spécifiez le nom de chaque argument.

SDK Vertex AI pour Python

La commande suivante est un exemple de stream_query du moteur de raisonnement:

remote_app = reasoning_engines.ReasoningEngine("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID")

stream_response = remote_app.stream_query(input="What is the exchange rate from US dollars to Swedish currency?")

REST

La commande suivante est un exemple de stream_query du moteur de raisonnement:

curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID:streamQuery?alt=sse -d '{
  "input": {
    "input": "What is the exchange rate from US dollars to Swedish currency?"
  }
}'

Le moteur de raisonnement diffuse les réponses sous la forme d'une séquence d'objets générés de manière itérée. Par exemple, un ensemble de trois réponses peut se présenter comme suit:

{'actions': [{'tool': 'get_exchange_rate', ...}]}  # first response
{'steps': [{'action': {'tool': 'get_exchange_rate', ...}}]}  # second response
{'output': 'The exchange rate is 11.0117 SEK per USD as of 2024-12-03.'}  # final response

Étape suivante