301 instantiations of ChatMessage
Aspire.Azure.AI.OpenAI.Tests (1)
Aspire.OpenAI.Tests (1)
Microsoft.Extensions.AI (4)
ChatCompletion\ChatClientStructuredOutputExtensions.cs (3)
74GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), options, useJsonSchemaResponseFormat, cancellationToken);
114GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), serializerOptions, options, useJsonSchemaResponseFormat, cancellationToken);
213promptAugmentation = new ChatMessage(ChatRole.User, $$"""
Microsoft.Extensions.AI.Abstractions (5)
ChatCompletion\ChatMessage.cs (1)
51new()
Microsoft.Extensions.AI.Abstractions.Tests (17)
Microsoft.Extensions.AI.AzureAIInference (1)
Microsoft.Extensions.AI.AzureAIInference.Tests (13)
AzureAIInferenceChatClientTests.cs (13)
145[new ChatMessage(ChatRole.User, "hello".Select(c => (AIContent)new TextContent(c.ToString())).ToList())] :
146[new ChatMessage(ChatRole.User, "hello")];
218[new ChatMessage(ChatRole.User, "hello".Select(c => (AIContent)new TextContent(c.ToString())).ToList())] :
219[new ChatMessage(ChatRole.User, "hello")];
986new(ChatRole.System, "You are a really nice friend."),
987new(ChatRole.User, "hello!"),
988new(ChatRole.Assistant, "hi, how are you?"),
989new(ChatRole.User, "i'm good. how are you?"),
990new(ChatRole.Assistant, [new FunctionCallContent("abcd123", "GetMood")]),
991new(ChatRole.Tool, [new FunctionResultContent("abcd123", "happy")]),
1067Assert.NotNull(await client.GetResponseAsync([new(ChatRole.User,
1132new(ChatRole.Assistant, (string?)null),
1133new(ChatRole.User, "hello!"),
Microsoft.Extensions.AI.Evaluation (3)
Microsoft.Extensions.AI.Evaluation.Integration.Tests (9)
Microsoft.Extensions.AI.Evaluation.NLP.Tests (17)
Microsoft.Extensions.AI.Evaluation.Quality (22)
Microsoft.Extensions.AI.Evaluation.Reporting (3)
Microsoft.Extensions.AI.Evaluation.Reporting.Tests (7)
Microsoft.Extensions.AI.Evaluation.Safety (2)
Microsoft.Extensions.AI.Evaluation.Tests (3)
Microsoft.Extensions.AI.Integration.Tests (33)
ChatClientIntegrationTests.cs (25)
69new(ChatRole.User, "Pick a city, any city"),
70new(ChatRole.Assistant, "Seattle"),
71new(ChatRole.User, "And another one"),
72new(ChatRole.Assistant, "Jakarta"),
73new(ChatRole.User, "What continent are they each in?"),
87new(ChatRole.System, []),
88new(ChatRole.User, []),
89new(ChatRole.Assistant, []),
90new(ChatRole.User, "What is 1 + 2? Reply with a single number."),
103new(ChatRole.User, "Quote, word for word, Neil Armstrong's famous words.")
161List<ChatMessage> history = [new(ChatRole.User, "Explain in 100 words how AI works")];
184new(ChatRole.User,
202new(ChatRole.User,
232new(ChatRole.User, "What is the current secret number?")
299new(ChatRole.User, "What is the secret number for id foo?")
331new(ChatRole.User, "What is the secret number for John aged 19?")
361new(ChatRole.User, "Can you add bacon, lettuce, and tomatoes to Peter's shopping cart?")
640var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
656var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
681var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
733var message = new ChatMessage(ChatRole.User, "What is the temperature?");
772var message = new ChatMessage(ChatRole.User, "What is the temperature?");
814var message = new ChatMessage(ChatRole.User, "What is the temperature?");
842await chatClient.GetResponseAsync([new(ChatRole.User, "What's the biggest animal?")]);
941var response = await chatClient.GetResponseAsync([new(ChatRole.User, "What's the biggest animal?")]);
ReducingChatClientTests.cs (5)
47new ChatMessage(ChatRole.User, "Hi there! Can you tell me about golden retrievers?"),
48new ChatMessage(ChatRole.Assistant, "Of course! Golden retrievers are known for their friendly and tolerant attitudes. They're great family pets and are very intelligent and easy to train."),
49new ChatMessage(ChatRole.User, "What kind of exercise do they need?"),
50new ChatMessage(ChatRole.Assistant, "Golden retrievers are quite active and need regular exercise. Daily walks, playtime, and activities like fetching or swimming are great for them."),
51new ChatMessage(ChatRole.User, "Are they good with kids?"),
Microsoft.Extensions.AI.Ollama (1)
Microsoft.Extensions.AI.Ollama.Tests (6)
OllamaChatClientTests.cs (6)
269new(ChatRole.User, "hello!"),
270new(ChatRole.Assistant, "hi, how are you?"),
271new(ChatRole.User, "i'm good. how are you?"),
467new(ChatRole.User, "How old is Alice?"),
468new(ChatRole.Assistant, [new FunctionCallContent("abcd1234", "GetPersonAge", new Dictionary<string, object?> { ["personName"] = "Alice" })]),
469new(ChatRole.Tool, [new FunctionResultContent("abcd1234", 42)]),
Microsoft.Extensions.AI.OpenAI (2)
Microsoft.Extensions.AI.OpenAI.Tests (17)
OpenAIChatClientTests.cs (17)
804new(ChatRole.System, "You are a really nice friend."),
805new(ChatRole.User, "hello!"),
806new(ChatRole.Assistant, "hi, how are you?"),
807new(ChatRole.User, "i'm good. how are you?"),
911new(ChatRole.System, [new TextContent("You are a really nice friend."), new TextContent("Really nice.")]),
912new(ChatRole.User, "hello!"),
1007new(ChatRole.System, "You are a really nice friend."),
1008new(ChatRole.User, "hello!"),
1009new(ChatRole.Assistant, (string?)null),
1010new(ChatRole.User, "i'm good. how are you?"),
1449new(ChatRole.System, "You are a really nice friend."),
1450new(ChatRole.User, "hello!"),
1451new(ChatRole.Assistant,
1457new (ChatRole.Tool,
1462new(ChatRole.Assistant, "You are great."),
1463new(ChatRole.User, "Thanks!"),
1569new(ChatRole.User,
Microsoft.Extensions.AI.Tests (122)
ChatCompletion\ChatClientStructuredOutputExtensionsTests.cs (13)
81var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
142var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
201var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "This is not valid JSON"));
207var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
220var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "null"));
226var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
239var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, [new UriContent("https://example.com", "image/*")]));
245var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
273var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
301var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
326var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, JsonSerializer.Serialize(expectedResult, jso)));
369var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
397var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
ChatCompletion\DistributedCachingChatClientTest.cs (12)
48new ChatMessage(new ChatRole("fakeRole"), "This is some content")
140return new ChatResponse(new ChatMessage(ChatRole.Assistant, "Hello"));
184var input = new ChatMessage(ChatRole.User, "abc");
214return new ChatResponse(new ChatMessage(ChatRole.Assistant, "A good result"));
223var input = new ChatMessage(ChatRole.User, "abc");
494var input = new ChatMessage(ChatRole.User, "abc");
532var input = new ChatMessage(ChatRole.User, "abc");
559return new(new ChatMessage(ChatRole.Assistant, options!.AdditionalProperties!["someKey"]!.ToString()));
610return new(new ChatMessage(ChatRole.Assistant, innerCallCount.ToString()));
656return new(new ChatMessage(ChatRole.Assistant, options!.AdditionalProperties!["someKey"]!.ToString()));
685new ChatMessage(new ChatRole("fakeRole"),
744new ChatMessage(ChatRole.Assistant, [new TextContent("Hey")])));
ChatCompletion\FunctionInvokingChatClientTests.cs (84)
87new ChatMessage(ChatRole.User, "hello"),
88new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
89new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
90new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
91new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
92new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
93new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
94new ChatMessage(ChatRole.Assistant, "world"),
118new ChatMessage(ChatRole.User, "hello"),
119new ChatMessage(ChatRole.Assistant,
125new ChatMessage(ChatRole.Tool,
131new ChatMessage(ChatRole.Assistant,
136new ChatMessage(ChatRole.Tool,
141new ChatMessage(ChatRole.Assistant, "world"),
178new ChatMessage(ChatRole.User, "hello"),
179new ChatMessage(ChatRole.Assistant,
184new ChatMessage(ChatRole.Tool,
189new ChatMessage(ChatRole.Assistant, "done"),
222new ChatMessage(ChatRole.User, "hello"),
223new ChatMessage(ChatRole.Assistant,
228new ChatMessage(ChatRole.Tool,
233new ChatMessage(ChatRole.Assistant, "done"),
256new ChatMessage(ChatRole.User, "hello"),
257new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
258new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1 from delegate")]),
259new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
260new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42 from delegate")]),
261new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
262new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
263new ChatMessage(ChatRole.Assistant, "world"),
305new ChatMessage(ChatRole.User, "hello"),
306new ChatMessage(ChatRole.Assistant, [new FunctionCallContent($"callId0", "VoidReturn")]),
313plan.Add(new ChatMessage(ChatRole.Tool, [new FunctionResultContent($"callId{i}", result: "Success: Function completed.")]));
314plan.Add(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent($"callId{(i + 1)}", "VoidReturn")]));
354new ChatMessage(ChatRole.User, "hello"),
432new ChatMessage(ChatRole.User, "hello"),
449var assistantMessage = new ChatMessage(ChatRole.Assistant, []);
450var toolMessage = new ChatMessage(ChatRole.Tool, []);
479new ChatMessage(ChatRole.User, "hello"),
480new ChatMessage(ChatRole.Assistant, [new TextContent("extra"), new FunctionCallContent("callId1", "Func1"), new TextContent("stuff")]),
481new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 1")]),
482new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
483new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
484new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } }), new TextContent("more")]),
485new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
486new ChatMessage(ChatRole.Assistant, "world"),
516new ChatMessage(ChatRole.User, "hello"),
517new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
518new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: detailedErrors ? "Error: Function failed. Exception: Oh no!" : "Error: Function failed.")]),
519new ChatMessage(ChatRole.Assistant, "world"),
538new ChatMessage(ChatRole.User, "hello"),
539new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
540new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
541new ChatMessage(ChatRole.Assistant, "world"),
594new ChatMessage(ChatRole.User, "hello"),
595new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
596new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
597new ChatMessage(ChatRole.Assistant, "world"),
655new(ChatRole.User, "Hello"),
713new(ChatRole.User, "Hello"),
723new(ChatRole.Assistant, [new FunctionCallContent($"callId{chatContents.Count()}", "Func1")]) :
724new(ChatRole.Assistant, "The answer is 42.");
771new ChatMessage(ChatRole.User, "hello"),
772new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["i"] = 41 })]),
773new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 41")]),
774new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func1", new Dictionary<string, object?> { ["i"] = 42 })]),
775new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 42")]),
782new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "Func1", new Dictionary<string, object?> { ["i"] = 43 })]),
783new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Result 43")]),
784new ChatMessage(ChatRole.Assistant, "world"),
847var message = new ChatMessage(ChatRole.Assistant, resultContents);
855List<ChatMessage> messages = [new(ChatRole.User, "hello")];
890return new ChatResponse(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId-abc", "Func1")]))
898return new ChatResponse(new ChatMessage(ChatRole.Assistant, "done!"));
927new ChatMessage(ChatRole.User, "hello"),
928new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
929new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
930new ChatMessage(ChatRole.Assistant, "world"),
956new ChatMessage(ChatRole.User, "hello"),
957new ChatMessage(ChatRole.Assistant, [
961new ChatMessage(ChatRole.Tool,
966new ChatMessage(ChatRole.Assistant, "world"),
1030var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1124ChatMessage message = new(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
ChatCompletion\OpenTelemetryChatClientTests.cs (7)
41return new ChatResponse(new ChatMessage(ChatRole.Assistant, "The blue whale, I think."))
110new(ChatRole.System, "You are a close friend."),
111new(ChatRole.User, "Hey!"),
112new(ChatRole.Assistant, [new FunctionCallContent("12345", "GetPersonName")]),
113new(ChatRole.Tool, [new FunctionResultContent("12345", "John")]),
114new(ChatRole.Assistant, "Hey John, what's up?"),
115new(ChatRole.User, "What's the biggest animal?")
Microsoft.ML.GenAI.Core (1)
Microsoft.ML.GenAI.Core.Tests (3)
Microsoft.ML.GenAI.LLaMA.Tests (3)
Microsoft.ML.GenAI.Samples (5)
527 references to ChatMessage
Aspire.Azure.AI.OpenAI.Tests (1)
Aspire.OpenAI.Tests (1)
Microsoft.Extensions.AI (66)
ChatCompletion\AnonymousDelegatingChatClient.cs (11)
23private readonly Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, Task<ChatResponse>>? _getResponseFunc;
31private readonly Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? _getStreamingResponseFunc;
34private readonly Func<IEnumerable<ChatMessage>, ChatOptions?, Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task>, CancellationToken, Task>? _sharedFunc;
53Func<IEnumerable<ChatMessage>, ChatOptions?, Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task>, CancellationToken, Task> sharedFunc)
79Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, Task<ChatResponse>>? getResponseFunc,
80Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? getStreamingResponseFunc)
91IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
100IEnumerable<ChatMessage> messages, ChatOptions? options, CancellationToken cancellationToken)
130IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
ChatCompletion\CachingChatClient.cs (6)
50IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
60IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
78IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
88IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
153protected abstract string GetCacheKey(IEnumerable<ChatMessage> messages, ChatOptions? options, params ReadOnlySpan<object?> additionalValues);
212protected virtual bool EnableCaching(IEnumerable<ChatMessage> messages, ChatOptions? options)
ChatCompletion\ChatClientBuilder.cs (4)
108public ChatClientBuilder Use(Func<IEnumerable<ChatMessage>, ChatOptions?, Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task>, CancellationToken, Task> sharedFunc)
143Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, Task<ChatResponse>>? getResponseFunc,
144Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? getStreamingResponseFunc)
ChatCompletion\FunctionInvokingChatClient.cs (22)
219IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
229List<ChatMessage> originalMessages = [.. messages];
232List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
234List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used for the final response
314IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
325List<ChatMessage> originalMessages = [.. messages];
328List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
330List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used in fallback cases to reconstitute history
397foreach (var message in modeAndMessages.MessagesAdded)
452IEnumerable<ChatMessage> originalMessages,
453ref IEnumerable<ChatMessage> messages,
454[NotNull] ref List<ChatMessage>? augmentedHistory,
456List<ChatMessage> allTurnsResponseMessages,
508IList<ChatMessage> messages, [NotNullWhen(true)] ref List<FunctionCallContent>? functionCalls)
568private async Task<(bool ShouldTerminate, int NewConsecutiveErrorCount, IList<ChatMessage> MessagesAdded)> ProcessFunctionCallsAsync(
569List<ChatMessage> messages, ChatOptions options, List<FunctionCallContent> functionCallContents, int iteration, int consecutiveErrorCount,
586IList<ChatMessage> addedMessages = CreateResponseMessages([result]);
631IList<ChatMessage> addedMessages = CreateResponseMessages(results.ToArray());
647private void UpdateConsecutiveErrorCountOrThrow(IList<ChatMessage> added, ref int consecutiveErrorCount)
679private void ThrowIfNoFunctionResultsAdded(IList<ChatMessage>? messages)
698List<ChatMessage> messages, ChatOptions options, List<FunctionCallContent> callContents,
754protected virtual IList<ChatMessage> CreateResponseMessages(
Microsoft.Extensions.AI.Abstractions (46)
ChatCompletion\ChatResponseExtensions.cs (16)
27public static void AddMessages(this IList<ChatMessage> list, ChatResponse response)
32if (list is List<ChatMessage> listConcrete)
38foreach (var message in response.Messages)
45/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
51/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, the
56public static void AddMessages(this IList<ChatMessage> list, IEnumerable<ChatResponseUpdate> updates)
69/// <summary>Converts the <paramref name="update"/> into a <see cref="ChatMessage"/> instance and adds it to <paramref name="list"/>.</summary>
77/// no <see cref="ChatMessage"/> will be added to the <paramref name="list"/>.
79public static void AddMessages(this IList<ChatMessage> list, ChatResponseUpdate update, Func<AIContent, bool>? filter = null)
96/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
104/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, tne
110this IList<ChatMessage> list, IAsyncEnumerable<ChatResponseUpdate> updates, CancellationToken cancellationToken = default)
118IList<ChatMessage> list, IAsyncEnumerable<ChatResponseUpdate> updates, CancellationToken cancellationToken) =>
128/// <see cref="ChatMessage"/> instances. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
156/// <see cref="ChatMessage"/> instances. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
256ChatMessage message;
Microsoft.Extensions.AI.Abstractions.Tests (29)
TestChatClient.cs (4)
20public Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task<ChatResponse>>? GetResponseAsyncCallback { get; set; }
22public Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? GetStreamingResponseAsyncCallback { get; set; }
29public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
32public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
Microsoft.Extensions.AI.AzureAIInference (7)
Microsoft.Extensions.AI.AzureAIInference.Tests (4)
Microsoft.Extensions.AI.Evaluation (49)
Microsoft.Extensions.AI.Evaluation.Integration.Tests (41)
Microsoft.Extensions.AI.Evaluation.NLP (3)
Microsoft.Extensions.AI.Evaluation.Quality (64)
Microsoft.Extensions.AI.Evaluation.Reporting (12)
Microsoft.Extensions.AI.Evaluation.Reporting.Tests (5)
Microsoft.Extensions.AI.Evaluation.Safety (36)
ContentSafetyServicePayloadUtilities.cs (22)
17IEnumerable<ChatMessage> conversation,
80IEnumerable<ChatMessage> conversation,
92List<Dictionary<string, ChatMessage>> turns;
112if (turn.TryGetValue("question", out ChatMessage? question))
117if (turn.TryGetValue("answer", out ChatMessage? answer))
168IEnumerable<ChatMessage> conversation,
186List<Dictionary<string, ChatMessage>> turns;
206if (turn.TryGetValue("question", out ChatMessage? question))
211if (turn.TryGetValue("answer", out ChatMessage? answer))
250IEnumerable<ChatMessage> conversation,
264List<Dictionary<string, ChatMessage>> turns;
278IEnumerable<JsonObject> GetMessages(Dictionary<string, ChatMessage> turn, int turnIndex)
282if (turn.TryGetValue("question", out ChatMessage? question))
293if (turn.TryGetValue("answer", out ChatMessage? answer))
318IEnumerable<JsonObject> GetContents(ChatMessage message)
388(List<Dictionary<string, ChatMessage>> turns,
392IEnumerable<ChatMessage> conversation,
399List<Dictionary<string, ChatMessage>> turns = [];
400Dictionary<string, ChatMessage> currentTurn = [];
420foreach (ChatMessage message in conversation)
454void ValidateContents(ChatMessage message)
482foreach (var message in turn.Values)
Microsoft.Extensions.AI.Evaluation.Tests (3)
Microsoft.Extensions.AI.Integration.Tests (33)
ChatClientIntegrationTests.cs (12)
101IList<ChatMessage> chatHistory =
161List<ChatMessage> history = [new(ChatRole.User, "Explain in 100 words how AI works")];
230List<ChatMessage> messages =
297List<ChatMessage> messages =
329List<ChatMessage> messages =
359List<ChatMessage> messages =
640var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
656var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
681var message = new ChatMessage(ChatRole.User, "Pick a random number, uniformly distributed between 1 and 1000000");
733var message = new ChatMessage(ChatRole.User, "What is the temperature?");
772var message = new ChatMessage(ChatRole.User, "What is the temperature?");
814var message = new ChatMessage(ChatRole.User, "What is the temperature?");
test\Libraries\Microsoft.Extensions.AI.Abstractions.Tests\TestChatClient.cs (4)
20public Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task<ChatResponse>>? GetResponseAsyncCallback { get; set; }
22public Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? GetStreamingResponseAsyncCallback { get; set; }
29public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
32public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
Microsoft.Extensions.AI.Ollama (5)
Microsoft.Extensions.AI.Ollama.Tests (2)
Microsoft.Extensions.AI.OpenAI (15)
Microsoft.Extensions.AI.OpenAI.Tests (4)
Microsoft.Extensions.AI.Tests (79)
ChatCompletion\DistributedCachingChatClientTest.cs (9)
122private sealed class CustomCachingChatClient(IChatClient innerClient, IDistributedCache storage, Func<IEnumerable<ChatMessage>, ChatOptions?, bool> enableCaching) :
125protected override bool EnableCaching(IEnumerable<ChatMessage> messages, ChatOptions? options) => enableCaching(messages, options);
184var input = new ChatMessage(ChatRole.User, "abc");
223var input = new ChatMessage(ChatRole.User, "abc");
494var input = new ChatMessage(ChatRole.User, "abc");
532var input = new ChatMessage(ChatRole.User, "abc");
805ChatMessage expectedMessage = expected.Messages.Last();
806ChatMessage actualMessage = actual.Messages.Last();
880protected override string GetCacheKey(IEnumerable<ChatMessage> messages, ChatOptions? options, params ReadOnlySpan<object?> additionalValues)
ChatCompletion\FunctionInvokingChatClientTests.cs (45)
85List<ChatMessage> plan =
116List<ChatMessage> plan =
176List<ChatMessage> plan =
220List<ChatMessage> plan =
254List<ChatMessage> plan =
303List<ChatMessage> plan =
352List<ChatMessage> plan =
430List<ChatMessage> plan =
447private static IEnumerable<ChatMessage> CreateFunctionCallIterationPlan(ref int callIndex, params bool[] shouldThrow)
449var assistantMessage = new ChatMessage(ChatRole.Assistant, []);
450var toolMessage = new ChatMessage(ChatRole.Tool, []);
477List<ChatMessage> plan =
494static void Validate(List<ChatMessage> finalChat)
514List<ChatMessage> plan =
536List<ChatMessage> plan =
592List<ChatMessage> plan =
653var messages = new List<ChatMessage>
711var messages = new List<ChatMessage>
722ChatMessage message = chatContents.Count() is 1 or 3 ?
769List<ChatMessage> planBeforeTermination =
779List<ChatMessage> plan =
794async Task InvokeAsync(Func<Task<List<ChatMessage>>> work)
838var existingSearchResult = chatContents.SingleOrDefault(m => m.Role == ChatRole.Tool);
847var message = new ChatMessage(ChatRole.Assistant, resultContents);
855List<ChatMessage> messages = [new(ChatRole.User, "hello")];
861var lastMessage = messages.Last();
882Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, ChatResponse> callback =
925List<ChatMessage> plan =
954List<ChatMessage> plan =
1004private static async Task<List<ChatMessage>> InvokeAndAssertAsync(
1006List<ChatMessage> plan,
1007List<ChatMessage>? expected = null,
1016List<ChatMessage> chat = [plan[0]];
1030var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1040var result = await service.GetResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token);
1049var expectedMessage = expected[i];
1050var chatMessage = chat[i];
1104private static async Task<List<ChatMessage>> InvokeAndAssertStreamingAsync(
1106List<ChatMessage> plan,
1107List<ChatMessage>? expected = null,
1116List<ChatMessage> chat = [plan[0]];
1124ChatMessage message = new(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1134var result = await service.GetStreamingResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token).ToChatResponseAsync();
1143var expectedMessage = expected[i];
1144var chatMessage = chat[i];
test\Libraries\Microsoft.Extensions.AI.Abstractions.Tests\TestChatClient.cs (4)
20public Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task<ChatResponse>>? GetResponseAsyncCallback { get; set; }
22public Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? GetStreamingResponseAsyncCallback { get; set; }
29public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
32public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
Microsoft.ML.GenAI.Core (7)
Microsoft.ML.GenAI.Core.Tests (3)
Microsoft.ML.GenAI.LLaMA (4)
Microsoft.ML.GenAI.Phi (4)
Microsoft.ML.GenAI.Samples (4)