-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSearchTool.cs
More file actions
104 lines (94 loc) · 2.81 KB
/
WebSearchTool.cs
File metadata and controls
104 lines (94 loc) · 2.81 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Microsoft.Extensions.AI;
using OpenAI.Responses;
using WebSearch = OpenAI.Responses.WebSearchTool;
namespace Devlooped.Extensions.AI.OpenAI;
/// <summary>
/// Basic web search tool that can limit the search to a specific country.
/// </summary>
public class WebSearchTool : HostedWebSearchTool
{
readonly Dictionary<string, object?> additionalProperties = new();
string? country;
string? region;
string? city;
string? timeZone;
string[]? allowedDomains;
/// <summary>
/// Initializes a new instance of the <see cref="WebSearchTool"/> class with the specified country.
/// </summary>
/// <param name="country">ISO alpha-2 country code.</param>
public WebSearchTool(string? country = null) => Country = country;
/// <summary>
/// Sets the user's country for web search results, using the ISO alpha-2 code.
/// </summary>
public string? Country
{
get => country;
set
{
country = value;
UpdateUserLocation();
}
}
/// <summary>
/// Optional free text additional information about the region to be used in the search.
/// </summary>
public string? Region
{
get => region;
set
{
region = value;
UpdateUserLocation();
}
}
/// <summary>
/// Optional free text additional information about the city to be used in the search.
/// </summary>
public string? City
{
get => city;
set
{
city = value;
UpdateUserLocation();
}
}
/// <summary>
/// Optional IANA timezone name to be used in the search.
/// </summary>
public string? TimeZone
{
get => timeZone;
set
{
timeZone = value;
UpdateUserLocation();
}
}
/// <summary>
/// Optional list of allowed domains to restrict the web search.
/// </summary>
public string[]? AllowedDomains
{
get => allowedDomains;
set
{
allowedDomains = value;
if (value is { Length: > 0 })
additionalProperties[nameof(WebSearch.Filters)] = new WebSearchToolFilters { AllowedDomains = value };
else
additionalProperties.Remove(nameof(WebSearch.Filters));
}
}
/// <inheritdoc/>
public override IReadOnlyDictionary<string, object?> AdditionalProperties => additionalProperties;
void UpdateUserLocation()
{
if (country != null || region != null || city != null || timeZone != null)
additionalProperties[nameof(WebSearch.UserLocation)] =
WebSearchToolLocation.CreateApproximateLocation(country, region, city, timeZone);
else
additionalProperties.Remove(nameof(WebSearch.UserLocation));
}
}