forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAIConfig.cs
More file actions
103 lines (86 loc) · 3.27 KB
/
OpenAIConfig.cs
File metadata and controls
103 lines (86 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json.Serialization;
#pragma warning disable IDE0130 // reduce number of "using" statements
// ReSharper disable once CheckNamespace - reduce number of "using" statements
namespace Microsoft.KernelMemory;
/// <summary>
/// OpenAI settings.
/// </summary>
public class OpenAIConfig
{
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum TextGenerationTypes
{
Auto = 0,
TextCompletion,
Chat,
}
/// <summary>
/// Model used for text generation. Chat models can be used too.
/// </summary>
public string TextModel { get; set; } = string.Empty;
/// <summary>
/// The max number of tokens supported by the text model.
/// </summary>
public int TextModelMaxTokenTotal { get; set; } = 8192;
/// <summary>
/// The type of OpenAI completion to use, either Text (legacy) or Chat.
/// When using Auto, the client uses OpenAI model names to detect the correct protocol.
/// Most OpenAI models use Chat. If you're using a non-OpenAI model, you might want to set this manually.
/// </summary>
public TextGenerationTypes TextGenerationType { get; set; } = TextGenerationTypes.Auto;
/// <summary>
/// Model used to embedding generation/
/// </summary>
public string EmbeddingModel { get; set; } = string.Empty;
/// <summary>
/// The max number of tokens supported by the embedding model.
/// Default to OpenAI ADA2 settings.
/// </summary>
public int EmbeddingModelMaxTokenTotal { get; set; } = 8191;
/// <summary>
/// OpenAI HTTP endpoint. You may need to override this to work with
/// OpenAI compatible services like LM Studio.
/// </summary>
public string Endpoint { get; set; } = "https://api.openai.com/v1";
/// <summary>
/// OpenAI API key.
/// </summary>
public string APIKey { get; set; } = string.Empty;
/// <summary>
/// Optional OpenAI Organization ID.
/// </summary>
public string? OrgId { get; set; } = string.Empty;
/// <summary>
/// How many times to retry in case of throttling.
/// </summary>
public int MaxRetries { get; set; } = 10;
/// <summary>
/// The number of dimensions output embeddings should have.
/// Only supported in "text-embedding-3" and later models developed with
/// MRL, see https://arxiv.org/abs/2205.13147
/// </summary>
public int? EmbeddingDimensions { get; set; }
/// <summary>
/// Verify that the current state is valid.
/// </summary>
public void Validate()
{
if (string.IsNullOrWhiteSpace(this.APIKey))
{
throw new ConfigurationException($"OpenAI: {nameof(this.APIKey)} is empty");
}
if (this.TextModelMaxTokenTotal < 1)
{
throw new ConfigurationException($"OpenAI: {nameof(this.TextModelMaxTokenTotal)} cannot be less than 1");
}
if (this.EmbeddingModelMaxTokenTotal < 1)
{
throw new ConfigurationException($"OpenAI: {nameof(this.EmbeddingModelMaxTokenTotal)} cannot be less than 1");
}
if (this.EmbeddingDimensions is < 1)
{
throw new ConfigurationException($"OpenAI: {nameof(this.EmbeddingDimensions)} cannot be less than 1");
}
}
}