332 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 (20)
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.OpenAI (4)
Microsoft.Extensions.AI.OpenAI.Tests (34)
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,
OpenAIConversionTests.cs (17)
90new(ChatRole.System, "You are a helpful assistant."),
91new(ChatRole.User, "Hello"),
92new(ChatRole.Assistant,
101new(ChatRole.Tool, [new FunctionResultContent("callid123", "theresult")]),
102new(ChatRole.Assistant, "The answer is 42."),
156new(ChatRole.System, "You are a helpful assistant."),
157new(ChatRole.User, "Hello"),
158new(ChatRole.Assistant,
167new(ChatRole.Tool, [new FunctionResultContent("callid123", "theresult")]),
168new(ChatRole.Assistant, "The answer is 42."),
335new(ChatRole.Assistant, "Hi there!")
361new(ChatRole.Assistant,
397var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant,
436var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Hello"));
466var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant,
511var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Test"))
534var chatResponse = new ChatResponse(new ChatMessage(inputRole, "Test"));
Microsoft.Extensions.AI.Tests (138)
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 (100)
93new ChatMessage(ChatRole.User, "hello"),
94new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
95new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
96new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
97new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
98new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
99new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
100new ChatMessage(ChatRole.Assistant, "world"),
127new ChatMessage(ChatRole.User, "hello"),
128new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
129new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
130new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
131new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
132new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
133new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
134new ChatMessage(ChatRole.Assistant, "world"),
160new ChatMessage(ChatRole.User, "hello"),
161new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
162new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
163new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
164new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
165new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
166new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
167new ChatMessage(ChatRole.Assistant, "world"),
191new ChatMessage(ChatRole.User, "hello"),
192new ChatMessage(ChatRole.Assistant,
198new ChatMessage(ChatRole.Tool,
204new ChatMessage(ChatRole.Assistant,
209new ChatMessage(ChatRole.Tool,
214new ChatMessage(ChatRole.Assistant, "world"),
251new ChatMessage(ChatRole.User, "hello"),
252new ChatMessage(ChatRole.Assistant,
257new ChatMessage(ChatRole.Tool,
262new ChatMessage(ChatRole.Assistant, "done"),
295new ChatMessage(ChatRole.User, "hello"),
296new ChatMessage(ChatRole.Assistant,
301new ChatMessage(ChatRole.Tool,
306new ChatMessage(ChatRole.Assistant, "done"),
329new ChatMessage(ChatRole.User, "hello"),
330new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
331new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1 from delegate")]),
332new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
333new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42 from delegate")]),
334new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
335new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
336new ChatMessage(ChatRole.Assistant, "world"),
378new ChatMessage(ChatRole.User, "hello"),
379new ChatMessage(ChatRole.Assistant, [new FunctionCallContent($"callId0", "VoidReturn")]),
386plan.Add(new ChatMessage(ChatRole.Tool, [new FunctionResultContent($"callId{i}", result: "Success: Function completed.")]));
387plan.Add(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent($"callId{(i + 1)}", "VoidReturn")]));
427new ChatMessage(ChatRole.User, "hello"),
505new ChatMessage(ChatRole.User, "hello"),
522var assistantMessage = new ChatMessage(ChatRole.Assistant, []);
523var toolMessage = new ChatMessage(ChatRole.Tool, []);
552new ChatMessage(ChatRole.User, "hello"),
553new ChatMessage(ChatRole.Assistant, [new TextContent("extra"), new FunctionCallContent("callId1", "Func1"), new TextContent("stuff")]),
554new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 1")]),
555new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
556new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
557new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } }), new TextContent("more")]),
558new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
559new ChatMessage(ChatRole.Assistant, "world"),
589new ChatMessage(ChatRole.User, "hello"),
590new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
591new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: detailedErrors ? "Error: Function failed. Exception: Oh no!" : "Error: Function failed.")]),
592new ChatMessage(ChatRole.Assistant, "world"),
611new ChatMessage(ChatRole.User, "hello"),
612new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
613new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
614new ChatMessage(ChatRole.Assistant, "world"),
667new ChatMessage(ChatRole.User, "hello"),
668new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
669new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
670new ChatMessage(ChatRole.Assistant, "world"),
728new(ChatRole.User, "Hello"),
786new(ChatRole.User, "Hello"),
796new(ChatRole.Assistant, [new FunctionCallContent($"callId{chatContents.Count()}", "Func1")]) :
797new(ChatRole.Assistant, "The answer is 42.");
844new ChatMessage(ChatRole.User, "hello"),
845new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["i"] = 41 })]),
846new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 41")]),
847new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func1", new Dictionary<string, object?> { ["i"] = 42 })]),
848new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 42")]),
855new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "Func1", new Dictionary<string, object?> { ["i"] = 43 })]),
856new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Result 43")]),
857new ChatMessage(ChatRole.Assistant, "world"),
920var message = new ChatMessage(ChatRole.Assistant, resultContents);
928List<ChatMessage> messages = [new(ChatRole.User, "hello")];
963return new ChatResponse(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId-abc", "Func1")]))
971return new ChatResponse(new ChatMessage(ChatRole.Assistant, "done!"));
1000new ChatMessage(ChatRole.User, "hello"),
1001new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
1002new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
1003new ChatMessage(ChatRole.Assistant, "world"),
1029new ChatMessage(ChatRole.User, "hello"),
1030new ChatMessage(ChatRole.Assistant, [
1034new ChatMessage(ChatRole.Tool,
1039new ChatMessage(ChatRole.Assistant, "world"),
1103var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1197ChatMessage 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)
550 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)
229IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
239List<ChatMessage> originalMessages = [.. messages];
242List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
244List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used for the final response
324IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
335List<ChatMessage> originalMessages = [.. messages];
338List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
340List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used in fallback cases to reconstitute history
407foreach (var message in modeAndMessages.MessagesAdded)
462IEnumerable<ChatMessage> originalMessages,
463ref IEnumerable<ChatMessage> messages,
464[NotNull] ref List<ChatMessage>? augmentedHistory,
466List<ChatMessage> allTurnsResponseMessages,
518IList<ChatMessage> messages, [NotNullWhen(true)] ref List<FunctionCallContent>? functionCalls)
585private async Task<(bool ShouldTerminate, int NewConsecutiveErrorCount, IList<ChatMessage> MessagesAdded)> ProcessFunctionCallsAsync(
586List<ChatMessage> messages, ChatOptions? options, List<FunctionCallContent> functionCallContents, int iteration, int consecutiveErrorCount,
603IList<ChatMessage> addedMessages = CreateResponseMessages([result]);
648IList<ChatMessage> addedMessages = CreateResponseMessages(results.ToArray());
664private void UpdateConsecutiveErrorCountOrThrow(IList<ChatMessage> added, ref int consecutiveErrorCount)
696private void ThrowIfNoFunctionResultsAdded(IList<ChatMessage>? messages)
715List<ChatMessage> messages, ChatOptions? options, List<FunctionCallContent> callContents,
788protected virtual IList<ChatMessage> CreateResponseMessages(
Microsoft.Extensions.AI.Abstractions (46)
ChatCompletion\ChatResponseExtensions.cs (16)
28public static void AddMessages(this IList<ChatMessage> list, ChatResponse response)
33if (list is List<ChatMessage> listConcrete)
39foreach (var message in response.Messages)
46/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
52/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, the
57public static void AddMessages(this IList<ChatMessage> list, IEnumerable<ChatResponseUpdate> updates)
70/// <summary>Converts the <paramref name="update"/> into a <see cref="ChatMessage"/> instance and adds it to <paramref name="list"/>.</summary>
78/// no <see cref="ChatMessage"/> will be added to the <paramref name="list"/>.
80public static void AddMessages(this IList<ChatMessage> list, ChatResponseUpdate update, Func<AIContent, bool>? filter = null)
98/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
106/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, tne
112this IList<ChatMessage> list, IAsyncEnumerable<ChatResponseUpdate> updates, CancellationToken cancellationToken = default)
120IList<ChatMessage> list, IAsyncEnumerable<ChatResponseUpdate> updates, CancellationToken cancellationToken) =>
130/// <see cref="ChatMessage"/> instances. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
158/// <see cref="ChatMessage"/> instances. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
272ChatMessage message;
Microsoft.Extensions.AI.Abstractions.Tests (34)
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.OllamaSharp.Integration.Tests (1)
Microsoft.Extensions.AI.OpenAI (26)
Microsoft.Extensions.AI.OpenAI.Tests (15)
Microsoft.Extensions.AI.Tests (81)
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 (47)
91List<ChatMessage> plan =
125List<ChatMessage> plan =
158List<ChatMessage> plan =
189List<ChatMessage> plan =
249List<ChatMessage> plan =
293List<ChatMessage> plan =
327List<ChatMessage> plan =
376List<ChatMessage> plan =
425List<ChatMessage> plan =
503List<ChatMessage> plan =
520private static IEnumerable<ChatMessage> CreateFunctionCallIterationPlan(ref int callIndex, params bool[] shouldThrow)
522var assistantMessage = new ChatMessage(ChatRole.Assistant, []);
523var toolMessage = new ChatMessage(ChatRole.Tool, []);
550List<ChatMessage> plan =
567static void Validate(List<ChatMessage> finalChat)
587List<ChatMessage> plan =
609List<ChatMessage> plan =
665List<ChatMessage> plan =
726var messages = new List<ChatMessage>
784var messages = new List<ChatMessage>
795ChatMessage message = chatContents.Count() is 1 or 3 ?
842List<ChatMessage> planBeforeTermination =
852List<ChatMessage> plan =
867async Task InvokeAsync(Func<Task<List<ChatMessage>>> work)
911var existingSearchResult = chatContents.SingleOrDefault(m => m.Role == ChatRole.Tool);
920var message = new ChatMessage(ChatRole.Assistant, resultContents);
928List<ChatMessage> messages = [new(ChatRole.User, "hello")];
934var lastMessage = messages.Last();
955Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, ChatResponse> callback =
998List<ChatMessage> plan =
1027List<ChatMessage> plan =
1077private static async Task<List<ChatMessage>> InvokeAndAssertAsync(
1079List<ChatMessage> plan,
1080List<ChatMessage>? expected = null,
1089List<ChatMessage> chat = [plan[0]];
1103var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1113var result = await service.GetResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token);
1122var expectedMessage = expected[i];
1123var chatMessage = chat[i];
1177private static async Task<List<ChatMessage>> InvokeAndAssertStreamingAsync(
1179List<ChatMessage> plan,
1180List<ChatMessage>? expected = null,
1189List<ChatMessage> chat = [plan[0]];
1197ChatMessage message = new(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1207var result = await service.GetStreamingResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token).ToChatResponseAsync();
1216var expectedMessage = expected[i];
1217var 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)