@@ -50,6 +50,7 @@ public class LLMClient : MonoBehaviour
50
50
[ ClientAdvanced ] public string host = "localhost" ;
51
51
[ ServerAdvanced ] public int port = 13333 ;
52
52
[ Server ] public bool stream = true ;
53
+ [ ServerAdvanced ] public string password = "" ;
53
54
54
55
[ ModelAddonAdvanced ] public string grammar = null ;
55
56
[ ModelAdvanced ] public int seed = 0 ;
@@ -86,7 +87,6 @@ public class LLMClient : MonoBehaviour
86
87
protected List < ChatMessage > chat ;
87
88
public string chatTemplate = ChatTemplate . DefaultTemplate ;
88
89
public ChatTemplate template ;
89
- private List < ( string , string ) > requestHeaders = new List < ( string , string ) > { ( "Content-Type" , "application/json" ) } ;
90
90
private string previousEndpoint ;
91
91
public bool setNKeepToPrompt = true ;
92
92
private List < UnityWebRequest > WIPRequests = new List < UnityWebRequest > ( ) ;
@@ -135,7 +135,11 @@ private void OnValidate()
135
135
if ( GetType ( ) == typeof ( LLMClient ) )
136
136
{
137
137
LLM server = GetServer ( ) ;
138
- if ( server != null ) templateToSet = server . chatTemplate ;
138
+ if ( server != null )
139
+ {
140
+ templateToSet = server . chatTemplate ;
141
+ password = server . password ;
142
+ }
139
143
}
140
144
SetTemplate ( templateToSet ) ;
141
145
previousEndpoint = newEndpoint ;
@@ -428,22 +432,48 @@ public async Task<bool> IsServerReachableAsync(int timeout = 5)
428
432
}
429
433
}
430
434
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
+
431
457
public async Task < Ret > PostRequest < Res , Ret > ( string json , string endpoint , ContentCallback < Res , Ret > getContent , Callback < Ret > callback = null )
432
458
{
433
459
// send a post request to the server and call the relevant callbacks to convert the received content and handle it
434
460
// this function has streaming functionality i.e. handles the answer while it is being received
435
461
Ret result = default ;
436
462
byte [ ] jsonToSend = new System . Text . UTF8Encoding ( ) . GetBytes ( json ) ;
463
+
464
+ if ( endpoint == "completion" && password != "" )
465
+ {
466
+ await TestAuthorisation ( jsonToSend , endpoint ) ;
467
+ }
468
+
469
+
437
470
using ( var request = UnityWebRequest . Put ( $ "{ host } :{ port } /{ endpoint } ", jsonToSend ) )
438
471
{
439
472
WIPRequests . Add ( request ) ;
440
473
441
474
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 } ") ;
447
477
448
478
// Start the request asynchronously
449
479
var asyncOperation = request . SendWebRequest ( ) ;
0 commit comments