Class ContextVariable<T>

  • Type Parameters:
    T - the type of the context variable

    public class ContextVariable<T>
    extends Object
    A context variable wraps an arbitrary value and a ContextVariableType. ContextVariableType is used throughout the Semantic Kernel for passing arguments to functions and as function return types.
    • Method Detail

      • convert

        public static <T,​U> ContextVariable<T> convert​(@Nullable
                                                             U it,
                                                             ContextVariableType<T> requestedResultType)
        Converts the given value to the requested result type.
        Type Parameters:
        T - the type of the requested result
        U - the type of the input value
        Parameters:
        it - the input value
        requestedResultType - the requested result type
        Returns:
        the converted value
      • convert

        public static <T,​U> ContextVariable<T> convert​(@Nullable
                                                             U it,
                                                             Class<T> requestedResultType,
                                                             @Nullable
                                                             ContextVariableTypes contextVariableTypes)
        Converts the given value to the requested result type. The ContextVariableTypes parameter is used to find the appropriate converter for the input value and the requested result type.
        Type Parameters:
        T - the type of the requested result
        U - the type of the input value
        Parameters:
        it - the input value
        requestedResultType - the requested result type
        contextVariableTypes - the context variable types
        Returns:
        the converted value
        Throws:
        SKException - if a type converter cannot be found, or the input value cannot be converted to the requested result type
      • of

        public static ContextVariable<com.azure.ai.openai.models.CompletionsUsage> of​(com.azure.ai.openai.models.CompletionsUsage x)
        Convenience method for creating a ContextVariable from the given CompletionsUsage instance.
        Parameters:
        x - the value
        Returns:
        the new instance
      • of

        public static ContextVariable<OffsetDateTime> of​(OffsetDateTime x)
        Convenience method for creating a ContextVariable from the given OffsetDateTime instance.
        Parameters:
        x - the value
        Returns:
        the new instance
      • of

        public static ContextVariable<String> of​(String value)
        Convenience method for creating a ContextVariable from the given String instance.
        Parameters:
        value - the value
        Returns:
        the new instance
      • ofGlobalType

        public static ContextVariable<Object> ofGlobalType​(Object x)
        Convenience method for creating a ContextVariable from the given object.
        Parameters:
        x - the object
        Returns:
        the new instance
      • getValue

        @Nullable
        public T getValue()
        Get the value of the context variable.
        Returns:
        the value
      • getValue

        @Nullable
        public <U> U getValue​(Class<U> clazz)
        Get the value of the context variable.
        Type Parameters:
        U - the type of the value
        Parameters:
        clazz - the class
        Returns:
        the value
        Throws:
        SKException - if the value cannot be cast to the requested type
      • getType

        public ContextVariableType<T> getType()
        Get the type of the context variable.
        Returns:
        the type
      • toPromptString

        public String toPromptString​(@Nullable
                                     ContextVariableTypes types,
                                     ContextVariableTypeConverter<T> converter)
        Use the given ContextVariableTypeConverter to convert the value of this ContextVariable to a prompt string. This method is useful when the convert of this ContextVariableType does not create the expected prompt string.
        Parameters:
        types - the types to use when converting the value, if null the global types are used
        converter - the converter to use when converting the value
        Returns:
        the value of this ContextVariable as a prompt string
      • toPromptString

        public String toPromptString​(ContextVariableTypes types)
        Use the given ContextVariableTypeConverter to convert the value of this ContextVariable to a prompt string. This method is useful when the convert of this ContextVariableType does not create the expected prompt string.
        Parameters:
        types - the types to use when converting the value
        Returns:
        the value of this ContextVariable as a prompt string
      • toPromptString

        public String toPromptString()
        Use the given ContextVariableTypeConverter to convert the value of this ContextVariable to a prompt string. This method is useful when the convert of this ContextVariableType does not create the expected prompt string.
        Returns:
        the value of this ContextVariable as a prompt string
      • isEmpty

        public boolean isEmpty()
        Returns true if the value of this ContextVariable is null or empty.
        Returns:
        true if the value is null or empty