Skip to content

Commit fd7ecd0

Browse files
committed
Refactor WebSearchTool and move to OpenAI namespace
- Moved ReasoningEffort, Verbosity, and WebSearchTool to Devlooped.Extensions.AI.OpenAI namespace for clarity. - Refactored WebSearchTool to support explicit properties for Country, Region, City, TimeZone, and AllowedDomains. - Integrated location and domain filtering logic directly into WebSearchTool. - Removed WebSearchToolExtensions as its functionality is now in the main class. - Updated tests to use the new WebSearchTool API and removed obsolete ContextSize usage.
1 parent cfec16e commit fd7ecd0

File tree

6 files changed

+107
-116
lines changed

6 files changed

+107
-116
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.ComponentModel;
22

3-
namespace Devlooped.Extensions.AI;
3+
namespace Devlooped.Extensions.AI.OpenAI;
44

55
/// <summary>
66
/// Effort a reasoning model should apply when generating a response.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Devlooped.Extensions.AI;
1+
namespace Devlooped.Extensions.AI.OpenAI;
22

33
/// <summary>
44
/// Verbosity determines how many output tokens are generated for models that support it, such as GPT-5.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using Microsoft.Extensions.AI;
2+
using OpenAI.Responses;
3+
using WebSearch = OpenAI.Responses.WebSearchTool;
4+
5+
namespace Devlooped.Extensions.AI.OpenAI;
6+
7+
/// <summary>
8+
/// Basic web search tool that can limit the search to a specific country.
9+
/// </summary>
10+
public class WebSearchTool : HostedWebSearchTool
11+
{
12+
readonly Dictionary<string, object?> additionalProperties = new();
13+
string? country;
14+
string? region;
15+
string? city;
16+
string? timeZone;
17+
string[]? allowedDomains;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="WebSearchTool"/> class with the specified country.
21+
/// </summary>
22+
/// <param name="country">ISO alpha-2 country code.</param>
23+
public WebSearchTool(string? country = null) => Country = country;
24+
25+
/// <summary>
26+
/// Sets the user's country for web search results, using the ISO alpha-2 code.
27+
/// </summary>
28+
public string? Country
29+
{
30+
get => country;
31+
set
32+
{
33+
country = value;
34+
UpdateUserLocation();
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Optional free text additional information about the region to be used in the search.
40+
/// </summary>
41+
public string? Region
42+
{
43+
get => region;
44+
set
45+
{
46+
region = value;
47+
UpdateUserLocation();
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Optional free text additional information about the city to be used in the search.
53+
/// </summary>
54+
public string? City
55+
{
56+
get => city;
57+
set
58+
{
59+
city = value;
60+
UpdateUserLocation();
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Optional IANA timezone name to be used in the search.
66+
/// </summary>
67+
public string? TimeZone
68+
{
69+
get => timeZone;
70+
set
71+
{
72+
timeZone = value;
73+
UpdateUserLocation();
74+
}
75+
}
76+
77+
/// <summary>
78+
/// Optional list of allowed domains to restrict the web search.
79+
/// </summary>
80+
public string[]? AllowedDomains
81+
{
82+
get => allowedDomains;
83+
set
84+
{
85+
allowedDomains = value;
86+
if (value is { Length: > 0 })
87+
additionalProperties[nameof(WebSearch.Filters)] = new WebSearchToolFilters { AllowedDomains = value };
88+
else
89+
additionalProperties.Remove(nameof(WebSearch.Filters));
90+
}
91+
}
92+
93+
/// <inheritdoc/>
94+
public override IReadOnlyDictionary<string, object?> AdditionalProperties => additionalProperties;
95+
96+
void UpdateUserLocation()
97+
{
98+
if (country != null || region != null || city != null || timeZone != null)
99+
additionalProperties[nameof(WebSearch.UserLocation)] =
100+
WebSearchToolLocation.CreateApproximateLocation(country, region, city, timeZone);
101+
else
102+
additionalProperties.Remove(nameof(WebSearch.UserLocation));
103+
}
104+
}

src/Extensions/OpenAI/WebSearchToolExtensions.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/Extensions/WebSearchTool.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/Tests/OpenAITests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,10 @@ public async Task WebSearchCountryHighContext()
276276

277277
var options = new ChatOptions
278278
{
279-
Tools = [new WebSearchTool("AR")
279+
Tools = [new Devlooped.Extensions.AI.OpenAI.WebSearchTool("AR")
280280
{
281281
Region = "Bariloche",
282282
TimeZone = "America/Argentina/Buenos_Aires",
283-
ContextSize = WebSearchToolContextSize.High
284283
}]
285284
};
286285

0 commit comments

Comments
 (0)