Skip to content

Commit 3db1aeb

Browse files
com.openai.unity 7.7.1 (#196)
- More Function utilities and invoking methods - Added FunctionPropertyAttribute to help better inform the feature how to format the Function json - Added FromFunc<,> overloads for convenance - Fixed invoke args sometimes being casting to wrong type - Added additional protections for static and instanced function calls - Added additional tool utilities: - Tool.ClearRegisteredTools - Tool.IsToolRegistered(Tool) - Tool.TryRegisterTool(Tool) - Updated Sample Chat Behaviour with updated tool implementations - com.utilities.rest -> 2.5.3
1 parent 0aa5d79 commit 3db1aeb

35 files changed

+912
-233
lines changed

Runtime/Assistants/AssistantExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static async Task<AssistantFileResponse> RetrieveFileAsync(this Assistant
121121
// => await assistantFile.Client.FilesEndpoint.DownloadFileAsync(assistantFile.Id, directory, deleteCachedFile, cancellationToken);
122122

123123
/// <summary>
124-
/// Remove AssistantFile.
124+
/// Remove the file from the assistant it is attached to.
125125
/// </summary>
126126
/// <remarks>
127127
/// Note that removing an AssistantFile does not delete the original File object,
@@ -135,7 +135,7 @@ public static async Task<bool> RemoveFileAsync(this AssistantFileResponse file,
135135
=> await file.Client.AssistantsEndpoint.RemoveFileAsync(file.AssistantId, file.Id, cancellationToken);
136136

137137
/// <summary>
138-
/// Remove AssistantFile.
138+
/// Remove the file from the assistant it is attached to.
139139
/// </summary>
140140
/// <remarks>
141141
/// Note that removing an AssistantFile does not delete the original File object,

Runtime/Assistants/AssistantsEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenAI.Assistants
1212
{
13-
public class AssistantsEndpoint : OpenAIBaseEndpoint
13+
public sealed class AssistantsEndpoint : OpenAIBaseEndpoint
1414
{
1515
internal AssistantsEndpoint(OpenAIClient client) : base(client) { }
1616

Runtime/Audio/AudioEndpoint.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ internal AudioEndpoint(OpenAIClient client) : base(client) { }
4646
/// <param name="request"><see cref="SpeechRequest"/>.</param>
4747
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
4848
/// <returns><see cref="AudioClip"/> and the cached path.</returns>
49+
[Function("Generates audio from the input text.")]
4950
public async Task<Tuple<string, AudioClip>> CreateSpeechAsync(SpeechRequest request, CancellationToken cancellationToken = default)
5051
{
5152
var audioFormat = request.ResponseFormat switch

Runtime/Audio/AudioTranscriptionRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public AudioTranscriptionRequest(
105105
/// The name of the audio file to transcribe.
106106
/// </param>
107107
/// <param name="model">
108-
/// ID of the model to use.
108+
/// ID of the model to use. Only whisper-1 is currently available.
109109
/// </param>
110110
/// <param name="prompt">
111111
/// Optional, An optional text to guide the model's style or continue a previous audio segment.<br/>
@@ -167,7 +167,7 @@ public AudioTranscriptionRequest(
167167
public string AudioName { get; }
168168

169169
/// <summary>
170-
/// ID of the model to use.
170+
/// ID of the model to use. Only whisper-1 is currently available.
171171
/// </summary>
172172
public string Model { get; }
173173

Runtime/Audio/AudioTranslationRequest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public sealed class AudioTranslationRequest : IDisposable
1616
/// The audio file to translate, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
1717
/// </param>
1818
/// <param name="model">
19-
/// ID of the model to use.
19+
/// ID of the model to use. Only whisper-1 is currently available.
2020
/// </param>
2121
/// <param name="prompt">
2222
/// Optional, An optional text to guide the model's style or continue a previous audio segment.<br/>
@@ -37,7 +37,7 @@ public AudioTranslationRequest(
3737
string model = null,
3838
string prompt = null,
3939
AudioResponseFormat responseFormat = AudioResponseFormat.Json,
40-
int? temperature = null)
40+
float? temperature = null)
4141
: this(File.OpenRead(audioPath), Path.GetFileName(audioPath), model, prompt, responseFormat, temperature)
4242
{
4343
}
@@ -136,7 +136,7 @@ public AudioTranslationRequest(
136136
public string AudioName { get; }
137137

138138
/// <summary>
139-
/// ID of the model to use.
139+
/// ID of the model to use. Only whisper-1 is currently available.
140140
/// </summary>
141141
public string Model { get; }
142142

Runtime/Audio/SpeechRequest.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Newtonsoft.Json;
44
using OpenAI.Models;
5+
using System;
56
using UnityEngine.Scripting;
67

78
namespace OpenAI.Audio
@@ -20,31 +21,51 @@ public sealed class SpeechRequest
2021
[Preserve]
2122
public SpeechRequest(string input, Model model = null, SpeechVoice voice = SpeechVoice.Alloy, SpeechResponseFormat responseFormat = SpeechResponseFormat.MP3, float? speed = null)
2223
{
23-
Input = input;
24+
Input = !string.IsNullOrWhiteSpace(input) ? input : throw new ArgumentException("Input cannot be null or empty.", nameof(input));
2425
Model = string.IsNullOrWhiteSpace(model?.Id) ? Models.Model.TTS_1 : model;
2526
Voice = voice;
2627
ResponseFormat = responseFormat;
2728
Speed = speed;
2829
}
2930

31+
/// <summary>
32+
/// One of the available TTS models. Defaults to tts-1.
33+
/// </summary>
3034
[Preserve]
3135
[JsonProperty("model")]
36+
[FunctionProperty("One of the available TTS models. Defaults to tts-1.", true, "tts-1", "tts-1-hd")]
3237
public string Model { get; }
3338

39+
/// <summary>
40+
/// The text to generate audio for. The maximum length is 4096 characters.
41+
/// </summary>
3442
[Preserve]
3543
[JsonProperty("input")]
44+
[FunctionProperty("The text to generate audio for. The maximum length is 4096 characters.", true)]
3645
public string Input { get; }
3746

47+
/// <summary>
48+
/// The voice to use when generating the audio.
49+
/// </summary>
3850
[Preserve]
3951
[JsonProperty("voice", DefaultValueHandling = DefaultValueHandling.Include)]
52+
[FunctionProperty("The voice to use when generating the audio.", true)]
4053
public SpeechVoice Voice { get; }
4154

55+
/// <summary>
56+
/// The format to audio in. Supported formats are mp3, opus, aac, and flac.
57+
/// </summary>
4258
[Preserve]
4359
[JsonProperty("response_format", DefaultValueHandling = DefaultValueHandling.Include)]
60+
[FunctionProperty("The format to audio in. Supported formats are mp3, opus, aac, and flac.", false, SpeechResponseFormat.MP3)]
4461
public SpeechResponseFormat ResponseFormat { get; }
4562

63+
/// <summary>
64+
/// The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
65+
/// </summary>
4666
[Preserve]
4767
[JsonProperty("speed")]
68+
[FunctionProperty("The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.", false, 1.0f)]
4869
public float? Speed { get; }
4970
}
5071
}

Runtime/Chat/ChatEndpoint.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ internal ChatEndpoint(OpenAIClient client) : base(client) { }
3030
public async Task<ChatResponse> GetCompletionAsync(ChatRequest chatRequest, CancellationToken cancellationToken = default)
3131
{
3232
var payload = JsonConvert.SerializeObject(chatRequest, OpenAIClient.JsonSerializationOptions);
33-
34-
if (EnableDebug)
35-
{
36-
Debug.Log(payload);
37-
}
38-
3933
var response = await Rest.PostAsync(GetUrl("/completions"), payload, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
4034
response.Validate(EnableDebug);
4135
return response.Deserialize<ChatResponse>(client);

Runtime/Chat/ChatRequest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed under the MIT License. See LICENSE in the project root for license information.
22

33
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Linq;
54
using System;
65
using System.Collections.Generic;
76
using System.Linq;

Runtime/Chat/ChatResponse.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public ChatResponse(
3535
this.choices = choices.ToList();
3636
}
3737

38+
/// <summary>
39+
/// A unique identifier for the chat completion.
40+
/// </summary>
3841
[Preserve]
3942
[JsonProperty("id")]
4043
public string Id { get; private set; }
@@ -77,6 +80,9 @@ public ChatResponse(
7780
[JsonIgnore]
7881
private List<Choice> choices;
7982

83+
/// <summary>
84+
/// A list of chat completion choices. Can be more than one if n is greater than 1.
85+
/// </summary>
8086
[Preserve]
8187
[JsonProperty("choices")]
8288
public IReadOnlyList<Choice> Choices

Runtime/Chat/Conversation.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
using Newtonsoft.Json;
44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
7+
using System.Linq;
68
using UnityEngine.Scripting;
79

810
namespace OpenAI.Chat
@@ -15,21 +17,30 @@ public sealed class Conversation
1517
[JsonConstructor]
1618
public Conversation([JsonProperty("messages")] List<Message> messages = null)
1719
{
18-
this.messages = messages ?? new List<Message>();
20+
21+
this.messages = new ConcurrentQueue<Message>();
22+
23+
if (messages != null)
24+
{
25+
foreach (var message in messages)
26+
{
27+
this.messages.Enqueue(message);
28+
}
29+
}
1930
}
2031

21-
private readonly List<Message> messages;
32+
private readonly ConcurrentQueue<Message> messages;
2233

2334
[Preserve]
2435
[JsonProperty("messages")]
25-
public IReadOnlyList<Message> Messages => messages;
36+
public IReadOnlyList<Message> Messages => messages.ToList();
2637

2738
/// <summary>
2839
/// Appends <see cref="Message"/> to the end of <see cref="Messages"/>.
2940
/// </summary>
3041
/// <param name="message">The message to add to the <see cref="Conversation"/>.</param>
3142
[Preserve]
32-
public void AppendMessage(Message message) => messages.Add(message);
43+
public void AppendMessage(Message message) => messages.Enqueue(message);
3344

3445
[Preserve]
3546
public override string ToString() => JsonConvert.SerializeObject(this, OpenAIClient.JsonSerializationOptions);

0 commit comments

Comments
 (0)