Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 67 additions & 35 deletions Assets/Editor/OpenAIUtil.cs
Original file line number Diff line number Diff line change
@@ -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<OpenAI.Response>(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<OpenAI.Response>(json);
return data.choices[0].message.content;
}
}
}

} // namespace AIShader