Skip to content

Commit 8e50900

Browse files
committed
Added voice feature
1 parent 4501d04 commit 8e50900

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

Runtime/KoalaGPTApi.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.IO;
34
using UnityEngine;
45
using System.Text;
@@ -195,6 +196,21 @@ private async Task<string> DispatchSimpleRequest(string path, byte[] payload = n
195196

196197
return data;
197198
}
199+
200+
private async Task<AudioClip> DispatchAudioRequest(string path, byte[] payload = null )
201+
{
202+
using var request = UnityWebRequest.Put(path, payload);
203+
request.method = UnityWebRequest.kHttpVerbGET;
204+
request.SetHeaders(Configuration, ContentType.ApplicationJson);
205+
request.downloadHandler = new DownloadHandlerAudioClip(path, AudioType.WAV);
206+
207+
var asyncOperation = request.SendWebRequest();
208+
while (!asyncOperation.isDone) await Task.Yield();
209+
210+
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(request);
211+
212+
return audioClip;
213+
}
198214

199215

200216
/// <summary>
@@ -335,7 +351,7 @@ public async Task<string> CreateChatCompletionSimplePrompt(CreateChatCompletionR
335351
/// </summary>
336352
/// <param name="request">See <see cref="CreateChatCompletionRequestPrompt"/></param>
337353
/// <returns>string</returns>
338-
public async Task<VoiceResponse> CreateSpeechPrompt(CreateChatCompletionRequestPrompt request)
354+
public async Task<AudioClip> CreateSpeechPrompt(CreateChatCompletionRequestPrompt request)
339355
{
340356
var path = $"{BASE_PATH}/unity/voice";
341357

@@ -360,9 +376,8 @@ public async Task<VoiceResponse> CreateSpeechPrompt(CreateChatCompletionRequestP
360376
Jailbreak = "default",
361377
};
362378

363-
364379
var payload = CreatePayloadKoala(req);
365-
return await DispatchSimpleRequest(path, payload);
380+
return await DispatchAudioRequest(path, payload);
366381
}
367382

368383

@@ -382,6 +397,34 @@ public void CreateChatCompletionAsync(CreateChatCompletionRequest request,
382397

383398
DispatchRequest(path, UnityWebRequest.kHttpVerbPOST, onResponse, onComplete, token, payload);
384399
}
400+
401+
// /// <summary>
402+
// /// Transcribes audio into the input language.
403+
// /// </summary>
404+
// /// <param name="request">See <see cref="CreateAudioTranscriptionsRequest"/></param>
405+
// /// <returns>See <see cref="CreateAudioResponse"/></returns>
406+
// public async Task<CreateAudioResponse> CreateAudioTranscription(CreateAudioTranscriptionsRequest request)
407+
// {
408+
// var path = $"{BASE_PATH}/audio/transcriptions";
409+
//
410+
// var form = new List<IMultipartFormSection>();
411+
// if (string.IsNullOrEmpty(request.File))
412+
// {
413+
// form.AddData(request.FileData, "file", $"audio/{Path.GetExtension(request.File)}");
414+
// }
415+
// else
416+
// {
417+
// form.AddFile(request.File, "file", $"audio/{Path.GetExtension(request.File)}");
418+
// }
419+
// form.AddValue(request.Model, "model");
420+
// form.AddValue(request.Prompt, "prompt");
421+
// form.AddValue(request.ResponseFormat, "response_format");
422+
// form.AddValue(request.Temperature, "temperature");
423+
// form.AddValue(request.Language, "language");
424+
//
425+
// return await DispatchRequest<CreateAudioResponse>(path, form);
426+
// }
427+
//
385428

386429
/// <summary>
387430
/// Classifies if text violates KoalaGPT's Content Policy

Tests/TestApiSendPrompt.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public async Task Create_Text_Completion()
4545
[Test]
4646
public async Task Create_Speech()
4747
{
48-
var _messages = new List<Part>();
48+
var messages = new List<Part>();
4949
var message = new Part();
5050
message.Role = "user";
5151
message.Content = "Hello";
5252

53-
_messages.Add(message);
53+
messages.Add(message);
5454

5555
var request = new CreateChatCompletionRequestPrompt();
56-
request.Messages = _messages;
56+
request.Messages = messages;
5757
request.Model = "gpt4";
5858

5959
var res = await _koalaGptApi.CreateSpeechPrompt(request);

Tests/TestApiSendPormpt.cs.meta renamed to Tests/TestApiSendPrompt.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.cyberkoalastudios.koalagpt",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"displayName": "KoalaGPT Unity",
55
"description": "An official KoalaGPT Unity Package that aims to help you use CyberKoala API directly in Unity Game engine.",
66
"unity": "2020.3",

0 commit comments

Comments
 (0)