-
Notifications
You must be signed in to change notification settings - Fork 857
Expand file tree
/
Copy pathAITool.cs
More file actions
84 lines (70 loc) · 3.56 KB
/
AITool.cs
File metadata and controls
84 lines (70 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Microsoft.Shared.Collections;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.AI;
/// <summary>Represents a tool that can be specified to an AI service.</summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class AITool
{
/// <summary>Initializes a new instance of the <see cref="AITool"/> class.</summary>
protected AITool()
{
}
/// <summary>Gets the name of the tool.</summary>
public virtual string Name => GetType().Name;
/// <summary>Gets a description of the tool, suitable for use in describing the purpose to a model.</summary>
public virtual string Description => string.Empty;
/// <summary>Gets any additional properties associated with the tool.</summary>
public virtual IReadOnlyDictionary<string, object?> AdditionalProperties => EmptyReadOnlyDictionary<string, object?>.Instance;
/// <inheritdoc/>
public override string ToString() => Name;
/// <summary>Asks the <see cref="AITool"/> for an object of the specified type <paramref name="serviceType"/>.</summary>
/// <param name="serviceType">The type of object being requested.</param>
/// <param name="serviceKey">An optional key that can be used to help identify the target service.</param>
/// <returns>The found object, otherwise <see langword="null"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="serviceType"/> is <see langword="null"/>.</exception>
/// <remarks>
/// The purpose of this method is to allow for the retrieval of strongly typed services that might be provided by the <see cref="AITool"/>,
/// including itself or any services it might be wrapping.
/// </remarks>
public virtual object? GetService(Type serviceType, object? serviceKey = null)
{
_ = Throw.IfNull(serviceType);
return
serviceKey is null && serviceType.IsInstanceOfType(this) ? this :
null;
}
/// <summary>Asks the <see cref="AITool"/> for an object of type <typeparamref name="TService"/>.</summary>
/// <typeparam name="TService">The type of the object to be retrieved.</typeparam>
/// <param name="serviceKey">An optional key that can be used to help identify the target service.</param>
/// <returns>The found object, otherwise <see langword="null"/>.</returns>
/// <remarks>
/// The purpose of this method is to allow for the retrieval of strongly typed services that may be provided by the <see cref="AITool"/>,
/// including itself or any services it might be wrapping.
/// </remarks>
public TService? GetService<TService>(object? serviceKey = null) =>
GetService(typeof(TService), serviceKey) is TService service ? service : default;
/// <summary>Gets the string to display in the debugger for this instance.</summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay
{
get
{
StringBuilder sb = new(Name);
if (Description is string description && !string.IsNullOrEmpty(description))
{
_ = sb.Append(" (").Append(description).Append(')');
}
foreach (var entry in AdditionalProperties)
{
_ = sb.Append(", ").Append(entry.Key).Append(" = ").Append(entry.Value);
}
return sb.ToString();
}
}
}