Skip to content

Commit f1d230a

Browse files
committed
Bring in M.E.AI integration from Devlooped.Extensions.AI
This will now be the core of the xAI package.
1 parent 8c50c04 commit f1d230a

24 files changed

+2546
-14
lines changed

.netconfig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
sha = 407aa2d9319f5db12964540810b446fecc22d419
181181
etag = 0dca55f20a72d3279554837f4eba867a1de37fe0f4a7535c2d9bc43867361cc5
182182
weak
183-
[file "src/Tests/Attributes.cs"]
183+
[file "src/xAI.Tests/Extensions/Attributes.cs"]
184184
url = https://github.com/devlooped/catbag/blob/main/Xunit/Attributes.cs
185185
sha = 40914971d4d6b42d6f8a90923b131136f7e609a5
186186
etag = c77e7b435ce1df06fb60a3b0e15a0833d8e45d4d19f366c6184140ebb4814b1a
@@ -190,3 +190,8 @@
190190
sha = 666a2a7c315f72199c418f11482a950fc69a8901
191191
etag = 91ea15c07bfd784036c6ca931f5b2df7e9767b8367146d96c79caef09d63899f
192192
weak
193+
[file "src/xAI/Extensions/Throw.cs"]
194+
url = https://github.com/devlooped/catbag/blob/main/System/Throw.cs
195+
sha = 3012d56be7554c483e5c5d277144c063969cada9
196+
etag = 43c81c6c6dcdf5baee40a9e3edc5e871e473e6c954c901b82bb87a3a48888ea0
197+
weak

readme.md

Lines changed: 184 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
![Icon](assets/icon.png) xAI .NET SDK
1+
![Icon](assets/icon.png) .NET SDK
22
============
33

