microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI.Models.OpenAI/OpenAIChatModel.cs
62lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.ClientModel; |
| 5 | |
| 6 | using Microsoft.Teams.Common.Logging; |
| 7 | |
| 8 | using OpenAI; |
| 9 | using OpenAI.Chat; |
| 10 | |
| 11 | namespace Microsoft.Teams.AI.Models.OpenAI; |
| 12 | |
| 13 | |
| 14 | public partial class OpenAIChatModel : IChatModel<ChatCompletionOptions> |
| 15 | { |
| 16 | public string Name => Model; |
| 17 | |
| 18 | /// <summary> |
| 19 | /// the OpenAI chat client used to |
| 20 | /// make requests |
| 21 | /// </summary> |
| 22 | public ChatClient ChatClient { get; set; } |
| 23 | |
| 24 | /// <summary> |
| 25 | /// the model name |
| 26 | /// </summary> |
| 27 | protected string Model { get; set; } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// the logger instance |
| 31 | /// </summary> |
| 32 | protected ILogger Logger { get; set; } |
| 33 | |
| 34 | public OpenAIChatModel(string model, OpenAIClient client) |
| 35 | { |
| 36 | Model = model; |
| 37 | ChatClient = client.GetChatClient(model); |
| 38 | Logger = new ConsoleLogger(model); |
| 39 | } |
| 40 | |
| 41 | public OpenAIChatModel(string model, string apiKey, Options? options = null) |
| 42 | { |
| 43 | options ??= new(); |
| 44 | options.NetworkTimeout ??= TimeSpan.FromSeconds(60); |
| 45 | |
| 46 | var client = new OpenAIClient(new ApiKeyCredential(apiKey), options); |
| 47 | Model = model; |
| 48 | ChatClient = client.GetChatClient(model); |
| 49 | Logger = (options?.Logger ?? new ConsoleLogger()).Child(model); |
| 50 | } |
| 51 | |
| 52 | public OpenAIChatModel(string model, ApiKeyCredential apiKey, Options? options = null) |
| 53 | { |
| 54 | options ??= new(); |
| 55 | options.NetworkTimeout ??= TimeSpan.FromSeconds(60); |
| 56 | |
| 57 | var client = new OpenAIClient(apiKey, options); |
| 58 | Model = model; |
| 59 | ChatClient = client.GetChatClient(model); |
| 60 | Logger = (options?.Logger ?? new ConsoleLogger()).Child(model); |
| 61 | } |
| 62 | } |