microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/close-pull-request

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

81lines · modecode

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