Skip to content

Commit

Permalink
2023.04.03 Update 1.0.1
Browse files Browse the repository at this point in the history
[Settings]
- Add model property on settings.

[Script Generator Window]
- Fix error FileName and SavePath doesn't insert value.
  • Loading branch information
Kokyung committed Apr 2, 2023
1 parent f2c60b2 commit 9fb9e29
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 10 deletions.
9 changes: 4 additions & 5 deletions Editor/ChatGPTAnswer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ public void SendAnswer()
{
Debug.Log("Answer Sent");
var settings = Resources.Load<ChatGPTSettings>("ChatGPTSettings");
EditorBackgroundUtility.StartBackgroundTask(GenerateResponse(settings.apiKey, question, SetAnswer));
EditorBackgroundUtility.StartBackgroundTask(GenerateResponse(settings.apiKey, settings.aiModel,question, SetAnswer));
}

private void SetAnswer(string _answer)
{
answer = _answer.Trim();
}

private IEnumerator GenerateResponse(string apiKey, string prompt, Action<string> resultAction)
private IEnumerator GenerateResponse(string apiKey, string model, string prompt, Action<string> resultAction)
{
ChatGPTCompletionData completionData = new ChatGPTCompletionData
{
model = "text-davinci-003",
model = model,
prompt = prompt,
max_tokens = 1000,
temperature = 0,
Expand Down Expand Up @@ -63,9 +63,8 @@ private IEnumerator GenerateResponse(string apiKey, string prompt, Action<string
Debug.Log("ChatGPT Answered!");
var result = JsonUtility.FromJson<ChatGPTResult>(request.downloadHandler.text);
resultAction?.Invoke(result.choices[0].text);

answerSent = false;
}
answerSent = false;
}
}

Expand Down
13 changes: 9 additions & 4 deletions Editor/ChatGPTAnswerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ChatGPTAnswerEditor : Editor
private const string standbyMessage = "Ready to Answer.";

private const string apiKeyErrorMessage = "API Key is Empty.";
private const string modelErrorMessage = "Model Name is Empty.";
private const string settingErrorMessage = "ChatGPTSettings not Exists.";

private Vector2 scrollA;
Expand All @@ -38,20 +39,20 @@ public override void OnInspectorGUI()

EditorGUILayout.Separator();

EditorGUILayout.TextField("File Name", answerAsset.fileName);
answerAsset.fileName = EditorGUILayout.TextField("File Name", answerAsset.fileName);

EditorGUILayout.TextField("Save Path", answerAsset.savePath);
answerAsset.savePath = EditorGUILayout.TextField("Save Path", answerAsset.savePath);

EditorGUILayout.Separator();

if (!settings || settings.ApiKeyIsEmpty()) GUI.enabled = false;
if (!settings || settings.ApiKeyIsEmpty() || settings.ModelIsEmpty()) GUI.enabled = false;
if (GUILayout.Button("Send Answer"))
{
answerAsset.SendAnswer();
answerAsset.answerSent = true;
}

if (!settings || settings.ApiKeyIsEmpty()) GUI.enabled = true;
if (!settings || settings.ApiKeyIsEmpty() || settings.ModelIsEmpty()) GUI.enabled = true;

EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
Expand All @@ -71,6 +72,10 @@ public override void OnInspectorGUI()
{
GUILayout.Label(apiKeyErrorMessage);
}
else if (settings.ModelIsEmpty())
{
GUILayout.Label(modelErrorMessage);
}
else
{
GUILayout.Label(standbyMessage);
Expand Down
8 changes: 8 additions & 0 deletions Editor/ChatGPTAnswers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Editor/ChatGPTSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace BKK.ChatGPTEditor
public class ChatGPTSettings : ScriptableObject
{
[SerializeField] private string openAiApiKey;
[SerializeField] private string model = "text-davinci-003";
[SerializeField] private string createAssetPath = "Assets/Editor/ChatGPTAnswers";

public string apiKey => openAiApiKey;
public string aiModel => model;
public string createPath => createAssetPath;

public const string settingPath = "Assets/Editor/Resources";
Expand Down Expand Up @@ -37,5 +39,10 @@ public bool ApiKeyIsEmpty()
{
return string.IsNullOrEmpty(apiKey) || string.IsNullOrWhiteSpace(apiKey);
}

public bool ModelIsEmpty()
{
return string.IsNullOrEmpty(model) || string.IsNullOrWhiteSpace(model);
}
}
}
31 changes: 31 additions & 0 deletions Editor/ChatGPTSettingsEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using UnityEditor;
using UnityEngine;

namespace BKK.ChatGPTEditor
{
[CustomEditor(typeof(ChatGPTSettings))]
public class ChatGPTSettingsEditor : Editor
{
private ChatGPTSettings chatGptSettings;
private SerializedProperty m_OpenAiApiKey;
private SerializedProperty m_Model;
private SerializedProperty m_CreateAssetPath;

private void OnEnable()
{
chatGptSettings = target as ChatGPTSettings;

m_OpenAiApiKey = serializedObject.FindProperty("openAiApiKey");
m_Model = serializedObject.FindProperty("model");
m_CreateAssetPath = serializedObject.FindProperty("createAssetPath");
}

public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(m_OpenAiApiKey);
EditorGUILayout.PropertyField(m_Model, new GUIContent("Model( Current text-davinci-003 Only )"));
EditorGUILayout.PropertyField(m_CreateAssetPath);
}
}
}
11 changes: 11 additions & 0 deletions Editor/ChatGPTSettingsEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Editor/Resources/ChatGPTSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ MonoBehaviour:
m_Name: ChatGPTSettings
m_EditorClassIdentifier:
openAiApiKey:
model: text-davinci-003
createAssetPath: Assets/Editor/ChatGPTAnswers
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.bkk.chatgptscriptgenerator",
"version": "1.0.0",
"version": "1.0.1",
"displayName": "ChatGPT Script Generator",
"description": "ChatGPT Script Generator provides question ask to ChatGPT and script save in asset folder.",
"unity": "2020.3",
Expand Down

0 comments on commit 9fb9e29

Please sign in to comment.