microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI.Models.OpenAI/Extensions/UserMessage.cs
48lines · modeblame
73e7847aAlex Acebo1 years ago | 1 | using Microsoft.Teams.AI.Messages; |
| 2 | | |
| 3 | using OpenAI.Chat; | |
| 4 | | |
| 5 | namespace Microsoft.Teams.AI.Models.OpenAI; | |
| 6 | | |
| 7 | public static partial class MessageExtensions | |
| 8 | { | |
| 9 | public static UserMessage<IEnumerable<IContent>> ToTeams(this UserChatMessage message) | |
| 10 | { | |
| 11 | var parts = message.Content.Select<ChatMessageContentPart, IContent>(part => | |
| 12 | { | |
| 13 | if (part.Kind == ChatMessageContentPartKind.Text) | |
| 14 | { | |
| 15 | return new TextContent() { Text = part.Text }; | |
| 16 | } | |
| 17 | | |
| 18 | return new ImageContent() { ImageUrl = part.ImageUri.ToString() }; | |
| 19 | }); | |
| 20 | | |
| 21 | return new(parts); | |
| 22 | } | |
| 23 | | |
| 24 | public static UserChatMessage ToOpenAI(this UserMessage<IEnumerable<IContent>> message) | |
| 25 | { | |
| 26 | var parts = message.Content.Select(part => | |
| 27 | { | |
| 28 | if (part is TextContent text) | |
| 29 | { | |
| 30 | return ChatMessageContentPart.CreateTextPart(text.Text); | |
| 31 | } | |
| 32 | | |
| 33 | if (part is ImageContent image) | |
| 34 | { | |
| 35 | return ChatMessageContentPart.CreateImagePart(new Uri(image.ImageUrl)); | |
| 36 | } | |
| 37 | | |
| 38 | throw new Exception("invalid content part"); | |
| 39 | }); | |
| 40 | | |
| 41 | return ChatMessage.CreateUserMessage(parts); | |
| 42 | } | |
| 43 | | |
| 44 | public static UserChatMessage ToOpenAI(this UserMessage<string> message) | |
| 45 | { | |
| 46 | return ChatMessage.CreateUserMessage(message.Content); | |
| 47 | } | |
| 48 | } |