Skip to content

Commit 99df6db

Browse files
committed
2023.10.29 Update 1.1.0
GPT3.5 and GPT4 Now supported! [Settings] - Change model property from string field to enum-like field. - Create "Choose folder" button.
1 parent f3dedcf commit 99df6db

File tree

6 files changed

+183
-10
lines changed

6 files changed

+183
-10
lines changed

Editor/BKKChatGPT.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public static class BKKChatGPT
6+
{
7+
public static string GPT35Turbo = "gpt-3.5-turbo";
8+
public static string GPT4 = "gpt-4";
9+
public static string TextDavinci003 = "text-davinci-003";
10+
11+
public static readonly string[] modelOptions = new string[]
12+
{
13+
TextDavinci003, GPT35Turbo, GPT4
14+
};
15+
16+
public enum ModelEnum
17+
{
18+
TextDavinci003,
19+
GPT35Turbo,
20+
GPT4,
21+
}
22+
}

Editor/BKKChatGPT.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ChatGPTAnswer.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,34 @@ public class ChatGPTAnswer : ScriptableObject
1717

1818
[HideInInspector] public bool answerSent;
1919

20+
private readonly ChatGPT4Message systemMessage = new ChatGPT4Message()
21+
{
22+
role = "user",
23+
content =
24+
"You are Unity Engine Script Generator. You will be given script related unity. Respond with only the script without explanation. If user talk about not unity engine related, respond \"I can't Respond about that.\""
25+
};
26+
2027
public void SendAnswer()
2128
{
2229
Debug.Log("Answer Sent");
2330
var settings = Resources.Load<ChatGPTSettings>("ChatGPTSettings");
24-
EditorBackgroundUtility.StartBackgroundTask(GenerateResponse(settings.apiKey, settings.aiModel,question, SetAnswer));
31+
32+
if (settings.aiModel.Contains(BKKChatGPT.TextDavinci003))
33+
{
34+
EditorBackgroundUtility.StartBackgroundTask(GenerateLegacyResponse(settings.apiKey, settings.aiModel,question, SetAnswer));
35+
}
36+
else
37+
{
38+
EditorBackgroundUtility.StartBackgroundTask(GenerateGPT4Response(settings.apiKey, settings.aiModel,question, SetAnswer));
39+
}
2540
}
2641

2742
private void SetAnswer(string _answer)
2843
{
2944
answer = _answer.Trim();
3045
}
3146

