Skip to content

Commit

Permalink
feat: Updated prices.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Nov 16, 2023
1 parent 1832be5 commit 42a53b3
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>2.0.0-alpha.2</Version>
<Version>2.0.0-alpha.3</Version>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>tryAGI and contributors</Authors>
Expand Down
48 changes: 30 additions & 18 deletions src/libs/OpenAI.Constants/Chat/ChatModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,46 @@ namespace OpenAI.Constants;
/// </summary>
public readonly record struct ChatModel(string Value)
{
/// <summary>
/// More capable than any GPT-3.5 model, able to do more complex tasks, and optimized for chat. <br/>
/// Will be updated with our latest model iteration 2 weeks after it is released. <br/>
/// Max tokens: 8,192 tokens <br/>
/// Training data: Up to Sep 2021 <br/>
/// <remarks>On June 27th, 2023, gpt-4 will be updated to point from gpt-4-0314 to gpt-4-0613, the latest model iteration.</remarks>
/// </summary>
/// <inheritdoc cref="Gpt4_0613Value"/>
/// <remarks>Currently points to gpt-4-0613.</remarks>
internal const string Gpt4Value = "gpt-4";

/// <summary>
/// Snapshot of gpt-4 from June 13th 2023 with function calling data. <br/>
/// Unlike gpt-4, this model will not receive updates, and will be deprecated 3 months after a new version is released. <br/>
/// Snapshot of gpt-4 from June 13th 2023 with improved function calling support. <br/>
/// According https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo <br/>
/// Max tokens: 8,192 tokens <br/>
/// Training data: Up to Sep 2021 <br/>
/// </summary>
internal const string Gpt4_0613Value = "gpt-4-0613";

/// <summary>
/// Same capabilities as the base gpt-4 mode but with 4x the context length. <br/>
/// Will be updated with our latest model iteration. <br/>
/// Max tokens: 32,768 tokens <br/>
/// Training data: Up to Sep 2021 <br/>
/// <remarks>On June 27th, 2023, gpt-4 will be updated to point from gpt-4-0314 to gpt-4-0613, the latest model iteration.</remarks>
/// </summary>
/// <inheritdoc cref="Gpt4_32k_0613Value"/>
/// <remarks>Currently points to gpt-4-32k-0613</remarks>
internal const string Gpt4_32kValue = "gpt-4-32";

/// <summary>
/// Snapshot of gpt-4-32 from June 13th 2023. <br/>
/// Unlike gpt-4-32k, this model will not receive updates, and will be deprecated 3 months after a new version is released. <br/>
/// Snapshot of gpt-4-32k from June 13th 2023 with improved function calling support. <br/>
/// According https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo <br/>
/// Max tokens: 32,768 tokens <br/>
/// Training data: Up to Sep 2021 <br/>
/// </summary>
internal const string Gpt4_32k_0613Value = "gpt-4-32k-0613";

/// <summary>
/// The latest GPT-4 model with improved instruction following, JSON mode, reproducible outputs, parallel function calling, and more. Returns a maximum of 4,096 output tokens. This preview model is not yet suited for production traffic. <br/>
/// According https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo <br/>
/// Max tokens: 128,000 tokens <br/>
/// Training data: Up to Apr 2023 <br/>
/// </summary>
internal const string Gpt4_1106_PreviewValue = "gpt-4-1106-preview";

/// <summary>
/// Ability to understand images, in addition to all other GPT-4 Turbo capabilties. Returns a maximum of 4,096 output tokens. This is a preview model version and not suited yet for production traffic. <br/>
/// According https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo <br/>
/// Max tokens: 128,000 tokens <br/>
/// Training data: Up to Apr 2023 <br/>
/// </summary>
internal const string Gpt4VisionPreviewValue = "gpt-4-vision-preview";

/// <summary>
/// Most capable GPT-3.5 model and optimized for chat at 1/10th the cost of text-davinci-003. <br/>
/// Will be updated with our latest model iteration 2 weeks after it is released. <br/>
Expand Down Expand Up @@ -91,6 +97,12 @@ public readonly record struct ChatModel(string Value)
/// <inheritdoc cref="Gpt4_32k_0613Value"/>
public static ChatModel Gpt4_32k_0613 { get; } = new(Gpt4_32k_0613Value);

/// <inheritdoc cref="Gpt4_1106_PreviewValue"/>
public static ChatModel Gpt4_1106_Preview { get; } = new(Gpt4_1106_PreviewValue);

/// <inheritdoc cref="Gpt4VisionPreviewValue"/>
public static ChatModel Gpt4VisionPreview { get; } = new(Gpt4VisionPreviewValue);

/// <inheritdoc cref="Gpt35TurboValue"/>
public static ChatModel Gpt35Turbo { get; } = new(Gpt35TurboValue);

Expand Down
27 changes: 15 additions & 12 deletions src/libs/OpenAI.Constants/Chat/ChatPrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ public static class ChatPrices
{
/// <inheritdoc cref="ChatPrices"/>
/// <param name="model"></param>
/// <param name="completionTokens"></param>
/// <param name="promptTokens"></param>
/// <param name="outputTokens"></param>
/// <param name="inputTokens"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static double? TryGet(
ChatModel model,
int completionTokens,
int promptTokens)
int outputTokens,
int inputTokens)
{
var (promptPricePerToken, completionPricePerToken) = model.Value switch
var (inputPricePerToken, outputPricePerToken) = model.Value switch
{
ChatModel.Gpt4Value => (0.03 * 0.001, 0.06 * 0.001),
ChatModel.Gpt4_0613Value => (0.03 * 0.001, 0.06 * 0.001),

ChatModel.Gpt4_32kValue => (0.06 * 0.001, 0.12 * 0.001),
ChatModel.Gpt4_32k_0613Value => (0.06 * 0.001, 0.12 * 0.001),

ChatModel.Gpt4_1106_PreviewValue => (0.01 * 0.001, 0.03 * 0.001),
ChatModel.Gpt4VisionPreviewValue => (0.01 * 0.001, 0.03 * 0.001),

ChatModel.Gpt35TurboValue => (0.0015 * 0.001, 0.002 * 0.001),
ChatModel.Gpt35Turbo_0613Value => (0.0015 * 0.001, 0.002 * 0.001),

Expand All @@ -33,25 +36,25 @@ public static class ChatPrices

_ => (-1.0, -1.0)
};
if (promptPricePerToken < 0.0)
if (inputPricePerToken < 0.0)
{
return null;
}

return completionTokens * completionPricePerToken +
promptTokens * promptPricePerToken;
return outputTokens * outputPricePerToken +
inputTokens * inputPricePerToken;
}

