Class Tool
- Namespace
- OpenAI
- Assembly
- OpenAI-DotNet.dll
public sealed class Tool : ITool
- Inheritance
-
Tool
- Implements
- Inherited Members
Constructors
Tool()
public Tool()
Tool(FileSearchOptions)
public Tool(FileSearchOptions fileSearchOptions)
Parameters
fileSearchOptionsFileSearchOptions
Tool(Function)
public Tool(Function function)
Parameters
functionFunction
Tool(ITool)
public Tool(ITool iTool)
Parameters
iToolITool
Tool(Tool)
public Tool(Tool other)
Parameters
otherTool
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
IsReference
[JsonIgnore]
public bool IsReference { get; }
Property Value
Reference
[JsonIgnore]
public ITool Reference { get; }
Property Value
Type
[JsonInclude]
[JsonPropertyName("type")]
public string Type { get; }
Property Value
Methods
AppendFrom(Tool)
public void AppendFrom(Tool other)
Parameters
otherTool
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
T1T2T3T4T5T6T7T8T9TResult
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
T1T2T3T4T5T6T7T8T9T10TResult
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
T1T2T3T4T5T6T7T8T9T10T11TResult
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
T1TResult
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
T1T2TResult
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
T1T2T3TResult
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
T1T2T3T4TResult
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
T1T2T3T4T5TResult
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
T1T2T3T4T5T6TResult
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
T1T2T3T4T5T6T7TResult
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
T1T2T3T4T5T6T7T8TResult
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
includeDefaultsboolOptional, Whether to include the default tools (Retrieval and CodeInterpreter).
forceUpdateboolOptional, Whether to force an update of the tool cache.
clearCacheboolOptional, 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
instanceobjectThe instance of the object containing the method.
methodNamestringThe name of the method.
descriptionstringOptional, 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
typeTypeThe type containing the static method.
methodNamestringThe name of the method.
descriptionstringOptional, 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 IToolCall parameter")]
public string InvokeFunction()
Returns
InvokeFunction(IToolCall)
Invokes the function and returns the result as json.
public string InvokeFunction(IToolCall 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(IToolCall, CancellationToken)
Invokes the function and returns the result as json.
public Task<string> InvokeFunctionAsync(IToolCall toolCall, CancellationToken cancellationToken = default)
Parameters
toolCallIToolCallThe ToolCall with the function arguments to invoke.
cancellationTokenCancellationTokenOptional, 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 IToolCall parameter")]
public Task<string> InvokeFunctionAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenOptional, A token to cancel the request.
Returns
InvokeFunctionAsync<T>(IToolCall, CancellationToken)
Invokes the function and returns the result.
public Task<T> InvokeFunctionAsync<T>(IToolCall toolCall, CancellationToken cancellationToken = default)
Parameters
toolCallIToolCallThe ToolCall with the function arguments to invoke.
cancellationTokenCancellationTokenOptional, A token to cancel the request.
Returns
- Task<T>
The result of the function.
Type Parameters
TThe 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 IToolCall parameter")]
public Task<T> InvokeFunctionAsync<T>(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenOptional, A token to cancel the request.
Returns
- Task<T>
The result of the function.
Type Parameters
TThe type to deserialize the result to.
InvokeFunction<T>()
Invokes the function and returns the result.
[Obsolete("Use overload with IToolCall parameter")]
public T InvokeFunction<T>()
Returns
- T
The result of the function.
Type Parameters
TThe type to deserialize the result to.
InvokeFunction<T>(IToolCall)
Invokes the function and returns the result.
public T InvokeFunction<T>(IToolCall toolCall)
Parameters
Returns
- T
The result of the function.
Type Parameters
TThe 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
toolToolThe 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
toolToolThe 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
toolToolThe 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
fileSearchOptionsFileSearchOptions
Returns
implicit operator Tool(Function)
public static implicit operator Tool(Function function)
Parameters
functionFunction