619 instantiations of ChatMessage
Aspire.Azure.AI.Inference.Tests (1)
Aspire.Azure.AI.OpenAI.Tests (2)
Aspire.Dashboard (29)
Model\Assistant\Prompts\KnownChatMessages.cs (25)
41return new ChatMessage(ChatRole.System, systemChatMessage);
65return new(ChatRole.User, resolvedPromptText);
76return new(ChatRole.User, prompt);
87return new(ChatRole.User, prompt);
97return new(ChatRole.User, message);
110return new(ChatRole.User, prompt);
120return new(ChatRole.User, prompt);
131return new(ChatRole.User, prompt);
140return new(ChatRole.User, prompt);
153return new(ChatRole.User, prompt);
163return new(ChatRole.User, prompt);
172return new(ChatRole.User, prompt);
193return new(ChatRole.User, prompt);
209return new(ChatRole.User, prompt);
219return new(ChatRole.User, prompt);
229return new(ChatRole.User, prompt);
238return new(ChatRole.User, prompt);
250return new(ChatRole.User, message);
259return new(ChatRole.User, message);
269return new ChatMessage(ChatRole.User, message);
279return new ChatMessage(ChatRole.User, message);
302return new(ChatRole.User, prompt);
325return new(ChatRole.User, prompt);
342return new(ChatRole.User, prompt);
351return new(ChatRole.User, prompt);
Aspire.OpenAI.Tests (2)
AzureAIFoundryEndToEnd.WebStory (2)
AzureOpenAIEndToEnd.WebStory (2)
GitHubModelsEndToEnd.WebStory (2)
Microsoft.Extensions.AI (11)
ChatCompletion\ChatClientStructuredOutputExtensions.cs (3)
61GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), options, useJsonSchemaResponseFormat, cancellationToken);
101GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), serializerOptions, options, useJsonSchemaResponseFormat, cancellationToken);
190promptAugmentation = 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 (67)
ChatClientIntegrationTests.cs (59)
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?")]);
1137new(ChatRole.User, "My name is Alice and I love hiking in the mountains."),
1138new(ChatRole.Assistant, "Nice to meet you, Alice! Hiking in the mountains sounds wonderful. Do you have a favorite trail?"),
1139new(ChatRole.User, "Yes, I love the Pacific Crest Trail. I hiked a section last summer."),
1140new(ChatRole.Assistant, "The Pacific Crest Trail is amazing! Which section did you hike?"),
1141new(ChatRole.User, "I hiked the section through the Sierra Nevada. It was challenging but beautiful."),
1142new(ChatRole.Assistant, "The Sierra Nevada section is known for its stunning views. How long did it take you?"),
1143new(ChatRole.User, "What's my name and what activity do I enjoy?")
1178new(ChatRole.System, "You are a pirate. Always respond in pirate speak."),
1179new(ChatRole.User, "Tell me about the weather"),
1180new(ChatRole.Assistant, "Ahoy matey! The weather be fine today, with clear skies on the horizon!"),
1181new(ChatRole.User, "What about tomorrow?"),
1182new(ChatRole.Assistant, "Arr, tomorrow be lookin' a bit cloudy, might be some rain blowin' in from the east!"),
1183new(ChatRole.User, "Should I bring an umbrella?"),
1184new(ChatRole.Assistant, "Aye, ye best be bringin' yer umbrella, unless ye want to be soaked like a barnacle!"),
1185new(ChatRole.User, "What's 2 + 2?")
1239new(ChatRole.User, "What's the weather in Seattle?"),
1240new(ChatRole.Assistant, "Let me check the weather in Seattle for you."),
1241new(ChatRole.User, "And what about Miami?"),
1242new(ChatRole.Assistant, "I'll check Miami's weather as well."),
1243new(ChatRole.User, "Which city had better weather?")
1273new(ChatRole.User, "I'm Bob and I work as a software engineer at a startup."),
1274new(ChatRole.Assistant, "Nice to meet you, Bob! Working at a startup must be exciting. What kind of software do you develop?"),
1275new(ChatRole.User, "We build AI-powered tools for education."),
1276new(ChatRole.Assistant, "That sounds impactful! AI in education has so much potential."),
1277new(ChatRole.User, "Yes, we focus on personalized learning experiences."),
1278new(ChatRole.Assistant, "Personalized learning is the future of education!"),
1279new(ChatRole.User, "Was anyone named in the conversation? Provide their name and job.")
1319new(ChatRole.User, "I have 3 cats and 2 dogs."),
1320new(ChatRole.Assistant, "That's 5 pets total! You must have a lively household."),
1321new(ChatRole.User, "Yes, and I spend about $200 per month on pet food."),
1322new(ChatRole.Assistant, "That's a significant expense, but I'm sure they're worth it!"),
1323new(ChatRole.User, "They eat 10 cans of food per week."),
1324new(ChatRole.Assistant, "That's quite a bit of food for your furry friends!"),
1325new(ChatRole.User, "How many pets do I have in total?")
ReducingChatClientTests.cs (5)
46new ChatMessage(ChatRole.User, "Hi there! Can you tell me about golden retrievers?"),
47new 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."),
48new ChatMessage(ChatRole.User, "What kind of exercise do they need?"),
49new 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."),
50new ChatMessage(ChatRole.User, "Are they good with kids?"),
Microsoft.Extensions.AI.OpenAI (4)
Microsoft.Extensions.AI.OpenAI.Tests (51)
OpenAIChatClientTests.cs (17)
819new(ChatRole.System, "You are a really nice friend."),
820new(ChatRole.User, "hello!"),
821new(ChatRole.Assistant, "hi, how are you?"),
822new(ChatRole.User, "i'm good. how are you?"),
926new(ChatRole.System, [new TextContent("You are a really nice friend."), new TextContent("Really nice.")]),
927new(ChatRole.User, "hello!"),
1022new(ChatRole.System, "You are a really nice friend."),
1023new(ChatRole.User, "hello!"),
1024new(ChatRole.Assistant, (string?)null),
1025new(ChatRole.User, "i'm good. how are you?"),
1464new(ChatRole.System, "You are a really nice friend."),
1465new(ChatRole.User, "hello!"),
1466new(ChatRole.Assistant,
1472new (ChatRole.Tool,
1477new(ChatRole.Assistant, "You are great."),
1478new(ChatRole.User, "Thanks!"),
1584new(ChatRole.User,
OpenAIConversionTests.cs (32)
161new(ChatRole.System, "You are a helpful assistant."),
162new(ChatRole.User, "Hello") { AuthorName = "Jane" },
163new(ChatRole.Assistant,
172new(ChatRole.Tool, [new FunctionResultContent("callid123", "theresult")]),
173new(ChatRole.Assistant, "The answer is 42.") { AuthorName = "@#$#$@$" },
230new(ChatRole.System, "You are a helpful assistant."),
231new(ChatRole.User, "Hello"),
232new(ChatRole.Assistant,
241new(ChatRole.Tool, [new FunctionResultContent("callid123", "theresult")]),
242new(ChatRole.Assistant, "The answer is 42."),
409new(ChatRole.Assistant, "Hi there!")
435new(ChatRole.Assistant,
471var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant,
510var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Hello"));
540var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant,
585var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Test"))
608var chatResponse = new ChatResponse(new ChatMessage(inputRole, "Test"));
947var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Test"))
960var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Hello, world!"))
985var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Test message"))
1035new(ChatRole.Assistant, "First message"),
1036new(ChatRole.Assistant, "Second message"),
1037new(ChatRole.Assistant,
1072new(ChatRole.Assistant,
1077new(ChatRole.Tool, [new FunctionResultContent("call-456", "The weather is sunny")]),
1078new(ChatRole.Assistant, "The weather in Seattle is sunny!")
1107new(ChatRole.System, "You are a helpful assistant."),
1108new(ChatRole.User, "Hello, how are you?"),
1109new(ChatRole.Assistant, "I'm doing well, thank you for asking!")
1134var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Default test"));
1152var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Model test"));
1167var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Model priority test"))
Microsoft.Extensions.AI.Tests (327)
ChatCompletion\ChatClientStructuredOutputExtensionsTests.cs (15)
75var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
136var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
198new ChatMessage(ChatRole.Assistant,
203new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callid123", "result")]),
239var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "This is not valid JSON"));
245var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
258var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "null"));
264var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
277var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, [new UriContent("https://example.com", "image/*")]));
283var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
311var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
339var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
364var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, JsonSerializer.Serialize(expectedResult, jso)));
401var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
429var 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\FunctionInvokingChatClientApprovalsTests.cs (103)
36new ChatMessage(ChatRole.User, "hello"),
41new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
46new ChatMessage(ChatRole.Assistant,
72new ChatMessage(ChatRole.User, "hello"),
77new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
82new ChatMessage(ChatRole.Assistant,
116new ChatMessage(ChatRole.User, "hello"),
121new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
126new ChatMessage(ChatRole.Assistant,
152new ChatMessage(ChatRole.User, "hello"),
153new ChatMessage(ChatRole.Assistant,
158new ChatMessage(ChatRole.User,
167new ChatMessage(ChatRole.User, "hello"),
168new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
169new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
174new ChatMessage(ChatRole.Assistant, "world"),
179new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
180new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
181new ChatMessage(ChatRole.Assistant, "world"),
203new ChatMessage(ChatRole.User, "hello"),
204new ChatMessage(ChatRole.Assistant,
208new ChatMessage(ChatRole.Assistant,
212new ChatMessage(ChatRole.User,
216new ChatMessage(ChatRole.User,
224new ChatMessage(ChatRole.User, "hello"),
225new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]) { MessageId = "resp1" },
226new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]) { MessageId = "resp2" },
227new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
232new ChatMessage(ChatRole.Assistant, "world"),
237new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]) { MessageId = "resp1" },
238new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]) { MessageId = "resp2" },
239new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
240new ChatMessage(ChatRole.Assistant, "world"),
262new ChatMessage(ChatRole.User, "hello"),
263new ChatMessage(ChatRole.Assistant,
268new ChatMessage(ChatRole.User,
277new ChatMessage(ChatRole.User, "hello"),
278new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
279new ChatMessage(ChatRole.Tool,
288new ChatMessage(ChatRole.Assistant, "world"),
293new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
294new ChatMessage(ChatRole.Tool,
299new ChatMessage(ChatRole.Assistant, "world"),
321new ChatMessage(ChatRole.User, "hello"),
322new ChatMessage(ChatRole.Assistant,
327new ChatMessage(ChatRole.User,
336new ChatMessage(ChatRole.User, "hello"),
337new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
338new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Error: Tool call invocation was rejected by user.")]),
339new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
344new ChatMessage(ChatRole.Assistant, "world"),
349new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
350new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Error: Tool call invocation was rejected by user.")]),
351new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
352new ChatMessage(ChatRole.Assistant, "world"),
357new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
358new ChatMessage(ChatRole.Tool,
363new ChatMessage(ChatRole.Assistant, "world"),
385new ChatMessage(ChatRole.User, "hello"),
386new ChatMessage(ChatRole.Assistant,
391new ChatMessage(ChatRole.User,
400new ChatMessage(ChatRole.User, "hello"),
401new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
402new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
407new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 3 } })]),
412new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
413new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
414new ChatMessage(ChatRole.Assistant,
439new ChatMessage(ChatRole.User, "hello"),
440new ChatMessage(ChatRole.Assistant,
445new ChatMessage(ChatRole.User,
450new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
451new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
452new ChatMessage(ChatRole.Assistant,
456new ChatMessage(ChatRole.User,
464new ChatMessage(ChatRole.User, "hello"),
465new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
466new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
467new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "Func1")]),
468new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Result 1")]),
473new ChatMessage(ChatRole.Assistant, "World"),
478new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "Func1")]),
479new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Result 1")]),
480new ChatMessage(ChatRole.Assistant, "World"),
502new ChatMessage(ChatRole.User, "hello"),
503new ChatMessage(ChatRole.Assistant,
532new ChatMessage(ChatRole.User, "hello"),
533new ChatMessage(ChatRole.User,
542new ChatMessage(ChatRole.User, "hello"),
543new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
544new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
549new ChatMessage(ChatRole.Assistant, "world"),
554new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
555new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
556new ChatMessage(ChatRole.Assistant, "world"),
579new ChatMessage(ChatRole.User,
588new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
593new ChatMessage(ChatRole.Assistant, "world"),
598new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1"), new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
599new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1"), new FunctionResultContent("callId2", result: "Result 2: 42")]),
600new ChatMessage(ChatRole.Assistant, "world"),
628List<ChatMessage> input = [new ChatMessage(ChatRole.User, "hello")];
701List<ChatMessage> input = [new ChatMessage(ChatRole.User, "hello")];
ChatCompletion\FunctionInvokingChatClientTests.cs (116)
92new ChatMessage(ChatRole.User, "hello"),
93new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
94new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
95new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
96new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
97new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
98new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
99new ChatMessage(ChatRole.Assistant, "world"),
126new ChatMessage(ChatRole.User, "hello"),
127new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
128new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
129new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
130new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
131new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
132new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
133new ChatMessage(ChatRole.Assistant, "world"),
159new ChatMessage(ChatRole.User, "hello"),
160new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
161new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
162new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
163new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
164new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
165new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
166new ChatMessage(ChatRole.Assistant, "world"),
190new ChatMessage(ChatRole.User, "hello"),
191new ChatMessage(ChatRole.Assistant,
197new ChatMessage(ChatRole.Tool,
203new ChatMessage(ChatRole.Assistant,
208new ChatMessage(ChatRole.Tool,
213new ChatMessage(ChatRole.Assistant, "world"),
250new ChatMessage(ChatRole.User, "hello"),
251new ChatMessage(ChatRole.Assistant,
256new ChatMessage(ChatRole.Tool,
261new ChatMessage(ChatRole.Assistant, "done"),
294new ChatMessage(ChatRole.User, "hello"),
295new ChatMessage(ChatRole.Assistant,
300new ChatMessage(ChatRole.Tool,
305new ChatMessage(ChatRole.Assistant, "done"),
328new ChatMessage(ChatRole.User, "hello"),
329new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
330new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1 from delegate")]),
331new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
332new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42 from delegate")]),
333new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } })]),
334new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
335new ChatMessage(ChatRole.Assistant, "world"),
377new ChatMessage(ChatRole.User, "hello"),
378new ChatMessage(ChatRole.Assistant, [new FunctionCallContent($"callId0", "VoidReturn")]),
385plan.Add(new ChatMessage(ChatRole.Tool, [new FunctionResultContent($"callId{i}", result: "Success: Function completed.")]));
386plan.Add(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent($"callId{(i + 1)}", "VoidReturn")]));
426new ChatMessage(ChatRole.User, "hello"),
504new ChatMessage(ChatRole.User, "hello"),
521var assistantMessage = new ChatMessage(ChatRole.Assistant, []);
522var toolMessage = new ChatMessage(ChatRole.Tool, []);
551new ChatMessage(ChatRole.User, "hello"),
552new ChatMessage(ChatRole.Assistant, [new TextContent("extra"), new FunctionCallContent("callId1", "Func1"), new TextContent("stuff")]),
553new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 1")]),
554new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func2", arguments: new Dictionary<string, object?> { { "i", 42 } })]),
555new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 2: 42")]),
556new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "VoidReturn", arguments: new Dictionary<string, object?> { { "i", 43 } }), new TextContent("more")]),
557new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Success: Function completed.")]),
558new ChatMessage(ChatRole.Assistant, "world"),
588new ChatMessage(ChatRole.User, "hello"),
589new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
590new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: detailedErrors ? "Error: Function failed. Exception: Oh no!" : "Error: Function failed.")]),
591new ChatMessage(ChatRole.Assistant, "world"),
610new ChatMessage(ChatRole.User, "hello"),
611new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
612new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
613new 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"),
745new(ChatRole.User, "Hello"),
803new(ChatRole.User, "Hello"),
813new(ChatRole.Assistant, [new FunctionCallContent($"callId{chatContents.Count()}", "Func1")]) :
814new(ChatRole.Assistant, "The answer is 42.");
861new ChatMessage(ChatRole.User, "hello"),
862new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["i"] = 41 })]),
863new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 41")]),
864new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func1", new Dictionary<string, object?> { ["i"] = 42 })]),
865new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 42")]),
872new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "Func1", new Dictionary<string, object?> { ["i"] = 43 })]),
873new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Result 43")]),
874new ChatMessage(ChatRole.Assistant, "world"),
937var message = new ChatMessage(ChatRole.Assistant, resultContents);
945List<ChatMessage> messages = [new(ChatRole.User, "hello")];
980return new ChatResponse(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId-abc", "Func1")]))
988return new ChatResponse(new ChatMessage(ChatRole.Assistant, "done!"));
1017new ChatMessage(ChatRole.User, "hello"),
1018new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
1019new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
1020new ChatMessage(ChatRole.Assistant, "world"),
1046new ChatMessage(ChatRole.User, "hello"),
1047new ChatMessage(ChatRole.Assistant, [
1051new ChatMessage(ChatRole.Tool,
1056new ChatMessage(ChatRole.Assistant, "world"),
1099new(ChatRole.User, "hello"),
1100new(ChatRole.Assistant, [
1104new(ChatRole.Tool, [
1108new(ChatRole.Assistant, "done"),
1118new(ChatRole.User, "hello"),
1119new(ChatRole.Assistant, [
1123new(ChatRole.Tool, [
1127new(ChatRole.Assistant, "done"),
1147new(ChatRole.User, "hello"),
1148new(ChatRole.Assistant, [new FunctionCallContent("callId1", "DefOnly")]),
1149new(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Should not be produced")]),
1150new(ChatRole.Assistant, "world"),
1176new(ChatRole.User, "hi"),
1177new(ChatRole.Assistant, [
1181new(ChatRole.Tool, [new FunctionResultContent("callId1", result: "OK"), new FunctionResultContent("callId2", result: "nope")]),
1182new(ChatRole.Assistant, "done"),
1235var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1299ChatMessage message = new(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
ChatCompletion\OpenTelemetryChatClientTests.cs (7)
38return new ChatResponse(new ChatMessage(ChatRole.Assistant, "The blue whale, I think."))
107new(ChatRole.System, "You are a close friend."),
108new(ChatRole.User, "Hey!"),
109new(ChatRole.Assistant, [new FunctionCallContent("12345", "GetPersonName")]),
110new(ChatRole.Tool, [new FunctionResultContent("12345", "John")]),
111new(ChatRole.Assistant, "Hey John, what's up?"),
112new(ChatRole.User, "What's the biggest animal?")
ChatCompletion\ReducingChatClientTests.cs (9)
36new(ChatRole.System, "You are a helpful assistant"),
37new(ChatRole.User, "Hello"),
38new(ChatRole.Assistant, "Hi there!"),
39new(ChatRole.User, "What's the weather?")
44new(ChatRole.System, "You are a helpful assistant"),
45new(ChatRole.User, "What's the weather?")
49var expectedResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "It's sunny!"));
97var reducedMessages = new List<ChatMessage> { new(ChatRole.User, "Reduced message") };
118await client.GetResponseAsync(new List<ChatMessage> { new(ChatRole.User, "Original message") });
ChatReduction\MessageCountingChatReducerTests.cs (28)
53new ChatMessage(ChatRole.System, "You are a helpful assistant."),
54new ChatMessage(ChatRole.User, "Hello"),
55new ChatMessage(ChatRole.Assistant, "Hi there!"),
56new ChatMessage(ChatRole.User, "How are you?"),
57new ChatMessage(ChatRole.Assistant, "I'm doing well, thanks!"),
88new ChatMessage(ChatRole.System, "First system message"),
89new ChatMessage(ChatRole.User, "Hello"),
90new ChatMessage(ChatRole.System, "Second system message"),
91new ChatMessage(ChatRole.Assistant, "Hi"),
92new ChatMessage(ChatRole.User, "How are you?"),
93new ChatMessage(ChatRole.Assistant, "I'm fine!"),
127new ChatMessage(ChatRole.User, "What's the weather?"),
128new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call1", "get_weather", new Dictionary<string, object?> { ["location"] = "Seattle" })]),
129new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "Sunny, 72°F")]),
130new ChatMessage(ChatRole.Assistant, "The weather in Seattle is sunny and 72°F."),
131new ChatMessage(ChatRole.User, "Thanks!"),
132new ChatMessage(ChatRole.Assistant, "You're welcome!"),
167messages.Add(new ChatMessage(i % 2 == 0 ? ChatRole.User : ChatRole.Assistant, $"Message {i}"));
202new ChatMessage(ChatRole.System, "System prompt"),
223new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call1", "func", null)]),
224new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "result")]),
225new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call2", "func", null)]),
226new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call2", "result")]),
241new ChatMessage(ChatRole.System, "System"),
242new ChatMessage(ChatRole.User, "First"),
243new ChatMessage(ChatRole.Assistant, "Second"),
244new ChatMessage(ChatRole.User, "Third"),
245new ChatMessage(ChatRole.Assistant, "Fourth"),
ChatReduction\SummarizingChatReducerTests.cs (31)
69new ChatMessage(ChatRole.System, "You are a helpful assistant."),
70new ChatMessage(ChatRole.User, "Hello"),
71new ChatMessage(ChatRole.Assistant, "Hi there!"),
72new ChatMessage(ChatRole.User, "How are you?"),
76Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, "Summary of conversation")));
94new ChatMessage(ChatRole.User, "What's the weather?"),
95new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call1", "get_weather", new Dictionary<string, object?> { ["location"] = "Seattle" })]),
96new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "Sunny, 72°F")]),
97new ChatMessage(ChatRole.Assistant, "The weather in Seattle is sunny and 72°F."),
98new ChatMessage(ChatRole.User, "Thanks!"),
124messages.Add(new ChatMessage(i % 2 == 0 ? ChatRole.User : ChatRole.Assistant, $"Message {i}"));
131return Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, "Summary")));
158new ChatMessage(ChatRole.User, "Message 1"),
159new ChatMessage(ChatRole.Assistant, "Response 1"),
160new ChatMessage(ChatRole.User, "Message 2"),
169return Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, "Summary")));
184new ChatMessage(ChatRole.System, "First system message"),
185new ChatMessage(ChatRole.User, "Hello"),
186new ChatMessage(ChatRole.System, "Second system message"),
187new ChatMessage(ChatRole.Assistant, "Hi"),
188new ChatMessage(ChatRole.User, "How are you?"),
192Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, "Summary")));
212new ChatMessage(ChatRole.User, "Hi there! Can you tell me about golden retrievers?"),
213new 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."),
214new ChatMessage(ChatRole.User, "What kind of exercise do they need?"),
215new 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."),
216new ChatMessage(ChatRole.User, "Are they good with kids?"),
232return Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, Summary)));
243messages.Add(new ChatMessage(ChatRole.Assistant, "Golden retrievers get along well with kids! They're able to be playful and energetic while remaining gentle."));
244messages.Add(new ChatMessage(ChatRole.User, "Do they make good lap dogs?"));
259return Task.FromResult(new ChatResponse(new ChatMessage(ChatRole.Assistant, Summary)));
Microsoft.ML.GenAI.Core (1)
Microsoft.ML.GenAI.Core.Tests (3)
Microsoft.ML.GenAI.LLaMA.Tests (3)
Microsoft.ML.GenAI.Samples (5)
OpenAIEndToEnd.WebStory (2)
781 references to ChatMessage
Aspire.Azure.AI.OpenAI.Tests (1)
Aspire.Dashboard (48)
Aspire.OpenAI.Tests (1)
AzureAIFoundryEndToEnd.WebStory (3)
AzureOpenAIEndToEnd.WebStory (3)
GitHubModelsEndToEnd.WebStory (3)
Microsoft.Extensions.AI (118)
ChatCompletion\AnonymousDelegatingChatClient.cs (11)
21private readonly Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, Task<ChatResponse>>? _getResponseFunc;
29private readonly Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? _getStreamingResponseFunc;
32private readonly Func<IEnumerable<ChatMessage>, ChatOptions?, Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task>, CancellationToken, Task>? _sharedFunc;
51Func<IEnumerable<ChatMessage>, ChatOptions?, Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, Task>, CancellationToken, Task> sharedFunc)
77Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, Task<ChatResponse>>? getResponseFunc,
78Func<IEnumerable<ChatMessage>, ChatOptions?, IChatClient, CancellationToken, IAsyncEnumerable<ChatResponseUpdate>>? getStreamingResponseFunc)
89IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
98IEnumerable<ChatMessage> messages, ChatOptions? options, CancellationToken cancellationToken)
128IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
ChatCompletion\CachingChatClient.cs (6)
47IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
57IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
75IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
85IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
150protected abstract string GetCacheKey(IEnumerable<ChatMessage> messages, ChatOptions? options, params ReadOnlySpan<object?> additionalValues);
209protected 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 (52)
274IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
284List<ChatMessage> originalMessages = [.. messages];
287List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
289List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used for the final response
306(IList<ChatMessage>? invokedApprovedFunctionApprovalResponses, bool shouldTerminate, consecutiveErrorCount) =
413IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
424List<ChatMessage> originalMessages = [.. messages];
428List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
430List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used in fallback cases to reconstitute history
459foreach (var message in preDownstreamCallHistory)
467(IList<ChatMessage>? invokedApprovedFunctionApprovalResponses, bool shouldTerminate, consecutiveErrorCount) =
472foreach (var message in invokedApprovedFunctionApprovalResponses)
610foreach (var message in modeAndMessages.MessagesAdded)
627private static ChatResponseUpdate ConvertToolResultMessageToUpdate(ChatMessage message, string? conversationId, string? messageId) =>
666IEnumerable<ChatMessage> originalMessages,
667ref IEnumerable<ChatMessage> messages,
668[NotNull] ref List<ChatMessage>? augmentedHistory,
670List<ChatMessage> allTurnsResponseMessages,
750private static bool HasAnyApprovalContent(List<ChatMessage> messages) =>
755IList<ChatMessage> messages, [NotNullWhen(true)] ref List<FunctionCallContent>? functionCalls)
870private async Task<(bool ShouldTerminate, int NewConsecutiveErrorCount, IList<ChatMessage> MessagesAdded)> ProcessFunctionCallsAsync(
871List<ChatMessage> messages, ChatOptions? options,
889IList<ChatMessage> addedMessages = CreateResponseMessages([result]);
934IList<ChatMessage> addedMessages = CreateResponseMessages(results.ToArray());
949private void UpdateConsecutiveErrorCountOrThrow(IList<ChatMessage> added, ref int consecutiveErrorCount)
979private void ThrowIfNoFunctionResultsAdded(IList<ChatMessage>? messages)
999List<ChatMessage> messages, ChatOptions? options,
1057protected virtual IList<ChatMessage> CreateResponseMessages(
1219private static (List<ChatMessage>? preDownstreamCallHistory, List<ApprovalResultWithRequestMessage>? approvals) ProcessFunctionApprovalResponses(
1220List<ChatMessage> originalMessages, bool hasConversationId, string? toolMessageId, string? functionCallContentFallbackMessageId)
1227ICollection<ChatMessage>? allPreDownstreamCallMessages = ConvertToFunctionCallContentMessages(
1233ChatMessage? rejectedPreDownstreamCallResultsMessage = rejectedFunctionCallResults is not null ?
1240List<ChatMessage>? preDownstreamCallHistory = null;
1271List<ChatMessage> messages)
1273Dictionary<string, ChatMessage>? allApprovalRequestsMessages = null;
1287var message = messages[i];
1327var newMessage = message.Clone();
1372ChatMessage? requestMessage = null;
1397private static ICollection<ChatMessage>? ConvertToFunctionCallContentMessages(
1402ChatMessage? currentMessage = null;
1403Dictionary<string, ChatMessage>? messagesById = null;
1439if (messagesById?.Values is ICollection<ChatMessage> cm)
1454/// Takes the <see cref="FunctionCallContent"/> from the <paramref name="resultWithRequestMessage"/> and wraps it in a <see cref="ChatMessage"/>
1457private static ChatMessage ConvertToFunctionCallContentMessage(ApprovalResultWithRequestMessage resultWithRequestMessage, string? fallbackMessageId)
1459ChatMessage functionCallMessage = resultWithRequestMessage.RequestMessage?.Clone() ?? new() { Role = ChatRole.Assistant };
1529private static IList<ChatMessage> ReplaceFunctionCallsWithApprovalRequests(
1530IList<ChatMessage> messages,
1577var message = lastMessageIndex != messageIndex ? outputMessages[messageIndex].Clone() : outputMessages[messageIndex];
1600/// wrapped in <see cref="ChatMessage"/> objects.
1602private async Task<(IList<ChatMessage>? FunctionResultContentMessages, bool ShouldTerminate, int ConsecutiveErrorCount)> InvokeApprovedFunctionApprovalResponsesAsync(
1605List<ChatMessage> originalMessages,
1695public ChatMessage? RequestMessage { get; set; }
Microsoft.Extensions.AI.Abstractions (48)
ChatCompletion\ChatResponseExtensions.cs (16)
26public static void AddMessages(this IList<ChatMessage> list, ChatResponse response)
31if (list is List<ChatMessage> listConcrete)
37foreach (var message in response.Messages)
44/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
50/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, the
55public static void AddMessages(this IList<ChatMessage> list, IEnumerable<ChatResponseUpdate> updates)
68/// <summary>Converts the <paramref name="update"/> into a <see cref="ChatMessage"/> instance and adds it to <paramref name="list"/>.</summary>
76/// no <see cref="ChatMessage"/> will be added to the <paramref name="list"/>.
78public 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
310ChatMessage message;
Microsoft.Extensions.AI.Abstractions.Tests (38)
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 (8)
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,
79IEnumerable<ChatMessage> conversation,
90List<Dictionary<string, ChatMessage>> turns;
110if (turn.TryGetValue("question", out ChatMessage? question))
115if (turn.TryGetValue("answer", out ChatMessage? answer))
165IEnumerable<ChatMessage> conversation,
182List<Dictionary<string, ChatMessage>> turns;
202if (turn.TryGetValue("question", out ChatMessage? question))
207if (turn.TryGetValue("answer", out ChatMessage? answer))
246IEnumerable<ChatMessage> conversation,
260List<Dictionary<string, ChatMessage>> turns;
274IEnumerable<JsonObject> GetMessages(Dictionary<string, ChatMessage> turn, int turnIndex)
278if (turn.TryGetValue("question", out ChatMessage? question))
289if (turn.TryGetValue("answer", out ChatMessage? answer))
314IEnumerable<JsonObject> GetContents(ChatMessage message)
384(List<Dictionary<string, ChatMessage>> turns,
388IEnumerable<ChatMessage> conversation,
395List<Dictionary<string, ChatMessage>> turns = [];
396Dictionary<string, ChatMessage> currentTurn = [];
416foreach (ChatMessage message in conversation)
450void ValidateContents(ChatMessage message)
478foreach (var message in turn.Values)
Microsoft.Extensions.AI.Evaluation.Tests (3)
Microsoft.Extensions.AI.Integration.Tests (38)
ChatClientIntegrationTests.cs (21)
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?");
1135List<ChatMessage> messages =
1176List<ChatMessage> messages =
1237List<ChatMessage> messages =
1271List<ChatMessage> messages =
1317List<ChatMessage> messages =
1336var summaryMessage = chatClient.LastSummarizedConversation[0];
1358public IReadOnlyList<ChatMessage>? LastSummarizedConversation { get; private set; }
1382public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
1385public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
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 (28)
Microsoft.Extensions.AI.Tests (172)
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\FunctionInvokingChatClientApprovalsTests.cs (63)
34List<ChatMessage> input =
39List<ChatMessage> downstreamClientOutput =
44List<ChatMessage> expectedOutput =
70List<ChatMessage> input =
75List<ChatMessage> downstreamClientOutput =
80List<ChatMessage> expectedOutput =
114List<ChatMessage> input =
119List<ChatMessage> downstreamClientOutput =
124List<ChatMessage> expectedOutput =
150List<ChatMessage> input =
165List<ChatMessage> expectedDownstreamClientInput =
172List<ChatMessage> downstreamClientOutput =
177List<ChatMessage> output =
201List<ChatMessage> input =
222List<ChatMessage> expectedDownstreamClientInput =
230List<ChatMessage> downstreamClientOutput =
235List<ChatMessage> output =
260List<ChatMessage> input =
275List<ChatMessage> expectedDownstreamClientInput =
286List<ChatMessage> downstreamClientOutput =
291List<ChatMessage> output =
319List<ChatMessage> input =
334List<ChatMessage> expectedDownstreamClientInput =
342List<ChatMessage> downstreamClientOutput =
347List<ChatMessage> nonStreamingOutput =
355List<ChatMessage> streamingOutput =
383List<ChatMessage> input =
398List<ChatMessage> expectedDownstreamClientInput =
405List<ChatMessage> downstreamClientOutput =
410List<ChatMessage> output =
437List<ChatMessage> input =
462List<ChatMessage> expectedDownstreamClientInput =
471List<ChatMessage> downstreamClientOutput =
476List<ChatMessage> output =
500List<ChatMessage> input =
530List<ChatMessage> input =
540List<ChatMessage> expectedDownstreamClientInput =
547List<ChatMessage> downstreamClientOutput =
552List<ChatMessage> output =
577List<ChatMessage> input =
586List<ChatMessage> expectedDownstreamClientInput =
591List<ChatMessage> downstreamClientOutput =
596List<ChatMessage> output =
628List<ChatMessage> input = [new ChatMessage(ChatRole.User, "hello")];
636IEnumerable<ChatMessage> contents, ChatOptions? actualOptions, [EnumeratorCancellation] CancellationToken actualCancellationToken)
657var updates = service.GetStreamingResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(input), options, cts.Token);
701List<ChatMessage> input = [new ChatMessage(ChatRole.User, "hello")];
709IEnumerable<ChatMessage> contents, ChatOptions? actualOptions, [EnumeratorCancellation] CancellationToken actualCancellationToken)
735var updates = service.GetStreamingResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(input), options, cts.Token);
784private static async Task<List<ChatMessage>> InvokeAndAssertAsync(
786List<ChatMessage> input,
787List<ChatMessage> downstreamClientOutput,
788List<ChatMessage> expectedOutput,
789List<ChatMessage>? expectedDownstreamClientInput = null,
822var result = await service.GetResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(input), options, cts.Token);
825var actualOutput = result.Messages as List<ChatMessage> ?? result.Messages.ToList();
854private static async Task<List<ChatMessage>> InvokeAndAssertStreamingAsync(
856List<ChatMessage> input,
857List<ChatMessage> downstreamClientOutput,
858List<ChatMessage> expectedOutput,
859List<ChatMessage>? expectedDownstreamClientInput = null,
886var result = await service.GetStreamingResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(input), options, cts.Token).ToChatResponseAsync();
889var actualOutput = result.Messages as List<ChatMessage> ?? result.Messages.ToList();
ChatCompletion\FunctionInvokingChatClientTests.cs (49)
90List<ChatMessage> plan =
124List<ChatMessage> plan =
157List<ChatMessage> plan =
188List<ChatMessage> plan =
248List<ChatMessage> plan =
292List<ChatMessage> plan =
326List<ChatMessage> plan =
375List<ChatMessage> plan =
424List<ChatMessage> plan =
502List<ChatMessage> plan =
519private static IEnumerable<ChatMessage> CreateFunctionCallIterationPlan(ref int callIndex, params bool[] shouldThrow)
521var assistantMessage = new ChatMessage(ChatRole.Assistant, []);
522var toolMessage = new ChatMessage(ChatRole.Tool, []);
549List<ChatMessage> plan =
566static void Validate(List<ChatMessage> finalChat)
586List<ChatMessage> plan =
608List<ChatMessage> plan =
665List<ChatMessage> plan =
743var messages = new List<ChatMessage>
801var messages = new List<ChatMessage>
812ChatMessage message = chatContents.Count() is 1 or 3 ?
859List<ChatMessage> planBeforeTermination =
869List<ChatMessage> plan =
884async Task InvokeAsync(Func<Task<List<ChatMessage>>> work)
928var existingSearchResult = chatContents.SingleOrDefault(m => m.Role == ChatRole.Tool);
937var message = new ChatMessage(ChatRole.Assistant, resultContents);
945List<ChatMessage> messages = [new(ChatRole.User, "hello")];
951var lastMessage = messages.Last();
972Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, ChatResponse> callback =
1015List<ChatMessage> plan =
1044List<ChatMessage> plan =
1097List<ChatMessage> planForContinue =
1116List<ChatMessage> fullPlanWithUnknown =
1145List<ChatMessage> fullPlan =
1153List<ChatMessage> expected = fullPlan.Take(2).ToList();
1174List<ChatMessage> fullPlan =
1185List<ChatMessage> expected = fullPlan.Take(2).ToList();
1209private static async Task<List<ChatMessage>> InvokeAndAssertAsync(
1211List<ChatMessage> plan,
1212List<ChatMessage>? expected = null,
1221List<ChatMessage> chat = [plan[0]];
1235var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1245var result = await service.GetResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token);
1279private static async Task<List<ChatMessage>> InvokeAndAssertStreamingAsync(
1281List<ChatMessage> plan,
1282List<ChatMessage>? expected = null,
1291List<ChatMessage> chat = [plan[0]];
1299ChatMessage message = new(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1309var result = await service.GetStreamingResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token).ToChatResponseAsync();
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)
OpenAIEndToEnd.WebStory (3)