microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI/Models/ChatModel.cs
79lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.AI.Messages; |
| 5 | |
| 6 | namespace Microsoft.Teams.AI.Models; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// a model that can reason over and |
| 10 | /// respond with text |
| 11 | /// </summary> |
| 12 | public interface IChatModel<TOptions> : IModel<TOptions> |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// send a message to the model |
| 16 | /// </summary> |
| 17 | /// <param name="message">the message to send</param> |
| 18 | /// <param name="options">the options</param> |
| 19 | /// <returns>the models response</returns> |
| 20 | public Task<ModelMessage<string>> Send(IMessage message, ChatModelOptions<TOptions> options, CancellationToken cancellationToken = default); |
| 21 | |
| 22 | /// <summary> |
| 23 | /// send a message to the model and stream |
| 24 | /// the response |
| 25 | /// </summary> |
| 26 | /// <param name="message">the message to send</param> |
| 27 | /// <param name="options">the options</param> |
| 28 | /// <param name="stream">the stream to use</param> |
| 29 | /// <returns>the models response</returns> |
| 30 | public Task<ModelMessage<string>> Send(IMessage message, ChatModelOptions<TOptions> options, IStream stream, CancellationToken cancellationToken = default); |
| 31 | } |
| 32 | |
| 33 | /// <summary> |
| 34 | /// options to send with the message |
| 35 | /// </summary> |
| 36 | public class ChatModelOptions<TOptions> |
| 37 | { |
| 38 | /// <summary> |
| 39 | /// the initial prompt message that defines |
| 40 | /// model behavior |
| 41 | /// </summary> |
| 42 | public DeveloperMessage? Prompt { get; set; } |
| 43 | |
| 44 | /// <summary> |
| 45 | /// the conversation history |
| 46 | /// </summary> |
| 47 | public IList<IMessage> Messages { get; set; } = []; |
| 48 | |
| 49 | /// <summary> |
| 50 | /// the registered functions that can be |
| 51 | /// called |
| 52 | /// </summary> |
| 53 | public required IList<IFunction> Functions { get; set; } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// the request options defined by the model |
| 57 | /// </summary> |
| 58 | public TOptions? Options { get; set; } |
| 59 | |
| 60 | /// <summary> |
| 61 | /// the handler used to invoke functions |
| 62 | /// </summary> |
| 63 | internal Func<FunctionCall, CancellationToken, Task<object?>>? OnInvoke; |
| 64 | |
| 65 | public ChatModelOptions(Func<FunctionCall, CancellationToken, Task<object?>>? onInvoke = null) |
| 66 | { |
| 67 | OnInvoke = onInvoke; |
| 68 | } |
| 69 | |
| 70 | /// <summary> |
| 71 | /// invoke a function |
| 72 | /// </summary> |
| 73 | /// <param name="call">the function call</param> |
| 74 | /// <returns>the function response</returns> |
| 75 | public Task<object?> Invoke(FunctionCall call, CancellationToken cancellationToken = default) |
| 76 | { |
| 77 | return OnInvoke is null ? Task.FromResult<object?>(null) : OnInvoke(call, cancellationToken); |
| 78 | } |
| 79 | } |