Skip to content

Commit

Permalink
com.openai.unity 7.0.2 (#139)
Browse files Browse the repository at this point in the history
- Fixed missing Threads.Message.Content.ImageFile property.
- Marked OpenAI.Completions Obsolete
  • Loading branch information
StephenHodgson authored Dec 7, 2023
1 parent 4aa565c commit c892282
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 69 deletions.
68 changes: 36 additions & 32 deletions Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ The recommended installation method is though the unity package manager and [Ope
- [List Fine Tune Job Events](#list-fine-tune-job-events) :construction:
- [Embeddings](#embeddings)
- [Create Embedding](#create-embeddings)
- [Completions](#completions) :construction:
- [Streaming](#completion-streaming) :construction:
- [Moderations](#moderations)
- [Create Moderation](#create-moderation)
- ~~[Completions](#completions)~~ :warning: Deprecated
- ~~[Streaming](#completion-streaming)~~ :warning: Deprecated
- ~~[Edits](#edits)~~ :warning: Deprecated
- ~~[Create Edit](#create-edit)~~ :warning: Deprecated

Expand Down Expand Up @@ -894,7 +894,7 @@ var messages = new List<Message>
var chatRequest = new ChatRequest(messages);
var response = await api.ChatEndpoint.StreamCompletionAsync(chatRequest, partialResponse =>
{
Console.Write(choice.Delta.ToString());
Console.Write(partialResponse.FirstChoice.Delta.ToString());
});
var choice = response.FirstChoice;
Debug.Log($"[{choice.Index}] {choice.Message.Role}: {choice.Message} | Finish Reason: {choice.FinishReason}");
Expand Down Expand Up @@ -1320,8 +1320,38 @@ var response = await api.EmbeddingsEndpoint.CreateEmbeddingAsync("The food was d
Debug.Log(response);
```

### [Moderations](https://platform.openai.com/docs/api-reference/moderations)

Given a input text, outputs if the model classifies it as violating OpenAI's content policy.

Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation)

The Moderations API can be accessed via `OpenAIClient.ModerationsEndpoint`

#### [Create Moderation](https://platform.openai.com/docs/api-reference/moderations/create)

Classifies if text violates OpenAI's Content Policy.

```csharp
var api = new OpenAIClient();
var isViolation = await api.ModerationsEndpoint.GetModerationAsync("I want to kill them.");
Assert.IsTrue(isViolation);
```

Additionally you can also get the scores of a given input.

```csharp
var response = await OpenAIClient.ModerationsEndpoint.CreateModerationAsync(new ModerationsRequest("I love you"));
Assert.IsNotNull(response);
Debug.Log(response.Results?[0]?.Scores?.ToString());
```

---

### [Completions](https://platform.openai.com/docs/api-reference/completions)

> :warning: Deprecated, and soon to be removed.
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.

The Completions API is accessed via `OpenAIClient.CompletionsEndpoint`
Expand All @@ -1336,6 +1366,8 @@ Debug.Log(response);
#### Completion Streaming

> :warning: Deprecated, and soon to be removed.
Streaming allows you to get results are they are generated, which can help your application feel more responsive, especially on slow models like Davinci.

```csharp
Expand All @@ -1350,37 +1382,9 @@ await api.CompletionsEndpoint.StreamCompletionAsync(response =>
}, "My name is Roger and I am a principal software engineer at Salesforce. This is my resume:", maxTokens: 200, temperature: 0.5, presencePenalty: 0.1, frequencyPenalty: 0.1, model: Model.Davinci);
```

### [Moderations](https://platform.openai.com/docs/api-reference/moderations)

Given a input text, outputs if the model classifies it as violating OpenAI's content policy.

Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation)

The Moderations API can be accessed via `OpenAIClient.ModerationsEndpoint`

#### [Create Moderation](https://platform.openai.com/docs/api-reference/moderations/create)

Classifies if text violates OpenAI's Content Policy.

```csharp
var api = new OpenAIClient();
var isViolation = await api.ModerationsEndpoint.GetModerationAsync("I want to kill them.");
Assert.IsTrue(isViolation);
```

Additionally you can also get the scores of a given input.

```csharp
var response = await OpenAIClient.ModerationsEndpoint.CreateModerationAsync(new ModerationsRequest("I love you"));
Assert.IsNotNull(response);
Debug.Log(response.Results?[0]?.Scores?.ToString());
```

---

### [Edits](https://platform.openai.com/docs/api-reference/edits)

> Deprecated, and soon to be removed.
> :warning: Deprecated, and soon to be removed.
Given a prompt and an instruction, the model will return an edited version of the prompt.

Expand Down
3 changes: 2 additions & 1 deletion Runtime/Assistants/AssistantExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public static async Task<bool> DeleteFileAsync(this AssistantFileResponse file,
public static async Task<bool> DeleteFileAsync(this AssistantResponse assistant, string fileId, CancellationToken cancellationToken = default)
{
var isRemoved = await assistant.Client.AssistantsEndpoint.RemoveFileAsync(assistant.Id, fileId, cancellationToken);
return isRemoved && await assistant.Client.FilesEndpoint.DeleteFileAsync(fileId, cancellationToken);
if (!isRemoved) { return false; }
return await assistant.Client.FilesEndpoint.DeleteFileAsync(fileId, cancellationToken);
}

#endregion Files
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Common/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum ContentType
[EnumMember(Value = "text")]
Text,
[EnumMember(Value = "image_url")]
ImageUrl
ImageUrl,
[EnumMember(Value = "image_file")]
ImageFile
}
}
22 changes: 22 additions & 0 deletions Runtime/Common/ImageFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace OpenAI
{
[Preserve]
public sealed class ImageFile
{
[Preserve]
[JsonConstructor]
public ImageFile([JsonProperty("file_id")] string fileId)
{
FileId = fileId;
}

[Preserve]
[JsonProperty("file_id")]
public string FileId { get; private set; }
}
}
11 changes: 11 additions & 0 deletions Runtime/Common/ImageFile.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Runtime/Completions/Choice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System;
using UnityEngine.Scripting;

namespace OpenAI.Completions
Expand All @@ -9,6 +10,7 @@ namespace OpenAI.Completions
/// Represents a completion choice returned by the <see cref="CompletionsEndpoint"/>.
/// </summary>
[Preserve]
[Obsolete("Deprecated")]
public sealed class Choice
{
[Preserve]
Expand Down
1 change: 1 addition & 0 deletions Runtime/Completions/CompletionResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace OpenAI.Completions
/// <summary>
/// Represents a result from calling the <see cref="CompletionsEndpoint"/>.
/// </summary>
[Obsolete("Deprecated")]
public sealed class CompletionResponse : BaseResponse
{
public CompletionResponse()
Expand Down
1 change: 1 addition & 0 deletions Runtime/Completions/CompletionsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace OpenAI.Completions
/// (see the prompt library for inspiration).<br/>
/// <see href="https://platform.openai.com/docs/api-reference/completions"/>
/// </summary>
[Obsolete("Deprecated")]
public sealed class CompletionsEndpoint : OpenAIBaseEndpoint
{
/// <inheritdoc />
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Completions/LogProbabilities.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using UnityEngine.Scripting;

namespace OpenAI.Completions
Expand All @@ -10,6 +11,7 @@ namespace OpenAI.Completions
/// Object belonging to a <see cref="Choice"/>
/// </summary>
[Preserve]
[Obsolete("Deprecated")]
public sealed class LogProbabilities
{
[Preserve]
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Edits/Choice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace OpenAI.Edits
{
[Obsolete]
[Preserve]
[Obsolete("Deprecated")]
public sealed class Choice
{
[Preserve]
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Edits/EditRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace OpenAI.Edits
{
[Obsolete]
[Preserve]
[Obsolete("Deprecated")]
public sealed class EditRequest
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Edits/EditResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace OpenAI.Edits
{
[Obsolete]
[Preserve]
[Obsolete("Deprecated")]
public sealed class EditResponse : BaseResponse
{
[Preserve]
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Edits/EditsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace OpenAI.Edits
/// Given a prompt and an instruction, the model will return an edited version of the prompt.
/// <see href="https://platform.openai.com/docs/api-reference/edits"/>
/// </summary>
[Obsolete]
[Obsolete("Deprecated")]
public sealed class EditsEndpoint : OpenAIBaseEndpoint
{
internal EditsEndpoint(OpenAIClient client) : base(client) { }
Expand Down
9 changes: 5 additions & 4 deletions Runtime/OpenAIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public OpenAIClient(OpenAIAuthentication authentication = null, OpenAISettings s
: base(authentication ?? OpenAIAuthentication.Default, settings ?? OpenAISettings.Default)
{
ModelsEndpoint = new ModelsEndpoint(this);
CompletionsEndpoint = new CompletionsEndpoint(this);
ChatEndpoint = new ChatEndpoint(this);
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
CompletionsEndpoint = new CompletionsEndpoint(this);
EditsEndpoint = new EditsEndpoint(this);
#pragma warning restore CS0612 // Type or member is obsolete
#pragma warning restore CS0618 // Type or member is obsolete
ImagesEndPoint = new ImagesEndpoint(this);
EmbeddingsEndpoint = new EmbeddingsEndpoint(this);
AudioEndpoint = new AudioEndpoint(this);
Expand Down Expand Up @@ -136,6 +136,7 @@ protected override void ValidateAuthentication()
/// (see the prompt library for inspiration).<br/>
/// <see href="https://platform.openai.com/docs/api-reference/completions"/>
/// </summary>
[Obsolete("Deprecated")]
public CompletionsEndpoint CompletionsEndpoint { get; }

/// <summary>
Expand All @@ -148,7 +149,7 @@ protected override void ValidateAuthentication()
/// Given a prompt and an instruction, the model will return an edited version of the prompt.<br/>
/// <see href="https://platform.openai.com/docs/api-reference/edits"/>
/// </summary>
[Obsolete]
[Obsolete("Deprecated")]
public EditsEndpoint EditsEndpoint { get; }

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions Runtime/Threads/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public sealed class Content
public Content(
[JsonProperty("type")] ContentType contentType,
[JsonProperty("text")] TextContent text,
[JsonProperty("image_url")] ImageUrl imageUrl)
[JsonProperty("image_file")] ImageFile imageUrl)
{
Type = contentType;
Text = text;
ImageUrl = imageUrl;
ImageFile = imageUrl;
}

[Preserve]
Expand All @@ -30,15 +30,15 @@ public Content(
public TextContent Text { get; }

[Preserve]
[JsonProperty("image_url")]
public ImageUrl ImageUrl { get; }
[JsonProperty("image_file")]
public ImageFile ImageFile { get; }

[Preserve]
public override string ToString()
=> Type switch
{
ContentType.Text => Text.Value,
ContentType.ImageUrl => ImageUrl.Url,
ContentType.ImageFile => ImageFile.FileId,
_ => throw new ArgumentOutOfRangeException()
};
}
Expand Down
10 changes: 10 additions & 0 deletions Runtime/Threads/ThreadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ public static async Task<ListResponse<RunResponse>> ListRunsAsync(this ThreadRes
public static async Task<RunResponse> UpdateAsync(this RunResponse run, CancellationToken cancellationToken = default)
=> await run.Client.ThreadsEndpoint.RetrieveRunAsync(run.ThreadId, run.Id, cancellationToken);

/// <summary>
/// Retrieves a run.
/// </summary>
/// <param name="thread">The thread that was run.</param>
/// <param name="runId">The id of the run to retrieve.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="RunResponse"/>.</returns>
public static async Task<RunResponse> RetrieveRunAsync(this ThreadResponse thread, string runId, CancellationToken cancellationToken = default)
=> await thread.Client.ThreadsEndpoint.RetrieveRunAsync(thread.Id, runId, cancellationToken);

/// <summary>
/// Modifies a run.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions Tests/TestFixture_02_Completions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
using NUnit.Framework;
using OpenAI.Completions;
using OpenAI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

namespace OpenAI.Tests
{
[Obsolete]
internal class TestFixture_02_Completions : AbstractTestFixture
{
private const string CompletionPrompts = "One Two Three Four Five Six Seven Eight Nine One Two Three Four Five Six Seven Eight";

[Test]
//[Test]
public async Task Test_01_GetBasicCompletion()
{
Assert.IsNotNull(OpenAIClient.CompletionsEndpoint);
Expand All @@ -31,7 +33,7 @@ public async Task Test_01_GetBasicCompletion()
Debug.Log(result);
}

[Test]
//[Test]
public async Task Test_02_GetStreamingCompletion()
{
Assert.IsNotNull(OpenAIClient.CompletionsEndpoint);
Expand Down
Loading

0 comments on commit c892282

Please sign in to comment.