From 4880f468eb6cec95d717b478fd6bdab3e58399cf Mon Sep 17 00:00:00 2001 From: Luchunpen Date: Mon, 20 Mar 2023 17:20:45 +0300 Subject: [PATCH] Update OpenAIUtil.cs Update UnityWebRequest for earlier Unity versions --- Assets/Editor/OpenAIUtil.cs | 102 +++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/Assets/Editor/OpenAIUtil.cs b/Assets/Editor/OpenAIUtil.cs index b831b80..57d10bf 100644 --- a/Assets/Editor/OpenAIUtil.cs +++ b/Assets/Editor/OpenAIUtil.cs @@ -1,52 +1,84 @@ +using System.Text; using UnityEngine; using UnityEditor; using UnityEngine.Networking; -namespace AIShader { - -static class OpenAIUtil +namespace AIShader { - static string CreateChatRequestBody(string prompt) + + static class OpenAIUtil { - var msg = new OpenAI.RequestMessage(); - msg.role = "user"; - msg.content = prompt; + static string CreateChatRequestBody(string prompt) + { + var msg = new OpenAI.RequestMessage(); + msg.role = "user"; + msg.content = prompt; - var req = new OpenAI.Request(); - req.model = "gpt-3.5-turbo"; - req.messages = new [] { msg }; + var req = new OpenAI.Request(); + req.model = "gpt-3.5-turbo"; + req.messages = new [] { msg }; - return JsonUtility.ToJson(req); - } + return JsonUtility.ToJson(req); + } - public static string InvokeChat(string prompt) - { - // POST - using var post = UnityWebRequest.Post - (OpenAI.Api.Url, CreateChatRequestBody(prompt), "application/json"); + private static UnityWebRequest CreateApiRequest(string url, object body) + { - // API key authorization - post.SetRequestHeader - ("Authorization", "Bearer " + AIShaderSettings.instance.apiKey); +#if UNITY_2022_2_OR_NEWER - // Request start - var req = post.SendWebRequest(); + var request = UnityWebRequest.Post(OpenAI.Api.Url, CreateChatRequestBody(prompt), "application/json"); + return request; +#else - // Progress bar (Totally fake! Don't try this at home.) - for (var progress = 0.0f; !req.isDone; progress += 0.01f) - { - EditorUtility.DisplayProgressBar - ("AI Shader Importer", "Generating...", progress); - System.Threading.Thread.Sleep(100); - progress += 0.01f; + string bodyString = null; + if (body is string) + { + bodyString = (string)body; + } + else if (body != null) + { + bodyString = JsonUtility.ToJson(body); + } + + var request = new UnityWebRequest(); + request.url = url; + request.method = "POST"; + request.downloadHandler = new DownloadHandlerBuffer(); + request.uploadHandler = new UploadHandlerRaw(string.IsNullOrEmpty(bodyString) ? null : Encoding.UTF8.GetBytes(bodyString)); + request.SetRequestHeader("Accept", "application/json"); + request.SetRequestHeader("Content-Type", "application/json"); + request.timeout = 60; + return request; +#endif } - EditorUtility.ClearProgressBar(); - // Response extraction - var json = post.downloadHandler.text; - var data = JsonUtility.FromJson(json); - return data.choices[0].message.content; + public static string InvokeChat(string prompt) + { + // POST + using var post = CreateApiRequest(OpenAI.Api.Url, CreateChatRequestBody(prompt)); + + // API key authorization + post.SetRequestHeader + ("Authorization", "Bearer " + AIShaderSettings.instance.apiKey); + + // Request start + var req = post.SendWebRequest(); + + // Progress bar (Totally fake! Don't try this at home.) + for (var progress = 0.0f; !req.isDone; progress += 0.01f) + { + EditorUtility.DisplayProgressBar + ("AI Shader Importer", "Generating...", progress); + System.Threading.Thread.Sleep(100); + progress += 0.01f; + } + EditorUtility.ClearProgressBar(); + + // Response extraction + var json = post.downloadHandler.text; + var data = JsonUtility.FromJson(json); + return data.choices[0].message.content; + } } -} } // namespace AIShader