Skip to content

Commit

Permalink
OpenAI-DotNet 5.0.2 (RageAgainstThePixel#36)
Browse files Browse the repository at this point in the history
- Added support for multiple inputs in `EmbeddingsRequest`
- Added better model validation in all endpoints
- Added missing `LogitBias` to chat request

---------

Co-authored-by: ZeekoZhu <[email protected]>
  • Loading branch information
StephenHodgson and ZeekoZhu committed Mar 6, 2023
1 parent b8cadb9 commit 3bf166f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 17 deletions.
9 changes: 5 additions & 4 deletions OpenAI-DotNet-Tests/TestFixture_03_Chat.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using NUnit.Framework;
using OpenAI.Chat;
using OpenAI.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenAI.Chat;

namespace OpenAI.Tests
{
Expand All @@ -20,7 +21,7 @@ public async Task Test_1_GetChatCompletion()
new ChatPrompt("assistant", "The Los Angeles Dodgers won the World Series in 2020."),
new ChatPrompt("user", "Where was it played?"),
};
var chatRequest = new ChatRequest(chatPrompts);
var chatRequest = new ChatRequest(chatPrompts, Model.GPT3_5_Turbo);
var result = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
Assert.IsNotNull(result);
Assert.NotNull(result.Choices);
Expand Down
19 changes: 17 additions & 2 deletions OpenAI-DotNet-Tests/TestFixture_05_Embeddings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework;
using System.Threading.Tasks;

namespace OpenAI.Tests
{
Expand All @@ -14,5 +14,20 @@ public async Task Test_1_CreateEmbedding()
Assert.IsNotNull(result);
Assert.IsNotEmpty(result.Data);
}

[Test]
public async Task Test_2_CreateEmbeddingsWithMultipleInputs()
{
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Assert.IsNotNull(api.EmbeddingsEndpoint);
var embeddings = new[]
{
"The food was delicious and the waiter...",
"The food was terrible and the waiter..."
};
var result = await api.EmbeddingsEndpoint.CreateEmbeddingAsync(embeddings);
Assert.IsNotNull(result);
Assert.AreEqual(result.Data.Count, 2);
}
}
}
14 changes: 11 additions & 3 deletions OpenAI-DotNet/Chat/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,32 @@ public ChatRequest(
int? maxTokens = null,
double? presencePenalty = null,
double? frequencyPenalty = null,
Dictionary<string, double> logitBias = null,
string user = null)
{
const string defaultModel = "gpt-3.5-turbo";
Model = model ?? new Model(defaultModel);
Model = model ?? Models.Model.GPT3_5_Turbo;

if (!Model.Contains(defaultModel))
{
throw new ArgumentException(nameof(model), $"{Model} not supported");
}

Messages = messages.ToList();
Messages = messages?.ToList();

if (Messages?.Count == 0)
{
throw new ArgumentNullException(nameof(messages), $"Missing required {nameof(messages)} parameter");
}

Temperature = temperature;
TopP = topP;
Number = number;
Stops = stops;
MaxTokens = maxTokens;
PresencePenalty = presencePenalty;
FrequencyPenalty = frequencyPenalty;
LogitBias = logitBias;
User = user;
}