/// <inheritdoc cref="TryGet(ChatModel, int, int)"/>
public static double Get(
ChatModel model,
int completionTokens,
int promptTokens)
int outputTokens,
int inputTokens)
{
return TryGet(
model: model,
completionTokens: completionTokens,
promptTokens: promptTokens) ??
outputTokens: outputTokens,
inputTokens: inputTokens) ??
throw new NotImplementedException();
}
}
3 changes: 3 additions & 0 deletions src/libs/OpenAI.Constants/Context/ContextLengths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static class ContextLengths
ChatModel.Gpt4_32kValue => 32_768,
ChatModel.Gpt4_32k_0613Value => 32_768,

ChatModel.Gpt4_1106_PreviewValue => 128_000,
ChatModel.Gpt4VisionPreviewValue => 128_000,

ChatModel.Gpt35TurboValue => 4_096,
ChatModel.Gpt35Turbo_0613Value => 4_096,

Expand Down
27 changes: 11 additions & 16 deletions src/libs/OpenAI.Constants/Embedding/EmbeddingPrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,41 @@ namespace OpenAI.Constants;
/// <summary>
/// All prices in USD. <br/>
/// According https://openai.com/pricing/ <br/>
/// According https://platform.openai.com/docs/guides/embeddings/embedding-models &lt;br/&gt;
/// According https://platform.openai.com/docs/guides/embeddings/embedding-models <br/>
/// </summary>
public static class EmbeddingPrices
{
/// <inheritdoc cref="EmbeddingPrices"/>
/// <param name="model"></param>
/// <param name="completionTokens"></param>
/// <param name="promptTokens"></param>
/// <param name="tokens"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static double? TryGet(
EmbeddingModel model,
int completionTokens,
int promptTokens)
int tokens)
{
var (promptPricePerToken, completionPricePerToken) = model.Value switch
var pricePerToken = model.Value switch
{
EmbeddingModel.Ada002Value => (0.0001 * 0.001, 0.0),
EmbeddingModel.Ada002Value => 0.0001 * 0.001,

_ => (-1.0, -1.0)
_ => -1.0,
};
if (promptPricePerToken < 0.0)
if (pricePerToken < 0.0)
{
return null;
}

return completionTokens * completionPricePerToken +
promptTokens * promptPricePerToken;
return tokens * pricePerToken;
}

/// <inheritdoc cref="TryGet(EmbeddingModel, int, int)"/>
/// <inheritdoc cref="TryGet(EmbeddingModel, int)"/>
public static double Get(
EmbeddingModel model,
int completionTokens,
int promptTokens)
int tokens)
{
return TryGet(
model: model,
completionTokens: completionTokens,
promptTokens: promptTokens) ??
tokens: tokens) ??
throw new NotImplementedException();
}
}
52 changes: 52 additions & 0 deletions src/libs/OpenAI.Constants/FineTuning/FineTuningPrices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace OpenAI.Constants;

