Skip to content

Support JSON response: CreateChatCompletionRequest class supports response_format field #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Runtime/DataTypes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace OpenAI
{
Expand Down Expand Up @@ -83,6 +84,13 @@ public class OpenAIModelResponse : OpenAIModel, IResponse
#endregion

#region Chat API Data Types

public enum ResponseFormat
{
Text,
JsonObject
}

public sealed class CreateChatCompletionRequest
{
public string Model { get; set; }
Expand All @@ -97,6 +105,40 @@ public sealed class CreateChatCompletionRequest
public Dictionary<string, string> LogitBias { get; set; }
public string User { get; set; }
public string SystemFingerprint { get; set; }

[JsonConverter(typeof(ResponseFormatJsonConverter))]
public ResponseFormat? ResponseFormat { get; set; }
}

public class ResponseFormatJsonConverter : JsonConverter<ResponseFormat>
{
public override void WriteJson(JsonWriter writer, ResponseFormat value, JsonSerializer serializer)
{
if (value == ResponseFormat.JsonObject)
{
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue("json_object");
writer.WriteEndObject();
} else
{
writer.WriteNull();
}
}

public override ResponseFormat ReadJson(JsonReader reader, System.Type objectType, ResponseFormat existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject)
{
JObject obj = JObject.Load(reader);
if (obj.TryGetValue("type", out JToken typeToken) && typeToken.ToString() == "json_object")
{
return ResponseFormat.JsonObject;
}
}

return ResponseFormat.Text;
}
}

public struct CreateChatCompletionResponse : IResponse
Expand Down
58 changes: 32 additions & 26 deletions Runtime/OpenAIApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ private Configuration Configuration
}
}

private const string OFFICIAL_BASE_PATH = "https://api.openai.com/v1";

/// OpenAI API base path for requests.
private const string BASE_PATH = "https://api.openai.com/v1";

