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/Prompts/ChatPrompt/ChatPrompt.Send.cs

124lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.AI.Messages;
5using Microsoft.Teams.AI.Models;
6
7namespace Microsoft.Teams.AI.Prompts;
8
9public partial class ChatPrompt<TOptions>
10{
11 public async Task<IMessage> Send(IMessage message, CancellationToken cancellationToken = default)
12 {
13 return await Send(message, null, null, cancellationToken);
14 }
15
16 public async Task<ModelMessage<string>> Send(string text, CancellationToken cancellationToken = default)
17 {
18 return await Send(text, null, null, cancellationToken);
19 }
20
21 public async Task<ModelMessage<string>> Send(string text, OnStreamChunk? onChunk, CancellationToken cancellationToken = default)
22 {
23 return await Send(text, null, onChunk, cancellationToken);
24 }
25
26 public Task<ModelMessage<string>> Send(string text, IChatPrompt<TOptions>.RequestOptions? options, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default)
27 {
28 var message = UserMessage.Text(text);
29 return Send((IMessage)message, options, onChunk, cancellationToken);
30 }
31
32 public Task<ModelMessage<string>> Send(IContent[] content, IChatPrompt<TOptions>.RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default)
33 {
34 var message = UserMessage.Text(content);
35 return Send((IMessage)message, options, onChunk, cancellationToken);
36 }
37
38 public Task<ModelMessage<string>> Send(UserMessage<string> message, IChatPrompt<TOptions>.RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default)
39 {
40 return Send((IMessage)message, options, onChunk, cancellationToken);
41 }
42
43 public Task<ModelMessage<string>> Send(UserMessage<IEnumerable<IContent>> message, IChatPrompt<TOptions>.RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default)
44 {
45 return Send((IMessage)message, options, onChunk, cancellationToken);
46 }
47
48 public async Task<ModelMessage<string>> Send(IMessage message, IChatPrompt<TOptions>.RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default)
49 {
50 var messages = options?.Messages ?? Messages;
51 var buffer = string.Empty;
52 var prompt = Template is not null ? await Template.Render(null, cancellationToken) : null;
53
54 async Task OnChunk(string chunk)
55 {
56 if (chunk == string.Empty || onChunk is null) return;
57 buffer += chunk;
58
59 try
60 {
61 await onChunk(buffer);
62 buffer = string.Empty;
63 }
64 catch { return; }
65 }
66
67 var functions = new FunctionCollection();
68 foreach (var kvp in Functions)
69 {
70 functions[kvp.Key] = kvp.Value;
71 }
72 var instructions = prompt is null ? null : new DeveloperMessage(prompt);
73
74 // allow plugins to modify functions and instructions before each send
75 foreach (var plugin in Plugins)
76 {
77 functions = await plugin.OnBuildFunctions(this, functions, cancellationToken);
78 instructions = await plugin.OnBuildInstructions(this, instructions);
79 }
80
81 ChatModelOptions<TOptions> requestOptions = new(Invoke(functions))
82 {
83 Functions = functions.List,
84 Messages = messages,
85 Prompt = instructions,
86 Options = options is null ? default : options.Request,
87 };
88
89 ModelMessage<string>? res;
90
91 try
92 {
93 Logger.Debug(message);
94
95 foreach (var plugin in Plugins)
96 {
97 message = await plugin.OnBeforeSend(this, message, requestOptions.Options, cancellationToken);
98 }
99
100 if (onChunk is null)
101 {
102 res = await Model.Send(message, requestOptions, cancellationToken);
103 }
104 else
105 {
106 res = await Model.Send(message, requestOptions, new Stream(OnChunk), cancellationToken);
107 }
108
109 Logger.Debug(res);
110
111 foreach (var plugin in Plugins)
112 {
113 res = (ModelMessage<string>)await plugin.OnAfterSend(this, res, requestOptions.Options, cancellationToken);
114 }
115
116 return res;
117 }
118 catch (Exception ex)
119 {
120 ErrorEvent(Model, ex);
121 throw new Exception("An error occurred while attempting to send the message", ex);
122 }
123 }
124}