Skip to content

Commit

Permalink
Merge branch '561-long-lived-api-keys' of github.com:defenseunicorns/…
Browse files Browse the repository at this point in the history
…leapfrogai into 561-long-lived-api-keys
  • Loading branch information
gphorvath committed Jul 3, 2024
2 parents b19a0b8 + af4055f commit d490e27
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 113 deletions.
6 changes: 4 additions & 2 deletions src/leapfrogai_api/backend/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def stream_completion(model: Model, request: lfai.CompletionRequest):

await stream.wait_for_connection()
return StreamingResponse(
recv_completion(stream), media_type="text/event-stream"
recv_completion(stream, model.name), media_type="text/event-stream"
)


Expand Down Expand Up @@ -66,7 +66,9 @@ async def stream_chat_completion(model: Model, request: lfai.ChatCompletionReque
stream = stub.ChatCompleteStream(request)

await stream.wait_for_connection()
return StreamingResponse(recv_chat(stream), media_type="text/event-stream")
return StreamingResponse(
recv_chat(stream, model.name), media_type="text/event-stream"
)


async def stream_chat_completion_raw(
Expand Down
16 changes: 10 additions & 6 deletions src/leapfrogai_api/backend/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Helper functions for the OpenAI backend."""

import time
import uuid
from typing import BinaryIO, Iterator, AsyncGenerator, Any
import grpc
import leapfrogai_sdk as lfai
Expand All @@ -15,15 +17,16 @@

async def recv_completion(
stream: grpc.aio.UnaryStreamCall[lfai.CompletionRequest, lfai.CompletionResponse],
model: str,
):
async for c in stream:
yield (
"data: "
+ CompletionResponse(
id="foo",
id=str(uuid.uuid4()),
object="completion.chunk",
created=55,
model="mpt-7b-8k-chat",
created=int(time.time()),
model=model,
choices=[
CompletionChoice(
index=0,
Expand All @@ -48,16 +51,17 @@ async def recv_chat(
stream: grpc.aio.UnaryStreamCall[
lfai.ChatCompletionRequest, lfai.ChatCompletionResponse
],
model: str,
) -> AsyncGenerator[str, Any]:
"""Generator that yields chat completion responses as Server-Sent Events."""
async for c in stream:
yield (
"data: "
+ ChatCompletionResponse(
id="foo",
id=str(uuid.uuid4()),
object="chat.completion.chunk",
created=55,
model="mpt-7b-8k-chat",
created=int(time.time()),
model=model,
choices=[
ChatStreamChoice(
index=0,
Expand Down
Loading

0 comments on commit d490e27

Please sign in to comment.