microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.0-preview.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.AI/Messages/ModelMessage.cs

78lines · modecode

1using System.Text.Json;
2using System.Text.Json.Serialization;
3
4namespace Microsoft.Teams.AI.Messages;
5
6public 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
13public 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>
51public 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}