580 instantiations of ChatMessage
Aspire.Azure.AI.Inference.Tests (1)
Aspire.Azure.AI.OpenAI.Tests (2)
Aspire.OpenAI.Tests (2)
Microsoft.Extensions.AI (9)
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)
53new()
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)
786new(ChatRole.System, "You are a really nice friend."),
787new(ChatRole.User, "hello!"),
788new(ChatRole.Assistant, "hi, how are you?"),
789new(ChatRole.User, "i'm good. how are you?"),
893new(ChatRole.System, [new TextContent("You are a really nice friend."), new TextContent("Really nice.")]),
894new(ChatRole.User, "hello!"),
989new(ChatRole.System, "You are a really nice friend."),
990new(ChatRole.User, "hello!"),
991new(ChatRole.Assistant, (string?)null),
992new(ChatRole.User, "i'm good. how are you?"),
1431new(ChatRole.System, "You are a really nice friend."),
1432new(ChatRole.User, "hello!"),
1433new(ChatRole.Assistant,
1439new (ChatRole.Tool,
1444new(ChatRole.Assistant, "You are great."),
1445new(ChatRole.User, "Thanks!"),
1551new(ChatRole.User,
OpenAIConversionTests.cs (32)
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"));
873var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Test"))
886var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Hello, world!"))
911var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Test message"))
961new(ChatRole.Assistant, "First message"),
962new(ChatRole.Assistant, "Second message"),
963new(ChatRole.Assistant,
998new(ChatRole.Assistant,
1003new(ChatRole.Tool, [new FunctionResultContent("call-456", "The weather is sunny")]),
1004new(ChatRole.Assistant, "The weather in Seattle is sunny!")
1033new(ChatRole.System, "You are a helpful assistant."),
1034new(ChatRole.User, "Hello, how are you?"),
1035new(ChatRole.Assistant, "I'm doing well, thank you for asking!")
1060var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Default test"));
1078var chatResponse = new ChatResponse(new ChatMessage(ChatRole.Assistant, "Model test"));
1093var 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"),
666new ChatMessage(ChatRole.User, "hello"),
667new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
668new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
669new ChatMessage(ChatRole.Assistant, "world"),
727new(ChatRole.User, "Hello"),
785new(ChatRole.User, "Hello"),
795new(ChatRole.Assistant, [new FunctionCallContent($"callId{chatContents.Count()}", "Func1")]) :
796new(ChatRole.Assistant, "The answer is 42.");
843new ChatMessage(ChatRole.User, "hello"),
844new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["i"] = 41 })]),
845new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 41")]),
846new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId2", "Func1", new Dictionary<string, object?> { ["i"] = 42 })]),
847new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId2", result: "Result 42")]),
854new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId3", "Func1", new Dictionary<string, object?> { ["i"] = 43 })]),
855new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId3", result: "Result 43")]),
856new ChatMessage(ChatRole.Assistant, "world"),
919var message = new ChatMessage(ChatRole.Assistant, resultContents);
927List<ChatMessage> messages = [new(ChatRole.User, "hello")];
962return new ChatResponse(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId-abc", "Func1")]))
970return new ChatResponse(new ChatMessage(ChatRole.Assistant, "done!"));
999new ChatMessage(ChatRole.User, "hello"),
1000new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1", new Dictionary<string, object?> { ["arg1"] = "value1" })]),
1001new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
1002new ChatMessage(ChatRole.Assistant, "world"),
1028new ChatMessage(ChatRole.User, "hello"),
1029new ChatMessage(ChatRole.Assistant, [
1033new ChatMessage(ChatRole.Tool,
1038new ChatMessage(ChatRole.Assistant, "world"),
1081new(ChatRole.User, "hello"),
1082new(ChatRole.Assistant, [
1086new(ChatRole.Tool, [
1090new(ChatRole.Assistant, "done"),
1100new(ChatRole.User, "hello"),
1101new(ChatRole.Assistant, [
1105new(ChatRole.Tool, [
1109new(ChatRole.Assistant, "done"),
1129new(ChatRole.User, "hello"),
1130new(ChatRole.Assistant, [new FunctionCallContent("callId1", "DefOnly")]),
1131new(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Should not be produced")]),
1132new(ChatRole.Assistant, "world"),
1158new(ChatRole.User, "hi"),
1159new(ChatRole.Assistant, [
1163new(ChatRole.Tool, [new FunctionResultContent("callId1", result: "OK"), new FunctionResultContent("callId2", result: "nope")]),
1164new(ChatRole.Assistant, "done"),
1217var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1281ChatMessage 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)
720 references to ChatMessage
Aspire.Azure.AI.OpenAI.Tests (1)
Aspire.OpenAI.Tests (1)
Microsoft.Extensions.AI (118)
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 (52)
280IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
290List<ChatMessage> originalMessages = [.. messages];
293List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
295List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used for the final response
312(IList<ChatMessage>? invokedApprovedFunctionApprovalResponses, bool shouldTerminate, consecutiveErrorCount) =
419IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
430List<ChatMessage> originalMessages = [.. messages];
434List<ChatMessage>? augmentedHistory = null; // the actual history of messages sent on turns other than the first
436List<ChatMessage>? responseMessages = null; // tracked list of messages, across multiple turns, to be used in fallback cases to reconstitute history
465foreach (var message in preDownstreamCallHistory)
473(IList<ChatMessage>? invokedApprovedFunctionApprovalResponses, bool shouldTerminate, consecutiveErrorCount) =
478foreach (var message in invokedApprovedFunctionApprovalResponses)
616foreach (var message in modeAndMessages.MessagesAdded)
633private static ChatResponseUpdate ConvertToolResultMessageToUpdate(ChatMessage message, string? conversationId, string? messageId) =>
672IEnumerable<ChatMessage> originalMessages,
673ref IEnumerable<ChatMessage> messages,
674[NotNull] ref List<ChatMessage>? augmentedHistory,
676List<ChatMessage> allTurnsResponseMessages,
756private static bool HasAnyApprovalContent(List<ChatMessage> messages) =>
761IList<ChatMessage> messages, [NotNullWhen(true)] ref List<FunctionCallContent>? functionCalls)
876private async Task<(bool ShouldTerminate, int NewConsecutiveErrorCount, IList<ChatMessage> MessagesAdded)> ProcessFunctionCallsAsync(
877List<ChatMessage> messages, ChatOptions? options,
895IList<ChatMessage> addedMessages = CreateResponseMessages([result]);
940IList<ChatMessage> addedMessages = CreateResponseMessages(results.ToArray());
955private void UpdateConsecutiveErrorCountOrThrow(IList<ChatMessage> added, ref int consecutiveErrorCount)
985private void ThrowIfNoFunctionResultsAdded(IList<ChatMessage>? messages)
1005List<ChatMessage> messages, ChatOptions? options,
1063protected virtual IList<ChatMessage> CreateResponseMessages(
1205private static (List<ChatMessage>? preDownstreamCallHistory, List<ApprovalResultWithRequestMessage>? approvals) ProcessFunctionApprovalResponses(
1206List<ChatMessage> originalMessages, bool hasConversationId, string? toolMessageId, string? functionCallContentFallbackMessageId)
1213ICollection<ChatMessage>? allPreDownstreamCallMessages = ConvertToFunctionCallContentMessages(
1219ChatMessage? rejectedPreDownstreamCallResultsMessage = rejectedFunctionCallResults is not null ?
1226List<ChatMessage>? preDownstreamCallHistory = null;
1257List<ChatMessage> messages)
1259Dictionary<string, ChatMessage>? allApprovalRequestsMessages = null;
1273var message = messages[i];
1313var newMessage = message.Clone();
1358ChatMessage? requestMessage = null;
1383private static ICollection<ChatMessage>? ConvertToFunctionCallContentMessages(
1388ChatMessage? currentMessage = null;
1389Dictionary<string, ChatMessage>? messagesById = null;
1425if (messagesById?.Values is ICollection<ChatMessage> cm)
1440/// Takes the <see cref="FunctionCallContent"/> from the <paramref name="resultWithRequestMessage"/> and wraps it in a <see cref="ChatMessage"/>
1443private static ChatMessage ConvertToFunctionCallContentMessage(ApprovalResultWithRequestMessage resultWithRequestMessage, string? fallbackMessageId)
1445ChatMessage functionCallMessage = resultWithRequestMessage.RequestMessage?.Clone() ?? new() { Role = ChatRole.Assistant };
1515private static IList<ChatMessage> ReplaceFunctionCallsWithApprovalRequests(
1516IList<ChatMessage> messages,
1563var message = lastMessageIndex != messageIndex ? outputMessages[messageIndex].Clone() : outputMessages[messageIndex];
1586/// wrapped in <see cref="ChatMessage"/> objects.
1588private async Task<(IList<ChatMessage>? FunctionResultContentMessages, bool ShouldTerminate, int ConsecutiveErrorCount)> InvokeApprovedFunctionApprovalResponsesAsync(
1591List<ChatMessage> originalMessages,
1681public ChatMessage? RequestMessage { get; set; }
Microsoft.Extensions.AI.Abstractions (48)
ChatCompletion\ChatResponseExtensions.cs (16)
29public static void AddMessages(this IList<ChatMessage> list, ChatResponse response)
34if (list is List<ChatMessage> listConcrete)
40foreach (var message in response.Messages)
47/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
53/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, the
58public static void AddMessages(this IList<ChatMessage> list, IEnumerable<ChatResponseUpdate> updates)
71/// <summary>Converts the <paramref name="update"/> into a <see cref="ChatMessage"/> instance and adds it to <paramref name="list"/>.</summary>
79/// no <see cref="ChatMessage"/> will be added to the <paramref name="list"/>.
81public static void AddMessages(this IList<ChatMessage> list, ChatResponseUpdate update, Func<AIContent, bool>? filter = null)
99/// <summary>Converts the <paramref name="updates"/> into <see cref="ChatMessage"/> instances and adds them to <paramref name="list"/>.</summary>
107/// As part of combining <paramref name="updates"/> into a series of <see cref="ChatMessage"/> instances, tne
113this IList<ChatMessage> list, IAsyncEnumerable<ChatResponseUpdate> updates, CancellationToken cancellationToken = default)
121IList<ChatMessage> list, IAsyncEnumerable<ChatResponseUpdate> updates, CancellationToken cancellationToken) =>
131/// <see cref="ChatMessage"/> instances. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
159/// <see cref="ChatMessage"/> instances. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
307ChatMessage 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,
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 (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 (27)
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 =
664List<ChatMessage> plan =
725var messages = new List<ChatMessage>
783var messages = new List<ChatMessage>
794ChatMessage message = chatContents.Count() is 1 or 3 ?
841List<ChatMessage> planBeforeTermination =
851List<ChatMessage> plan =
866async Task InvokeAsync(Func<Task<List<ChatMessage>>> work)
910var existingSearchResult = chatContents.SingleOrDefault(m => m.Role == ChatRole.Tool);
919var message = new ChatMessage(ChatRole.Assistant, resultContents);
927List<ChatMessage> messages = [new(ChatRole.User, "hello")];
933var lastMessage = messages.Last();
954Func<IEnumerable<ChatMessage>, ChatOptions?, CancellationToken, ChatResponse> callback =
997List<ChatMessage> plan =
1026List<ChatMessage> plan =
1079List<ChatMessage> planForContinue =
1098List<ChatMessage> fullPlanWithUnknown =
1127List<ChatMessage> fullPlan =
1135List<ChatMessage> expected = fullPlan.Take(2).ToList();
1156List<ChatMessage> fullPlan =
1167List<ChatMessage> expected = fullPlan.Take(2).ToList();
1191private static async Task<List<ChatMessage>> InvokeAndAssertAsync(
1193List<ChatMessage> plan,
1194List<ChatMessage>? expected = null,
1203List<ChatMessage> chat = [plan[0]];
1217var message = new ChatMessage(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1227var result = await service.GetResponseAsync(new EnumeratedOnceEnumerable<ChatMessage>(chat), options, cts.Token);
1261private static async Task<List<ChatMessage>> InvokeAndAssertStreamingAsync(
1263List<ChatMessage> plan,
1264List<ChatMessage>? expected = null,
1273List<ChatMessage> chat = [plan[0]];
1281ChatMessage message = new(ChatRole.Assistant, [.. plan[contents.Count()].Contents])
1291var 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)