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
5 changes: 5 additions & 0 deletions .netconfig
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,8 @@
sha = 3012d56be7554c483e5c5d277144c063969cada9
etag = 43c81c6c6dcdf5baee40a9e3edc5e871e473e6c954c901b82bb87a3a48888ea0
weak
[file "src/Grok/Extensions/AIContentExtensions.cs"]
url = https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContentExtensions.cs
sha = c221abef4b4f1bf3fcf0bda27490e8b26bb479f4
etag = 510868eaae58941d71cdcb5416f44ebf4436a22d342ca5838172127b04252fe0
weak
Binary file added assets/img/cli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ See for example the [introduction of tool output and citations](https://github.c

<!-- #protocol -->

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

<!-- #cli -->
Sample Grok CLI client based on the xAI

![](https://raw.githubusercontent.com/devlooped/xAI/main/assets/img/cli.png)

Uses your own API Key.

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

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.targets
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project InitialTargets="SetLocalVersion">
<Project>

<Target Name="SetLocalVersion" Condition="!$(CI)">
<GetVersion>
Expand Down
4 changes: 4 additions & 0 deletions src/Grok/Extensions/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*.cs]
generated_code = true
116 changes: 116 additions & 0 deletions src/Grok/Extensions/AIContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
#if NET
using System.Runtime.CompilerServices;
#else
using System.Text;
#endif

namespace Microsoft.Extensions.AI;

/// <summary>Internal extensions for working with <see cref="AIContent"/>.</summary>
internal static class AIContentExtensions
{
/// <summary>Concatenates the text of all <see cref="TextContent"/> instances in the list.</summary>
public static string ConcatText(this IEnumerable<AIContent> contents)
{
if (contents is IList<AIContent> list)
{
int count = list.Count;
switch (count)
{
case 0:
return string.Empty;

case 1:
return (list[0] as TextContent)?.Text ?? string.Empty;

default:
#if NET
DefaultInterpolatedStringHandler builder = new(count, 0, null, stackalloc char[512]);
for (int i = 0; i < count; i++)
{
if (list[i] is TextContent text)
{
builder.AppendLiteral(text.Text);
}
}

return builder.ToStringAndClear();
#else
StringBuilder builder = new();
for (int i = 0; i < count; i++)
{
if (list[i] is TextContent text)
{
builder.Append(text.Text);
}
}

return builder.ToString();
#endif
}
}

return string.Concat(contents.OfType<TextContent>());
}

/// <summary>Concatenates the <see cref="ChatMessage.Text"/> of all <see cref="ChatMessage"/> instances in the list.</summary>
/// <remarks>A newline separator is added between each non-empty piece of text.</remarks>
public static string ConcatText(this IList<ChatMessage> messages)
{
int count = messages.Count;
switch (count)
{
case 0:
return string.Empty;

case 1:
return messages[0].Text;

default:
#if NET
DefaultInterpolatedStringHandler builder = new(count, 0, null, stackalloc char[512]);
bool needsSeparator = false;
for (int i = 0; i < count; i++)
{
string text = messages[i].Text;
if (text.Length > 0)
{
if (needsSeparator)
{
builder.AppendLiteral(Environment.NewLine);
}

builder.AppendLiteral(text);

needsSeparator = true;
}
}

return builder.ToStringAndClear();
#else
StringBuilder builder = new();
for (int i = 0; i < count; i++)
{
string text = messages[i].Text;
if (text.Length > 0)
{
if (builder.Length > 0)
{
builder.AppendLine();
}

builder.Append(text);
}
}

return builder.ToString();
#endif
}
}
}
47 changes: 47 additions & 0 deletions src/Grok/Grok.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<PackageId>grok</PackageId>
<Description>Sample Grok CLI using xAI and xAI.Protocol packages</Description>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<ToolCommandName>grok</ToolCommandName>
<PackageTags>dotnet-tool xai ai grok llm</PackageTags>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<PackAsTool>true</PackAsTool>
<NoWarn>MEAI001;xAI001;$(NoWarn)</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\xAI.Tests\DotEnv.cs" Link="DotEnv.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NuGetizer" Version="1.4.6" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0" />
<PackageReference Include="Spectre.Console" Version="0.54.0" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="DotNetConfig.Configuration" Version="1.2.0" />
<PackageReference Include="Spectre.Console.Json" Version="0.54.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\xAI\xAI.csproj" />
</ItemGroup>

<ItemGroup>
<EditorConfigFiles Remove="C:\Code\xAI\src\Grok\Extensions\.editorconfig" />
</ItemGroup>

<ItemGroup>
<None Include="C:\Code\xAI\src\Grok\Extensions\.editorconfig" />
</ItemGroup>

</Project>
Loading