Skip to content

Commit 04898b3

Browse files
committed
Switch to new hosted tools options and citations
1 parent d5400b0 commit 04898b3

14 files changed

+170
-750
lines changed

src/Extensions.Grok/Extensions.Grok.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
5-
<!--<TargetFramework>net10.0</TargetFramework>-->
6-
<!--<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>-->
4+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
75
<AssemblyName>Devlooped.Extensions.AI.Grok</AssemblyName>
86
<RootNamespace>$(AssemblyName)</RootNamespace>
97
<PackageId>$(AssemblyName)</PackageId>
@@ -24,11 +22,11 @@
2422
</ItemGroup>
2523

2624
<ItemGroup>
27-
<ProjectReference Include="..\Extensions\Extensions.csproj" />
25+
<Protobuf Include="proto\*.proto" GrpcServices="Client" ProtoRoot="proto" />
2826
</ItemGroup>
2927

3028
<ItemGroup>
31-
<Protobuf Include="proto\*.proto" GrpcServices="Client" ProtoRoot="proto" />
29+
<Compile Include="..\Extensions\Extensions\ChatOptionsExtensions.cs" Link="Extensions\ChatOptionsExtensions.cs" />
3230
</ItemGroup>
3331

3432
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.ComponentModel;
2+
using Microsoft.Extensions.AI;
3+
4+
namespace Devlooped.Extensions.AI.Grok;
5+
6+
/// <summary>Customizes Grok's agentic search tools.</summary>
7+
/// <remarks>See https://docs.x.ai/docs/guides/tools/search-tools.</remarks>
8+
[Flags]
9+
public enum GrokSearch
10+
{
11+
/// <summary>Disables agentic search capabilities.</summary>
12+
None = 0,
13+
/// <summary>Enables all available agentic search capabilities.</summary>
14+
All = Web | X,
15+
/// <summary>Allows the agent to search the web and browse pages.</summary>
16+
Web = 1,
17+
/// <summary>Allows the agent to perform keyword search, semantic search, user search, and thread fetch on X.</summary>
18+
X = 2,
19+
[EditorBrowsable(EditorBrowsableState.Never)]
20+
[Obsolete("Use either GrokSearch.Web or GrokSearch.X")]
21+
Auto = Web,
22+
[EditorBrowsable(EditorBrowsableState.Never)]
23+
[Obsolete("Use either GrokSearch.Web or GrokSearch.X")]
24+
On = Web,
25+
[EditorBrowsable(EditorBrowsableState.Never)]
26+
[Obsolete("Use GrokSearch.None")]
27+
Off = None
28+
}
29+
30+
/// <summary>Grok-specific chat options that extend the base <see cref="ChatOptions"/>.</summary>
31+
public class GrokChatOptions : ChatOptions
32+
{
33+
/// <summary>Configures Grok's agentic search capabilities.</summary>
34+
/// <remarks>See https://docs.x.ai/docs/guides/tools/search-tools.</remarks>
35+
public GrokSearch Search { get; set; } = GrokSearch.None;
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.Extensions.AI;
2+
3+
namespace Devlooped.Extensions.AI.Grok;
4+
5+
/// <summary>Configures Grok's agentic search tool.</summary>
6+
/// <remarks>See https://docs.x.ai/docs/guides/tools/search-tools</remarks>
7+
public class GrokSearchTool : HostedWebSearchTool
8+
{
9+
/// <inheritdoc/>
10+
public override string Name => "web_search";
11+
12+
/// <inheritdoc/>
13+
public override string Description => "Performs agentic web search";
14+
15+
/// <summary>Use to make the web search only perform the search and web browsing on web pages that fall within the specified domains. Can include a maximum of five domains.</summary>
16+
public IList<string>? AllowedDomains { get; set; }
17+
18+
/// <summary>Use to prevent the model from including the specified domains in any web search tool invocations and from browsing any pages on those domains. Can include a maximum of five domains.</summary>
19+
public IList<string>? ExcludedDomains { get; set; }
20+
21+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#enable-image-understanding</summary>
22+
public bool EnableImageUnderstanding { get; set; }
23+
}

src/Extensions.Grok/GrokXSearch.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Text.Json.Serialization;
2+
using Microsoft.Extensions.AI;
3+
4+
namespace Devlooped.Extensions.AI.Grok;
5+
6+
/// <summary>Configures Grok's agentic search tool for X.</summary>
7+
/// <remarks>See https://docs.x.ai/docs/guides/tools/search-tools#x-search-parameters</remarks>
8+
public class GrokXSearchTool : HostedWebSearchTool
9+
{
10+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#only-consider-x-posts-from-specific-handles</summary>
11+
[JsonPropertyName("allowed_x_handles")]
12+
public IList<string>? AllowedHandles { get; set; }
13+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#exclude-x-posts-from-specific-handles</summary>
14+
[JsonPropertyName("excluded_x_handles")]
15+
public IList<string>? ExcludedHandles { get; set; }
16+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#date-range</summary>
17+
public DateOnly? FromDate { get; set; }
18+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#date-range</summary>
19+
public DateOnly? ToDate { get; set; }
20+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#enable-image-understanding-1</summary>
21+
public bool EnableImageUnderstanding { get; set; }
22+
/// <summary>See https://docs.x.ai/docs/guides/tools/search-tools#enable-video-understanding</summary>
23+
public bool EnableVideoUnderstanding { get; set; }
24+
}

src/Extensions/ConfigurableChatClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerabl
102102
Throw.IfNullOrEmpty(apikey, $"{section}:apikey");
103103

104104
IChatClient client = options.Endpoint?.Host == "api.x.ai"
105-
? new GrokChatClient2(apikey, options.ModelId, options)
105+
? new GrokClient(apikey, configSection.Get<GrokClientOptions>() ?? new()).AsIChatClient(options.ModelId)
106106
: options.Endpoint?.Host == "ai.azure.com"
107107
? new ChatCompletionsClient(options.Endpoint, new AzureKeyCredential(apikey), SetOptions<ConfigurableInferenceOptions>(configSection)).AsIChatClient(options.ModelId)
108108
: options.Endpoint?.Host.EndsWith("openai.azure.com") == true

src/Extensions/Extensions.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
55
<LangVersion>Preview</LangVersion>
66
<AssemblyName>Devlooped.Extensions.AI</AssemblyName>
77
<RootNamespace>$(AssemblyName)</RootNamespace>
@@ -44,6 +44,7 @@
4444

4545
<ItemGroup>
4646
<ProjectReference Include="..\Extensions.CodeAnalysis\Extensions.CodeAnalysis.csproj" ReferenceOutputAssembly="false" />
47+
<ProjectReference Include="..\Extensions.Grok\Extensions.Grok.csproj" />
4748
</ItemGroup>
4849

4950
<ItemGroup>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Extensions.AI;
2+
3+
namespace Devlooped.Extensions.AI;
4+
5+
/// <summary>Extensions for <see cref="ChatOptions"/>.</summary>
6+
static partial class ChatOptionsExtensions
7+
{
8+
extension(ChatOptions options)
9+
{
10+
/// <summary>Gets or sets the end user ID for the chat session.</summary>
11+
public string? EndUserId
12+
{
13+
get => (options.AdditionalProperties ??= []).TryGetValue("EndUserId", out var value) ? value as string : null;
14+
set => (options.AdditionalProperties ??= [])["EndUserId"] = value;
15+
}
16+
}
17+
}

src/Extensions/Grok/GrokChatClient2.cs

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

src/Extensions/Grok/GrokChatOptions.cs

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

0 commit comments

Comments
 (0)