Skip to content
Merged
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
15 changes: 13 additions & 2 deletions src/AI/OpenAI/OpenAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class OpenAIChatClient : IChatClient
readonly string modelId;
readonly ClientPipeline pipeline;
readonly OpenAIClientOptions? options;
readonly ChatClientMetadata? metadata;

/// <summary>
/// Initializes the client with the specified API key, model ID, and optional OpenAI client options.
Expand All @@ -28,7 +29,12 @@ public OpenAIChatClient(string apiKey, string modelId, OpenAIClientOptions? opti

// NOTE: by caching the pipeline, we speed up creation of new chat clients per model,
// since the pipeline will be the same for all of them.
pipeline = new OpenAIClient(new ApiKeyCredential(apiKey), options).Pipeline;
var client = new OpenAIClient(new ApiKeyCredential(apiKey), options);
metadata = client.GetChatClient(modelId)
.AsIChatClient()
.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata;

pipeline = client.Pipeline;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -67,7 +73,12 @@ IChatClient GetChatClient(string modelId) => clients.GetOrAdd(modelId, model

void IDisposable.Dispose() => GC.SuppressFinalize(this);

public object? GetService(Type serviceType, object? serviceKey = null) => null;
/// <inheritdoc />
public object? GetService(Type serviceType, object? serviceKey = null) => serviceType switch
{
Type t when t == typeof(ChatClientMetadata) => metadata,
_ => null
};

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