Saltar al contenido principal
Versión: Siguiente

Asynchronous Call

Si no desea esperar la respuesta del OpenAPI al hacer una petición a su API, debe utilizar un código asíncrono.

To make asynchronous calls, you must provide a callback 4D.Function(Formula) in the OpenAIParameters object parameter to receive the result.

The callback function will receive the same result object type (one of OpenAIResult child classes) that would be returned by the function in synchronous code. Ver ejemplos más abajo.

Consideraciones sobre el proceso

The asynchronous method is based on 4D.HTTPRequest, so the response will be received within the current process.

⚠️ If your process ends at the conclusion of the current method (e.g., using New process, or playing in the method editor), the callback formula might not be called asynchronously. En estos casos, considere la posibilidad de utilizar CALL WORKER o CALL FORM.

Ejemplos de uso

lista de modelos

$client.models.list({formula: Formula(MyReceiveMethod($1))})

$1 será una instancia de OpenAIModelListResult, por lo que el método MyReceiveMethod podría ser:

#DECLARE($result: cs.AIKit.OpenAIModelListResult)

If($result.success)

Form.models:=$result.models

Else

Alert($result.errors.formula(Formula(JSON Stringify($1))).join("\n"))

End if

finalización del chat

var $messages:=[{role: "system"; content: "You are a helpful assistant."}]
$messages.push({role: "user"; content: "Could you explain me why 42 is a special number"})

$client.chat.completions.create($messages; { onResponse: Formula(MyChatCompletionsReceiveMethod($1))})

$1 será una instancia de OpenAIChatCompletionsResult, así que el método MyChatCompletionsReceiveMethod podría ser:

#DECLARE($result: cs.AIKit.OpenAIChatCompletionsResult)

ASSERT($result.success) // We use onResponse here, callback receive only if success
Form.assistantMessage:=$result.choices[0].text