microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI.Models.OpenAI/Extensions/AssistantMessage.cs
43lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.AI.Messages; |
| 5 | |
| 6 | using OpenAI.Chat; |
| 7 | |
| 8 | namespace Microsoft.Teams.AI.Models.OpenAI; |
| 9 | |
| 10 | public static partial class MessageExtensions |
| 11 | { |
| 12 | public static ModelMessage<string> ToTeams(this AssistantChatMessage message) |
| 13 | { |
| 14 | var calls = message.ToolCalls.Select(call => |
| 15 | { |
| 16 | var args = call.FunctionArguments.ToString(); |
| 17 | return new FunctionCall() |
| 18 | { |
| 19 | Id = call.Id, |
| 20 | Name = call.FunctionName, |
| 21 | Arguments = args == string.Empty ? null : args |
| 22 | }; |
| 23 | }); |
| 24 | |
| 25 | return new ModelMessage<string>(message.Content.FirstOrDefault()?.Text ?? string.Empty, calls.ToList()); |
| 26 | } |
| 27 | |
| 28 | public static AssistantChatMessage ToOpenAI(this ModelMessage<string> message) |
| 29 | { |
| 30 | var calls = message.FunctionCalls?.Select(call => ChatToolCall.CreateFunctionToolCall( |
| 31 | call.Id, |
| 32 | call.Name, |
| 33 | call.Arguments is null ? BinaryData.Empty : BinaryData.FromString(call.Arguments) |
| 34 | )); |
| 35 | |
| 36 | if (calls is not null && calls.Count() > 0) |
| 37 | { |
| 38 | return new AssistantChatMessage(calls?.ToList() ?? []); |
| 39 | } |
| 40 | |
| 41 | return new AssistantChatMessage(message.Content); |
| 42 | } |
| 43 | } |