Skip to content

Commit 381b2b8

Browse files
committed
Make sure Grok client provides metadata for telemetry
We otherwise miss the ProviderName that's used by the OpenTelemetry chat client decorator.
1 parent 088ec78 commit 381b2b8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/AI/Grok/GrokChatClient.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public partial class GrokChatClient : IChatClient
1818
readonly string modelId;
1919
readonly ClientPipeline pipeline;
2020
readonly OpenAIClientOptions options;
21+
readonly ChatClientMetadata metadata;
2122

2223
/// <summary>
2324
/// Initializes the client with the specified API key, model ID, and optional OpenAI client options.
@@ -27,6 +28,7 @@ public GrokChatClient(string apiKey, string modelId, OpenAIClientOptions? option
2728
this.modelId = modelId;
2829
this.options = options ?? new();
2930
this.options.Endpoint ??= new Uri("https://api.x.ai/v1");
31+
metadata = new ChatClientMetadata("x.ai", this.options.Endpoint, modelId);
3032

3133
// NOTE: by caching the pipeline, we speed up creation of new chat clients per model,
3234
// since the pipeline will be the same for all of them.
@@ -114,7 +116,12 @@ IChatClient GetChatClient(string modelId) => clients.GetOrAdd(modelId, model
114116

115117
void IDisposable.Dispose() { }
116118

117-
public object? GetService(Type serviceType, object? serviceKey = null) => null;
119+
/// <inheritdoc />
120+
public object? GetService(Type serviceType, object? serviceKey = null) => serviceType switch
121+
{
122+
Type t when t == typeof(ChatClientMetadata) => metadata,
123+
_ => null
124+
};
118125

119126
// Allows creating the base OpenAIClient with a pre-created pipeline.
120127
class PipelineClient(ClientPipeline pipeline, OpenAIClientOptions options) : OpenAIClient(pipeline, options) { }

0 commit comments

Comments
 (0)