public OpenAIApi(string apiKey = null, string organization = null)
private readonly string _basePath;
public OpenAIApi(string apiKey = null, string organization = null, string baseUrl = null)
{
if (string.IsNullOrEmpty(baseUrl))
_basePath = OFFICIAL_BASE_PATH;
else
_basePath = baseUrl;

if (apiKey != null)
{
configuration = new Configuration(apiKey, organization);
Expand Down Expand Up @@ -202,7 +209,7 @@ private byte[] CreatePayload<T>(T request)
/// </summary>
public async Task<ListModelsResponse> ListModels()
{
var path = $"{BASE_PATH}/models";
var path = $"{_basePath}/models";
return await DispatchRequest<ListModelsResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -213,7 +220,7 @@ public async Task<ListModelsResponse> ListModels()
/// <returns>See <see cref="Model"/></returns>
public async Task<OpenAIModel> RetrieveModel(string id)
{
var path = $"{BASE_PATH}/models/{id}";
var path = $"{_basePath}/models/{id}";
return await DispatchRequest<OpenAIModelResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -224,9 +231,8 @@ public async Task<OpenAIModel> RetrieveModel(string id)
/// <returns>See <see cref="CreateChatCompletionResponse"/></returns>
public async Task<CreateChatCompletionResponse> CreateChatCompletion(CreateChatCompletionRequest request)
{
var path = $"{BASE_PATH}/chat/completions";
var path = $"{_basePath}/chat/completions";
var payload = CreatePayload(request);

return await DispatchRequest<CreateChatCompletionResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
}

Expand All @@ -240,7 +246,7 @@ public async Task<CreateChatCompletionResponse> CreateChatCompletion(CreateChatC
public void CreateChatCompletionAsync(CreateChatCompletionRequest request, Action<List<CreateChatCompletionResponse>> onResponse, Action onComplete, CancellationTokenSource token)
{
request.Stream = true;
var path = $"{BASE_PATH}/chat/completions";
var path = $"{_basePath}/chat/completions";
var payload = CreatePayload(request);

DispatchRequest(path, UnityWebRequest.kHttpVerbPOST, onResponse, onComplete, token, payload);
Expand All @@ -253,7 +259,7 @@ public void CreateChatCompletionAsync(CreateChatCompletionRequest request, Actio
/// <returns>See <see cref="CreateImageResponse"/></returns>
public async Task<CreateImageResponse> CreateImage(CreateImageRequest request)
{
var path = $"{BASE_PATH}/images/generations";
var path = $"{_basePath}/images/generations";
var payload = CreatePayload(request);
return await DispatchRequest<CreateImageResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
}
Expand All @@ -265,7 +271,7 @@ public async Task<CreateImageResponse> CreateImage(CreateImageRequest request)
/// <returns>See <see cref="CreateImageResponse"/></returns>
public async Task<CreateImageResponse> CreateImageEdit(CreateImageEditRequest request)
{
var path = $"{BASE_PATH}/images/edits";
var path = $"{_basePath}/images/edits";

var form = new List<IMultipartFormSection>();
form.AddFile(request.Image, "image", "image/png");
Expand All @@ -285,7 +291,7 @@ public async Task<CreateImageResponse> CreateImageEdit(CreateImageEditRequest re
/// <returns>See <see cref="CreateImageResponse"/></returns>
public async Task<CreateImageResponse> CreateImageVariation(CreateImageVariationRequest request)
{
var path = $"{BASE_PATH}/images/variations";
var path = $"{_basePath}/images/variations";

var form = new List<IMultipartFormSection>();
form.AddFile(request.Image, "image", "image/png");
Expand All @@ -304,7 +310,7 @@ public async Task<CreateImageResponse> CreateImageVariation(CreateImageVariation
/// <returns>See <see cref="CreateEmbeddingsResponse"/></returns>
public async Task<CreateEmbeddingsResponse> CreateEmbeddings(CreateEmbeddingsRequest request)
{
var path = $"{BASE_PATH}/embeddings";
var path = $"{_basePath}/embeddings";
var payload = CreatePayload(request);
return await DispatchRequest<CreateEmbeddingsResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
}
Expand All @@ -316,7 +322,7 @@ public async Task<CreateEmbeddingsResponse> CreateEmbeddings(CreateEmbeddingsReq
/// <returns>See <see cref="CreateAudioResponse"/></returns>
public async Task<CreateAudioResponse> CreateAudioTranscription(CreateAudioTranscriptionsRequest request)
{
var path = $"{BASE_PATH}/audio/transcriptions";
var path = $"{_basePath}/audio/transcriptions";

var form = new List<IMultipartFormSection>();
if (string.IsNullOrEmpty(request.File))
Expand All @@ -343,7 +349,7 @@ public async Task<CreateAudioResponse> CreateAudioTranscription(CreateAudioTrans
/// <returns>See <see cref="CreateAudioResponse"/></returns>
public async Task<CreateAudioResponse> CreateAudioTranslation(CreateAudioTranslationRequest request)
{
var path = $"{BASE_PATH}/audio/translations";
var path = $"{_basePath}/audio/translations";

var form = new List<IMultipartFormSection>();
if (string.IsNullOrEmpty(request.File))
Expand All @@ -368,7 +374,7 @@ public async Task<CreateAudioResponse> CreateAudioTranslation(CreateAudioTransla
/// <returns>See <see cref="ListFilesResponse"/></returns>
public async Task<ListFilesResponse> ListFiles()
{
var path = $"{BASE_PATH}/files";
var path = $"{_basePath}/files";
return await DispatchRequest<ListFilesResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -381,7 +387,7 @@ public async Task<ListFilesResponse> ListFiles()
/// <returns>See <see cref="OpenAIFile"/></returns>
public async Task<OpenAIFile> CreateFile(CreateFileRequest request)
{
var path = $"{BASE_PATH}/files";
var path = $"{_basePath}/files";

var form = new List<IMultipartFormSection>();
form.AddFile(request.File, "file", "application/json");
Expand All @@ -397,7 +403,7 @@ public async Task<OpenAIFile> CreateFile(CreateFileRequest request)
/// <returns>See <see cref="DeleteResponse"/></returns>
public async Task<DeleteResponse> DeleteFile(string id)
{
var path = $"{BASE_PATH}/files/{id}";
var path = $"{_basePath}/files/{id}";
return await DispatchRequest<DeleteResponse>(path, UnityWebRequest.kHttpVerbDELETE);
}

Expand All @@ -408,7 +414,7 @@ public async Task<DeleteResponse> DeleteFile(string id)
/// <returns>See <see cref="OpenAIFile"/></returns>
public async Task<OpenAIFile> RetrieveFile(string id)
{
var path = $"{BASE_PATH}/files/{id}";
var path = $"{_basePath}/files/{id}";
return await DispatchRequest<OpenAIFileResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -419,7 +425,7 @@ public async Task<OpenAIFile> RetrieveFile(string id)
/// <returns>See <see cref="OpenAIFile"/></returns>
public async Task<OpenAIFile> DownloadFile(string id)
{
var path = $"{BASE_PATH}/files/{id}/content";
var path = $"{_basePath}/files/{id}/content";
return await DispatchRequest<OpenAIFileResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -431,7 +437,7 @@ public async Task<OpenAIFile> DownloadFile(string id)
/// <returns>See <see cref="FineTune"/></returns>
public async Task<FineTune> CreateFineTune(CreateFineTuneRequest request)
{
var path = $"{BASE_PATH}/fine-tunes";
var path = $"{_basePath}/fine-tunes";
var payload = CreatePayload(request);
return await DispatchRequest<FineTuneResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
}
Expand All @@ -442,7 +448,7 @@ public async Task<FineTune> CreateFineTune(CreateFineTuneRequest request)
/// <returns>See <see cref="ListFineTunesResponse"/></returns>
public async Task<ListFineTunesResponse> ListFineTunes()
{
var path = $"{BASE_PATH}/fine-tunes";
var path = $"{_basePath}/fine-tunes";
return await DispatchRequest<ListFineTunesResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -453,7 +459,7 @@ public async Task<ListFineTunesResponse> ListFineTunes()
/// <returns>See <see cref="FineTune"/></returns>
public async Task<FineTune> RetrieveFineTune(string id)
{
var path = $"{BASE_PATH}/fine-tunes/{id}";
var path = $"{_basePath}/fine-tunes/{id}";
return await DispatchRequest<FineTuneResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -464,7 +470,7 @@ public async Task<FineTune> RetrieveFineTune(string id)
/// <returns>See <see cref="FineTune"/></returns>
public async Task<FineTune> CancelFineTune(string id)
{
var path = $"{BASE_PATH}/fine-tunes/{id}/cancel";
var path = $"{_basePath}/fine-tunes/{id}/cancel";
return await DispatchRequest<FineTuneResponse>(path, UnityWebRequest.kHttpVerbPOST);
}

Expand All @@ -479,7 +485,7 @@ public async Task<FineTune> CancelFineTune(string id)
/// <returns>See <see cref="ListFineTuneEventsResponse"/></returns>
public async Task<ListFineTuneEventsResponse> ListFineTuneEvents(string id, bool stream = false)
{
var path = $"{BASE_PATH}/fine-tunes/{id}/events?stream={stream}";
var path = $"{_basePath}/fine-tunes/{id}/events?stream={stream}";
return await DispatchRequest<ListFineTuneEventsResponse>(path, UnityWebRequest.kHttpVerbGET);
}

Expand All @@ -490,7 +496,7 @@ public async Task<ListFineTuneEventsResponse> ListFineTuneEvents(string id, bool
/// <returns>See <see cref="DeleteResponse"/></returns>
public async Task<DeleteResponse> DeleteFineTunedModel(string model)
{
var path = $"{BASE_PATH}/models/{model}";
var path = $"{_basePath}/models/{model}";
return await DispatchRequest<DeleteResponse>(path, UnityWebRequest.kHttpVerbDELETE);
}

Expand All @@ -501,7 +507,7 @@ public async Task<DeleteResponse> DeleteFineTunedModel(string model)
/// <returns>See <see cref="CreateModerationResponse"/></returns>
public async Task<CreateModerationResponse> CreateModeration(CreateModerationRequest request)
{
var path = $"{BASE_PATH}/moderations";
var path = $"{_basePath}/moderations";
var payload = CreatePayload(request);
return await DispatchRequest<CreateModerationResponse>(path, UnityWebRequest.kHttpVerbPOST, payload);
}
Expand Down