Skip to content

Commit e76dcf8

Browse files
committed
use authorisation bearer and test authorisation method
1 parent 80fa2bd commit e76dcf8

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

Runtime/LLMClient.cs

+37-7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class LLMClient : MonoBehaviour
5050
[ClientAdvanced] public string host = "localhost";
5151
[ServerAdvanced] public int port = 13333;
5252
[Server] public bool stream = true;
53+
[ServerAdvanced] public string password = "";
5354

5455
[ModelAddonAdvanced] public string grammar = null;
5556
[ModelAdvanced] public int seed = 0;
@@ -86,7 +87,6 @@ public class LLMClient : MonoBehaviour
8687
protected List<ChatMessage> chat;
8788
public string chatTemplate = ChatTemplate.DefaultTemplate;
8889
public ChatTemplate template;
89-
private List<(string, string)> requestHeaders = new List<(string, string)> { ("Content-Type", "application/json") };
9090
private string previousEndpoint;
9191
public bool setNKeepToPrompt = true;
9292
private List<UnityWebRequest> WIPRequests = new List<UnityWebRequest>();
@@ -135,7 +135,11 @@ private void OnValidate()
135135
if (GetType() == typeof(LLMClient))
136136
{
137137
LLM server = GetServer();
138-
if (server != null) templateToSet = server.chatTemplate;
138+
if (server != null)
139+
{
140+
templateToSet = server.chatTemplate;
141+
password = server.password;
142+
}
139143
}
140144
SetTemplate(templateToSet);
141145
previousEndpoint = newEndpoint;
@@ -428,22 +432,48 @@ public async Task<bool> IsServerReachableAsync(int timeout = 5)
428432
}
429433
}
430434

435+
public async Task TestAuthorisation(byte[] jsonToSend, string endpoint)
436+
{
437+
using (var request = UnityWebRequest.Put($"{host}:{port}/{endpoint}", jsonToSend))
438+
{
439+
request.method = "POST";
440+
request.SetRequestHeader("Content-Type", "application/json");
441+
442+
var asyncOperation = request.SendWebRequest();
443+
while (!asyncOperation.isDone)
444+
{
445+
await Task.Yield();
446+
}
447+
448+
if (request.responseCode != 401)
449+
{
450+
string error = "Unauthorised server response";
451+
Debug.LogError(error);
452+
throw new Exception(error);
453+
}
454+
}
455+
}
456+
431457
public async Task<Ret> PostRequest<Res, Ret>(string json, string endpoint, ContentCallback<Res, Ret> getContent, Callback<Ret> callback = null)
432458
{
433459
// send a post request to the server and call the relevant callbacks to convert the received content and handle it
434460
// this function has streaming functionality i.e. handles the answer while it is being received
435461
Ret result = default;
436462
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
463+
464+
if (endpoint == "completion" && password != "")
465+
{
466+
await TestAuthorisation(jsonToSend, endpoint);
467+
}
468+
469+
437470
using (var request = UnityWebRequest.Put($"{host}:{port}/{endpoint}", jsonToSend))
438471
{
439472
WIPRequests.Add(request);
440473

441474
request.method = "POST";
442-
if (requestHeaders != null)
443-
{
444-
for (int i = 0; i < requestHeaders.Count; i++)
445-
request.SetRequestHeader(requestHeaders[i].Item1, requestHeaders[i].Item2);
446-
}
475+
request.SetRequestHeader("Content-Type", "application/json");
476+
if (password != "") request.SetRequestHeader("Authorization", $"Bearer {password}");
447477

448478
// Start the request asynchronously
449479
var asyncOperation = request.SendWebRequest();

0 commit comments

Comments
 (0)