-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfastapi_server_llm.py
More file actions
1455 lines (1263 loc) · 56.3 KB
/
Copy pathfastapi_server_llm.py
File metadata and controls
1455 lines (1263 loc) · 56.3 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
RKLLM OpenAI and Ollama API compatible server.
The server exposes the OpenAI-compatible API used by existing clients and a
small Ollama-compatible API for tools which expect ``/api/chat`` or
``/api/generate``. When started directly it also opens a simple terminal chat
so the device can be demonstrated without configuring a separate client.
"""
import ctypes
import sys
import os
import threading
import time
import uuid
import json
import asyncio
from contextlib import asynccontextmanager
from concurrent.futures import ThreadPoolExecutor
from typing import List, Optional, Dict, Any, Generator, Union
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import uvicorn
import argparse
# ==================== System Library Preloading ====================
def preload_libraries():
"""Preload necessary system libraries to fix OpenCL issues"""
try:
# Set environment variables
os.environ['LD_LIBRARY_PATH'] = '/usr/lib/aarch64-linux-gnu:/usr/lib:' + os.environ.get('LD_LIBRARY_PATH', '')
# Preload libraries
libs = [
'librknnrt.so',
'/usr/lib/librkllmrt.so'
]
for lib in libs:
try:
ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL)
print(f"✓ Preloaded: {lib}")
except Exception as e:
print(f"⚠ Failed to preload {lib}: {e}")
except Exception as e:
print(f"⚠ Error during library preloading: {e}")
print("Preloading system libraries...")
preload_libraries()
# ==================== Load RKLLM Library ====================
try:
rkllm_lib = ctypes.CDLL('/usr/lib/librkllmrt.so')
print("✓ Successfully loaded librkllmrt.so")
except Exception as e:
print(f"✗ Failed to load librkllmrt.so: {e}")
print("Please ensure RKLLM runtime is installed: sudo apt install librkllmrt")
sys.exit(1)
# ==================== RKLLM Structure Definitions ====================
RKLLM_Handle_t = ctypes.c_void_p
# Enum definitions
class LLMCallState:
RKLLM_RUN_NORMAL = 0
RKLLM_RUN_WAITING = 1
RKLLM_RUN_FINISH = 2
RKLLM_RUN_ERROR = 3
class RKLLMInputType:
RKLLM_INPUT_PROMPT = 0
RKLLM_INPUT_TOKEN = 1
RKLLM_INPUT_EMBED = 2
RKLLM_INPUT_MULTIMODAL = 3
class RKLLMInferMode:
RKLLM_INFER_GENERATE = 0
class RKLLMExtendParam(ctypes.Structure):
_fields_ = [
("base_domain_id", ctypes.c_int32),
("embed_flash", ctypes.c_int8),
("enabled_cpus_num", ctypes.c_int8),
("enabled_cpus_mask", ctypes.c_uint32),
("n_batch", ctypes.c_uint8),
("use_cross_attn", ctypes.c_int8),
("reserved", ctypes.c_uint8 * 104),
]
class RKLLMParam(ctypes.Structure):
"""RKLLMParam from the airockchip/rknn-llm v1.3.0 header.
v1.3.0 removed the image marker strings from this structure. Leaving
those v1.2.x fields in place shifts ``extend_param`` and makes the runtime
read pointer bytes as ``n_batch`` and ``enabled_cpus_num``.
"""
_fields_ = [
("model_path", ctypes.c_char_p),
("max_context_len", ctypes.c_int32),
("max_new_tokens", ctypes.c_int32),
("top_k", ctypes.c_int32),
("n_keep", ctypes.c_int32),
("top_p", ctypes.c_float),
("temperature", ctypes.c_float),
("repeat_penalty", ctypes.c_float),
("frequency_penalty", ctypes.c_float),
("presence_penalty", ctypes.c_float),
("mirostat", ctypes.c_int32),
("mirostat_tau", ctypes.c_float),
("mirostat_eta", ctypes.c_float),
("skip_special_token", ctypes.c_bool),
("ignore_eos_token", ctypes.c_bool),
("is_async", ctypes.c_bool),
("extend_param", RKLLMExtendParam),
]
class RKLLMEmbedInput(ctypes.Structure):
_fields_ = [
("embed", ctypes.POINTER(ctypes.c_float)),
("n_tokens", ctypes.c_size_t),
]
class RKLLMTokenInput(ctypes.Structure):
_fields_ = [
("input_ids", ctypes.POINTER(ctypes.c_int32)),
("n_tokens", ctypes.c_size_t),
]
class RKLLMImageInput(ctypes.Structure):
_fields_ = [
("image_embed", ctypes.POINTER(ctypes.c_float)),
("n_image_tokens", ctypes.c_size_t),
("n_image", ctypes.c_size_t),
("image_start", ctypes.c_char_p),
("image_end", ctypes.c_char_p),
("image_content", ctypes.c_char_p),
("image_width", ctypes.c_size_t),
("image_height", ctypes.c_size_t),
]
class RKLLMVideoInput(ctypes.Structure):
_fields_ = [
("video_embed", ctypes.POINTER(ctypes.c_float)),
("n_frame_tokens", ctypes.c_size_t),
("n_frame_per_video", ctypes.c_size_t),
("n_video", ctypes.c_size_t),
("video_start", ctypes.c_char_p),
("video_end", ctypes.c_char_p),
("video_content", ctypes.c_char_p),
("frame_width", ctypes.c_size_t),
("frame_height", ctypes.c_size_t),
]
class RKLLMMultiModalInput(ctypes.Structure):
_fields_ = [
("prompt", ctypes.c_char_p),
("image", RKLLMImageInput),
("video", RKLLMVideoInput),
]
class RKLLMInputUnion(ctypes.Union):
_fields_ = [
("prompt_input", ctypes.c_char_p),
("embed_input", RKLLMEmbedInput),
("token_input", RKLLMTokenInput),
("multimodal_input", RKLLMMultiModalInput),
]
class RKLLMInput(ctypes.Structure):
_fields_ = [
("role", ctypes.c_char_p),
("enable_thinking", ctypes.c_bool),
("input_type", ctypes.c_int),
("input_data", RKLLMInputUnion)
]
class RKLLMInferParam(ctypes.Structure):
_fields_ = [
("mode", ctypes.c_int),
("lora_params", ctypes.c_void_p),
("prompt_cache_params", ctypes.c_void_p),
("sampling_params", ctypes.c_void_p),
("keep_history", ctypes.c_int),
("max_new_tokens", ctypes.c_int32),
]
class RKLLMResultLastHiddenLayer(ctypes.Structure):
_fields_ = [
("hidden_states", ctypes.POINTER(ctypes.c_float)),
("embd_size", ctypes.c_int),
("num_tokens", ctypes.c_int),
]
class RKLLMResultLogits(ctypes.Structure):
_fields_ = [
("logits", ctypes.POINTER(ctypes.c_float)),
("vocab_size", ctypes.c_int),
("num_tokens", ctypes.c_int),
]
class RKLLMPerfStat(ctypes.Structure):
_fields_ = [
("prefill_time_ms", ctypes.c_float),
("prefill_tokens", ctypes.c_int),
("generate_time_ms", ctypes.c_float),
("generate_tokens", ctypes.c_int),
("memory_usage_mb", ctypes.c_float),
]
class RKLLMResult(ctypes.Structure):
_fields_ = [
("text", ctypes.c_char_p),
("token_id", ctypes.c_int32),
("last_hidden_layer", RKLLMResultLastHiddenLayer),
("logits", RKLLMResultLogits),
("perf", RKLLMPerfStat),
]
callback_type = ctypes.CFUNCTYPE(
ctypes.c_int,
ctypes.POINTER(RKLLMResult),
ctypes.c_void_p,
ctypes.c_int,
)
class RKLLMCallback(ctypes.Structure):
_fields_ = [
("result_callback", callback_type),
("result_userdata", ctypes.c_void_p),
("tokenizer_callback", ctypes.c_void_p),
("tokenizer_userdata", ctypes.c_void_p),
("embed_callback", ctypes.c_void_p),
("embed_userdata", ctypes.c_void_p),
]
# ==================== Pydantic Model Definitions ====================
class ChatMessage(BaseModel):
role: str = Field(..., description="Message role: system, user, assistant")
content: Union[str, List[Dict[str, Any]]] = Field(..., description="Message content")
class Function(BaseModel):
name: str = Field(..., description="Function name")
description: Optional[str] = Field(None, description="Function description")
parameters: Optional[Dict[str, Any]] = Field(None, description="Function parameters")
class Tool(BaseModel):
type: str = Field(default="function", description="Tool type")
function: Optional[Function] = Field(None, description="Function definition")
class ChatCompletionRequest(BaseModel):
model: str = Field(default="rkllm-model", description="Model name")
messages: List[ChatMessage] = Field(..., description="List of messages")
temperature: Optional[float] = Field(default=0.8, ge=0.0, le=2.0, description="Temperature parameter (0.0-2.0)")
top_p: Optional[float] = Field(default=0.9, ge=0.0, le=1.0, description="Top-p sampling parameter (0.0-1.0)")
top_k: Optional[int] = Field(default=None, ge=1, le=100, description="Top-k sampling parameter (1-100)")
n: Optional[int] = Field(default=1, ge=1, le=10, description="Number of completions to generate")
stream: Optional[bool] = Field(default=False, description="Whether to stream the response")
max_tokens: Optional[int] = Field(default=512, ge=1, le=8192, description="Maximum tokens to generate")
presence_penalty: Optional[float] = Field(default=0.0, ge=-2.0, le=2.0, description="Presence penalty")
frequency_penalty: Optional[float] = Field(default=0.0, ge=-2.0, le=2.0, description="Frequency penalty")
logit_bias: Optional[Dict[str, float]] = Field(None, description="Logit bias")
user: Optional[str] = Field(None, description="User identifier")
stop: Optional[List[str]] = Field(None, description="Stop sequences")
tools: Optional[List[Tool]] = Field(None, description="List of tools")
tool_choice: Optional[str] = Field(None, description="Tool choice")
class UsageInfo(BaseModel):
prompt_tokens: int = Field(default=0, description="Prompt tokens")
completion_tokens: int = Field(default=0, description="Completion tokens")
total_tokens: int = Field(default=0, description="Total tokens")
class ChatCompletionResponseChoice(BaseModel):
index: int = Field(..., description="Choice index")
message: ChatMessage = Field(..., description="Message")
finish_reason: Optional[str] = Field(default="stop", description="Finish reason")
class ChatCompletionResponse(BaseModel):
id: str = Field(..., description="Request ID")
object: str = Field(default="chat.completion", description="Object type")
created: int = Field(..., description="Creation timestamp")
model: str = Field(..., description="Model name")
choices: List[ChatCompletionResponseChoice] = Field(..., description="List of choices")
usage: UsageInfo = Field(..., description="Usage information")
system_fingerprint: Optional[str] = Field(default="fp_rkllm", description="System fingerprint")
class DeltaMessage(BaseModel):
role: Optional[str] = Field(None, description="Role")
content: Optional[str] = Field(None, description="Content")
class ChatCompletionStreamResponseChoice(BaseModel):
index: int = Field(..., description="Choice index")
delta: DeltaMessage = Field(..., description="Delta message")
finish_reason: Optional[str] = Field(None, description="Finish reason")
class ChatCompletionStreamResponse(BaseModel):
id: str = Field(..., description="Request ID")
object: str = Field(default="chat.completion.chunk", description="Object type")
created: int = Field(..., description="Creation timestamp")
model: str = Field(..., description="Model name")
choices: List[ChatCompletionStreamResponseChoice] = Field(..., description="List of choices")
system_fingerprint: Optional[str] = Field(default="fp_rkllm", description="System fingerprint")
class ModelInfo(BaseModel):
id: str = Field(..., description="Model ID")
object: str = Field(default="model", description="Object type")
created: int = Field(..., description="Creation time")
owned_by: str = Field(default="rkllm", description="Owner")
class ModelsListResponse(BaseModel):
object: str = Field(default="list", description="Object type")
data: List[ModelInfo] = Field(..., description="List of models")
class OllamaChatRequest(BaseModel):
"""Request shape accepted by Ollama's /api/chat endpoint."""
model: str = Field(default="rkllm-model")
messages: List[ChatMessage] = Field(...)
stream: bool = Field(default=True)
options: Optional[Dict[str, Any]] = Field(default=None)
keep_alive: Optional[Union[str, int]] = Field(default=None)
class OllamaGenerateRequest(BaseModel):
"""Request shape accepted by Ollama's /api/generate endpoint."""
model: str = Field(default="rkllm-model")
prompt: str = Field(default="")
system: Optional[str] = Field(default=None)
stream: bool = Field(default=True)
options: Optional[Dict[str, Any]] = Field(default=None)
keep_alive: Optional[Union[str, int]] = Field(default=None)
# ==================== Global State Management ====================
class RequestState:
"""State management for individual requests"""
def __init__(self, request_id: str):
self.request_id = request_id
self.text_queue = []
self.state = -1
self.completed = threading.Event()
self.lock = threading.Lock()
self.full_response = ""
self.error = None
self.start_time = time.time()
# Global variables
request_lock = threading.Lock()
active_requests = 0
request_states: Dict[str, RequestState] = {}
rkllm_model = None
executor = None
api_model_name = "rkllm-model"
# Server configuration
class ServerConfig:
def __init__(self):
self.max_context_len = 2048 # Default context length
self.default_temperature = 0.8 # Default temperature
self.default_top_p = 0.9 # Default top_p
self.default_top_k = 1 # Default top_k
self.default_max_tokens = 512 # Default max tokens
self.max_concurrent_requests = 2 # Max concurrent requests
self.timeout_seconds = 120 # Timeout in seconds
config = ServerConfig()
# ==================== RKLLM Callback Function ====================
def callback_impl(result, userdata, state):
"""RKLLM callback function implementation"""
if not userdata:
return 0
try:
request_id = ctypes.cast(userdata, ctypes.c_char_p).value.decode('utf-8')
if request_id not in request_states:
return 0
req_state = request_states[request_id]
with req_state.lock:
if state == LLMCallState.RKLLM_RUN_FINISH:
req_state.state = state
req_state.completed.set()
elif state == LLMCallState.RKLLM_RUN_ERROR:
req_state.state = state
req_state.error = "RKLLM runtime error"
req_state.completed.set()
elif state == LLMCallState.RKLLM_RUN_NORMAL:
req_state.state = state
if result and result.contents.text:
try:
text = result.contents.text.decode('utf-8', errors='ignore')
req_state.text_queue.append(text)
req_state.full_response += text
except Exception as e:
print(f"Callback decoding error: {e}")
return 0
except Exception as e:
print(f"Callback function error: {e}")
return -1
callback = callback_type(callback_impl)
rkllm_callback = RKLLMCallback()
rkllm_callback.result_callback = callback
rkllm_callback.result_userdata = None
rkllm_callback.tokenizer_callback = None
rkllm_callback.tokenizer_userdata = None
rkllm_callback.embed_callback = None
rkllm_callback.embed_userdata = None
# ==================== RKLLM Model Manager ====================
class RKLLMModel:
"""RKLLM model manager class"""
def __init__(self, model_path: str, platform: str = "rk3588"):
self.model_path = model_path
self.platform = platform
self.handle = ctypes.c_void_p()
self.initialized = False
self.model_lock = threading.Lock()
# Configuration parameters
self.max_context_len = config.max_context_len
self.default_temperature = config.default_temperature
self.default_top_p = config.default_top_p
self.default_top_k = config.default_top_k
self.default_max_tokens = config.default_max_tokens
def initialize(self):
"""Initialize the model"""
with self.model_lock:
try:
print(f"Initializing RKLLM model: {self.model_path}")
# Prepare model parameters
rkllm_param = RKLLMParam()
rkllm_param.model_path = ctypes.c_char_p(self.model_path.encode('utf-8'))
rkllm_param.max_context_len = self.max_context_len
rkllm_param.max_new_tokens = self.default_max_tokens
rkllm_param.n_keep = 0
rkllm_param.top_k = self.default_top_k # Use default top_k
rkllm_param.top_p = self.default_top_p
rkllm_param.temperature = self.default_temperature
rkllm_param.repeat_penalty = 1.1
rkllm_param.frequency_penalty = 0.0
rkllm_param.presence_penalty = 0.0
rkllm_param.mirostat = 0
rkllm_param.mirostat_tau = 5.0
rkllm_param.mirostat_eta = 0.1
rkllm_param.skip_special_token = True
rkllm_param.ignore_eos_token = False
rkllm_param.is_async = False
# Extended parameters - critical settings to avoid GGML errors
rkllm_param.extend_param.base_domain_id = 0
rkllm_param.extend_param.embed_flash = 0 # Set to 0 to avoid GGML assertion error
rkllm_param.extend_param.n_batch = 1
rkllm_param.extend_param.use_cross_attn = 0
# Set CPU mask based on platform
if self.platform.lower() in ["rk3576", "rk3588", "rk3588s"]:
cpu_mask = 0xF0 # CPU 4-7 (big cores)
else:
cpu_mask = 0x0F # CPU 0-3
rkllm_param.extend_param.enabled_cpus_mask = cpu_mask
rkllm_param.extend_param.enabled_cpus_num = cpu_mask.bit_count()
# Set function prototypes
rkllm_init = rkllm_lib.rkllm_init
rkllm_init.argtypes = [
ctypes.POINTER(ctypes.c_void_p),
ctypes.POINTER(RKLLMParam),
ctypes.POINTER(RKLLMCallback),
]
rkllm_init.restype = ctypes.c_int
# Call initialization
ret = rkllm_init(
ctypes.byref(self.handle),
ctypes.byref(rkllm_param),
ctypes.byref(rkllm_callback),
)
if ret != 0:
raise RuntimeError(f"RKLLM initialization failed with error code: {ret}")
self.initialized = True
print("✅ RKLLM model initialized successfully!")
return True
except Exception as e:
print(f"❌ Model initialization failed: {e}")
return False
def generate(self, prompt: str, request_id: str, temperature: float = None,
top_p: float = None, top_k: int = None, max_tokens: int = None) -> int:
"""Generate text with the model"""
with self.model_lock:
if not self.initialized:
raise RuntimeError("Model not initialized")
try:
# First update the model parameters if top_k is provided
if top_k is not None:
# Need to update the model's top_k parameter
# Note: RKLLM might require reinitialization or parameter update
# For now, we'll log it and use the value in generation
print(f"[{request_id}] Setting top_k to {top_k}")
# Update RKLLM parameter structure for this generation
# This might require calling rkllm_set_param or similar function
# For simplicity, we'll use the existing handle with default params
# In a real implementation, you might need to:
# 1. Call a parameter update function if available
# 2. Or handle it differently based on RKLLM API
pass
# Prepare input
rkllm_input = RKLLMInput()
rkllm_input.role = ctypes.c_char_p(b"user")
rkllm_input.enable_thinking = False
rkllm_input.input_type = RKLLMInputType.RKLLM_INPUT_PROMPT
rkllm_input.input_data.prompt_input = ctypes.c_char_p(prompt.encode('utf-8'))
# Prepare inference parameters
infer_param = RKLLMInferParam()
infer_param.mode = RKLLMInferMode.RKLLM_INFER_GENERATE
infer_param.lora_params = None
infer_param.prompt_cache_params = None
infer_param.sampling_params = None
infer_param.keep_history = 0
infer_param.max_new_tokens = max_tokens if max_tokens else 0
# Prepare user data
userdata_ptr = None
if request_id:
userdata_ptr = ctypes.c_char_p(request_id.encode('utf-8'))
# Set up run function
rkllm_run = rkllm_lib.rkllm_run
rkllm_run.argtypes = [
ctypes.c_void_p,
ctypes.POINTER(RKLLMInput),
ctypes.POINTER(RKLLMInferParam),
ctypes.c_void_p
]
rkllm_run.restype = ctypes.c_int
# Call run function
ret = rkllm_run(self.handle, ctypes.byref(rkllm_input),
ctypes.byref(infer_param), userdata_ptr)
return ret
except Exception as e:
print(f"❌ Generation error: {e}")
return -1
def release(self):
"""Release model resources"""
with self.model_lock:
if self.initialized and self.handle:
try:
rkllm_destroy = rkllm_lib.rkllm_destroy
rkllm_destroy.argtypes = [ctypes.c_void_p]
rkllm_destroy.restype = ctypes.c_int
ret = rkllm_destroy(self.handle)
if ret != 0:
print(f"⚠ rkllm_destroy returned error code: {ret}")
self.initialized = False
self.handle = None
print("✅ Model resources released")
except Exception as e:
print(f"❌ Error releasing model resources: {e}")
# ==================== Helper Functions ====================
def message_text(content: Union[str, List[Dict[str, Any]]]) -> str:
"""Convert OpenAI/Ollama text or content-part messages to plain text."""
if isinstance(content, str):
return content
parts = []
for part in content:
if isinstance(part, dict) and part.get("type") == "text":
parts.append(str(part.get("text", "")))
return "".join(parts)
def build_prompt(messages: List[ChatMessage]) -> str:
"""Build prompt from messages"""
prompt = ""
for msg in messages:
content = message_text(msg.content)
if msg.role == 'system':
prompt += f"System: {content}\n\n"
elif msg.role == 'user':
prompt += f"Human: {content}\n"
elif msg.role == 'assistant':
prompt += f"Assistant: {content}\n"
# Ensure it ends with Assistant:
if not prompt.strip().endswith("Assistant:"):
prompt += "Assistant:"
return prompt
def estimate_tokens(text: str) -> int:
"""Estimate token count (rough approximation)"""
if not text:
return 0
# Simple estimation: Chinese characters ~1.5 tokens, others ~0.3 tokens
chinese_chars = sum(1 for c in text if '\u4e00' <= c <= '\u9fff')
other_chars = len(text) - chinese_chars
return int(chinese_chars * 1.5 + other_chars * 0.3)
def reserve_request_slot() -> None:
"""Reserve one inference slot or raise an API-compatible 429 error."""
global active_requests
with request_lock:
if active_requests >= config.max_concurrent_requests:
raise HTTPException(
status_code=429,
detail={
"error": {
"message": "Too many requests, please try again later",
"type": "rate_limit_error",
"code": "rate_limit_exceeded"
}
}
)
active_requests += 1
def release_request_slot() -> None:
"""Release a previously reserved inference slot."""
global active_requests
with request_lock:
active_requests = max(0, active_requests - 1)
def ollama_request_from_chat(request: OllamaChatRequest) -> ChatCompletionRequest:
"""Translate Ollama options into the common internal request format."""
options = request.options or {}
def positive_int(name: str, default: int) -> int:
value = options.get(name, default)
return value if isinstance(value, int) and value > 0 else default
top_k = options.get("top_k")
if not isinstance(top_k, int) or top_k < 1:
top_k = None
stop = options.get("stop")
if isinstance(stop, str):
stop = [stop]
elif not isinstance(stop, list):
stop = None
return ChatCompletionRequest(
model=request.model,
messages=request.messages,
temperature=options.get("temperature", config.default_temperature),
top_p=options.get("top_p", config.default_top_p),
top_k=top_k,
max_tokens=positive_int("num_predict", config.default_max_tokens),
stop=stop,
stream=request.stream,
)
def ollama_request_from_generate(request: OllamaGenerateRequest) -> ChatCompletionRequest:
messages = []
if request.system:
messages.append(ChatMessage(role="system", content=request.system))
messages.append(ChatMessage(role="user", content=request.prompt))
return ollama_request_from_chat(
OllamaChatRequest(
model=request.model,
messages=messages,
stream=request.stream,
options=request.options,
keep_alive=request.keep_alive,
)
)
def ollama_created_at() -> str:
"""Return Ollama's RFC3339-style timestamp without an extra dependency."""
return time.strftime("%Y-%m-%dT%H:%M:%S.000000000Z", time.gmtime())
def ollama_chunk(model: str, content: str, done: bool = False,
prompt_tokens: int = 0, completion_tokens: int = 0) -> str:
payload = {
"model": model,
"created_at": ollama_created_at(),
"message": {"role": "assistant", "content": content},
"done": done,
}
if done:
payload.update({
"done_reason": "stop",
"prompt_eval_count": prompt_tokens,
"eval_count": completion_tokens,
})
return json.dumps(payload, ensure_ascii=False) + "\n"
def process_chat_completion(request: ChatCompletionRequest, request_id: str) -> RequestState:
"""Process chat completion request"""
global rkllm_model
# Create request state
req_state = RequestState(request_id)
request_states[request_id] = req_state
try:
# Build prompt
prompt = build_prompt(request.messages)
# Print debug information
print(f"[{request_id}] Processing request:")
print(f" Prompt length: {len(prompt)} characters")
print(f" Temperature: {request.temperature}")
print(f" Top-p: {request.top_p}")
print(f" Top-k: {request.top_k}")
print(f" Max tokens: {request.max_tokens}")
# Run model inference
ret = rkllm_model.generate(
prompt=prompt,
request_id=request_id,
temperature=request.temperature,
top_p=request.top_p,
top_k=request.top_k,
max_tokens=request.max_tokens
)
if ret != 0:
req_state.error = f"Model inference failed with code: {ret}"
req_state.completed.set()
return req_state
# Wait for completion
timeout = config.timeout_seconds
print(f"[{request_id}] Waiting for inference completion (timeout: {timeout}s)...")
if not req_state.completed.wait(timeout=timeout):
req_state.error = f"Inference timeout ({timeout}s)"
print(f"✗ [{request_id}] {req_state.error}")
elapsed = time.time() - req_state.start_time
print(f"✅ [{request_id}] Inference completed in {elapsed:.2f}s")
return req_state
except Exception as e:
error_msg = f"Error processing request {request_id}: {str(e)}"
print(f"✗ {error_msg}")
req_state.error = error_msg
req_state.completed.set()
return req_state
# ==================== Application Lifecycle Management ====================
@asynccontextmanager
async def lifespan(app: FastAPI):
"""FastAPI lifespan context manager"""
global rkllm_model, executor
# Startup
print("=" * 60)
print("Starting RKLLM OpenAI API Server")
print("=" * 60)
# Initialize thread pool
executor = ThreadPoolExecutor(
max_workers=config.max_concurrent_requests + 2,
thread_name_prefix="rkllm_worker"
)
print("✅ Thread pool initialized")
# Initialize model
try:
rkllm_model = RKLLMModel(args.rkllm_model_path, args.target_platform)
# Apply configuration parameters
if args.max_context_len:
config.max_context_len = args.max_context_len
rkllm_model.max_context_len = args.max_context_len
if args.default_temperature:
config.default_temperature = args.default_temperature
rkllm_model.default_temperature = args.default_temperature
if args.default_top_p:
config.default_top_p = args.default_top_p
rkllm_model.default_top_p = args.default_top_p
if args.default_top_k:
config.default_top_k = args.default_top_k
rkllm_model.default_top_k = args.default_top_k
if args.default_max_tokens:
config.default_max_tokens = args.default_max_tokens
rkllm_model.default_max_tokens = args.default_max_tokens
if args.max_concurrent:
config.max_concurrent_requests = args.max_concurrent
rkllm_model.initialize()
if not rkllm_model.initialized:
raise RuntimeError("RKLLM model did not initialize")
display_host = "127.0.0.1" if args.host in ("0.0.0.0", "::") else args.host
print("=" * 60)
print("✅ API is ready and listening")
print(f" OpenAI API: http://{display_host}:{args.port}/v1")
print(f" Ollama API: http://{display_host}:{args.port}/api")
print(f" API docs: http://{display_host}:{args.port}/docs")
print(" Terminal chat: enabled (type /help for commands)")
print("=" * 60)
except Exception as e:
print(f"❌ Failed to initialize model: {e}")
print("Please check:")
print("1. Model file exists and is accessible")
print("2. RKLLM runtime is properly installed")
print("3. OpenCL drivers are installed")
raise
yield
# Shutdown
print("\nShutting down server...")
# Clean up request states
request_states.clear()
# Shutdown thread pool
if executor:
executor.shutdown(wait=False)
print("✅ Thread pool shut down")
# Release model
if rkllm_model:
rkllm_model.release()
# ==================== FastAPI Application ====================
app = FastAPI(
title="RKLLM OpenAI API Server",
version="1.0.0",
description="OpenAI API compatible server for RKLLM models",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ==================== API Endpoints ====================
@app.get("/")
async def root():
"""Root endpoint"""
return {
"message": "RKLLM OpenAI API Server",
"status": "running",
"model": api_model_name,
"platform": args.target_platform,
"version": "1.0.0",
"endpoints": {
"GET /": "Server information",
"GET /health": "Health check",
"GET /v1/models": "List models",
"POST /v1/chat/completions": "Chat completion"
}
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy" if rkllm_model and rkllm_model.initialized else "unhealthy",
"model_initialized": rkllm_model.initialized if rkllm_model else False,
"active_requests": active_requests,
"max_concurrent": config.max_concurrent_requests,
"timestamp": int(time.time())
}
@app.get("/v1/models", response_model=ModelsListResponse)
async def list_models():
"""List available models"""
return ModelsListResponse(
data=[
ModelInfo(
id=api_model_name,
created=int(time.time()),
owned_by="rkllm"
)
]
)
@app.post("/v1/chat/completions")
async def create_chat_completion(request: ChatCompletionRequest):
"""Create chat completion - Fully OpenAI API compatible"""
reserve_request_slot()
try:
request_id = f"chatcmpl-{uuid.uuid4().hex[:24]}"
created = int(time.time())
print(f"[{request_id}] New request: stream={request.stream}, messages={len(request.messages)}")
if request.top_k is not None:
print(f"[{request_id}] Top-k parameter: {request.top_k}")
if request.stream:
# Streaming response
async def generate_stream():
nonlocal request_id
try:
# Submit task to thread pool
future = executor.submit(process_chat_completion, request, request_id)
# Send initial message - FIXED: use model_dump_json() instead of json()
initial_chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request.model,
choices=[
ChatCompletionStreamResponseChoice(
index=0,
delta=DeltaMessage(role="assistant"),
finish_reason=None
)
]
)
yield f"data: {initial_chunk.model_dump_json(exclude_unset=True, ensure_ascii=False)}\n\n"
# Stream results
start_time = time.time()
last_activity = start_time
while True:
if request_id in request_states:
req_state = request_states[request_id]
with req_state.lock:
if req_state.text_queue:
for text in req_state.text_queue:
chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,
model=request.model,
choices=[
ChatCompletionStreamResponseChoice(
index=0,
delta=DeltaMessage(content=text),
finish_reason=None
)
]
)
# FIXED: use model_dump_json() instead of json()
yield f"data: {chunk.model_dump_json(exclude_unset=True, ensure_ascii=False)}\n\n"
last_activity = time.time()
# Clear sent text
req_state.text_queue.clear()
# Check if completed
if req_state.completed.is_set():
if req_state.error:
error_data = {
"error": {
"message": req_state.error,
"type": "server_error"
}
}
yield f"data: {json.dumps(error_data, ensure_ascii=False)}\n\n"
break
# Check timeout
if time.time() - last_activity > 30: # 30 seconds no activity
print(f"[{request_id}] Streaming response timeout")
break
# Brief wait
await asyncio.sleep(0.05)
# Send completion marker - FIXED: use model_dump_json() instead of json()
done_chunk = ChatCompletionStreamResponse(
id=request_id,
created=created,