Skip to content

Commit

Permalink
com.openai.unity 5.0.10 (#108)
Browse files Browse the repository at this point in the history
- added Preserve to all json serialized objects to fix overzealous Unity code stripping
- fixed processing time string culture conversion when parsing double
- updated deps
  • Loading branch information
StephenHodgson authored Oct 7, 2023
1 parent 95d1196 commit be89340
Show file tree
Hide file tree
Showing 44 changed files with 486 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ public sealed class AudioEndpoint : OpenAIBaseEndpoint
[Preserve]
private class AudioResponse
{
[Preserve]
[JsonConstructor]
public AudioResponse([JsonProperty("text")] string text)
{
Text = text;
}

[Preserve]
[JsonProperty("text")]
public string Text { get; }
}
Expand Down
17 changes: 17 additions & 0 deletions OpenAI/Packages/com.openai.unity/Runtime/Chat/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Scripting;

namespace OpenAI.Chat
{
[Preserve]
public sealed class ChatRequest
{
/// <summary>
Expand Down Expand Up @@ -75,6 +77,7 @@ public sealed class ChatRequest
/// <param name="functions">
/// An optional list of functions to get arguments for. Null or empty for none.
/// </param>
[Preserve]
public ChatRequest(
IEnumerable<Message> messages,
string model = null,
Expand Down Expand Up @@ -144,12 +147,14 @@ public ChatRequest(
/// ID of the model to use.<br/>
/// Currently, only gpt-4, gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported.
/// </summary>
[Preserve]
[JsonProperty("model")]
public string Model { get; }

/// <summary>
/// The messages to generate chat completions for, in the chat format.
/// </summary>
[Preserve]
[JsonProperty("messages")]
public IReadOnlyList<Message> Messages { get; }

Expand All @@ -160,6 +165,7 @@ public ChatRequest(
/// We generally recommend altering this or top_p but not both.<br/>
/// Defaults to 1
/// </summary>
[Preserve]
[JsonProperty("temperature")]
public double? Temperature { get; }

Expand All @@ -170,13 +176,15 @@ public ChatRequest(
/// We generally recommend altering this or temperature but not both.<br/>
/// Defaults to 1
/// </summary>
[Preserve]
[JsonProperty("top_p")]
public double? TopP { get; }

/// <summary>
/// How many chat completion choices to generate for each input message.<br/>
/// Defaults to 1
/// </summary>
[Preserve]
[JsonProperty("n")]
public int? Number { get; }

Expand All @@ -185,19 +193,22 @@ public ChatRequest(
/// Do not set this yourself, use the appropriate methods on <see cref="ChatEndpoint"/> instead.<br/>
/// Defaults to false
/// </summary>
[Preserve]
[JsonProperty("stream")]
public bool Stream { get; internal set; }

/// <summary>
/// Up to 4 sequences where the API will stop generating further tokens.
/// </summary>
[Preserve]
[JsonProperty("stop")]
public string[] Stops { get; }

/// <summary>
/// The maximum number of tokens allowed for the generated answer.
/// By default, the number of tokens the model can return will be (4096 - prompt tokens).
/// </summary>
[Preserve]
[JsonProperty("max_tokens")]
public int? MaxTokens { get; }

Expand All @@ -207,6 +218,7 @@ public ChatRequest(
/// increasing the model's likelihood to talk about new topics.<br/>
/// Defaults to 0
/// </summary>
[Preserve]
[JsonProperty("presence_penalty")]
public double? PresencePenalty { get; }

Expand All @@ -216,6 +228,7 @@ public ChatRequest(
/// decreasing the model's likelihood to repeat the same line verbatim.<br/>
/// Defaults to 0
/// </summary>
[Preserve]
[JsonProperty("frequency_penalty")]
public double? FrequencyPenalty { get; }

Expand All @@ -228,24 +241,28 @@ public ChatRequest(
/// in a ban or exclusive selection of the relevant token.<br/>
/// Defaults to null
/// </summary>
[Preserve]
[JsonProperty("logit_bias")]
public IReadOnlyDictionary<string, double> LogitBias { get; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
[Preserve]
[JsonProperty("user")]
public string User { get; }

/// <summary>
/// Pass "auto" to let the OpenAI service decide, "none" if none are to be called, or "functionName" to force function call. Defaults to "auto".
/// </summary>
[Preserve]
[JsonProperty("function_call")]
public dynamic FunctionCall { get; }

/// <summary>
/// An optional list of functions to get arguments for.
/// </summary>
[Preserve]
[JsonProperty("functions")]
public IReadOnlyList<Function> Functions { get; }

Expand Down
13 changes: 13 additions & 0 deletions OpenAI/Packages/com.openai.unity/Runtime/Chat/ChatResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Scripting;

namespace OpenAI.Chat
{
[Preserve]
public sealed class ChatResponse : BaseResponse
{
[Preserve]
internal ChatResponse(ChatResponse other) => CopyFrom(other);

[Preserve]
[JsonConstructor]
public ChatResponse(
[JsonProperty("id")] string id,
Expand All @@ -28,34 +32,43 @@ public ChatResponse(
this.choices = choices;
}

[Preserve]
[JsonProperty("id")]
public string Id { get; internal set; }

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

[Preserve]
[JsonProperty("created")]
public int Created { get; internal set; }

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

[Preserve]
[JsonProperty("usage")]
public Usage Usage { get; internal set; }

[Preserve]
[JsonIgnore]
private List<Choice> choices;

[Preserve]
[JsonProperty("choices")]
public IReadOnlyList<Choice> Choices => choices;

[Preserve]
[JsonIgnore]
public Choice FirstChoice => Choices?.FirstOrDefault(choice => choice.Index == 0);

public override string ToString() => FirstChoice?.ToString() ?? string.Empty;

public static implicit operator string(ChatResponse response) => response.ToString();

[Preserve]
internal void CopyFrom(ChatResponse other)
{
if (!string.IsNullOrWhiteSpace(other?.Id))
Expand Down
8 changes: 8 additions & 0 deletions OpenAI/Packages/com.openai.unity/Runtime/Chat/Choice.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace OpenAI.Chat
{
[Preserve]
public sealed class Choice
{
[Preserve]
[JsonConstructor]
public Choice(
[JsonProperty("message")] Message message,
Expand All @@ -19,22 +22,27 @@ public Choice(
Index = index;
}

[Preserve]
[JsonProperty("message")]
public Message Message { get; internal set; }

[Preserve]
[JsonProperty("delta")]
public Delta Delta { get; internal set; }

[Preserve]
[JsonProperty("finish_reason")]
public string FinishReason { get; internal set; }

[Preserve]
[JsonProperty("index")]
public int Index { get; internal set; }

public override string ToString() => Message?.Content ?? Delta?.Content ?? string.Empty;

public static implicit operator string(Choice choice) => choice.ToString();

[Preserve]
internal void CopyFrom(Choice other)
{
if (other?.Message != null)
Expand Down
7 changes: 7 additions & 0 deletions OpenAI/Packages/com.openai.unity/Runtime/Chat/Delta.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using UnityEngine.Scripting;

namespace OpenAI.Chat
{
[Preserve]
public sealed class Delta
{
[Preserve]
[JsonConstructor]
public Delta(
[JsonProperty("role")] Role role,
Expand All @@ -22,25 +25,29 @@ public Delta(
/// <summary>
/// The <see cref="Chat.Role"/> of the author of this message.
/// </summary>
[Preserve]
[JsonProperty("role")]
public Role Role { get; }

/// <summary>
/// The contents of the message.
/// </summary>
[Preserve]
[JsonProperty("content")]
public string Content { get; }

/// <summary>
/// Optional, The name of the author of this message.<br/>
/// May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
/// </summary>
[Preserve]
[JsonProperty("name")]
public string Name { get; }

/// <summary>
/// The function that should be called, as generated by the model.
/// </summary>
[Preserve]
[JsonProperty("function_call")]
public Function Function { get; }

Expand Down
9 changes: 9 additions & 0 deletions OpenAI/Packages/com.openai.unity/Runtime/Chat/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine.Scripting;

namespace OpenAI.Chat
{
/// <summary>
/// <see href="https://platform.openai.com/docs/guides/gpt/function-calling"/>
/// </summary>
[Preserve]
public class Function
{
[Preserve]
internal Function(Delta other) => CopyFrom(other);

/// <summary>
Expand All @@ -28,6 +31,7 @@ public class Function
/// <param name="arguments">
/// The arguments to use when calling the function.
/// </param>
[Preserve]
[JsonConstructor]
public Function(
[JsonProperty("name")] string name,
Expand All @@ -46,12 +50,14 @@ public Function(
/// May contain a-z, A-Z, 0-9, and underscores and dashes, with a maximum length of 64 characters.
/// Recommended to not begin with a number or a dash.
/// </summary>
[Preserve]
[JsonProperty("name")]
public string Name { get; private set; }

/// <summary>
/// The optional description of the function.
/// </summary>
[Preserve]
[JsonProperty("description")]
public string Description { get; private set; }

Expand All @@ -63,6 +69,7 @@ public Function(
/// The optional parameters of the function.
/// Describe the parameters that the model should generate in JSON schema format (json-schema.org).
/// </summary>
[Preserve]
[JsonProperty("parameters")]
public JToken Parameters
{
Expand All @@ -86,6 +93,7 @@ public JToken Parameters
/// <summary>
/// The arguments to use when calling the function.
/// </summary>
[Preserve]
[JsonProperty("arguments")]
public JToken Arguments
{
Expand All @@ -102,6 +110,7 @@ public JToken Arguments
private set => arguments = value;
}

[Preserve]
internal void CopyFrom(Delta other)
{
var otherFunction = other.Function;
Expand Down
Loading

0 comments on commit be89340

Please sign in to comment.