Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .netconfig
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
sha = 407aa2d9319f5db12964540810b446fecc22d419
etag = 0dca55f20a72d3279554837f4eba867a1de37fe0f4a7535c2d9bc43867361cc5
weak
[file "src/Tests/Attributes.cs"]
[file "src/xAI.Tests/Extensions/Attributes.cs"]
url = https://github.com/devlooped/catbag/blob/main/Xunit/Attributes.cs
sha = 40914971d4d6b42d6f8a90923b131136f7e609a5
etag = c77e7b435ce1df06fb60a3b0e15a0833d8e45d4d19f366c6184140ebb4814b1a
Expand All @@ -190,3 +190,8 @@
sha = 666a2a7c315f72199c418f11482a950fc69a8901
etag = 91ea15c07bfd784036c6ca931f5b2df7e9767b8367146d96c79caef09d63899f
weak
[file "src/xAI/Extensions/Throw.cs"]
url = https://github.com/devlooped/catbag/blob/main/System/Throw.cs
sha = 3012d56be7554c483e5c5d277144c063969cada9
etag = 43c81c6c6dcdf5baee40a9e3edc5e871e473e6c954c901b82bb87a3a48888ea0
weak
189 changes: 184 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
![Icon](assets/icon.png) xAI .NET SDK
![Icon](assets/icon.png) .NET SDK
============

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

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

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

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

<!-- #content -->
<!-- #xai -->
xAI/Grok integration for Microsoft.Extensions.AI `IChatClient` with full support for all
[agentic tools](https://docs.x.ai/docs/guides/tools/overview):

```csharp
var grok = new GrokClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!)
.AsIChatClient("grok-4.1-fast");
```
## Web Search

```csharp
var messages = new Chat()
{
{ "system", "You are an AI assistant that knows how to search the web." },
{ "user", "What's Tesla stock worth today? Search X and the news for latest info." },
};

var grok = new GrokClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!).AsIChatClient("grok-4.1-fast");

var options = new ChatOptions
{
Tools = [new HostedWebSearchTool()] // 👈 compatible with OpenAI
};

var response = await grok.GetResponseAsync(messages, options);
```

In addition to basic web search as shown above, Grok supports more
[advanced search](https://docs.x.ai/docs/guides/tools/search-tools) scenarios,
which can be opted-in by using Grok-specific types:

```csharp
var grok = new GrokChatClient(Environment.GetEnvironmentVariable("XAI_API_KEY")!)
.AsIChatClient("grok-4.1-fast");
var response = await grok.GetResponseAsync(
"What are the latest product news by Tesla?",
new ChatOptions
{
Tools = [new GrokSearchTool()
{
AllowedDomains = [ "ir.tesla.com" ]
}]
});
```

You can alternatively set `ExcludedDomains` instead, and enable image
understanding with `EnableImageUndestanding`. Learn more about these filters
at [web search parameters](https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters).

## X Search

In addition to web search, Grok also supports searching on X (formerly Twitter):

```csharp
var response = await grok.GetResponseAsync(
"What's the latest on Optimus?",
new ChatOptions
{
Tools = [new GrokXSearchTool
{
// AllowedHandles = [...],
// ExcludedHandles = [...],
// EnableImageUnderstanding = true,
// EnableVideoUnderstanding = true,
// FromDate = ...,
// ToDate = ...,
}]
});
```

Learn more about available filters at [X search parameters](https://docs.x.ai/docs/guides/tools/search-tools#x-search-parameters).

You can combine both web and X search in the same request by adding both tools.

## Code Execution

The code execution tool enables Grok to write and execute Python code in real-time,
dramatically expanding its capabilities beyond text generation. This powerful feature
allows Grok to perform precise calculations, complex data analysis, statistical
computations, and solve mathematical problems that would be impossible through text alone.

This is Grok's equivalent of the OpenAI code interpreter, and is configured the same way:

```csharp
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
var response = await grok.GetResponseAsync(
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
new ChatOptions
{
Tools = [new HostedCodeInterpreterTool()]
});

var text = response.Text;
Assert.Contains("$6,288.95", text);
```

If you want to access the output from the code execution, you can add that as an
include in the options:

```csharp
var grok = new GrokClient(Configuration["XAI_API_KEY"]!).AsIChatClient("grok-4-fast");
var options = new GrokChatOptions
{
Include = { IncludeOption.CodeExecutionCallOutput },
Tools = [new HostedCodeInterpreterTool()]
};

var response = await grok.GetResponseAsync(
"Calculate the compound interest for $10,000 at 5% annually for 10 years",
options);

var content = response.Messages
.SelectMany(x => x.Contents)
.OfType<CodeInterpreterToolResultContent>()
.First();

foreach (AIContent output in content.Outputs)
// process outputs from code interpreter
```

Learn more about the [code execution tool](https://docs.x.ai/docs/guides/tools/code-execution-tool).

## Collection Search

If you maintain a [collection](https://docs.x.ai/docs/key-information/collections),
Grok can perform semantic search on it:

```csharp
var options = new ChatOptions
{
Tools = [new HostedFileSearchTool {
Inputs = [new HostedVectorStoreContent("[collection_id]")]
}]
};
```

Learn more about [collection search](https://docs.x.ai/docs/guides/tools/collections-search-tool).

## Remote MCP

Remote MCP Tools allow Grok to connect to external MCP (Model Context Protocol) servers.
This example sets up the GitHub MCP server so queries about releases (limited specifically
in this case):

```csharp
var options = new ChatOptions
{
Tools = [new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") {
AuthorizationToken = Configuration["GITHUB_TOKEN"]!,
AllowedTools = ["list_releases"],
}]
};
```

Just like with code execution, you can opt-in to surfacing the MCP outputs in
the response:

```csharp
var options = new GrokChatOptions
{
// Exposes McpServerToolResultContent in responses
Include = { IncludeOption.McpCallOutput },
Tools = [new HostedMcpServerTool("GitHub", "https://api.githubcopilot.com/mcp/") {
AuthorizationToken = Configuration["GITHUB_TOKEN"]!,
AllowedTools = ["list_releases"],
}]
};

```

Learn more about [Remote MCP tools](https://docs.x.ai/docs/guides/tools/remote-mcp-tools).
<!-- #xai -->

# xAI.Protocol

[![Version](https://img.shields.io/nuget/vpre/xAI.Protocol.svg?color=royalblue)](https://www.nuget.org/packages/xAI.Protocol)
[![Downloads](https://img.shields.io/nuget/dt/xAI.Protocol.svg?color=green)](https://www.nuget.org/packages/xAI.Protocol)

<!-- #protocol -->
## Usage

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

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

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

<!-- #content -->
<!-- #protocol -->

<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
# Sponsors
Expand Down
2 changes: 1 addition & 1 deletion src/xAI.Protocol/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Grok client based on the official gRPC API reference from xAI

<!-- include ../../readme.md#protocol -->
<!-- include https://github.com/devlooped/.github/raw/main/osmf.md -->
<!-- include ../../readme.md#content -->
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
<!-- exclude -->
3 changes: 1 addition & 2 deletions src/xAI.Protocol/xAI.Protocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGetizer" Version="1.4.6" PrivateAssets="all" />
<PackageReference Include="Google.Protobuf" Version="3.33.2" />
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.71.0" />
<PackageReference Include="Grpc.Tools" Version="2.76.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
<PackageReference Include="NuGetizer" Version="1.4.6" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
<None Include="..\..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
<Protobuf Include="*.proto" GrpcServices="Client" />
</ItemGroup>
Expand Down
Loading