Class Tool
- Namespace
- OpenAI
- Assembly
- OpenAI-DotNet.dll
public sealed class Tool
- Inheritance
-
Tool
- Inherited Members
Constructors
Tool()
public Tool()
Tool(FileSearchOptions)
public Tool(FileSearchOptions fileSearchOptions)
Parameters
fileSearchOptions
FileSearchOptions
Tool(Function)
public Tool(Function function)
Parameters
function
Function
Tool(Tool)
public Tool(Tool other)
Parameters
other
Tool
Tool(string, string, JsonNode, bool?)
[Obsolete("use new OpenAI.Tools.ToolCall class")]
public Tool(string toolCallId, string functionName, JsonNode functionArguments, bool? strict = null)
Parameters
Properties
CodeInterpreter
public static Tool CodeInterpreter { get; }
Property Value
FileSearch
public static Tool FileSearch { get; }
Property Value
FileSearchOptions
[JsonInclude]
[JsonPropertyName("file_search")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public FileSearchOptions FileSearchOptions { get; }
Property Value
Function
[JsonInclude]
[JsonPropertyName("function")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Function Function { get; }
Property Value
Id
[JsonInclude]
[JsonPropertyName("id")]
public string Id { get; }
Property Value
Index
[JsonInclude]
[JsonPropertyName("index")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int? Index { get; }
Property Value
- int?
IsFunction
[JsonIgnore]
public bool IsFunction { get; }
Property Value
Type
[JsonInclude]
[JsonPropertyName("type")]
public string Type { get; }
Property Value
Methods
AppendFrom(Tool)
public void AppendFrom(Tool other)
Parameters
other
Tool
ClearRegisteredTools()
Clears the tool cache of all previously registered tools.
public static void ClearRegisteredTools()
FromFunc<TResult>(string, Func<TResult>, string)
public static Tool FromFunc<TResult>(string name, Func<TResult> function, string description = null)
Parameters
Returns
Type Parameters
TResult
FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(string, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(string name, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
T6
T7
T8
T9
TResult
FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(string, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(string name, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
T6
T7
T8
T9
T10
TResult
FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(string, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(string name, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
T6
T7
T8
T9
T10
T11
TResult
FromFunc<T1, TResult>(string, Func<T1, TResult>, string)
public static Tool FromFunc<T1, TResult>(string name, Func<T1, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
TResult
FromFunc<T1, T2, TResult>(string, Func<T1, T2, TResult>, string)
public static Tool FromFunc<T1, T2, TResult>(string name, Func<T1, T2, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
TResult
FromFunc<T1, T2, T3, TResult>(string, Func<T1, T2, T3, TResult>, string)
public static Tool FromFunc<T1, T2, T3, TResult>(string name, Func<T1, T2, T3, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
TResult
FromFunc<T1, T2, T3, T4, TResult>(string, Func<T1, T2, T3, T4, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, TResult>(string name, Func<T1, T2, T3, T4, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
TResult
FromFunc<T1, T2, T3, T4, T5, TResult>(string, Func<T1, T2, T3, T4, T5, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, TResult>(string name, Func<T1, T2, T3, T4, T5, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
TResult
FromFunc<T1, T2, T3, T4, T5, T6, TResult>(string, Func<T1, T2, T3, T4, T5, T6, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, T6, TResult>(string name, Func<T1, T2, T3, T4, T5, T6, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
T6
TResult
FromFunc<T1, T2, T3, T4, T5, T6, T7, TResult>(string, Func<T1, T2, T3, T4, T5, T6, T7, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, T6, T7, TResult>(string name, Func<T1, T2, T3, T4, T5, T6, T7, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
T6
T7
TResult
FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(string, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>, string)
public static Tool FromFunc<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(string name, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> function, string description = null)
Parameters
Returns
Type Parameters
T1
T2
T3
T4
T5
T6
T7
T8
TResult
GetAllAvailableTools(bool, bool, bool)
Gets a list of all available tools.
public static IReadOnlyList<Tool> GetAllAvailableTools(bool includeDefaults = true, bool forceUpdate = false, bool clearCache = false)
Parameters
includeDefaults
boolOptional, Whether to include the default tools (Retrieval and CodeInterpreter).
forceUpdate
boolOptional, Whether to force an update of the tool cache.
clearCache
boolOptional, whether to force the tool cache to be cleared before updating.
Returns
- IReadOnlyList<Tool>
A list of all available tools.
Remarks
This method will scan all assemblies for static methods decorated with the FunctionAttribute.
GetOrCreateTool(object, string, string)
Get or create a tool from a method of an instance of an object.
public static Tool GetOrCreateTool(object instance, string methodName, string description = null)
Parameters
instance
objectThe instance of the object containing the method.
methodName
stringThe name of the method.
description
stringOptional, The description of the method.
Returns
- Tool
The tool for the method.
Remarks
If the tool already exists, it will be returned. Otherwise, a new tool will be created.
The method doesn't need to be decorated with the FunctionAttribute.
GetOrCreateTool(Type, string, string)
Get or create a tool from a static method.
public static Tool GetOrCreateTool(Type type, string methodName, string description = null)
Parameters
type
TypeThe type containing the static method.
methodName
stringThe name of the method.
description
stringOptional, The description of the method.
Returns
- Tool
The tool for the method.
Remarks
If the tool already exists, it will be returned. Otherwise, a new tool will be created.
The method doesn't need to be decorated with the FunctionAttribute.
InvokeFunction()
[Obsolete("Use overload with ToolCall parameter")]
public string InvokeFunction()
Returns
InvokeFunction(ToolCall)
Invokes the function and returns the result as json.
public string InvokeFunction(ToolCall toolCall)
Parameters
Returns
- string
The result of the function as json.
Exceptions
- InvalidOperationException
Raised if function call is invalid or tool is not a function.
InvokeFunctionAsync(ToolCall, CancellationToken)
Invokes the function and returns the result as json.
public Task<string> InvokeFunctionAsync(ToolCall toolCall, CancellationToken cancellationToken = default)
Parameters
toolCall
ToolCallThe ToolCall with the function arguments to invoke.
cancellationToken
CancellationTokenOptional, A token to cancel the request.
Returns
Exceptions
- InvalidOperationException
Raised if function call is invalid or tool is not a function.
InvokeFunctionAsync(CancellationToken)
Invokes the function and returns the result as json.
[Obsolete("Use overload with ToolCall parameter")]
public Task<string> InvokeFunctionAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenOptional, A token to cancel the request.
Returns
InvokeFunctionAsync<T>(ToolCall, CancellationToken)
Invokes the function and returns the result.
public Task<T> InvokeFunctionAsync<T>(ToolCall toolCall, CancellationToken cancellationToken = default)
Parameters
toolCall
ToolCallThe ToolCall with the function arguments to invoke.
cancellationToken
CancellationTokenOptional, A token to cancel the request.
Returns
- Task<T>
The result of the function.
Type Parameters
T
The type to deserialize the result to.
Exceptions
- InvalidOperationException
Raised if function call is invalid or tool is not a function.
InvokeFunctionAsync<T>(CancellationToken)
Invokes the function and returns the result.
[Obsolete("Use overload with ToolCall parameter")]
public Task<T> InvokeFunctionAsync<T>(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenOptional, A token to cancel the request.
Returns
- Task<T>
The result of the function.
Type Parameters
T
The type to deserialize the result to.
InvokeFunction<T>()
Invokes the function and returns the result.
[Obsolete("Use overload with ToolCall parameter")]
public T InvokeFunction<T>()
Returns
- T
The result of the function.
Type Parameters
T
The type to deserialize the result to.
InvokeFunction<T>(ToolCall)
Invokes the function and returns the result.
public T InvokeFunction<T>(ToolCall toolCall)
Parameters
Returns
- T
The result of the function.
Type Parameters
T
The type to deserialize the result to.
Exceptions
- InvalidOperationException
Raised if function call is invalid or tool is not a function.
IsToolRegistered(Tool)
Checks if tool exists in cache.
public static bool IsToolRegistered(Tool tool)
Parameters
tool
ToolThe tool to check.
Returns
- bool
True, if the tool is already registered in the tool cache.
TryRegisterTool(Tool)
Tries to register a tool into the Tool cache.
public static bool TryRegisterTool(Tool tool)
Parameters
tool
ToolThe tool to register.
Returns
- bool
True, if the tool was added to the cache.
TryUnregisterTool(Tool)
Tries to remove a tool from the Tool cache.
public static bool TryUnregisterTool(Tool tool)
Parameters
tool
ToolThe tool to remove.
Returns
- bool
True, if the tool was removed from the cache.
Exceptions
Operators
implicit operator Tool(FileSearchOptions)
public static implicit operator Tool(FileSearchOptions fileSearchOptions)
Parameters
fileSearchOptions
FileSearchOptions
Returns
implicit operator Tool(Function)
public static implicit operator Tool(Function function)
Parameters
function
Function