Skip to content

Commit e200577

Browse files
author
Anil Pathak
committed
Support Accept text/event-stream in chat and completion endpoints, resolves abetlen#1083
1 parent 139257b commit e200577

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

llama_cpp/server/app.py

+57-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,36 @@ async def authenticate(
197197

198198

199199
@router.post(
200-
"/v1/completions", summary="Completion", dependencies=[Depends(authenticate)]
200+
"/v1/completions",
201+
summary="Completion",
202+
dependencies=[Depends(authenticate)],
203+
response_model= Union[
204+
llama_cpp.CreateCompletionResponse,
205+
str,
206+
],
207+
responses={
208+
"200": {
209+
"description": "Successful Response",
210+
"content": {
211+
"application/json": {
212+
"schema": {
213+
"anyOf": [
214+
{"$ref": "#/components/schemas/CreateCompletionResponse"}
215+
],
216+
"title": "Completion response, when stream=False",
217+
}
218+
},
219+
"text/event-stream":{
220+
"schema": {
221+
"type": "string",
222+
"title": "Server Side Streaming response, when stream=True. " +
223+
"See SSE format: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format", # noqa: E501
224+
"example": """data: {... see CreateCompletionResponse ...} \\n\\n data: ... \\n\\n ... data: [DONE]"""
225+
}
226+
}
227+
},
228+
}
229+
},
201230
)
202231
@router.post(
203232
"/v1/engines/copilot-codex/completions",
@@ -280,7 +309,33 @@ async def create_embedding(
280309

281310

282311
@router.post(
283-
"/v1/chat/completions", summary="Chat", dependencies=[Depends(authenticate)]
312+
"/v1/chat/completions", summary="Chat", dependencies=[Depends(authenticate)],
313+
response_model= Union[
314+
llama_cpp.ChatCompletion, str
315+
],
316+
responses={
317+
"200": {
318+
"description": "Successful Response",
319+
"content": {
320+
"application/json": {
321+
"schema": {
322+
"anyOf": [
323+
{"$ref": "#/components/schemas/CreateChatCompletionResponse"}
324+
],
325+
"title": "Completion response, when stream=False",
326+
}
327+
},
328+
"text/event-stream":{
329+
"schema": {
330+
"type": "string",
331+
"title": "Server Side Streaming response, when stream=True" +
332+
"See SSE format: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format", # noqa: E501
333+
"example": """data: {... see CreateChatCompletionResponse ...} \\n\\n data: ... \\n\\n ... data: [DONE]"""
334+
}
335+
}
336+
},
337+
}
338+
},
284339
)
285340
async def create_chat_completion(
286341
request: Request,

0 commit comments

Comments
 (0)