|
| 1 | +namespace Devlooped.Extensions.AI; |
| 2 | + |
| 3 | +using Microsoft.Extensions.AI; |
| 4 | +using static ConfigurationExtensions; |
| 5 | + |
| 6 | +public class GrokTests |
| 7 | +{ |
| 8 | + [SecretsFact("XAI_API_KEY")] |
| 9 | + public async Task GrokInvokesTools() |
| 10 | + { |
| 11 | + var messages = new Chat() |
| 12 | + { |
| 13 | + { "system", "You are a bot that invokes the tool get_date when asked for the date." }, |
| 14 | + { "user", "What day is today?" }, |
| 15 | + }; |
| 16 | + |
| 17 | + var grok = new GrokClient(Configuration["XAI_API_KEY"]!); |
| 18 | + |
| 19 | + var options = new GrokChatOptions |
| 20 | + { |
| 21 | + ModelId = "grok-3-mini", |
| 22 | + Search = GrokSearch.Auto, |
| 23 | + Tools = [AIFunctionFactory.Create(() => DateTimeOffset.Now.ToString("O"), "get_date")] |
| 24 | + }; |
| 25 | + |
| 26 | + var response = await grok.GetResponseAsync(messages, options); |
| 27 | + var getdate = response.Messages |
| 28 | + .SelectMany(x => x.Contents.OfType<FunctionCallContent>()) |
| 29 | + .Any(x => x.Name == "get_date"); |
| 30 | + |
| 31 | + Assert.True(getdate); |
| 32 | + } |
| 33 | + |
| 34 | + [SecretsFact("XAI_API_KEY")] |
| 35 | + public async Task GrokInvokesToolAndSearch() |
| 36 | + { |
| 37 | + var messages = new Chat() |
| 38 | + { |
| 39 | + { "system", "You are a bot that invokes the tool 'get_date' before responding to anything since it's important context." }, |
| 40 | + { "user", "What's Tesla stock worth today?" }, |
| 41 | + }; |
| 42 | + |
| 43 | + var grok = new GrokClient(Configuration["XAI_API_KEY"]!) |
| 44 | + .AsBuilder() |
| 45 | + .UseFunctionInvocation() |
| 46 | + .Build(); |
| 47 | + |
| 48 | + var options = new GrokChatOptions |
| 49 | + { |
| 50 | + ModelId = "grok-3-mini", |
| 51 | + Search = GrokSearch.On, |
| 52 | + Tools = [AIFunctionFactory.Create(() => DateTimeOffset.Now.ToString("O"), "get_date")] |
| 53 | + }; |
| 54 | + |
| 55 | + var response = await grok.GetResponseAsync(messages, options); |
| 56 | + |
| 57 | + // The get_date result shows up as a tool role |
| 58 | + Assert.Contains(response.Messages, x => x.Role == ChatRole.Tool); |
| 59 | + |
| 60 | + var text = response.Text; |
| 61 | + |
| 62 | + Assert.Contains("TSLA", text); |
| 63 | + Assert.Contains("$", text); |
| 64 | + Assert.Contains("Nasdaq", text, StringComparison.OrdinalIgnoreCase); |
| 65 | + } |
| 66 | + |
| 67 | + [SecretsFact("XAI_API_KEY")] |
| 68 | + public async Task GrokThinksHard() |
| 69 | + { |
| 70 | + var messages = new Chat() |
| 71 | + { |
| 72 | + { "system", "You are an intelligent AI assistant that's an expert on financial matters." }, |
| 73 | + { "user", "If you have a debt of 100k and accumulate a compounding 5% debt on top of it every year, how long before you are a negative millonaire?" }, |
| 74 | + }; |
| 75 | + |
| 76 | + var grok = new GrokClient(Configuration["XAI_API_KEY"]!) |
| 77 | + .AsBuilder() |
| 78 | + .UseFunctionInvocation() |
| 79 | + .Build(); |
| 80 | + |
| 81 | + var options = new GrokChatOptions |
| 82 | + { |
| 83 | + ModelId = "grok-3-mini", |
| 84 | + Search = GrokSearch.Off, |
| 85 | + ReasoningEffort = ReasoningEffort.High, |
| 86 | + }; |
| 87 | + |
| 88 | + var response = await grok.GetResponseAsync(messages, options); |
| 89 | + |
| 90 | + var text = response.Text; |
| 91 | + |
| 92 | + Assert.Contains("48 years", text); |
| 93 | + } |
| 94 | +} |
0 commit comments