Share via


ContextVariable<T> Class

  • java.lang.Object
    • com.microsoft.semantickernel.contextvariables.ContextVariable<T>

Type Parameters

T

the type of the context variable

public class ContextVariable<T>

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.

Constructor Summary

Constructor Description
ContextVariable(ContextVariableType<T> type, T value)

Creates a new instance of the ContextVariable<T> class.

Method Summary

Modifier and Type Method and Description
static ContextVariable<T> <T,U>convert(U it, ContextVariableType<T> requestedResultType)

Converts the given value to the requested result type.

static ContextVariable<T> <T,U>convert(U it, Class<T> requestedResultType, ContextVariableTypes contextVariableTypes)

Converts the given value to the requested result type.

static ContextVariable<T> of(T value, ContextVariableTypeConverter<T> converter)

Creates a new instance of the ContextVariable<T> class.

U getValue(Class<U> clazz)

Get the value of the context variable.

ContextVariableType<T> getType()

Get the type of the context variable.

T getValue()

Get the value of the context variable.

boolean isEmpty()

Returns true if the value of this ContextVariable is null or empty.

static ContextVariable<CompletionsUsage> of(CompletionsUsage x)

Convenience method for creating a ContextVariable from the given CompletionsUsage instance.

static ContextVariable<java.lang.String> of(String value)

Convenience method for creating a ContextVariable from the given String instance.

static ContextVariable<java.time.OffsetDateTime> of(OffsetDateTime x)

Convenience method for creating a ContextVariable from the given OffsetDateTime instance.

static ContextVariable<java.lang.Object> ofGlobalType(Object x)

Convenience method for creating a ContextVariable from the given object.

java.lang.String toPromptString()

Use the given ContextVariableTypeConverter to convert the value of this ContextVariable to a prompt string.

java.lang.String toPromptString(ContextVariableTypes types)

Use the given ContextVariableTypeConverter to convert the value of this ContextVariable to a prompt string.

java.lang.String toPromptString(ContextVariableTypes types, ContextVariableTypeConverter<T> converter)

Use the given ContextVariableTypeConverter to convert the value of this ContextVariable to a prompt string.

static ContextVariable<?> untypedOf(Object value, Class<?> clazz, ContextVariableTypes types)

Creates a new instance of the ContextVariable<T> class without using strong typing.

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Constructor Details

ContextVariable

public ContextVariable(ContextVariableType type, T value)

Creates a new instance of the ContextVariable<T> class.

Parameters:

type - the type
value - the value

Method Details

<T,U>convert

public static ContextVariable convert(U it, ContextVariableType requestedResultType)

Converts the given value to the requested result type.

Parameters:

it - the input value
requestedResultType - the requested result type

Returns:

the converted value

<T,U>convert

public static ContextVariable convert(U it, Class requestedResultType, 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.

Parameters:

it - the input value
requestedResultType - the requested result type
contextVariableTypes - the context variable types

Returns:

the converted value

of

public static ContextVariable of(T value, ContextVariableTypeConverter converter)

Creates a new instance of the ContextVariable<T> class.

Parameters:

value - the value
converter - the converter

Returns:

the new instance

getValue

public U getValue(Class clazz)

Get the value of the context variable.

Parameters:

clazz - the class

Returns:

the value

getType

public ContextVariableType getType()

Get the type of the context variable.

Returns:

the type

getValue

public T getValue()

Get the value of the context variable.

Returns:

the value

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

of

public static ContextVariable of(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 of(String value)

Convenience method for creating a ContextVariable from the given String instance.

Parameters:

value - the value

Returns:

the new instance

of

public static ContextVariable of(OffsetDateTime x)

Convenience method for creating a ContextVariable from the given OffsetDateTime instance.

Parameters:

x - the value

Returns:

the new instance

ofGlobalType

public static ContextVariable ofGlobalType(Object x)

Convenience method for creating a ContextVariable from the given object.

Parameters:

x - the object

Returns:

the new instance

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

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(ContextVariableTypes types, ContextVariableTypeConverter 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

untypedOf

public static ContextVariable untypedOf(Object value, Class clazz, ContextVariableTypes types)

Creates a new instance of the ContextVariable<T> class without using strong typing.

Parameters:

value - the value
clazz - the class
types - the types

Returns:

the new instance

Applies to