microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI/Messages/ModelMessage.cs
78lines · modecode
| 1 | using System.Text.Json; |
| 2 | using System.Text.Json.Serialization; |
| 3 | |
| 4 | namespace Microsoft.Teams.AI.Messages; |
| 5 | |
| 6 | public class ModelMessage(object? content) : ModelMessage<object?>(content) |
| 7 | { |
| 8 | public static ModelMessage<string> Text(string content) => new(content); |
| 9 | public static ModelMessage<IEnumerable<IContent>> Text(IEnumerable<IContent> content) => new(content); |
| 10 | public static ModelMessage<Stream> Media(Stream content) => new(content); |
| 11 | } |
| 12 | |
| 13 | public class ModelMessage<T> : IMessage |
| 14 | { |
| 15 | [JsonPropertyName("role")] |
| 16 | [JsonPropertyOrder(0)] |
| 17 | public Role Role => Role.Model; |
| 18 | |
| 19 | [JsonPropertyName("content")] |
| 20 | [JsonPropertyOrder(1)] |
| 21 | public T Content { get; set; } |
| 22 | |
| 23 | [JsonPropertyName("function_calls")] |
| 24 | [JsonPropertyOrder(2)] |
| 25 | public IList<FunctionCall>? FunctionCalls { get; set; } |
| 26 | |
| 27 | [JsonIgnore] |
| 28 | public bool HasFunctionCalls => FunctionCalls is not null && FunctionCalls.Count > 0; |
| 29 | |
| 30 | [JsonConstructor] |
| 31 | public ModelMessage(T content, IList<FunctionCall>? functionCalls = null) |
| 32 | { |
| 33 | Content = content; |
| 34 | FunctionCalls = functionCalls; |
| 35 | } |
| 36 | |
| 37 | public override string ToString() |
| 38 | { |
| 39 | return JsonSerializer.Serialize(this, new JsonSerializerOptions() |
| 40 | { |
| 41 | WriteIndented = true, |
| 42 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 43 | }); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// <summary> |
| 48 | /// represents a models request to |
| 49 | /// invoke a function |
| 50 | /// </summary> |
| 51 | public class FunctionCall |
| 52 | { |
| 53 | [JsonPropertyName("id")] |
| 54 | [JsonPropertyOrder(0)] |
| 55 | public required string Id { get; set; } |
| 56 | |
| 57 | [JsonPropertyName("name")] |
| 58 | [JsonPropertyOrder(1)] |
| 59 | public required string Name { get; set; } |
| 60 | |
| 61 | [JsonPropertyName("arguments")] |
| 62 | [JsonPropertyOrder(2)] |
| 63 | public string? Arguments { get; set; } |
| 64 | |
| 65 | public IDictionary<string, object?>? Parse() |
| 66 | { |
| 67 | return JsonSerializer.Deserialize<Dictionary<string, object?>>(Arguments ?? "{}"); |
| 68 | } |
| 69 | |
| 70 | public override string ToString() |
| 71 | { |
| 72 | return JsonSerializer.Serialize(this, new JsonSerializerOptions() |
| 73 | { |
| 74 | WriteIndented = true, |
| 75 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 76 | }); |
| 77 | } |
| 78 | } |