Auto Light Dark
Auto Light Dark
German English

addObject

Die AIUtils stehen nur zur Verfügung, wenn man das Modul „KI-Assistent“ lizenziert hat.

Code

AIStructuredOutputConfig addObject(AIStructuredOutputConfig config, String key, boolean required, String description, AIStructuredOutputConfig objectConfig)

Beschreibung

Fügt einer AIStructuredOutputConfig ein verschachteltes Objekt hinzu.

Mit dieser Methode definieren Sie ein Unterobjekt innerhalb einer strukturierten KI-Antwort. Dabei legen Sie den Feldnamen, die fachliche Beschreibung, ob das Objekt erforderlich ist, sowie eine untergeordnete AIStructuredOutputConfig für die Struktur des Unterobjekts fest.

Die übergebene Konfiguration wird direkt verändert. Eine erneute Zuweisung des Rückgabewerts ist daher nicht erforderlich.

Parameter

  • config (AIStructuredOutputConfig) - Die AIStructuredOutputConfig, die erweitert werden soll.

  • key (String) - Der Feldschlüssel innerhalb des aktuellen Objekts, z. B. "customer".

  • required (boolean) - Legt fest, ob das Objekt erforderlich ist.

    • true: Das Objekt ist erforderlich.

    • false: Die KI darf das gesamte Objekt weglassen.

  • description (String) - Fachliche Beschreibung des Objekts. Diese Beschreibung wird verwendet, um der KI die Bedeutung und den erwarteten Inhalt des Unterobjekts zu verdeutlichen.

  • objectConfig (AIStructuredOutputConfig) - Die untergeordnete AIStructuredOutputConfig, die die Felder des verschachtelten Objekts definiert.

Rückgabe

AIStructuredOutputConfig - Im Erfolgsfall gibt die Methode dieselbe AIStructuredOutputConfig-Instanz zurück. Im Fehlerfall wird keine Konfiguration zurückgegeben. Stattdessen wird eine IllegalArgumentException ausgelöst, z. B. wenn übergebene Argumente ungültig sind.

Beispiele

Verschachteltes Objekt definieren

AIStructuredOutputConfig customerConfig = AIUtils.createStructuredConfig()
AIUtils.addString(customerConfig, "name", true, "Name of the customer")
AIUtils.addString(customerConfig, "city", false, "City of the customer")

AIStructuredOutputConfig config = AIUtils.createStructuredConfig()
AIUtils.addObject(
  config,
  "customer",
  true,
  "Customer information extracted from the text",
  customerConfig
)

Optionales Unterobjekt definieren

AIStructuredOutputConfig addressConfig = AIUtils.createStructuredConfig()
AIUtils.addString(addressConfig, "street", false, "Street including house number")
AIUtils.addString(addressConfig, "city", false, "City")
AIUtils.addString(addressConfig, "country", false, "Country")

AIStructuredOutputConfig personConfig = AIUtils.createStructuredConfig()
AIUtils.addObject(
  personConfig,
  "address",
  false,
  "Address of the person. If not clearly recognizable, omit the object.",
  addressConfig
)

Verwendung in einer strukturierten Antwort

AIStructuredOutputConfig contactAddressConfig = AIUtils.createStructuredConfig()
AIUtils.addString(contactAddressConfig, "city", true, "City of the person")
AIUtils.addString(contactAddressConfig, "country", false, "Country of the person")

AIStructuredOutputConfig contactConfig = AIUtils.createStructuredConfig()
AIUtils.addString(contactConfig, "fullName", true, "Full name of the person")
AIUtils.addObject(contactConfig, "address", false, "Address of the person", contactAddressConfig)

Map<String, Object> systemVars = [:]
Map<String, Object> userVars = ["text": "Erika Mustermann lives in Frankfurt."]

IAIResult result = AIUtils.executePrompt("AnalyseCheck", contactConfig, systemVars, userVars)

if (AIUtils.isAIResponseSuccessful(result)) {
  Map<String, Object> data = AIUtils.getAIStructuredResponse(result)
  Map<String, Object> address = (Map<String, Object>) data.get("address")
} else {
  String errorMessage = AIUtils.getAIResponseError(result)
}