Skip to content

Commit

Permalink
com.openai.unity 2.1.0 (#23)
Browse files Browse the repository at this point in the history
- Closes #20 
- Closes #21 
- bumped version to 2.1.0
- Misc formatting and cleanup
  • Loading branch information
StephenHodgson authored Jan 28, 2023
1 parent ea3213b commit 212f66b
Show file tree
Hide file tree
Showing 57 changed files with 1,783 additions and 141 deletions.
1 change: 1 addition & 0 deletions Editor/OpenAI.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "OpenAI.Editor",
"rootNamespace": "OpenAI.Editor",
"references": [
"GUID:3248779d86bd31747b5d2214f30b01ac"
],
Expand Down
160 changes: 160 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Based on [OpenAI-DotNet](https://github.com/RageAgainstThePixel/OpenAI-DotNet)

A [OpenAI](https://openai.com/) package for the [Unity](https://unity.com/) Game Engine to use with GPT-3 API (currently in beta). Independently developed, this is not an official library and I am not affiliated with OpenAI. An OpenAI API account is required.

> This repository is available to transfer to the OpenAI organization if they so choose to accept it.
## Installing

### Via Unity Package Manager and OpenUPM
Expand Down Expand Up @@ -44,6 +46,7 @@ var api = new OpenAIClient();
- [Models](#models)
- [List Models](#list-models)
- [Retrieve Models](#retrieve-model)
- [Delete Fine Tuned Model](#delete-fine-tuned-model)
- [Completions](#completions)
- [Streaming](#streaming)
- [Edits](#edits)
Expand All @@ -54,6 +57,19 @@ var api = new OpenAIClient();
- [Create Image](#create-image)
- [Edit Image](#edit-image)
- [Create Image Variation](#create-image-variation)
- [Files](#files)
- [List Files](#list-files)
- [Upload File](#upload-file)
- [Delete File](#delete-file)
- [Retrieve File Info](#retrieve-file-info)
- [Download File Content](#download-file-content)
- [Fine Tuning](#fine-tuning)
- [Create Fine Tune Job](#create-fine-tune-job)
- [List Fine Tune Jobs](#list-fine-tune-jobs)
- [Retrieve Fine Tune Job Info](#retrieve-fine-tune-job-info)
- [Cancel Fine Tune Job](#cancel-fine-tune-job)
- [List Fine Tune Events](#list-fine-tune-events)
- [Stream Fine Tune Events](#stream-fine-tune-events)
- [Moderations](#moderations)
- [Create Moderation](#create-moderation)

Expand Down Expand Up @@ -150,6 +166,16 @@ var api = new OpenAIClient();
var model = await api.ModelsEndpoint.GetModelDetailsAsync("text-davinci-003");
```

#### [Delete Fine Tuned Model](https://beta.openai.com/docs/api-reference/fine-tunes/delete-model)

Delete a fine-tuned model. You must have the Owner role in your organization.

```csharp
var api = new OpenAIClient();
var result = await api.ModelsEndpoint.DeleteFineTuneModelAsync("your-fine-tuned-model");
// result == true
```

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

The Completion API is accessed via `OpenAIClient.CompletionsEndpoint`:
Expand Down Expand Up @@ -274,6 +300,140 @@ var results = await api.ImagesEndPoint.CreateImageVariationAsync(Path.GetFullPat
// result.Value == Texture2D
```

### [Files](https://beta.openai.com/docs/api-reference/files)

Files are used to upload documents that can be used with features like [Fine-tuning](#fine-tuning).

The Files API is accessed via `OpenAIClient.FilesEndpoint`.

#### [List Files](https://beta.openai.com/docs/api-reference/files/list)

Returns a list of files that belong to the user's organization.

```csharp
var api = new OpenAIClient();
var files = await api.FilesEndpoint.ListFilesAsync();

foreach (var file in result)
{
Debug.Log($"{file.Id} -> {file.Object}: {file.FileName} | {file.Size} bytes");
}
```

#### [Upload File](https://beta.openai.com/docs/api-reference/files/upload)

Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.

```csharp
var api = new OpenAIClient();
var fileData = await api.FilesEndpoint.UploadFileAsync("path/to/your/file.jsonl", "fine-tune");
```

#### [Delete File](https://beta.openai.com/docs/api-reference/files/delete)

Delete a file.

```csharp
var api = new OpenAIClient();
var result = await api.FilesEndpoint.DeleteFileAsync(fileData);
// result == true
```

#### [Retrieve File Info](https://beta.openai.com/docs/api-reference/files/retrieve)

Returns information about a specific file.

```csharp
var api = new OpenAIClient();
var fileData = await GetFileInfoAsync(fileId);
```

#### [Download File Content](https://beta.openai.com/docs/api-reference/files/retrieve-content)

Downloads the specified file.

```csharp
var api = new OpenAIClient();
var downloadedFilePath = await api.FilesEndpoint.DownloadFileAsync(fileId, "path/to/your/save/directory");
```

### [Fine Tuning](https://beta.openai.com/docs/api-reference/fine-tunes)

Manage fine-tuning jobs to tailor a model to your specific training data.

Related guide: [Fine-tune models](https://beta.openai.com/docs/guides/fine-tuning)

The Files API is accessed via `OpenAIClient.FineTuningEndpoint`.

#### [Create Fine Tune Job](https://beta.openai.com/docs/api-reference/fine-tunes/create)

Creates a job that fine-tunes a specified model from a given dataset.

Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.

```csharp
var api = new OpenAIClient();
var request = new CreateFineTuneRequest(fileData);
var fineTuneResponse = await api.FineTuningEndpoint.CreateFineTuneAsync(request);
```

#### [List Fine Tune Jobs](https://beta.openai.com/docs/api-reference/fine-tunes/list)

List your organization's fine-tuning jobs.

```csharp
var api = new OpenAIClient();
var fineTuneJobs = await api.FineTuningEndpoint.ListFineTuneJobsAsync();
```

#### [Retrieve Fine Tune Job Info](https://beta.openai.com/docs/api-reference/fine-tunes/retrieve)

Gets info about the fine-tune job.

```csharp
var api = new OpenAIClient();
var request = await api.FineTuningEndpoint.RetrieveFineTuneJobInfoAsync(fineTuneJob);
```

#### [Cancel Fine Tune Job](https://beta.openai.com/docs/api-reference/fine-tunes/cancel)

Immediately cancel a fine-tune job.

```csharp
var api = new OpenAIClient();
var result = await api.FineTuningEndpoint.CancelFineTuneJob(job);
// result = true
```

#### [List Fine Tune Events](https://beta.openai.com/docs/api-reference/fine-tunes/events)

Get fine-grained status updates for a fine-tune job.

```csharp
var api = new OpenAIClient();
var fineTuneEvents = await api.FineTuningEndpoint.ListFineTuneEventsAsync(fineTuneJob);
```

#### [Stream Fine Tune Events](https://beta.openai.com/docs/api-reference/fine-tunes/events#fine-tunes/events-stream)

```csharp
var api = new OpenAIClient();
await api.FineTuningEndpoint.StreamFineTuneEventsAsync(fineTuneJob, fineTuneEvent =>
{
Debug.Log($" {fineTuneEvent.CreatedAt} [{fineTuneEvent.Level}] {fineTuneEvent.Message}");
});
```

Or if using [`IAsyncEnumerable{T}`](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1?view=net-5.0) ([C# 8.0+](https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8))

```csharp
var api = new OpenAIClient();
await foreach (var fineTuneEvent in api.FineTuningEndpoint.StreamFineTuneEventsEnumerableAsync(fineTuneJob))
{
Debug.Log($" {fineTuneEvent.CreatedAt} [{fineTuneEvent.Level}] {fineTuneEvent.Message}");
}
```

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

Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
Expand Down
2 changes: 1 addition & 1 deletion Runtime/AuthInfo.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/CompletionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public sealed class CompletionResult : BaseResponse
[JsonProperty("created")]
public int CreatedUnixTime { get; set; }

/// <summary>
/// The time when the result was generated
/// </summary>
[JsonIgnore]
public DateTime Created => DateTimeOffset.FromUnixTimeSeconds(CreatedUnixTime).DateTime;

Expand Down
13 changes: 6 additions & 7 deletions Runtime/Completions/CompletionsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public async Task<CompletionResult> CreateCompletionAsync(
int? logProbabilities = null,
bool? echo = null,
string[] stopSequences = null,
Model model = null
)
Model model = null)
{
var request = new CompletionRequest(
model ?? Api.DefaultModel,
Expand Down Expand Up @@ -200,7 +199,7 @@ public async Task StreamCompletionAsync(CompletionRequest completionRequest, Act
{
completionRequest.Stream = true;
var jsonContent = JsonConvert.SerializeObject(completionRequest, Api.JsonSerializationOptions);
var request = new HttpRequestMessage(HttpMethod.Post, GetEndpoint())
using var request = new HttpRequestMessage(HttpMethod.Post, GetEndpoint())
{
Content = jsonContent.ToJsonStringContent()
};
Expand All @@ -215,7 +214,7 @@ public async Task StreamCompletionAsync(CompletionRequest completionRequest, Act
{
if (line.StartsWith("data: "))
{
line = line[5..].Trim();
line = line["data: ".Length..];
}

if (line == "[DONE]")
Expand All @@ -225,7 +224,7 @@ public async Task StreamCompletionAsync(CompletionRequest completionRequest, Act

if (!string.IsNullOrWhiteSpace(line))
{
resultHandler(DeserializeResult(response, line));
resultHandler(DeserializeResult(response, line.Trim()));
}
}
}
Expand Down Expand Up @@ -317,7 +316,7 @@ public async IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(
{
completionRequest.Stream = true;
var jsonContent = JsonConvert.SerializeObject(completionRequest, Api.JsonSerializationOptions);
var request = new HttpRequestMessage(HttpMethod.Post, GetEndpoint())
using var request = new HttpRequestMessage(HttpMethod.Post, GetEndpoint())
{
Content = jsonContent.ToJsonStringContent()
};
Expand All @@ -332,7 +331,7 @@ public async IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(
{
if (line.StartsWith("data: "))
{
line = line[5..].Trim();
line = line["data: ".Length..];
}

if (line == "[DONE]")
Expand Down
18 changes: 18 additions & 0 deletions Runtime/Edits/EditRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ namespace OpenAI.Edits
{
public sealed class EditRequest
{
/// <summary>
/// Creates a new edit request for the provided input, instruction, and parameters.
/// </summary>
/// <param name="input">The input text to use as a starting point for the edit.</param>
/// <param name="instruction">The instruction that tells the model how to edit the prompt.</param>
/// <param name="editCount">How many edits to generate for the input and instruction.</param>
/// <param name="temperature">
/// What sampling temperature to use. Higher values means the model will take more risks.
/// Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.
/// We generally recommend altering this or top_p but not both.
/// </param>
/// <param name="topP">
/// An alternative to sampling with temperature, called nucleus sampling, where the model considers the
/// results of the tokens with top_p probability mass.
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
/// We generally recommend altering this or temperature but not both.
/// </param>
/// <param name="model">ID of the model to use. Defaults to text-davinci-edit-001.</param>
public EditRequest(
string input,
string instruction,
Expand Down
18 changes: 0 additions & 18 deletions Runtime/Edits/Usage.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Runtime/Embeddings/EmbeddingsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public EmbeddingsRequest(string input, Model model = null, string user = null)
}

[JsonProperty("input")]
public string Input { get; set; }
public string Input { get; }

[JsonProperty("model")]
public string Model { get; set; }
public string Model { get; }

[JsonProperty("user")]
public string User { get; set; }
public string User { get; }
}
}
15 changes: 0 additions & 15 deletions Runtime/Embeddings/Usage.cs

This file was deleted.

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

using Newtonsoft.Json;
using System;

namespace OpenAI
{
public sealed class Event
{
[JsonConstructor]
public Event(
string @object,
int createdAtUnixTime,
string level,
string message
)
{
Object = @object;
CreatedAtUnixTime = createdAtUnixTime;
Level = level;
Message = message;
}

[JsonProperty("object")]
public string Object { get; }

[JsonProperty("created_at")]
public int CreatedAtUnixTime { get; }

[JsonIgnore]
public DateTime CreatedAt => DateTimeOffset.FromUnixTimeSeconds(CreatedAtUnixTime).DateTime;

[JsonProperty("level")]
public string Level { get; }

[JsonProperty("message")]
public string Message { get; }
}
}
2 changes: 1 addition & 1 deletion Runtime/Embeddings/Usage.cs.meta → Runtime/Event.cs.meta

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

Loading

0 comments on commit 212f66b

Please sign in to comment.