microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI/Prompts/ChatPrompt/ChatPrompt.cs
242lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Reflection; |
| 5 | |
| 6 | using Json.Schema; |
| 7 | using Json.Schema.Generation; |
| 8 | |
| 9 | using Microsoft.Teams.AI.Annotations; |
| 10 | using Microsoft.Teams.AI.Messages; |
| 11 | using Microsoft.Teams.AI.Models; |
| 12 | using Microsoft.Teams.Common.Extensions; |
| 13 | using Microsoft.Teams.Common.Logging; |
| 14 | |
| 15 | namespace Microsoft.Teams.AI.Prompts; |
| 16 | |
| 17 | /// <summary> |
| 18 | /// a prompt that can send/receive text |
| 19 | /// messages and expose chat model specific |
| 20 | /// features like streaming/functions |
| 21 | /// </summary> |
| 22 | public interface IChatPrompt : IPrompt |
| 23 | { |
| 24 | /// <summary> |
| 25 | /// the message history |
| 26 | /// </summary> |
| 27 | public IList<IMessage> Messages { get; } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// the collection of registered functions |
| 31 | /// </summary> |
| 32 | public FunctionCollection Functions { get; } |
| 33 | } |
| 34 | |
| 35 | /// <summary> |
| 36 | /// a prompt that can send/receive text |
| 37 | /// messages and expose chat model specific |
| 38 | /// features like streaming/functions |
| 39 | /// </summary> |
| 40 | public interface IChatPrompt<TOptions> : IChatPrompt |
| 41 | { |
| 42 | /// <summary> |
| 43 | /// register an error handler |
| 44 | /// </summary> |
| 45 | public IChatPrompt<TOptions> OnError(Action<Exception> onError); |
| 46 | |
| 47 | /// <summary> |
| 48 | /// register an error handler |
| 49 | /// </summary> |
| 50 | public IChatPrompt<TOptions> OnError(Func<Exception, Task> onError); |
| 51 | |
| 52 | /// <summary> |
| 53 | /// send a message via the prompt using string content |
| 54 | /// </summary> |
| 55 | /// <param name="text">the message text</param> |
| 56 | /// <param name="options">the request options</param> |
| 57 | /// <param name="onChunk"> |
| 58 | /// the stream chunk handler (if notnull streaming is enabled) |
| 59 | /// </param> |
| 60 | /// <returns>the models response</returns> |
| 61 | public Task<ModelMessage<string>> Send(string text, RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default); |
| 62 | |
| 63 | /// <summary> |
| 64 | /// send a message via the prompt using content blocks |
| 65 | /// </summary> |
| 66 | /// <param name="content">the message content</param> |
| 67 | /// <param name="options">the request options</param> |
| 68 | /// <param name="onChunk"> |
| 69 | /// the stream chunk handler (if notnull streaming is enabled) |
| 70 | /// </param> |
| 71 | /// <returns>the models response</returns> |
| 72 | public Task<ModelMessage<string>> Send(IContent[] content, RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default); |
| 73 | |
| 74 | /// <summary> |
| 75 | /// send a message via the prompt |
| 76 | /// </summary> |
| 77 | /// <param name="message">the message to send</param> |
| 78 | /// <param name="options">the request options</param> |
| 79 | /// <param name="onChunk"> |
| 80 | /// the stream chunk handler (if notnull streaming is enabled) |
| 81 | /// </param> |
| 82 | /// <returns>the models response</returns> |
| 83 | public Task<ModelMessage<string>> Send(UserMessage<string> message, RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default); |
| 84 | |
| 85 | /// <summary> |
| 86 | /// send a message via the prompt |
| 87 | /// </summary> |
| 88 | /// <param name="message">the message to send</param> |
| 89 | /// <param name="options">the request options</param> |
| 90 | /// <param name="onChunk"> |
| 91 | /// the stream chunk handler (if notnull streaming is enabled) |
| 92 | /// </param> |
| 93 | /// <returns>the models response</returns> |
| 94 | public Task<ModelMessage<string>> Send(UserMessage<IEnumerable<IContent>> message, RequestOptions? options = null, OnStreamChunk? onChunk = null, CancellationToken cancellationToken = default); |
| 95 | |
| 96 | /// <summary> |
| 97 | /// options to send when invoking a prompt |
| 98 | /// </summary> |
| 99 | public class RequestOptions |
| 100 | { |
| 101 | /// <summary> |
| 102 | /// the conversation history |
| 103 | /// </summary> |
| 104 | public IList<IMessage>? Messages { get; set; } |
| 105 | |
| 106 | /// <summary> |
| 107 | /// the model request options |
| 108 | /// </summary> |
| 109 | public TOptions? Request { get; set; } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// <summary> |
| 114 | /// a prompt that can send/receive text |
| 115 | /// messages and expose chat model specific |
| 116 | /// features like streaming/functions |
| 117 | /// </summary> |
| 118 | public partial class ChatPrompt<TOptions> : IChatPrompt<TOptions> |
| 119 | { |
| 120 | public string Name { get; private set; } |
| 121 | public string Description { get; private set; } |
| 122 | public IList<IMessage> Messages { get; private set; } |
| 123 | public FunctionCollection Functions { get; private set; } |
| 124 | |
| 125 | protected IChatModel<TOptions> Model { get; } |
| 126 | protected ITemplate? Template { get; } |
| 127 | protected ILogger Logger { get; } |
| 128 | protected IList<IPlugin> Plugins { get; } |
| 129 | protected IList<IChatPlugin> ChatPlugins => Plugins.Where(p => p is IChatPlugin).Select(p => (IChatPlugin)p).ToList(); |
| 130 | protected event EventHandler<Exception> ErrorEvent; |
| 131 | |
| 132 | public ChatPrompt(IChatModel<TOptions> model, ChatPromptOptions? options = null) |
| 133 | { |
| 134 | options ??= new(); |
| 135 | Name = options.Name ?? "Chat"; |
| 136 | Description = options.Description ?? "an agent you can chat with"; |
| 137 | Model = model; |
| 138 | Template = options.Instructions; |
| 139 | Messages = options.Messages ?? []; |
| 140 | Functions = new(); |
| 141 | Logger = (options.Logger ?? new ConsoleLogger()).Child($"AI.{Name}"); |
| 142 | Plugins = []; |
| 143 | ErrorEvent = (_, ex) => Logger.Error(ex); |
| 144 | } |
| 145 | |
| 146 | public ChatPrompt(ChatPrompt<TOptions> prompt) |
| 147 | { |
| 148 | Name = prompt.Name; |
| 149 | Description = prompt.Description; |
| 150 | Messages = prompt.Messages; |
| 151 | Functions = prompt.Functions; |
| 152 | Model = prompt.Model; |
| 153 | Template = prompt.Template; |
| 154 | Logger = prompt.Logger; |
| 155 | Plugins = prompt.Plugins; |
| 156 | ErrorEvent = prompt.ErrorEvent; |
| 157 | } |
| 158 | |
| 159 | public ChatPrompt(string name, ChatPrompt<TOptions> prompt) |
| 160 | { |
| 161 | Name = name; |
| 162 | Description = prompt.Description; |
| 163 | Messages = prompt.Messages; |
| 164 | Functions = prompt.Functions; |
| 165 | Model = prompt.Model; |
| 166 | Template = prompt.Template; |
| 167 | Logger = prompt.Logger.Peer(name); |
| 168 | Plugins = prompt.Plugins; |
| 169 | ErrorEvent = prompt.ErrorEvent; |
| 170 | } |
| 171 | |
| 172 | /// <summary> |
| 173 | /// create a ChatPrompt from any class |
| 174 | /// utilizing the ChatPromptAttribute |
| 175 | /// </summary> |
| 176 | /// <param name="model">the model to use</param> |
| 177 | /// <param name="value">the class instance to use</param> |
| 178 | /// <returns>a ChatPrompt</returns> |
| 179 | public static ChatPrompt<TOptions> From<T>(IChatModel<TOptions> model, T value, ChatPromptOptions? options = null) where T : class |
| 180 | { |
| 181 | var type = value.GetType(); |
| 182 | var promptAttribute = type.GetCustomAttribute<PromptAttribute>(); |
| 183 | var nameAttribute = type.GetCustomAttribute<Prompt.NameAttribute>(); |
| 184 | var descriptionAttribute = type.GetCustomAttribute<Prompt.DescriptionAttribute>(); |
| 185 | var instructionsAttribute = type.GetCustomAttribute<Prompt.InstructionsAttribute>(); |
| 186 | |
| 187 | if (promptAttribute is null) |
| 188 | { |
| 189 | throw new Exception("only types utilizing the ChatPromptAttribute can be turned into a ChatPrompt"); |
| 190 | } |
| 191 | |
| 192 | var name = promptAttribute.Name ?? nameAttribute?.Name ?? type.Name; |
| 193 | var description = promptAttribute.Description ?? descriptionAttribute?.Description; |
| 194 | var instructions = promptAttribute.Instructions ?? instructionsAttribute?.Instructions; |
| 195 | options ??= new(); |
| 196 | options.WithName(name); |
| 197 | |
| 198 | if (description is not null) |
| 199 | { |
| 200 | options = options.WithDescription(description); |
| 201 | } |
| 202 | |
| 203 | if (instructions is not null) |
| 204 | { |
| 205 | options = options.WithInstructions(instructions); |
| 206 | } |
| 207 | |
| 208 | var prompt = new ChatPrompt<TOptions>(model, options); |
| 209 | |
| 210 | foreach (var method in type.GetMethods()) |
| 211 | { |
| 212 | var functionAttribute = method.GetCustomAttribute<FunctionAttribute>(); |
| 213 | var functionDescriptionAttribute = method.GetCustomAttribute<Annotations.Function.DescriptionAttribute>(); |
| 214 | |
| 215 | if (functionAttribute is null) continue; |
| 216 | |
| 217 | var parameters = method.GetParameters().Select(p => |
| 218 | { |
| 219 | var name = p.GetCustomAttribute<ParamAttribute>()?.Name ?? p.Name ?? p.Position.ToString(); |
| 220 | var schema = new JsonSchemaBuilder().FromType(p.ParameterType).Build(); |
| 221 | var required = !p.IsOptional; |
| 222 | return (name, schema, required); |
| 223 | }); |
| 224 | |
| 225 | var schema = new JsonSchemaBuilder() |
| 226 | .Type(SchemaValueType.Object) |
| 227 | .Properties(parameters.Select(item => (item.name, item.schema)).ToArray()) |
| 228 | .Required(parameters.Where(item => item.required).Select(item => item.name)); |
| 229 | |
| 230 | var function = new Function( |
| 231 | functionAttribute.Name ?? method.Name, |
| 232 | functionAttribute.Description ?? functionDescriptionAttribute?.Description, |
| 233 | parameters.Count() > 0 ? schema.Build() : null, |
| 234 | method.CreateDelegate(value) |
| 235 | ); |
| 236 | |
| 237 | prompt.Function(function); |
| 238 | } |
| 239 | |
| 240 | return prompt; |
| 241 | } |
| 242 | } |