Skip to content

Commit

Permalink
com.openai.unity 8.5.0 (#327)
Browse files Browse the repository at this point in the history
- Added Role.Developer for reasoning model messages
- Added reasoning effort parameter for o1 series models in ChatRequest
- Fix possible misinterpretation of function parameter types in function calls
  • Loading branch information
StephenHodgson authored Jan 25, 2025
1 parent 9ce8222 commit c1a466c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
25 changes: 23 additions & 2 deletions Runtime/Chat/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public ChatRequest(
bool? parallelToolCalls = null,
JsonSchema jsonSchema = null,
AudioConfig audioConfig = null,
ReasoningEffort? reasoningEffort = null,
string user = null)
: this(messages, model, frequencyPenalty, logitBias, maxTokens, number, presencePenalty,
responseFormat, seed, stops, temperature, topP, topLogProbs, parallelToolCalls, jsonSchema, audioConfig, user)
responseFormat, seed, stops, temperature, topP, topLogProbs, parallelToolCalls, jsonSchema, audioConfig, reasoningEffort, user)
{
var toolList = tools?.ToList();

Expand Down Expand Up @@ -149,6 +150,11 @@ public ChatRequest(
/// <param name="audioConfig">
/// Parameters for audio output. <see cref="Chat.AudioConfig"/>.
/// </param>
/// <param name="reasoningEffort">
/// Constrains the effort of reasoning for <see href="https://platform.openai.com/docs/guides/reasoning">Reasoning Models</see>.<br/>
/// Currently supported values are: Low, Medium, High. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning response.<br/>
/// <b>o1 models only!</b>
/// </param>
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
Expand All @@ -170,6 +176,7 @@ public ChatRequest(
bool? parallelToolCalls = null,
JsonSchema jsonSchema = null,
AudioConfig audioConfig = null,
ReasoningEffort? reasoningEffort = null,
string user = null)
{
Messages = messages?.ToList();
Expand All @@ -181,6 +188,16 @@ public ChatRequest(

Model = string.IsNullOrWhiteSpace(model) ? Models.Model.GPT4o : model;

if (reasoningEffort.HasValue)
{
if (!Model.Contains("o1"))
{
throw new ArgumentException("Only o1 series models support reasoning effort", nameof(reasoningEffort));
}

ReasoningEffort = reasoningEffort.Value;
}

if (audioConfig != null && !Model.Contains("audio"))
{
throw new ArgumentException("Audio settings are only valid for models that support audio output", nameof(audioConfig));
Expand Down Expand Up @@ -246,6 +263,10 @@ public ChatRequest(
[JsonProperty("store")]
public bool? Store { get; set; }

[Preserve]
[JsonProperty("reasoning_effort", DefaultValueHandling = DefaultValueHandling.Ignore)]
public ReasoningEffort ReasoningEffort { get; }

/// <summary>
/// Developer-defined tags and values used for filtering completions in the dashboard.
/// </summary>
Expand Down Expand Up @@ -304,7 +325,7 @@ public ChatRequest(
/// </summary>
[JsonIgnore]
[Obsolete("Use MaxCompletionTokens instead")]
public int? MaxTokens { get; }
public int? MaxTokens => MaxCompletionTokens;

/// <summary>
/// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens.
Expand Down
6 changes: 6 additions & 0 deletions Runtime/Common/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ private static async Task<T> InvokeInternalAsync<T>(Function function, object[]
}
else
{
// check that the value assigned type matches the parameter type. If not then attempt to change it.
if (value.GetType() != parameter.ParameterType)
{
value = Convert.ChangeType(value, parameter.ParameterType);
}

invokeArgs[i] = value;
}
}
Expand Down
18 changes: 18 additions & 0 deletions Runtime/Common/ReasoningEffort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace OpenAI
{
/// <summary>
/// Constrains the effort of reasoning for <see href="https://platform.openai.com/docs/guides/reasoning">Reasoning Models</see>.<br/>
/// Currently supported values are: Low, Medium, High. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning response.
/// </summary>
/// <remarks>
/// <b>o1 models only!</b>
/// </remarks>
public enum ReasoningEffort
{
Low = 1,
Medium,
High
}
}
11 changes: 11 additions & 0 deletions Runtime/Common/ReasoningEffort.cs.meta

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

9 changes: 5 additions & 4 deletions Runtime/Common/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ namespace OpenAI
public enum Role
{
System = 1,
Assistant,
User,
Developer = 1,
Assistant = 2,
User = 3,
[Obsolete("Use Tool")]
Function,
Tool
Function = 4,
Tool = 4
}
}
9 changes: 9 additions & 0 deletions Tests/TestFixture_00_02_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using OpenAI.Audio;
using OpenAI.Images;
using OpenAI.Tests.StructuredOutput;
using OpenAI.Tests.Weather;
Expand All @@ -23,7 +24,15 @@ public void Test_01_01_GetTools()
Assert.IsNotNull(tools);
Assert.IsNotEmpty(tools);
tools.Add(Tool.GetOrCreateTool(OpenAIClient.ImagesEndPoint, nameof(ImagesEndpoint.GenerateImageAsync)));
tools.Add(Tool.GetOrCreateTool(OpenAIClient.AudioEndpoint, nameof(AudioEndpoint.GetSpeechAsync)));
tools.Add(Tool.FromFunc<GameObject, Vector2, Vector3, Quaternion, string>("complex_objects", (gameObject, vector2, vector3, quaternion) => "success"));


foreach (var tool in tools)
{
Debug.Log(tool.Function?.Name ?? tool.Type);
}

var json = JsonConvert.SerializeObject(tools, Formatting.Indented, OpenAIClient.JsonSerializationOptions);
Debug.Log(json);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "OpenAI",
"description": "A OpenAI package for the Unity Game Engine to use GPT-4, GPT-3.5, GPT-3 and Dall-E though their RESTful API (currently in beta).\n\nIndependently developed, this is not an official library and I am not affiliated with OpenAI.\n\nAn OpenAI API account is required.",
"keywords": [],
"version": "8.4.5",
"version": "8.5.0",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.openai.unity#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.openai.unity/releases",
Expand Down

0 comments on commit c1a466c

Please sign in to comment.