44
[![Version](https://img.shields.io/nuget/vpre/xAI.svg?color=royalblue)](https://www.nuget.org/packages/xAI)
55
[![Downloads](https://img.shields.io/nuget/dt/xAI.svg?color=darkmagenta)](https://www.nuget.org/packages/xAI)
66
[![EULA](https://img.shields.io/badge/EULA-OSMF-blue?labelColor=black&color=C9FF30)](osmfeula.txt)
77
[![OSS](https://img.shields.io/github/license/devlooped/oss.svg?color=blue)](license.txt)
88

9-
xAI .NET SDK based on the official gRPC API reference from xAI
9+
xAI .NET SDK based on the official gRPC API reference from xAI with integration for
10+
Microsoft.Extensions.AI and Microsoft.Agents.AI.
1011

1112
<!-- include https://github.com/devlooped/.github/raw/main/osmf.md -->
1213
## Open Source Maintenance Fee
@@ -21,10 +22,188 @@ OSMF tier. A single fee covers all of [Devlooped packages](https://www.nuget.org
2122

2223
<!-- https://github.com/devlooped/.github/raw/main/osmf.md -->
2324

24-
<!-- #content -->
25+
<!-- #xai -->
26+
xAI/Grok integration for Microsoft.Extensions.AI `IChatClient` with full support for all
27+
[agentic tools](https://docs.x.ai/docs/guides/tools/overview):
28+
29+
```csharp
30+
var grok = new GrokClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!)
31+
.AsIChatClient("grok-4.1-fast");
32+
```
33+
## Web Search
34+
35+
```csharp
36+
var messages = new Chat()
37+
{
38+
{ "system", "You are an AI assistant that knows how to search the web." },
39+
{ "user", "What's Tesla stock worth today? Search X and the news for latest info." },
40+
};
41+
42+
var grok = new GrokClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!).AsIChatClient("grok-4.1-fast");
43+
44+
var options = new ChatOptions
45+
{
46+
Tools = [new HostedWebSearchTool()] // 👈 compatible with OpenAI
47+
};
48+
49+
var response = await grok.GetResponseAsync(messages, options);
50+
```
51+
52+
In addition to basic web search as shown above, Grok supports more
53+
[advanced search](https://docs.x.ai/docs/guides/tools/search-tools) scenarios,
54+
which can be opted-in by using Grok-specific types:
55+
56+
```csharp
57+
var grok = new GrokChatClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!)
58+
.AsIChatClient("grok-4.1-fast");
59+
var response = await grok.GetResponseAsync(
60+
"What are the latest product news by Tesla?",
61+
new ChatOptions
62+
{
63+
Tools = [new GrokSearchTool()
64+
{
65+
AllowedDomains = [ "ir.tesla.com" ]
66+
}]
67+
});
68+
```
69+
70+
You can alternatively set `ExcludedDomains` instead, and enable image
71+
understanding with `EnableImageUndestanding`. Learn more about these filters
72+
at [web search parameters](https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters).
73+
74+
## X Search
75+
76+
In addition to web search, Grok also supports searching on X (formerly Twitter):
77+
78+
```csharp
79+
var response = await grok.GetResponseAsync(
80+
"What's the latest on Optimus?",
81+
new ChatOptions
82+
{
83+
Tools = [new GrokXSearchTool
84+
{
85+
// AllowedHandles = [...],
86+
// ExcludedHandles = [...],
87+
// EnableImageUnderstanding = true,
88+
// EnableVideoUnderstanding = true,
89+
// FromDate = ...,
90+
// ToDate = ...,
91+
}]
92+
});
93+
```
94+
95+
Learn more about available filters at [X search parameters](https://docs.x.ai/docs/guides/tools/search-tools#x-search-parameters).
96+
97+
You can combine both web and X search in the same request by adding both tools.
98+
99+
## Code Execution
100+
101+
The code execution tool enables Grok to write and execute Python code in real-time,
102+
dramatically expanding its capabilities beyond text generation. This powerful feature
103+
allows Grok to perform precise calculations, complex data analysis, statistical
104+
computations, and solve mathematical problems that would be impossible through text alone.
105+
106+
This is Grok's equivalent of the OpenAI code interpreter, and is configured the same way:
107+
108+
```csharp
109+
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
110+
var response = await grok.GetResponseAsync(
111+
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
112+
new ChatOptions
113+
{
114+
Tools = [new HostedCodeInterpreterTool()]
115+
});
116+
117+
var text = response.Text;
118+
Assert.Contains("$6,288.95", text);
119+
```
120+
121+
If you want to access the output from the code execution, you can add that as an
122+
include in the options:
123+
124+
```csharp
125+
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
126+
var options = new GrokChatOptions
127+
{
128+
Include = { IncludeOption.CodeExecutionCallOutput },
129+
Tools = [new HostedCodeInterpreterTool()]
130+
};
131+
132+
var response = await grok.GetResponseAsync(
133+
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
134+
options);
135+
136+
var content = response.Messages
137+
.SelectMany(x => x.Contents)
138+
.OfType<CodeInterpreterToolResultContent>()
139+
.First();
140+
141+
foreach (AIContent output in content.Outputs)
142+
// process outputs from code interpreter
143+
```
144+
145+
Learn more about the [code execution tool](https://docs.x.ai/docs/guides/tools/code-execution-tool).
146+
147+
## Collection Search
148+
149+
If you maintain a [collection](https://docs.x.ai/docs/key-information/collections),
150+
Grok can perform semantic search on it:
151+
152+
```csharp
153+
var options = new ChatOptions
154+
{
155+
Tools = [new HostedFileSearchTool {
156+
Inputs = [new HostedVectorStoreContent("[collection_id]")]
157+
}]
158+
};
159+
```
160+
161+
Learn more about [collection search](https://docs.x.ai/docs/guides/tools/collections-search-tool).
162+
163+
## Remote MCP
164+
165+
Remote MCP Tools allow Grok to connect to external MCP (Model Context Protocol) servers.
166+
This example sets up the GitHub MCP server so queries about releases (limited specifically
167+
in this case):
168+
169+
```csharp
170+
var options = new ChatOptions
171+
{
172+
Tools = [new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") {
173+
AuthorizationToken = Configuration["GITHUB_TOKEN"]!,
174+
AllowedTools = ["list_releases"],
175+
}]
176+
};
177+
```
178+
179+
Just like with code execution, you can opt-in to surfacing the MCP outputs in
180+
the response:
181+
182+
```csharp
183+
var options = new GrokChatOptions
184+
{
185+
// Exposes McpServerToolResultContent in responses
186+
Include = { IncludeOption.McpCallOutput },
187+
Tools = [new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") {
188+
AuthorizationToken = Configuration["GITHUB_TOKEN"]!,
189+
AllowedTools = ["list_releases"],
190+
}]
191+
};
192+
193+
```
194+
195+
Learn more about [Remote MCP tools](https://docs.x.ai/docs/guides/tools/remote-mcp-tools).
196+
<!-- #xai -->
197+
198+
# xAI.Protocol
199+
200+
[![Version](https://img.shields.io/nuget/vpre/xAI.Protocol.svg?color=royalblue)](https://www.nuget.org/packages/xAI.Protocol)
201+
[![Downloads](https://img.shields.io/nuget/dt/xAI.Protocol.svg?color=green)](https://www.nuget.org/packages/xAI.Protocol)
202+
203+
<!-- #protocol -->
25204
## Usage
26205

27-
This project provides a .NET client for the gRPC API of xAI with full support for all services
206+
The xAI.Protocol package provides a .NET client for the gRPC API of xAI with full support for all services
28207
documented in the [official API reference](https://docs.x.ai/docs/grpc-reference) and
29208
corresponding [proto files](https://github.com/xai-org/xai-proto/tree/main/proto/xai/api/v1).
30209

@@ -54,7 +233,7 @@ ensuring it remains up-to-date with any changes or additions made to the API as
54233

55234
See for example the [introduction of tool output and citations](https://github.com/devlooped/GrokClient/pull/3).
56235

57-
<!-- #content -->
236+
<!-- #protocol -->
58237

59238
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
60239
# Sponsors

src/xAI.Protocol/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Grok client based on the official gRPC API reference from xAI
66

7+
<!-- include ../../readme.md#protocol -->
78
<!-- include https://github.com/devlooped/.github/raw/main/osmf.md -->
8-
<!-- include ../../readme.md#content -->
99
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
1010
<!-- exclude -->

src/xAI.Protocol/xAI.Protocol.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16+
<PackageReference Include="NuGetizer" Version="1.4.6" PrivateAssets="all" />
1617
<PackageReference Include="Google.Protobuf" Version="3.33.2" />
1718
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
1819
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.71.0" />
1920
<PackageReference Include="Grpc.Tools" Version="2.76.0" PrivateAssets="all" />
2021
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
21-
<PackageReference Include="NuGetizer" Version="1.4.6" PrivateAssets="all" />
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
2625
<None Include="..\..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
2726
<Protobuf Include="*.proto" GrpcServices="Client" />
2827
</ItemGroup>

0 commit comments

Comments
 (0)