Class ContextVariable<T>
- java.lang.Object
-
- com.microsoft.semantickernel.contextvariables.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 aContextVariableType.ContextVariableTypeis used throughout the Semantic Kernel for passing arguments to functions and as function return types.
-
-
Constructor Summary
Constructors Constructor Description ContextVariable(ContextVariableType<T> type, T value)Creates a new instance of theContextVariableclass.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T,U>
ContextVariable<T>convert(U it, ContextVariableType<T> requestedResultType)Converts the given value to the requested result type.static <T,U>
ContextVariable<T>convert(U it, Class<T> requestedResultType, ContextVariableTypes contextVariableTypes)Converts the given value to the requested result type.ContextVariableType<T>getType()Get the type of the context variable.TgetValue()Get the value of the context variable.<U> UgetValue(Class<U> clazz)Get the value of the context variable.booleanisEmpty()Returns true if the value of thisContextVariableisnullor empty.static ContextVariable<com.azure.ai.openai.models.CompletionsUsage>of(com.azure.ai.openai.models.CompletionsUsage x)Convenience method for creating aContextVariablefrom the givenCompletionsUsageinstance.static ContextVariable<String>of(String value)Convenience method for creating aContextVariablefrom the givenStringinstance.static ContextVariable<OffsetDateTime>of(OffsetDateTime x)Convenience method for creating aContextVariablefrom the givenOffsetDateTimeinstance.static <T> ContextVariable<T>of(T value, ContextVariableTypeConverter<T> converter)Creates a new instance of theContextVariableclass.static ContextVariable<Object>ofGlobalType(Object x)Convenience method for creating aContextVariablefrom the given object.StringtoPromptString()Use the givenContextVariableTypeConverterto convert the value of thisContextVariableto a prompt string.StringtoPromptString(ContextVariableTypes types)Use the givenContextVariableTypeConverterto convert the value of thisContextVariableto a prompt string.StringtoPromptString(ContextVariableTypes types, ContextVariableTypeConverter<T> converter)Use the givenContextVariableTypeConverterto convert the value of thisContextVariableto a prompt string.static ContextVariable<?>untypedOf(Object value, Class<?> clazz, ContextVariableTypes types)Creates a new instance of theContextVariableclass without using strong typing.
-
-
-
Constructor Detail
-
ContextVariable
public ContextVariable(ContextVariableType<T> type, @Nullable T value)
Creates a new instance of theContextVariableclass.- Parameters:
type- the typevalue- the value
-
-
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 resultU- the type of the input value- Parameters:
it- the input valuerequestedResultType- 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. TheContextVariableTypesparameter is used to find the appropriate converter for the input value and the requested result type.- Type Parameters:
T- the type of the requested resultU- the type of the input value- Parameters:
it- the input valuerequestedResultType- the requested result typecontextVariableTypes- 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
-
untypedOf
public static ContextVariable<?> untypedOf(@Nullable Object value, Class<?> clazz, ContextVariableTypes types)
Creates a new instance of theContextVariableclass without using strong typing.- Parameters:
value- the valueclazz- the classtypes- the types- Returns:
- the new instance
-
of
public static ContextVariable<com.azure.ai.openai.models.CompletionsUsage> of(com.azure.ai.openai.models.CompletionsUsage x)
Convenience method for creating aContextVariablefrom the givenCompletionsUsageinstance.- Parameters:
x- the value- Returns:
- the new instance
-
of
public static ContextVariable<OffsetDateTime> of(OffsetDateTime x)
Convenience method for creating aContextVariablefrom the givenOffsetDateTimeinstance.- Parameters:
x- the value- Returns:
- the new instance
-
of
public static ContextVariable<String> of(String value)
Convenience method for creating aContextVariablefrom the givenStringinstance.- Parameters:
value- the value- Returns:
- the new instance
-
ofGlobalType
public static ContextVariable<Object> ofGlobalType(Object x)
Convenience method for creating aContextVariablefrom the given object.- Parameters:
x- the object- Returns:
- the new instance
-
of
public static <T> ContextVariable<T> of(T value, ContextVariableTypeConverter<T> converter)
Creates a new instance of theContextVariableclass.- Type Parameters:
T- the type of the value- Parameters:
value- the valueconverter- the converter- Returns:
- the new instance
-
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 givenContextVariableTypeConverterto convert the value of thisContextVariableto a prompt string. This method is useful when the convert of thisContextVariableTypedoes not create the expected prompt string.- Parameters:
types- the types to use when converting the value, ifnullthe global types are usedconverter- the converter to use when converting the value- Returns:
- the value of this
ContextVariableas a prompt string
-
toPromptString
public String toPromptString(ContextVariableTypes types)
Use the givenContextVariableTypeConverterto convert the value of thisContextVariableto a prompt string. This method is useful when the convert of thisContextVariableTypedoes not create the expected prompt string.- Parameters:
types- the types to use when converting the value- Returns:
- the value of this
ContextVariableas a prompt string
-
toPromptString
public String toPromptString()
Use the givenContextVariableTypeConverterto convert the value of thisContextVariableto a prompt string. This method is useful when the convert of thisContextVariableTypedoes not create the expected prompt string.- Returns:
- the value of this
ContextVariableas a prompt string
-
isEmpty
public boolean isEmpty()
Returns true if the value of thisContextVariableisnullor empty.- Returns:
- true if the value is
nullor empty
-
-