/// <summary>
/// All prices in USD. <br/>
/// According https://openai.com/pricing/ <br/>
/// </summary>
public static class FineTuningPrices
{
/// <inheritdoc cref="FineTuningPrices"/>
/// <param name="model"></param>
/// <param name="trainingTokens"></param>
/// <param name="inputTokens"></param>
/// <param name="outputTokens"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static double? TryGet(
ChatModel model,
int trainingTokens,
int inputTokens,
int outputTokens)
{
var (trainingPricePerToken, inputPricePerToken, outputPricePerToken) = model.Value switch
{
ChatModel.Gpt35TurboValue => (0.0080 * 0.001, 0.0030 * 0.001, 0.0060 * 0.001),

_ => (-1.0, -1.0, -1.0),
};
if (trainingPricePerToken < 0.0)
{
return null;
}

return trainingTokens * trainingPricePerToken +
inputTokens * inputPricePerToken +
outputTokens * outputPricePerToken;
}

/// <inheritdoc cref="TryGet(ChatModel, int, int, int)"/>
public static double Get(
ChatModel model,
int trainingTokens,
int inputTokens,
int outputTokens)
{
return TryGet(
model: model,
trainingTokens: trainingTokens,
inputTokens: inputTokens,
outputTokens: outputTokens) ??
throw new NotImplementedException();
}
}
7 changes: 5 additions & 2 deletions src/libs/OpenAI.Constants/OpenAI.Constants.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
<PropertyGroup Label="Nuget">
<Description>Static classes that provide actual constants(models/context lengts/prices) for the OpenAI API.
Available types in OpenAI.Constants namespace:
- AudioModel
- AudioPrices
- SpeechToTextModel
- SpeechToTextPrices
- TextToSpeechModel
- TextToSpeechPrices
- FineTuningPrices
- ChatModel
- ChatPrices
- EmbeddingModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace OpenAI.Constants;
/// <summary>
/// According https://platform.openai.com/docs/guides/speech-to-text
/// </summary>
public readonly record struct AudioModel(string Value)
public readonly record struct SpeechToTextModel(string Value)
{
/// <summary>
/// Transcribe audio into whatever language the audio is in. <br/>
Expand All @@ -12,7 +12,7 @@ public readonly record struct AudioModel(string Value)
internal const string Whisper1Value = "whisper-1";

/// <inheritdoc cref="Whisper1Value"/>
public static AudioModel Whisper1 { get; } = new(Whisper1Value);
public static SpeechToTextModel Whisper1 { get; } = new(Whisper1Value);

/// <inheritdoc/>
public override string ToString()
Expand All @@ -21,11 +21,11 @@ public override string ToString()
}

/// <summary>
/// Implicitly converts <see cref="AudioModel"/> to <see cref="string"/>.
/// Implicitly converts <see cref="SpeechToTextModel"/> to <see cref="string"/>.
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static implicit operator string(AudioModel model)
public static implicit operator string(SpeechToTextModel model)
{
return model.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public static class AudioPrices
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static double? TryGet(
AudioModel model,
SpeechToTextModel model,
int seconds)
{
var pricePerMinute = model.Value switch
{
AudioModel.Whisper1Value => 0.006,
SpeechToTextModel.Whisper1Value => 0.006,

_ => -1.0,
};
Expand All @@ -29,9 +29,9 @@ public static class AudioPrices
return seconds * pricePerMinute / 60.0;
}

/// <inheritdoc cref="TryGet(AudioModel, int)"/>
/// <inheritdoc cref="TryGet(SpeechToTextModel, int)"/>
public static double Get(
AudioModel model,
SpeechToTextModel model,
int seconds)
{
return TryGet(
Expand Down
41 changes: 41 additions & 0 deletions src/libs/OpenAI.Constants/TextToSpeech/TextToSpeechModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace OpenAI.Constants;

/// <summary>
/// According https://platform.openai.com/docs/guides/text-to-speech
/// </summary>
public readonly record struct TextToSpeechModel(string Value)
{
/// <summary>
/// Transcribe audio into whatever language the audio is in. <br/>
/// File uploads are currently limited to 25 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm. <br/>
/// </summary>
internal const string Tts1Value = "tts-1";

/// <summary>
/// Transcribe audio into whatever language the audio is in. <br/>
/// File uploads are currently limited to 25 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm. <br/>
/// </summary>
internal const string Tts1HdValue = "tts-1-hd";

/// <inheritdoc cref="Tts1Value"/>
public static TextToSpeechModel Tts1 { get; } = new(Tts1Value);

/// <inheritdoc cref="Tts1HdValue"/>
public static TextToSpeechModel Tts1Hd { get; } = new(Tts1HdValue);

/// <inheritdoc/>
public override string ToString()
{
return Value;
}

/// <summary>
/// Implicitly converts <see cref="TextToSpeechModel"/> to <see cref="string"/>.
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static implicit operator string(TextToSpeechModel model)
{
return model.Value;
}
}
Loading

0 comments on commit 42a53b3

Please sign in to comment.