Skip to content

Commit 6599f49

Browse files
committed
Fix support for simplified Search modes
Easily enable Web/X search from options
1 parent 04898b3 commit 6599f49

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/Extensions.Grok/GrokChatClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ GetCompletionsRequest MapToRequest(IEnumerable<ChatMessage> messages, ChatOption
118118
request.Messages.Add(pMessage);
119119
}
120120

121+
if (options is GrokChatOptions grokOptions)
122+
{
123+
if (grokOptions.Search is GrokSearch.Web)
124+
{
125+
(options.Tools ??= []).Add(new GrokSearchTool());
126+
}
127+
else if (grokOptions.Search is GrokSearch.X)
128+
{
129+
(options.Tools ??= []).Add(new GrokXSearchTool());
130+
}
131+
}
132+
121133
if (options?.Tools is not null)
122134
{
123135
foreach (var tool in options.Tools)

src/Tests/GrokTests.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Devlooped.Extensions.AI.Grok;
44
using Microsoft.Extensions.AI;
55
using OpenAI.Realtime;
6+
using XaiApi;
67
using static ConfigurationExtensions;
78
using OpenAIClientOptions = OpenAI.OpenAIClientOptions;
89

@@ -26,7 +27,7 @@ public async Task GrokInvokesTools()
2627

2728
var options = new GrokChatOptions
2829
{
29-
ModelId = "grok-4-fast",
30+
ModelId = "grok-4-fast-non-reasoning",
3031
Tools = [AIFunctionFactory.Create(() => DateTimeOffset.Now.ToString("O"), "get_date")],
3132
AdditionalProperties = new()
3233
{
@@ -42,15 +43,14 @@ public async Task GrokInvokesTools()
4243
Assert.True(getdate);
4344
// NOTE: the chat client was requested as grok-3 but the chat options wanted a
4445
// different model and the grok client honors that choice.
45-
Assert.Equal("grok-4-fast-reasoning", response.ModelId);
46+
Assert.Equal(options.ModelId, response.ModelId);
4647
}
4748

4849
[SecretsFact("XAI_API_KEY")]
4950
public async Task GrokInvokesToolAndSearch()
5051
{
5152
var messages = new Chat()
5253
{
53-
{ "system", "You are a bot that invokes the tool 'get_date' before responding to anything since it's important context." },
5454
{ "user", "What's Tesla stock worth today?" },
5555
};
5656

@@ -62,9 +62,10 @@ public async Task GrokInvokesToolAndSearch()
6262

6363
var options = new GrokChatOptions
6464
{
65-
ModelId = "grok-4-1-fast",
65+
ModelId = "grok-4-1-fast-non-reasoning",
6666
Search = GrokSearch.Web,
67-
Tools = [AIFunctionFactory.Create(() => DateTimeOffset.Now.ToString("O"), "get_date")]
67+
Tools = [AIFunctionFactory.Create(() => DateTimeOffset.Now.ToString("O"), "get_date")],
68+
ToolMode = ChatToolMode.RequireAny
6869
};
6970

7071
var response = await grok.GetResponseAsync(messages, options);
@@ -81,7 +82,7 @@ public async Task GrokInvokesToolAndSearch()
8182
.Select(x => x.Url!.AbsoluteUri)
8283
.ToList();
8384

84-
Assert.Contains("https://finance.yahoo.com/quote/TSLA/", citations);
85+
Assert.Contains(citations, url => url.Contains("/TSLA"));
8586
Assert.Equal(options.ModelId, response.ModelId);
8687
}
8788

@@ -120,7 +121,7 @@ public async Task GrokInvokesSpecificSearchUrl()
120121
}
121122

122123
[SecretsFact("XAI_API_KEY")]
123-
public async Task GrokGrpcInvokesHostedSearchTool()
124+
public async Task GrokInvokesHostedSearchTool()
124125
{
125126
var messages = new Chat()
126127
{
@@ -140,14 +141,19 @@ public async Task GrokGrpcInvokesHostedSearchTool()
140141

141142
Assert.Contains("TSLA", text);
142143
Assert.NotNull(response.ModelId);
143-
Assert.Contains(new Uri("https://finance.yahoo.com/quote/TSLA/news/"), response.Messages
144+
145+
var urls = response.Messages
144146
.SelectMany(x => x.Contents)
145147
.SelectMany(x => x.Annotations?.OfType<CitationAnnotation>() ?? [])
146-
.Select(x => x.Url));
148+
.Where(x => x.Url is not null)
149+
.Select(x => x.Url!)
150+
.ToList();
151+
152+
Assert.Contains(urls, url => url.AbsoluteUri.Contains("/TSLA"));
147153
}
148154

149155
[SecretsFact("XAI_API_KEY")]
150-
public async Task GrokGrpcInvokesGrokSearchToolIncludesDomain()
156+
public async Task GrokInvokesGrokSearchToolIncludesDomain()
151157
{
152158
var messages = new Chat()
153159
{
@@ -186,7 +192,7 @@ public async Task GrokGrpcInvokesGrokSearchToolIncludesDomain()
186192
}
187193

188194
[SecretsFact("XAI_API_KEY")]
189-
public async Task GrokGrpcInvokesGrokSearchToolExcludesDomain()
195+
public async Task GrokInvokesGrokSearchToolExcludesDomain()
190196
{
191197
var messages = new Chat()
192198
{

0 commit comments

Comments
 (0)