32-
private IEnumerator GenerateResponse(string apiKey, string model, string prompt, Action<string> resultAction)
47+
private IEnumerator GenerateLegacyResponse(string apiKey, string model, string prompt, Action<string> resultAction)
3348
{
3449
ChatGPTCompletionData completionData = new ChatGPTCompletionData
3550
{
@@ -67,6 +82,86 @@ private IEnumerator GenerateResponse(string apiKey, string model, string prompt,
6782
answerSent = false;
6883
}
6984
}
85+
86+
private IEnumerator GenerateGPT4Response(string apiKey, string model, string message, Action<string> resultAction)
87+
{
88+
ChatGPT4CompletionData completionData = new ChatGPT4CompletionData
89+
{
90+
model = model,
91+
messages = new List<ChatGPT4Message>()
92+
{
93+
systemMessage,
94+
new ChatGPT4Message()
95+
{
96+
role = "user",
97+
content = message,
98+
}
99+
},
100+
temperature = 0,
101+
};
102+
103+
string json = JsonUtility.ToJson(completionData);
104+
byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(json);
105+
106+
using (UnityWebRequest request = UnityWebRequest.Post("https://api.openai.com/v1/chat/completions", json))
107+
{
108+
request.SetRequestHeader("Content-Type", "application/json");
109+
request.SetRequestHeader("Authorization", $"Bearer {apiKey}");
110+
request.uploadHandler = new UploadHandlerRaw(jsonBytes);
111+
request.downloadHandler = new DownloadHandlerBuffer();
112+
113+
yield return request.SendWebRequest();
114+
115+
while (request.isDone == false) yield return null;
116+
117+
if (request.result == UnityWebRequest.Result.ConnectionError ||
118+
request.result == UnityWebRequest.Result.ProtocolError)
119+
{
120+
Debug.Log(request.error);
121+
}
122+
else
123+
{
124+
Debug.Log("ChatGPT Answered!");
125+
ChatGPT4Result result = JsonUtility.FromJson<ChatGPT4Result>(request.downloadHandler.text);
126+
resultAction?.Invoke(result.choices[0].message.content);
127+
}
128+
answerSent = false;
129+
}
130+
}
131+
132+
[Serializable]
133+
public class ChatGPT4CompletionData
134+
{
135+
public string model;
136+
public List<ChatGPT4Message> messages = new List<ChatGPT4Message>();
137+
public int temperature;
138+
}
139+
140+
[Serializable]
141+
public class ChatGPT4Result
142+
{
143+
public string id;
144+
public string @object;
145+
public int created;
146+
public string model;
147+
public ChatGPTUsage usage;
148+
public List<ChatGPT4Choice> choices = new List<ChatGPT4Choice>();
149+
}
150+
151+
[Serializable]
152+
public class ChatGPT4Message
153+
{
154+
public string role;
155+
public string content;
156+
}
157+
158+
[Serializable]
159+
public class ChatGPT4Choice
160+
{
161+
public ChatGPT4Message message;
162+
public string finish_reason;
163+
public int index;
164+
}
70165

71166
[Serializable]
72167
public class ChatGPTCompletionData

Editor/ChatGPTAnswers.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/ChatGPTSettingsEditor.cs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ namespace BKK.ChatGPTEditor
77
[CustomEditor(typeof(ChatGPTSettings))]
88
public class ChatGPTSettingsEditor : Editor
99
{
10-
private ChatGPTSettings chatGptSettings;
1110
private SerializedProperty m_OpenAiApiKey;
1211
private SerializedProperty m_Model;
1312
private SerializedProperty m_CreateAssetPath;
1413

1514
private GUIStyle warnLabelStyle;
16-
15+
16+
private readonly string choosePathButtonText = "Choose";
17+
private readonly string choosePathTitleText = "Choose Asset Path";
18+
19+
private readonly string modelFieldLabel = "Model";
20+
21+
private readonly string modelPrefsName = "UnityChatGPTScriptGenerator_Model";
22+
1723
private void OnEnable()
1824
{
19-
chatGptSettings = target as ChatGPTSettings;
20-
2125
m_OpenAiApiKey = serializedObject.FindProperty("openAiApiKey");
2226
m_Model = serializedObject.FindProperty("model");
2327
m_CreateAssetPath = serializedObject.FindProperty("createAssetPath");
@@ -36,10 +40,43 @@ public override void OnInspectorGUI()
3640
serializedObject.Update();
3741

3842
EditorGUILayout.PropertyField(m_OpenAiApiKey);
39-
EditorGUILayout.PropertyField(m_Model);
40-
EditorGUILayout.LabelField("Warning: Current text-davinci-003 Only", warnLabelStyle);
41-
EditorGUILayout.PropertyField(m_CreateAssetPath);
43+
44+
int modelIndex = EditorPrefs.GetInt("UnityChatGPTScriptGenerator_Model", 0);
45+
46+
modelIndex = EditorGUILayout.Popup(modelFieldLabel, modelIndex, BKKChatGPT.modelOptions);
47+
48+
EditorPrefs.SetInt(modelPrefsName, modelIndex);
4249

50+
BKKChatGPT.ModelEnum modelEnum = (BKKChatGPT.ModelEnum) modelIndex;
51+
52+
switch (modelEnum)
53+
{
54+
case BKKChatGPT.ModelEnum.TextDavinci003:
55+
m_Model.stringValue = BKKChatGPT.TextDavinci003;
56+
break;
57+
case BKKChatGPT.ModelEnum.GPT35Turbo:
58+
m_Model.stringValue = BKKChatGPT.GPT35Turbo;
59+
break;
60+
case BKKChatGPT.ModelEnum.GPT4:
61+
m_Model.stringValue = BKKChatGPT.GPT4;
62+
break;
63+
default:
64+
break;
65+
}
66+
67+
EditorGUILayout.BeginHorizontal();
68+
EditorGUILayout.PropertyField(m_CreateAssetPath);
69+
if (GUILayout.Button(choosePathButtonText, GUILayout.Width(100)))
70+
{
71+
string absolutePath = EditorUtility.OpenFolderPanel(choosePathTitleText, Application.dataPath, "");
72+
73+
if (!string.IsNullOrEmpty(absolutePath))
74+
{
75+
string relativePath = "Assets" + absolutePath.Substring(Application.dataPath.Length);
76+
m_CreateAssetPath.stringValue = relativePath;
77+
}
78+
}
79+
EditorGUILayout.EndHorizontal();
4380
serializedObject.ApplyModifiedProperties();
4481
}
4582
}

Editor/Resources/ChatGPTSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ MonoBehaviour:
1313
m_Name: ChatGPTSettings
1414
m_EditorClassIdentifier:
1515
openAiApiKey:
16-
model: text-davinci-003
16+
model: gpt-3.5-turbo
1717
createAssetPath: Assets/Editor/ChatGPTAnswers

0 commit comments

Comments
 (0)