forked from GraesonB/ChatGPT-Wrapper-For-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPTConversation.cs
154 lines (126 loc) · 5.25 KB
/
ChatGPTConversation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using UnityEngine;
using System.Collections.Generic;
using Reqs;
namespace ChatGPTWrapper {
public class ChatGPTConversation : MonoBehaviour
{
[SerializeField]
private bool _useProxy = false;
[SerializeField]
private string _proxyUri = null;
[SerializeField]
private string _apiKey = null;
public enum Model {
ChatGPT,
Davinci,
Curie
}
[SerializeField]
public Model _model = Model.ChatGPT;
private string _selectedModel = null;
[SerializeField]
private int _maxTokens = 500;
[SerializeField]
private float _temperature = 0.5f;
private string _uri;
private List<(string, string)> _reqHeaders;
private Requests requests = new Requests();
private Prompt _prompt;
private Chat _chat;
private string _lastUserMsg;
private string _lastChatGPTMsg;
[SerializeField]
private string _chatbotName = "ChatGPT";
[TextArea(4,6)]
[SerializeField]
private string _initialPrompt = "You are ChatGPT, a large language model trained by OpenAI.";
public UnityStringEvent chatGPTResponse = new UnityStringEvent();
private void OnEnable()
{
TextAsset textAsset = Resources.Load<TextAsset>("APIKEY");
if (textAsset != null) {
_apiKey = textAsset.text;
}
_reqHeaders = new List<(string, string)>
{
("Authorization", $"Bearer {_apiKey}"),
("Content-Type", "application/json")
};
switch (_model) {
case Model.ChatGPT:
_chat = new Chat(_initialPrompt);
_uri = "https://api.openai.com/v1/chat/completions";
_selectedModel = "gpt-3.5-turbo";
break;
case Model.Davinci:
_prompt = new Prompt(_chatbotName, _initialPrompt);
_uri = "https://api.openai.com/v1/completions";
_selectedModel = "text-davinci-003";
break;
case Model.Curie:
_prompt = new Prompt(_chatbotName, _initialPrompt);
_uri = "https://api.openai.com/v1/completions";
_selectedModel = "text-curie-001";
break;
}
}
public void ResetChat(string initialPrompt) {
switch (_model) {
case Model.ChatGPT:
_chat = new Chat(initialPrompt);
break;
default:
_prompt = new Prompt(_chatbotName, initialPrompt);
break;
}
}
public void SendToChatGPT(string message)
{
_lastUserMsg = message;
if (_model == Model.ChatGPT) {
if (_useProxy) {
ProxyReq proxyReq = new ProxyReq();
proxyReq.max_tokens = _maxTokens;
proxyReq.temperature = _temperature;
proxyReq.messages = new List<Message>(_chat.CurrentChat);
proxyReq.messages.Add(new Message("user", message));
string proxyJson = JsonUtility.ToJson(proxyReq);
StartCoroutine(requests.PostReq<ChatGPTRes>(_proxyUri, proxyJson, ResolveChatGPT, _reqHeaders));
} else {
ChatGPTReq chatGPTReq = new ChatGPTReq();
chatGPTReq.model = _selectedModel;
chatGPTReq.max_tokens = _maxTokens;
chatGPTReq.temperature = _temperature;
chatGPTReq.messages = _chat.CurrentChat;
chatGPTReq.messages.Add(new Message("user", message));
string chatGPTJson = JsonUtility.ToJson(chatGPTReq);
StartCoroutine(requests.PostReq<ChatGPTRes>(_uri, chatGPTJson, ResolveChatGPT, _reqHeaders));
}
} else {
_prompt.AppendText(Prompt.Speaker.User, message);
GPTReq reqObj = new GPTReq();
reqObj.model = _selectedModel;
reqObj.prompt = _prompt.CurrentPrompt;
reqObj.max_tokens = _maxTokens;
reqObj.temperature = _temperature;
string json = JsonUtility.ToJson(reqObj);
StartCoroutine(requests.PostReq<GPTRes>(_uri, json, ResolveGPT, _reqHeaders));
}
}
private void ResolveChatGPT(ChatGPTRes res)
{
_lastChatGPTMsg = res.choices[0].message.content;
_chat.AppendMessage(Chat.Speaker.User, _lastUserMsg);
_chat.AppendMessage(Chat.Speaker.ChatGPT, _lastChatGPTMsg);
chatGPTResponse.Invoke(_lastChatGPTMsg);
}
private void ResolveGPT(GPTRes res)
{
_lastChatGPTMsg = res.choices[0].text
.TrimStart('\n')
.Replace("<|im_end|>", "");
_prompt.AppendText(Prompt.Speaker.Bot, _lastChatGPTMsg);
chatGPTResponse.Invoke(_lastChatGPTMsg);
}
}
}