Skip to content

Commit 7a22fb9

Browse files
committed
Leverage overload resolution attribute to direct compiler
We want our overload to take precedence over the built-in MEAI to provider smarter casting for your clients.
1 parent 0a4334a commit 7a22fb9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/AI.CodeAnalysis/ChatClientExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
/// </summary>
88
static class ChatClientExtensions
99
{
10+
#if NET9_0_OR_GREATER
11+
[System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute(100)]
12+
#endif
1013
/// <summary>Gets an <see cref="IChatClient"/> for use with this <see cref="ChatClient"/>.</summary>
1114
public static IChatClient AsIChatClient(this ChatClient client) =>
1215
client as IChatClient ?? OpenAIClientExtensions.AsIChatClient(client);

src/AI.Tests/OpenAITests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Nodes;
1+
using System.ClientModel;
2+
using System.Text.Json.Nodes;
23
using Devlooped.Extensions.AI.OpenAI;
34
using Microsoft.Extensions.AI;
45
using OpenAI;
@@ -9,6 +10,18 @@ namespace Devlooped.Extensions.AI;
910

1011
public class OpenAITests(ITestOutputHelper output)
1112
{
13+
[SecretsFact("OPENAI_API_KEY")]
14+
public void CanGetAsIChatClient()
15+
{
16+
var inner = new OpenAIClient(new ApiKeyCredential(Configuration["OPENAI_API_KEY"]!),
17+
new OpenAIClientOptions
18+
{
19+
//Endpoint = new Uri("https://api.x.ai/v1"),
20+
}).GetChatClient("grok-4").AsIChatClient();
21+
22+
Assert.NotNull(inner);
23+
}
24+
1225
[SecretsFact("OPENAI_API_KEY")]
1326
public async Task OpenAISwitchesModel()
1427
{

0 commit comments

Comments
 (0)