Skip to content

Commit f4fb0ce

Browse files
authored
fix: correct missing async with and format code
1 parent 6f9cfc3 commit f4fb0ce

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

llama_cpp/server/app.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from anyio import Lock
99
from functools import partial
10-
from typing import Iterator, List, Optional, Union, Dict
10+
from typing import List, Optional, Union, Dict
1111

1212
import llama_cpp
1313

@@ -154,11 +154,13 @@ def create_app(
154154

155155
return app
156156

157+
157158
def prepare_request_resources(
158159
body: CreateCompletionRequest | CreateChatCompletionRequest,
159160
llama_proxy: LlamaProxy,
160161
body_model: str,
161-
kwargs) -> llama_cpp.Llama:
162+
kwargs,
163+
) -> llama_cpp.Llama:
162164
if llama_proxy is None:
163165
raise HTTPException(
164166
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
@@ -199,7 +201,9 @@ async def get_event_publisher(
199201
server_settings.interrupt_requests if server_settings else False
200202
)
201203
async with contextlib.AsyncExitStack() as exit_stack:
202-
llama_proxy: LlamaProxy = await exit_stack.enter_async_context(contextlib.asynccontextmanager(get_llama_proxy)())
204+
llama_proxy: LlamaProxy = await exit_stack.enter_async_context(
205+
contextlib.asynccontextmanager(get_llama_proxy)()
206+
)
203207
llama = prepare_request_resources(body, llama_proxy, body_model, kwargs)
204208
async with inner_send_chan:
205209
try:
@@ -215,7 +219,9 @@ async def get_event_publisher(
215219
except anyio.get_cancelled_exc_class() as e:
216220
print("disconnected")
217221
with anyio.move_on_after(1, shield=True):
218-
print(f"Disconnected from client (via refresh/close) {request.client}")
222+
print(
223+
f"Disconnected from client (via refresh/close) {request.client}"
224+
)
219225
raise e
220226

221227

@@ -340,11 +346,15 @@ async def create_completion(
340346

341347
# handle regular request
342348
async with contextlib.AsyncExitStack() as exit_stack:
343-
llama_proxy: LlamaProxy = await exit_stack.enter_async_context(contextlib.asynccontextmanager(get_llama_proxy)())
349+
llama_proxy: LlamaProxy = await exit_stack.enter_async_context(
350+
contextlib.asynccontextmanager(get_llama_proxy)()
351+
)
344352
llama = prepare_request_resources(body, llama_proxy, body_model, kwargs)
345353

346354
if await request.is_disconnected():
347-
print(f"Disconnected from client (via refresh/close) before llm invoked {request.client}")
355+
print(
356+
f"Disconnected from client (via refresh/close) before llm invoked {request.client}"
357+
)
348358
raise HTTPException(
349359
status_code=status.HTTP_400_BAD_REQUEST,
350360
detail="Client closed request",
@@ -507,12 +517,16 @@ async def create_chat_completion(
507517
)
508518

509519
# handle regular request
510-
with contextlib.AsyncExitStack() as exit_stack:
511-
llama_proxy: LlamaProxy = await exit_stack.enter_async_context(contextlib.asynccontextmanager(get_llama_proxy)())
520+
async with contextlib.AsyncExitStack() as exit_stack:
521+
llama_proxy: LlamaProxy = await exit_stack.enter_async_context(
522+
contextlib.asynccontextmanager(get_llama_proxy)()
523+
)
512524
llama = prepare_request_resources(body, llama_proxy, body_model, kwargs)
513525

514526
if await request.is_disconnected():
515-
print(f"Disconnected from client (via refresh/close) before llm invoked {request.client}")
527+
print(
528+
f"Disconnected from client (via refresh/close) before llm invoked {request.client}"
529+
)
516530
raise HTTPException(
517531
status_code=status.HTTP_400_BAD_REQUEST,
518532
detail="Client closed request",

0 commit comments

Comments
 (0)