microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
df2202d1bf4bccd37efa56c709f911109c0d9ec5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.AI.Models.OpenAI/OpenAIChatModel.cs

60lines · modecode

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