microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.AI.Models.OpenAI/OpenAIChatModel.cs
63lines · 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 | [Obsolete("Microsoft.Teams.AI.Models.OpenAI is deprecated and will be removed by end of summer 2026.")] |
| 15 | public partial class OpenAIChatModel : IChatModel<ChatCompletionOptions> |
| 16 | { |
| 17 | public string Name => Model; |
| 18 | |
| 19 | /// <summary> |
| 20 | /// the OpenAI chat client used to |
| 21 | /// make requests |
| 22 | /// </summary> |
| 23 | public ChatClient ChatClient { get; set; } |
| 24 | |
| 25 | /// <summary> |
| 26 | /// the model name |
| 27 | /// </summary> |
| 28 | protected string Model { get; set; } |
| 29 | |
| 30 | /// <summary> |
| 31 | /// the logger instance |
| 32 | /// </summary> |
| 33 | protected ILogger Logger { get; set; } |
| 34 | |
| 35 | public OpenAIChatModel(string model, OpenAIClient client) |
| 36 | { |
| 37 | Model = model; |
| 38 | ChatClient = client.GetChatClient(model); |
| 39 | Logger = new ConsoleLogger(model); |
| 40 | } |
| 41 | |
| 42 | public OpenAIChatModel(string model, string apiKey, Options? options = null) |
| 43 | { |
| 44 | options ??= new(); |
| 45 | options.NetworkTimeout ??= TimeSpan.FromSeconds(60); |
| 46 | |
| 47 | var client = new OpenAIClient(new ApiKeyCredential(apiKey), options); |
| 48 | Model = model; |
| 49 | ChatClient = client.GetChatClient(model); |
| 50 | Logger = (options?.Logger ?? new ConsoleLogger()).Child(model); |
| 51 | } |
| 52 | |
| 53 | public OpenAIChatModel(string model, ApiKeyCredential apiKey, Options? options = null) |
| 54 | { |
| 55 | options ??= new(); |
| 56 | options.NetworkTimeout ??= TimeSpan.FromSeconds(60); |
| 57 | |
| 58 | var client = new OpenAIClient(apiKey, options); |
| 59 | Model = model; |
| 60 | ChatClient = client.GetChatClient(model); |
| 61 | Logger = (options?.Logger ?? new ConsoleLogger()).Child(model); |
| 62 | } |
| 63 | } |