Skip to content

Commit 30a0826

Browse files
committed
Add usability overloads for List model client operations
1 parent 1aa2e99 commit 30a0826

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

src/GrokClient/Extensions.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
using System.Collections.Generic;
2+
using System.ComponentModel;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Google.Protobuf.WellKnownTypes;
56

6-
namespace Devlooped.Grok
7+
namespace Devlooped.Grok;
8+
9+
[EditorBrowsable(EditorBrowsableState.Never)]
10+
public static class GrpcExtensions
711
{
8-
public static class ModelsClientExtensions
12+
extension(Models.ModelsClient client)
913
{
10-
extension(Models.ModelsClient client)
14+
public async Task<IEnumerable<LanguageModel>> ListLanguageModelsAsync(CancellationToken cancellation = default)
1115
{
12-
public async Task<IEnumerable<LanguageModel>> ListLanguageModelsAsync(CancellationToken cancellation = default)
13-
{
14-
var models = await client.ListLanguageModelsAsync(new Empty(), cancellationToken: cancellation);
15-
return models.Models;
16-
}
16+
var models = await client.ListLanguageModelsAsync(new Empty(), cancellationToken: cancellation);
17+
return models.Models;
1718
}
18-
}
19-
}
2019

21-
namespace Devlooped.Grok
22-
{
23-
/// <summary>
24-
/// An API service that let users get details of available models on the
25-
/// platform.
26-
/// </summary>
27-
partial class Models
28-
{
29-
static Models() => __ServiceName = "xai_api.Models";
20+
public async Task<IEnumerable<EmbeddingModel>> ListEmbeddingModelsAsync(CancellationToken cancellation)
21+
{
22+
var models = await client.ListEmbeddingModelsAsync(new Empty(), cancellationToken: cancellation);
23+
return models.Models;
24+
}
25+
26+
public async Task<IEnumerable<ImageGenerationModel>> ListImageGenerationModelsAsync(CancellationToken cancellation = default)
27+
{
28+
var models = await client.ListImageGenerationModelsAsync(new Empty(), cancellationToken: cancellation);
29+
return models.Models;
30+
}
3031
}
31-
}
32+
}

src/GrokClient/GrokServiceCollectionExtensions.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
1414
{
1515
var address = new Uri("https://api.x.ai/");
1616

17-
Action<HttpClient> http = client => client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {apiKey}");
18-
19-
services.AddGrpcClient<Auth.AuthClient>(options =>
17+
var builder = services.AddGrpcClient<Auth.AuthClient>(options =>
2018
{
2119
options.Address = address;
2220
configureClient?.Invoke(options);
@@ -27,7 +25,9 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
2725
return Task.CompletedTask;
2826
});
2927

30-
services.AddGrpcClient<Chat.ChatClient>(options =>
28+
configureHttp?.Invoke(builder);
29+
30+
builder = services.AddGrpcClient<Chat.ChatClient>(options =>
3131
{
3232
options.Address = address;
3333
configureClient?.Invoke(options);
@@ -38,7 +38,9 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
3838
return Task.CompletedTask;
3939
});
4040

41-
services.AddGrpcClient<Embedder.EmbedderClient>(options =>
41+
configureHttp?.Invoke(builder);
42+
43+
builder = services.AddGrpcClient<Embedder.EmbedderClient>(options =>
4244
{
4345
options.Address = address;
4446
configureClient?.Invoke(options);
@@ -49,7 +51,9 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
4951
return Task.CompletedTask;
5052
});
5153

52-
services.AddGrpcClient<Image.ImageClient>(options =>
54+
configureHttp?.Invoke(builder);
55+
56+
builder = services.AddGrpcClient<Image.ImageClient>(options =>
5357
{
5458
options.Address = address;
5559
configureClient?.Invoke(options);
@@ -60,21 +64,22 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
6064
return Task.CompletedTask;
6165
});
6266

63-
var builder = services.AddGrpcClient<Models.ModelsClient>(options =>
67+
configureHttp?.Invoke(builder);
68+
69+
builder = services.AddGrpcClient<Models.ModelsClient>(options =>
6470
{
6571
options.Address = address;
6672
configureClient?.Invoke(options);
67-
}).ConfigureHttpClient(http);
73+
})
74+
.AddCallCredentials((context, metadata) =>
75+
{
76+
metadata.Add("Authorization", $"Bearer {apiKey}");
77+
return Task.CompletedTask;
78+
});
6879

6980
configureHttp?.Invoke(builder);
7081

71-
//.AddCallCredentials((context, metadata) =>
72-
//{
73-
// metadata.Add("Authorization", $"Bearer {apiKey}");
74-
// return Task.CompletedTask;
75-
//});
76-
77-
services.AddGrpcClient<Sample.SampleClient>(options =>
82+
builder = services.AddGrpcClient<Sample.SampleClient>(options =>
7883
{
7984
options.Address = address;
8085
configureClient?.Invoke(options);
@@ -85,7 +90,9 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
8590
return Task.CompletedTask;
8691
});
8792

88-
services.AddGrpcClient<Tokenize.TokenizeClient>(options =>
93+
configureHttp?.Invoke(builder);
94+
95+
builder = services.AddGrpcClient<Tokenize.TokenizeClient>(options =>
8996
{
9097
options.Address = address;
9198
configureClient?.Invoke(options);
@@ -96,7 +103,9 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
96103
return Task.CompletedTask;
97104
});
98105

99-
services.AddGrpcClient<Documents.DocumentsClient>(options =>
106+
configureHttp?.Invoke(builder);
107+
108+
builder = services.AddGrpcClient<Documents.DocumentsClient>(options =>
100109
{
101110
options.Address = address;
102111
configureClient?.Invoke(options);
@@ -107,6 +116,8 @@ public static IServiceCollection AddGrokClient(this IServiceCollection services,
107116
return Task.CompletedTask;
108117
});
109118

119+
configureHttp?.Invoke(builder);
120+
110121
return services;
111122
}
112123
}

src/Tests/ModelsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task ListModelsAsync()
1414
.BuildServiceProvider();
1515

1616
var client = services.GetRequiredService<Models.ModelsClient>();
17-
17+
1818
var models = await client.ListLanguageModelsAsync();
1919

2020
Assert.NotNull(models);

0 commit comments

Comments
 (0)