-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
APILoader.cs
155 lines (127 loc) · 4.72 KB
/
APILoader.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 System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json.Linq;
using UnityEngine.Networking;
public class APILoader : MonoBehaviour
{
private string apiKey = "myAPI" ; // Your OpenAI API key
private string chatGptModel ="text-davinci-003"; // The ID of the ChatGPT model to use
[SerializeField] private string userName = "jao"; // The user's name
private string botName = "JAO"; // The bot's name
public string conversationId; // The ID of the conversation
// UI elements for displaying the conversation
[SerializeField] public Text conversationText;
[SerializeField] public InputField messageInput;
// A queue to store the messages that have not yet been sent to ChatGPT
Queue<string> messageQueue = new Queue<string>();
// A flag to indicate whether ChatGPT is currently processing a request
bool waitingForResponse = false;
bool requestSent = false;
public void AddMessage(string name, string message)
{
// Add a new message to the conversation
conversationText.text += $"{name}: {message}\r\n";
}
void Start()
{
// Initialize the conversation ID if it is not already set
if (string.IsNullOrEmpty(conversationId))
{
conversationId = Guid.NewGuid().ToString();
}
// Display a greeting message from the bot
AddMessage(botName, "Hello! How can I help you today?");
Debug.Log(conversationId);
}
void Update()
{
// Check if the user has entered a new message
if (messageInput.isFocused && Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("Enter key was pressed");
// Get the message from the input field
string message = messageInput.text;
// Add the user's message to the conversation
AddMessage(userName, message);
// Clear the input field
messageInput.text = "";
// Add the message to the message queue
messageQueue.Enqueue(message);
// Send the next message in the queue to ChatGPT if it is not already processing a request
if (!waitingForResponse)
{
string nextMessage = messageQueue.Dequeue();
SendMessageToChatGPT(nextMessage);
}
}
}
public void OnEndEdit()
{
if (waitingForResponse || requestSent)
{
return;
}
// Get the message from the InputField component
string message = messageInput.text;
// Clear the InputField component
messageInput.text = "";
// Send the message to ChatGPT
SendMessageToChatGPT(message);
requestSent = true;
}
void SendMessageToChatGPT(string message)
{
// Set the waitingForResponse flag to indicate that ChatGPT is processing a request
waitingForResponse = true;
// Create a HTTP request to the OpenAI API
UnityWebRequest request = new UnityWebRequest(
"https://api.openai.com/v1/models/chat/generate", "POST");
request.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes("model=" + chatGptModel + "&prompt=" + botName + ": " + message + "&conversation_id=" + conversationId + "&max_tokens=256"));
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.SetRequestHeader("Authorization", $"Bearer {apiKey}");
Debug.Log(request.uploadHandler);
Debug.Log(request.downloadHandler);
// Check if the request has already been sent
if (!request.isDone)
{
// Send the request and wait for the response
StartCoroutine(WaitForResponse(request));
}
}
IEnumerator WaitForResponse(UnityWebRequest request)
{
Debug.Log("Waiting for response");
yield return request.SendWebRequest();
// Check for errors
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError(request.error);
}
if (request.responseCode == 404)
{
Debug.LogError("API endpoint not found: " + request.error);
}
else
{
// Parse the response
JObject response = JObject.Parse(request.downloadHandler.text);
string botMessage = (string)response["data"]["text"];
// Add the bot's message to the conversation
AddMessage(botName, botMessage);
// Set the waitingForResponse flag to false
waitingForResponse = false;
// Set the requestSent flag to false
requestSent = false;
// Send the next message in the queue if there are any left
if (messageQueue.Count > 0)
{
string message = messageQueue.Dequeue();
SendMessageToChatGPT(message);
}
}
}
}//class