-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (57 loc) · 2.17 KB
/
Copy pathapp.py
File metadata and controls
66 lines (57 loc) · 2.17 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
import time
from fastapi import Depends, FastAPI
from fastapi.responses import StreamingResponse
from dependencies import get_llm_service
from logger import jsonl_logger
from schemas import GenerateRequest, GenerateResponse
from services import LLMService
app = FastAPI()
@app.post("/generate")
async def generate(
body: GenerateRequest,
llm_service: LLMService = Depends(get_llm_service),
) -> GenerateResponse:
started_at = time.time()
llm_response = await llm_service.generate(body.prompt)
finished_at = time.time()
jsonl_logger.log(
{
"endpoint": "/generate",
"prompt": body.prompt,
"response": llm_response,
"llm_type": llm_service.get_type().value,
"started_at": started_at,
"finished_at": finished_at,
"duration_ms": (finished_at - started_at) * 1000,
}
)
return GenerateResponse(response=llm_response)
@app.post("/stream")
async def stream(
body: GenerateRequest,
llm_service: LLMService = Depends(get_llm_service),
) -> StreamingResponse:
async def stream_generator():
# NOTE: Here is a tradeoff between logging and latency.
# 1. Logging the full response after the stream is complete would add latency and use
# more memory because we need to keep the chunks in memory.
# 2. Logging the each chunk would add logging overhead and later we would need
# to aggregate the chunks.
started_at = time.time()
full_response: list[str] = []
async for chunk in await llm_service.stream(body.prompt):
yield chunk
full_response.append(chunk)
finished_at = time.time()
jsonl_logger.log(
{
"endpoint": "/stream",
"prompt": body.prompt,
"response": "".join(full_response),
"llm_type": llm_service.get_type().value,
"started_at": started_at,
"finished_at": finished_at,
"duration_ms": (finished_at - started_at) * 1000,
}
)
return StreamingResponse(stream_generator(), media_type="text/plain")