-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi__init__.py
More file actions
1362 lines (1185 loc) · 47.7 KB
/
api__init__.py
File metadata and controls
1362 lines (1185 loc) · 47.7 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
import asyncio
import json
import sys
from datetime import datetime
from functools import wraps
from typing import Any, Dict, List, Optional
from uuid import UUID, uuid4
from quart import Quart, Response, jsonify, request
from quart_cors import cors, route_cors
from webai_element_sdk import Context, Element
from webai_element_sdk.comms.messages import Frame, Preview
from webai_element_sdk.element import ElementInputs, ElementOutputs, Input, Output
from webai_element_sdk.element.settings import (
ElementSettings,
NumberSetting,
TextSetting,
)
from werkzeug.exceptions import HTTPException
# Import metrics classes
from .metrics import TPS, TTFT, TPSSummary, TTFTSummary
# Import utility functions
from .utils import (
PromptRequest,
_handle_non_streaming_response,
_parse_request_json,
_validate_parameter,
check_queue_capacity,
generate_openai_error_response,
queue_capacity_check,
token_required,
validate_base64_image,
)
class Inputs(ElementInputs):
in1 = Input[Frame]()
class Outputs(ElementOutputs):
preview = Output[Preview]()
out1 = Output[Frame]()
class Settings(ElementSettings):
api_key = TextSetting(
name="api_key",
display_name="API key",
description="An optional key to help prevent unwanted network access. A default key is provided.",
default="",
sensitive=True,
required=False,
hints=["advanced"],
)
timeout = NumberSetting[float](
name="timeout",
display_name="Endpoint Timeout Seconds",
description="Timeout for SSE endpoint, default is 0 or no timeout",
default=0.0,
min_value=0.0,
hints=["advanced"],
)
max_concurrent_requests = NumberSetting[int](
name="max_requests",
display_name="Maximum Concurrent Requests",
description="Maximum number of requests that can be processed simultaneously in parallel",
default=1,
min_value=1,
hints=["advanced"],
)
max_queued_requests = NumberSetting[int](
name="max_queued_requests",
display_name="Maximum Queued Requests",
description="Maximum number of requests that can be queued waiting for processing. Additional requests will be rejected with 429 status.",
default=10,
min_value=1,
hints=["advanced"],
)
element = Element(
id=UUID("68f81646-53de-4952-b171-6ee7cdbd9fb0"),
name="api",
display_name="API",
description="Facilitates communication with a Large Language Model. It sends a user-defined prompt (instruction or question) to the model, receives the model's generated response, and returns the response.",
version="0.1.7",
inputs=Inputs(),
outputs=Outputs(),
settings=Settings(),
init_run=True,
)
class ElementPreviewServer(Quart):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.startup_event = asyncio.Event()
async def startup(self):
await super().startup()
self.startup_event.set()
app = ElementPreviewServer(__name__)
app = cors(app, allow_origin="*")
# Middleware for common request handling
@app.before_request
async def handle_common_request_logic():
"""
Middleware to handle CORS preflight requests globally.
"""
# Handle CORS preflight requests globally
if request.method == "OPTIONS":
response = Response("", status=200)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = (
"Content-Type, X-API-Key, Authorization"
)
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
return response
requests: asyncio.Queue[PromptRequest] = asyncio.Queue()
curr_request: Optional[PromptRequest] = None
request_ids: asyncio.Queue[int] = asyncio.Queue()
curr_requests: List[Optional[PromptRequest]] = []
num_requests: int = 0
last_request_id: Optional[int] = None
api_key_list: List[str] = []
initialFrame: bool = True
prev_token_count: int = 0
api_tokens: List[str] = []
endpoint_timeout: float = 0.0
first_token: bool = True
max_queued_requests: int = 10
request_ttft: float = 0
ttft: List[TTFT] = []
tps: List[TPS] = []
tps_summary: TPSSummary = TPSSummary()
ttft_summary: TTFTSummary = TTFTSummary()
def set_api_token(secret_value: str):
global api_tokens
api_tokens = secret_value.split(",")
def get_api_tokens():
"""Helper function to get current API tokens for the decorator."""
global api_tokens
return api_tokens
async def check_queue_capacity_wrapper():
"""Wrapper function for queue capacity check that uses global variables."""
global max_queued_requests, requests
return await check_queue_capacity(requests, max_queued_requests)
# Create decorator instances with the helper functions
token_required_decorator = token_required(get_api_tokens)
queue_capacity_check_decorator = queue_capacity_check(check_queue_capacity_wrapper)
@element.startup
async def startup(ctx: Context[Inputs, Outputs, Settings]):
global api_tokens, endpoint_timeout, request_ids, curr_requests, ttft, tps, first_token, max_queued_requests
user_api_key = ctx.settings.api_key.value
endpoint_timeout = ctx.settings.timeout.value
max_queued_requests = ctx.settings.max_queued_requests.value
first_token = True
api_tokens = (
[]
if user_api_key.strip() == ""
else [t.strip() for t in user_api_key.split(",")]
)
tps = [TPS()] * ctx.settings.max_concurrent_requests.value
ttft = [TTFT()] * ctx.settings.max_concurrent_requests.value
curr_requests = [None] * ctx.settings.max_concurrent_requests.value
for i in range(ctx.settings.max_concurrent_requests.value):
await request_ids.put(i)
port = ctx.preview_port
print(f"Serving on {port}")
asyncio.create_task(app.run_task(host="0.0.0.0", port=port, debug=False))
@element.executor
async def run(ctx: Context[Inputs, Outputs, Settings]):
"""
OpenAI Response Structure
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The 2020 World Series was played in Texas at Globe Life Field in Arlington.",
"role": "assistant"
},
"logprobs": null
}
],
"created": 1677664795,
"id": "chatcmpl-7QyqpwdfhqwajicIEznoc6Q47XAyW",
"model": "gpt-4o-mini",
# we can add these later if they need them
"usage": {
"completion_tokens": 17,
"prompt_tokens": 57,
"total_tokens": 74
}
}
"""
global initialFrame, prev_token_count, curr_request, first_token, ttft, request_ttft, tps, curr_requests, request_ids, num_requests, tps_summary, ttft_summary, last_request_id
while True:
if num_requests == 0:
request_id = await request_ids.get()
last_request_id = request_id
curr_requests[request_id] = await requests.get()
num_requests += 1
await tps[request_id].start()
await ttft[request_id].start()
current_request = curr_requests[request_id]
yield ctx.outputs.out1(
Frame(
ndframe=None,
other_data={
"api": (
current_request.messages
if current_request is not None
else []
),
"requestId": request_id,
},
)
)
else:
# there's capacity for another client, check if there is one waiting
if (num_requests > 0) and (
num_requests < ctx.settings.max_concurrent_requests.value
):
try:
req = requests.get_nowait()
request_id = await request_ids.get()
curr_requests[request_id] = req
num_requests += 1
await ttft[request_id].start()
current_request = curr_requests[request_id]
yield ctx.outputs.out1(
Frame(
ndframe=None,
other_data={
"api": (
current_request.messages
if current_request is not None
else []
),
"requestId": request_id,
},
)
)
except asyncio.QueueEmpty:
pass
frame = ctx.inputs.in1.value # TODO: make sure we don't propogate old data
if (
frame is None
or "init" in frame.other_data
or "message" not in frame.other_data
or (frame.other_data["token_number"] == prev_token_count)
):
return
request_id = frame.other_data.get("requestId", last_request_id)
if frame.other_data.get("token_number") == 1:
ttft_value = await ttft[request_id].stop()
await tps[request_id].start()
await ttft_summary.add(ttft_value)
output = {
"choices": [
{
"finish_reason": (
None if not frame.other_data["done"] else "stop"
),
"index": 0,
"message": {
"content": frame.other_data["message"],
"role": "assistant",
},
"logprobs": None,
}
],
"created": 1677664795,
"id": "webaichat-6ee7cdbd9fb0",
"model": "nav",
"object": "chat.completion",
}
# Only include usage when the frame is done (final frame)
if frame.other_data["done"]:
usage_data = frame.other_data.get("usage", {})
output["usage"] = usage_data
if usage_data: # Only print if there's actual usage data
print(f"API: Including usage data: {usage_data}")
if frame.other_data["done"] == True:
tps_value = await tps[request_id].stop(prev_token_count - 1)
await tps_summary.add(tps_value)
output["metrics"] = {"tps": tps_value, "ttft": ttft[request_id].value}
if curr_requests[request_id] is not None:
await curr_requests[request_id].queue.put(output)
await request_ids.put(request_id)
num_requests -= 1
curr_request = None
first_token = True
request_ttft = 0
prev_token_count = frame.other_data["token_number"]
continue
else:
prev_token_count = frame.other_data["token_number"]
if curr_requests[request_id] is not None:
await curr_requests[request_id].queue.put(output)
return
@app.route("/")
async def index():
return "Please use the API endpoint /prompt"
@app.route("/config")
@route_cors(
allow_headers=["Content-Type", "Access-Control-Allow-Origin"],
allow_methods=["GET"],
allow_origin=["*"],
)
async def config():
return jsonify({"type": "prompt", "endpoint": "/prompt"})
@app.route("/prompt", methods=["POST", "OPTIONS"])
@route_cors(
allow_headers=["Content-Type", "X-API-Key"],
allow_methods=["POST"],
)
@queue_capacity_check_decorator
async def prompt():
@token_required_decorator
async def handle_prompt():
global endpoint_timeout
# TODO: Check the key in the header and make sure it's in the setting's list
# api_key = request.headers.get("x-api-key")
# if api_key != user_api_key:
# abort(401, description="Invalid API key")
data = await request.get_json()
if "message" not in data:
return generate_openai_error_response(
status_code=400,
message="Missing 'message' field in JSON.",
error_type="invalid_request_error",
error_code="missing_field",
param="message",
)
# Pass the prompt
# should come in as a list in the openAI style
promptRequest = PromptRequest(data["message"])
queue = promptRequest.queue
await requests.put(promptRequest)
# Wait for responses and stream them out as they are received
# TODO: Are these the correct headers?
headers = {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
}
async def event_stream(queue: asyncio.Queue):
while True:
try:
response = await queue.get()
if "choices" not in response or not isinstance(
response["choices"], list
):
print("Invalid response format")
continue
# TODO: Structure response as OpenAPI chunk messages before sending back
yield json.dumps(response)
if response["choices"][-1].get("finish_reason") is not None:
break
except Exception as e:
print(f"API: Error: {e}")
continue
resp = Response(event_stream(queue), headers=headers)
resp.timeout = endpoint_timeout if endpoint_timeout > 0 else None
return resp
return await handle_prompt()
@app.route("/metrics", methods=["GET"])
async def metrics():
global ttft
return {
"ttft": await ttft_summary.summary(),
"tps": await tps_summary.summary(),
}
@app.route("/v1/chat/completions", methods=["POST", "OPTIONS"])
@route_cors(
allow_headers=["Content-Type", "X-API-Key", "Authorization"],
allow_methods=["POST", "OPTIONS"],
)
@queue_capacity_check_decorator
async def chat_completions():
"""OpenAI-compatible chat completions endpoint."""
@token_required_decorator
async def handle_chat_completion():
global endpoint_timeout
data, error_resp = await _parse_request_json(request)
if error_resp:
return error_resp
# Validate 'model'
model_val, error_resp = _validate_parameter(
data, "model", required=True, expected_type=str, can_be_empty_str=False
)
if error_resp:
return error_resp
# Validate 'messages' (already partially validated, enhancing here)
messages_val = data.get("messages")
if messages_val is None:
return generate_openai_error_response(
status_code=400,
message="Missing 'messages' field in request body.",
error_type="invalid_request_error",
error_code="missing_field",
param="messages",
)
if not isinstance(messages_val, list):
return generate_openai_error_response(
status_code=400,
message="'messages' must be a list.",
error_type="invalid_request_error",
error_code="invalid_type",
param="messages",
)
if not messages_val: # Check if list is empty
return generate_openai_error_response(
status_code=400,
message="'messages' list cannot be empty.",
error_type="invalid_request_error",
error_code="invalid_value",
param="messages",
)
validated_messages = []
for i, msg_dict in enumerate(messages_val):
param_path_prefix = f"messages.[{i}]"
if not isinstance(msg_dict, dict):
return generate_openai_error_response(
status_code=400,
message=f"Each message in 'messages' list must be an object. Error at index {i}.",
error_type="invalid_request_error",
error_code="invalid_type",
param=param_path_prefix,
)
role, error_resp = _validate_parameter(
msg_dict,
"role",
required=True,
expected_type=str,
allowed_values=["system", "user", "assistant"],
param_path=f"{param_path_prefix}.role",
)
if error_resp:
return error_resp
content, error_resp = _validate_parameter(
msg_dict,
"content",
required=True,
expected_type=(str, list), # Ensure content can be str or list
can_be_empty_str=True,
param_path=f"{param_path_prefix}.content",
)
if error_resp:
return error_resp
# Normalize content based on type
if isinstance(content, str):
# String content - keep as is
normalized_msg = {"role": role, "content": content}
elif isinstance(content, list):
# List content - normalize input_text and input_image types
normalized_content = []
for item in content:
if item.get("type") == "input_text":
# Convert input_text to text format
normalized_content.append(
{"type": "text", "text": item.get("text", "")}
)
elif item.get("type") == "input_image":
# Convert input_image to image_url format
normalized_content.append(
{
"type": "image_url",
"image_url": {"url": item.get("image_url", "")},
}
)
else:
# Keep other formats as-is (text, image_url, etc.)
normalized_content.append(item)
normalized_msg = {"role": role, "content": normalized_content}
else:
# Other content types - keep as is
normalized_msg = {"role": role, "content": content}
# Validate base64 image data in normalized message
if isinstance(normalized_msg.get("content"), list):
for content_idx, content_item in enumerate(normalized_msg["content"]):
if (
isinstance(content_item, dict)
and content_item.get("type") == "image_url"
and isinstance(content_item.get("image_url"), dict)
):
image_url = content_item["image_url"].get("url", "")
if image_url:
param_path = f"{param_path_prefix}.content.[{content_idx}].image_url.url"
base64_err = validate_base64_image(image_url, param_path)
if base64_err:
return base64_err
validated_messages.append(normalized_msg)
# Parameters not yet supported - return 501
# Note skipped properties are not supported yet, but we're not blocking
# them as some 3rd party applications pass them in. For now they are just ignored
unsupported_params = [
"audio",
"function_call",
"functions",
"logit_bias",
"logprobs",
"metadata",
"modalities",
"parallel_tool_calls",
"prediction",
"reasoning_effort",
"response_format",
"seed",
"service_tier",
"stop",
"store",
"tool_choice",
"tools",
"top_logprobs",
"user",
"web_search_options",
"stream_options", # stream_options is only used when stream is true, but not supported yet
]
for param_name in unsupported_params:
if param_name in data and data.get(param_name) is not None:
return generate_openai_error_response(
status_code=501,
message=f"The parameter '{param_name}' is not yet supported.",
error_type="api_error",
error_code="feature_not_implemented",
param=param_name,
)
# Validate supported optional parameters
stream_val, error_resp = _validate_parameter(
data, "stream", required=False, expected_type=bool, default_value=False
)
if error_resp:
return error_resp
temperature_val, error_resp = _validate_parameter(
data,
"temperature",
required=False,
expected_type=(int, float),
default_value=1.0,
min_value=0.0,
max_value=2.0,
)
if error_resp:
return error_resp
top_p_val, error_resp = _validate_parameter(
data,
"top_p",
required=False,
expected_type=(int, float),
default_value=1.0,
min_value=0.0,
max_value=1.0,
) # OpenAI spec implies 0-1 range for top_p
if error_resp:
return error_resp
# Validate max_output_tokens parameter
max_output_tokens_val, error_resp = _validate_parameter(
data,
"max_output_tokens",
required=False,
expected_type=int,
min_value=1,
max_value=100000, # Reasonable upper limit
)
if error_resp:
return error_resp
print(
f"API: Normalized messages for /v1/chat/completions: {validated_messages}"
)
try:
promptRequest = PromptRequest(validated_messages)
queue = promptRequest.queue
await requests.put(promptRequest)
except Exception as e:
print(f"API: Error creating or queuing prompt request: {e}")
return generate_openai_error_response(
status_code=500,
message=f"Error processing request: {str(e)}",
error_type="api_error",
error_code="internal_error",
)
if not stream_val:
return await _handle_non_streaming_response(queue, "chat.completion")
# For streaming responses, set up SSE headers
headers = {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
}
async def event_stream(queue: asyncio.Queue):
while True:
try:
response = await queue.get()
if "choices" not in response or not isinstance(
response["choices"], list
):
print("Invalid response format")
continue
# Format response to match OpenAPI's structure
openai_response = {
"id": response["id"],
"object": "chat.completion",
"created": response["created"],
"model": response["model"],
"choices": response["choices"],
"usage": response.get("usage", {}),
}
if "metrics" in response:
openai_response["metrics"] = response["metrics"]
# Format as proper SSE with data: prefix
yield f"data: {json.dumps(openai_response)}\n\n"
if response["choices"][-1].get("finish_reason") is not None:
break
except Exception as e:
print(f"API: Error: {e}")
continue
resp = Response(event_stream(queue), headers=headers)
resp.timeout = endpoint_timeout if endpoint_timeout > 0 else None
return resp
return await handle_chat_completion()
# https://platform.openai.com/docs/api-reference/responses
@app.route("/v1/responses", methods=["POST", "OPTIONS"])
@route_cors(
allow_headers=["Content-Type", "X-API-Key", "Authorization"],
allow_methods=["POST", "OPTIONS"],
)
@queue_capacity_check_decorator
async def responses():
@token_required_decorator
async def handle_response():
global endpoint_timeout
data, error_resp = await _parse_request_json(request)
if error_resp:
return error_resp
# Required fields validation
input_val, error_resp = _validate_parameter(
data,
"input",
required=True,
expected_type=(str, list),
can_be_empty_str=False,
)
if error_resp:
return error_resp
model_val, error_resp = _validate_parameter(
data, "model", required=True, expected_type=str, can_be_empty_str=False
)
if error_resp:
return error_resp
# Check for parameters that are not yet supported
unsupported_params = [
"parallel_tool_calls",
"background",
"include",
"instructions",
"reasoning",
"previous_response_id",
"service_tier",
"store",
"tools",
"tool_choice",
"user",
"truncation",
]
for param_name in unsupported_params:
if param_name in data and data.get(param_name) is not None:
return generate_openai_error_response(
status_code=501,
message=f"The '{param_name}' parameter is not yet supported.",
error_type="api_error",
error_code="feature_not_implemented",
param=param_name,
)
# Optional fields validation
stream_val = data.get("stream", False)
if not isinstance(stream_val, bool):
return generate_openai_error_response(
status_code=400,
message="'stream' must be a boolean.",
error_type="invalid_request_error",
error_code="invalid_type",
param="stream",
)
temperature_val = data.get("temperature", 1.0)
if not isinstance(temperature_val, (int, float)) or not (
0.0 <= temperature_val <= 2.0
):
return generate_openai_error_response(
status_code=400,
message="'temperature' must be a number between 0.0 and 2.0.",
error_type="invalid_request_error",
error_code="invalid_value",
param="temperature",
)
text_val = data.get("text")
if text_val is not None and not isinstance(text_val, dict):
return generate_openai_error_response(
status_code=400,
message="'text' must be an object.",
error_type="invalid_request_error",
error_code="invalid_type",
param="text",
)
top_p_val = data.get("top_p", 1.0)
if not isinstance(top_p_val, (int, float)):
return generate_openai_error_response(
status_code=400,
message="'top_p' must be a number.",
error_type="invalid_request_error",
error_code="invalid_type",
param="top_p",
)
# Create a prompt request with a single message
messages = []
if isinstance(input_val, str):
messages = [{"role": "user", "content": input_val}]
elif isinstance(input_val, list):
# Normalize the input format for downstream compatibility
normalized_messages = []
for msg in input_val:
if isinstance(msg, dict) and "role" in msg and "content" in msg:
# Handle message object format
normalized_msg = {"role": msg["role"]}
content = msg["content"]
# Normalize content items to the format downstream element expects
normalized_content = []
for item in content:
if item.get("type") == "input_text":
# Convert input_text to text format
normalized_content.append(
{"type": "text", "text": item.get("text", "")}
)
elif item.get("type") == "input_image":
# Convert input_image to image_url format
normalized_content.append(
{
"type": "image_url",
"image_url": {"url": item.get("image_url", "")},
}
)
else:
# Keep other formats as-is (text, image_url, etc.)
normalized_content.append(item)
normalized_msg["content"] = normalized_content
# Validate base64 image data in normalized message
if isinstance(normalized_msg.get("content"), list):
for content_idx, content_item in enumerate(
normalized_msg["content"]
):
if (
isinstance(content_item, dict)
and content_item.get("type") == "image_url"
and isinstance(content_item.get("image_url"), dict)
):
image_url = content_item["image_url"].get("url", "")
if image_url:
param_path = f"input.[{input_val.index(msg)}].content.[{content_idx}].image_url.url"
base64_err = validate_base64_image(
image_url, param_path
)
if base64_err:
return base64_err
normalized_messages.append(normalized_msg)
else:
# Handle direct content item format (fallback)
normalized_messages.append(msg)
messages = normalized_messages
print(f"API: Normalized messages for /v1/responses: {messages}")
else:
return generate_openai_error_response(
status_code=400,
message="'input' must be a string or a list.",
error_type="invalid_request_error",
error_code="invalid_type",
param="input",
)
try:
promptRequest = PromptRequest(messages)
queue = promptRequest.queue
await requests.put(promptRequest)
except Exception as e:
print(f"API: Error creating or queuing prompt request: {e}")
return generate_openai_error_response(
status_code=500,
message=f"Error processing request: {str(e)}",
error_type="api_error",
error_code="internal_error",
)
if not stream_val:
# For non-streaming, collect all responses and return a single response
return await _handle_non_streaming_response(queue, "response")
# For streaming responses, set up SSE headers
headers = {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
}
async def event_stream(queue: asyncio.Queue):
while True:
try:
response = await queue.get()
if "choices" not in response or not isinstance(
response["choices"], list
):
print("Invalid response format")
continue
# Format response to match OpenAI's structure
openai_response = {
"id": response["id"],
"object": "response",
"created": response["created"],
"model": response["model"],
"choices": [
{
"text": response["choices"][0]["message"]["content"]
or "",
"index": 0,
"logprobs": None,
"finish_reason": response["choices"][0].get(
"finish_reason"
),
}
],
"usage": response.get("usage", {}),
}
if "metrics" in response:
openai_response["metrics"] = response["metrics"]
# Format as proper SSE with data: prefix
yield f"data: {json.dumps(openai_response)}\n\n"
if response["choices"][-1].get("finish_reason") is not None:
break
except Exception as e:
print(f"API: Error: {e}")
continue
resp = Response(event_stream(queue), headers=headers)
resp.timeout = endpoint_timeout if endpoint_timeout > 0 else None
return resp
return await handle_response()
@app.route("/v1/completions", methods=["POST", "OPTIONS"])
@route_cors(
allow_headers=["Content-Type", "X-API-Key", "Authorization"],
allow_methods=["POST", "OPTIONS"],
)
@queue_capacity_check_decorator
async def completions():
"""Handle requests to the /v1/completions endpoint (OpenAI legacy)."""
@token_required_decorator
async def handle_completion():
data, error_response = await _parse_request_json(request)
if error_response:
return error_response
if not data:
return generate_openai_error_response(
400,
"Request body is missing or not valid JSON.",
"invalid_request_error",
)
# Define supported parameters for the legacy /v1/completions endpoint
SUPPORTED_COMPLETIONS_PARAMS = [
"model",
"prompt",
"temperature",
"top_p",
"stream",
"user",
"suffix",
"max_tokens",
"n",
"logprobs",
"echo",
"stop",
"presence_penalty",
"frequency_penalty",