@@ -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
0 commit comments