Expand Down Expand Up @@ -127,7 +135,7 @@ public ChatRequest(
/// Defaults to null
/// </summary>
[JsonPropertyName("logit_bias")]
public Dictionary<string, double> LogitBias { get; set; }
public IReadOnlyDictionary<string, double> LogitBias { get; set; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
Expand Down
9 changes: 8 additions & 1 deletion OpenAI-DotNet/Edits/EditRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System;
using System.Text.Json.Serialization;
using OpenAI.Models;

namespace OpenAI.Edits
Expand Down Expand Up @@ -32,6 +33,12 @@ public EditRequest(
Model model = null)
{
Model = model ?? new Model("text-davinci-edit-001");

if (!Model.Contains("-edit-"))
{
throw new ArgumentException(nameof(model), $"{Model} does not support editing");
}

Input = input;
Instruction = instruction;
EditCount = editCount;
Expand Down
20 changes: 20 additions & 0 deletions OpenAI-DotNet/Embeddings/EmbeddingsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenAI.Models;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -37,6 +38,25 @@ protected override string GetEndpoint()
public async Task<EmbeddingsResponse> CreateEmbeddingAsync(string input, Model model = null, string user = null)
=> await CreateEmbeddingAsync(new EmbeddingsRequest(input, model, user)).ConfigureAwait(false);

/// <summary>
/// Creates an embedding vector representing the input text.
/// </summary>
/// <param name="input">
/// Input text to get embeddings for, encoded as a string or array of tokens.
/// To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays.
/// Each input must not exceed 8192 tokens in length.
/// </param>
/// <param name="model">
/// ID of the model to use.
/// Defaults to: text-embedding-ada-002
/// </param>
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
/// <returns><see cref="EmbeddingsResponse"/></returns>
public async Task<EmbeddingsResponse> CreateEmbeddingAsync(IEnumerable<string> input, Model model = null, string user = null)
=> await CreateEmbeddingAsync(new EmbeddingsRequest(input, model, user)).ConfigureAwait(false);

/// <summary>
/// Creates an embedding vector representing the input text.
/// </summary>
Expand Down
38 changes: 36 additions & 2 deletions OpenAI-DotNet/Embeddings/EmbeddingsRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using OpenAI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

namespace OpenAI.Embeddings
Expand All @@ -23,19 +25,51 @@ public sealed class EmbeddingsRequest
/// </param>
/// <exception cref="ArgumentNullException">A valid <see cref="input"/> string is a Required parameter.</exception>
public EmbeddingsRequest(string input, Model model = null, string user = null)
: this(new List<string> { input }, model, user)
{
if (string.IsNullOrWhiteSpace(input))
{
throw new ArgumentNullException(nameof(input));
}
}

/// <summary>
/// Constructor.
/// </summary>
/// <param name="input">
/// Input text to get embeddings for, encoded as a string or array of tokens.
/// To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays.
/// Each input must not exceed 8192 tokens in length.
/// </param>
/// <param name="model">
/// The <see cref="OpenAI.Models.Model"/> to use.
/// Defaults to: text-embedding-ada-002
/// </param>
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
/// <exception cref="ArgumentNullException">A valid <see cref="input"/> string is a Required parameter.</exception>
public EmbeddingsRequest(IEnumerable<string> input, Model model = null, string user = null)
{
Input = input?.ToList();

if (Input?.Count == 0)
{
throw new ArgumentNullException(nameof(input), $"Missing required {nameof(input)} parameter");
}

Input = input;
Model = model ?? new Model("text-embedding-ada-002");

if (!Model.Contains("text-embedding"))
{
throw new ArgumentException(nameof(model), $"{Model} is not supported for embedding.");
}

User = user;
}

[JsonPropertyName("input")]
public string Input { get; }
public IReadOnlyList<string> Input { get; }

[JsonPropertyName("model")]
public string Model { get; }
Expand Down
8 changes: 7 additions & 1 deletion OpenAI-DotNet/Moderations/ModerationsRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenAI.Models;
using System;
using OpenAI.Models;
using System.Text.Json.Serialization;

namespace OpenAI.Moderations
Expand All @@ -24,6 +25,11 @@ public ModerationsRequest(string input, Model model = null)
{
Input = input;
Model = model ?? new Model("text-moderation-latest");

if (!Model.Contains("text-moderation"))
{
throw new ArgumentException(nameof(model), $"{Model} is not supported.");
}
}

[JsonPropertyName("input")]
Expand Down
11 changes: 7 additions & 4 deletions OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ Based on OpenAI-API by OKGoDoIt (Roger Pincombe)</Description>
<RepositoryUrl>https://github.com/RageAgainstThePixel/OpenAI-DotNet</RepositoryUrl>
<PackageTags>OpenAI, AI, ML, API, gpt, gpt-3, chatGPT</PackageTags>
<Title>OpenAI API</Title>
<PackageReleaseNotes>Bump version to 5.0.1
<PackageReleaseNotes>Bump version to 5.0.2
- Support multiple inputs in embedding
- Added better model validation in all endpoints
Bump version to 5.0.1
- Fixed chat parameters
Bump version to 5.0.0
- Added Chat endpoint
Expand Down Expand Up @@ -46,9 +49,9 @@ Bump version to 4.4.0
<AssemblyOriginatorKeyFile>OpenAI-DotNet.pfx</AssemblyOriginatorKeyFile>
<DelaySign>true</DelaySign>
<PackageId>OpenAI-DotNet</PackageId>
<Version>5.0.1</Version>
<AssemblyVersion>5.0.1.0</AssemblyVersion>
<FileVersion>5.0.1.0</FileVersion>
<Version>5.0.2</Version>
<AssemblyVersion>5.0.2.0</AssemblyVersion>
<FileVersion>5.0.2.0</FileVersion>
<Company>RageAgainstThePixel</Company>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down

0 comments on commit 3bf166f

Please sign in to comment.