-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathstreaming_query.py
More file actions
777 lines (671 loc) · 26.4 KB
/
Copy pathstreaming_query.py
File metadata and controls
777 lines (671 loc) · 26.4 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
"""Handler for REST API call to provide answer to streaming query."""
import json
import logging
from typing import Annotated, Any, AsyncIterator, Iterator
import pydantic
from llama_stack_client import APIConnectionError
from llama_stack_client import AsyncLlamaStackClient # type: ignore
from llama_stack_client.types import UserMessage # type: ignore
from llama_stack_client.lib.agents.event_logger import interleaved_content_as_str
from llama_stack_client.types.shared import ToolCall
from llama_stack_client.types.shared.interleaved_content_item import TextContentItem
from fastapi import APIRouter, HTTPException, Request, Depends, status
from fastapi.responses import StreamingResponse
from auth import get_auth_dependency
from auth.interface import AuthTuple
from client import AsyncLlamaStackClientHolder
from configuration import configuration
import metrics
from models.requests import QueryRequest
from models.database.conversations import UserConversation
from models.responses import ReferencedDocument
from utils.endpoints import check_configuration_loaded, get_agent, get_system_prompt
from utils.mcp_headers import mcp_headers_dependency, handle_mcp_headers_with_toolgroups
from utils.metadata import parse_knowledge_search_metadata
from app.endpoints.query import (
get_rag_toolgroups,
is_input_shield,
is_output_shield,
is_transcripts_enabled,
store_transcript,
select_model_and_provider_id,
validate_attachments_metadata,
validate_conversation_ownership,
persist_user_conversation_details,
evaluate_model_hints,
)
logger = logging.getLogger("app.endpoints.handlers")
router = APIRouter(tags=["streaming_query"])
auth_dependency = get_auth_dependency()
def format_stream_data(d: dict) -> str:
"""
Format a dictionary as a Server-Sent Events (SSE) data string.
Parameters:
d (dict): The data to be formatted as an SSE event.
Returns:
str: The formatted SSE data string.
"""
data = json.dumps(d)
return f"data: {data}\n\n"
def stream_start_event(conversation_id: str) -> str:
"""
Yield the start of the data stream.
Format a Server-Sent Events (SSE) start event containing the
conversation ID.
Parameters:
conversation_id (str): Unique identifier for the
conversation.
Returns:
str: SSE-formatted string representing the start event.
"""
return format_stream_data(
{
"event": "start",
"data": {
"conversation_id": conversation_id,
},
}
)
def stream_end_event(metadata_map: dict) -> str:
"""
Yield the end of the data stream.
Format and return the end event for a streaming response,
including referenced document metadata and placeholder token
counts.
Parameters:
metadata_map (dict): A mapping containing metadata about
referenced documents.
Returns:
str: A Server-Sent Events (SSE) formatted string
representing the end of the data stream.
"""
# Create ReferencedDocument objects and convert them to serializable dict format
referenced_documents = []
for v in filter(
lambda v: ("docs_url" in v) and ("title" in v),
metadata_map.values(),
):
try:
doc = ReferencedDocument(doc_url=v["docs_url"], doc_title=v["title"])
referenced_documents.append(
{
"doc_url": str(
doc.doc_url
), # Convert AnyUrl to string for JSON serialization
"doc_title": doc.doc_title,
}
)
except (pydantic.ValidationError, ValueError) as e:
logger.warning(
"Skipping invalid referenced document with docs_url='%s', title='%s': %s",
v.get("docs_url", "<missing>"),
v.get("title", "<missing>"),
str(e),
)
continue
return format_stream_data(
{
"event": "end",
"data": {
"referenced_documents": referenced_documents,
"truncated": None, # TODO(jboos): implement truncated
"input_tokens": 0, # TODO(jboos): implement input tokens
"output_tokens": 0, # TODO(jboos): implement output tokens
},
"available_quotas": {}, # TODO(jboos): implement available quotas
}
)
def stream_build_event(chunk: Any, chunk_id: int, metadata_map: dict) -> Iterator[str]:
"""Build a streaming event from a chunk response.
This function processes chunks from the LLama Stack streaming response and formats
them into Server-Sent Events (SSE) format for the client. It handles two main
event types:
1. step_progress: Contains text deltas from the model inference process
2. step_complete: Contains information about completed tool execution steps
Args:
chunk: The streaming chunk from LLama Stack containing event data
chunk_id: The current chunk ID counter (gets incremented for each token)
Returns:
Iterator[str]: An iterable list of formatted SSE data strings with event information
"""
if hasattr(chunk, "error"):
yield from _handle_error_event(chunk, chunk_id)
return
event_type = chunk.event.payload.event_type
step_type = getattr(chunk.event.payload, "step_type", None)
if event_type in {"turn_start", "turn_awaiting_input"}:
yield from _handle_turn_start_event(chunk_id)
elif event_type == "turn_complete":
yield from _handle_turn_complete_event(chunk, chunk_id)
elif step_type == "shield_call":
yield from _handle_shield_event(chunk, chunk_id)
elif step_type == "inference":
yield from _handle_inference_event(chunk, chunk_id)
elif step_type == "tool_execution":
yield from _handle_tool_execution_event(chunk, chunk_id, metadata_map)
else:
yield from _handle_heartbeat_event(chunk_id)
# -----------------------------------
# Error handling
# -----------------------------------
def _handle_error_event(chunk: Any, chunk_id: int) -> Iterator[str]:
"""
Yield error event.
Yield a formatted Server-Sent Events (SSE) error event
containing the error message from a streaming chunk.
Parameters:
chunk_id (int): The unique identifier for the current
streaming chunk.
"""
yield format_stream_data(
{
"event": "error",
"data": {
"id": chunk_id,
"token": chunk.error["message"],
},
}
)
# -----------------------------------
# Turn handling
# -----------------------------------
def _handle_turn_start_event(chunk_id: int) -> Iterator[str]:
"""
Yield turn start event.
Yield a Server-Sent Event (SSE) token event indicating the
start of a new conversation turn.
Parameters:
chunk_id (int): The unique identifier for the current
chunk.
Yields:
str: SSE-formatted token event with an empty token to
signal turn start.
"""
yield format_stream_data(
{
"event": "token",
"data": {
"id": chunk_id,
"token": "",
},
}
)
def _handle_turn_complete_event(chunk: Any, chunk_id: int) -> Iterator[str]:
"""
Yield turn complete event.
Yields a Server-Sent Event (SSE) indicating the completion of a
conversation turn, including the full output message content.
Parameters:
chunk_id (int): The unique identifier for the current
chunk.
Yields:
str: SSE-formatted string containing the turn completion
event and output message content.
"""
yield format_stream_data(
{
"event": "turn_complete",
"data": {
"id": chunk_id,
"token": interleaved_content_as_str(
chunk.event.payload.turn.output_message.content
),
},
}
)
# -----------------------------------
# Shield handling
# -----------------------------------
def _handle_shield_event(chunk: Any, chunk_id: int) -> Iterator[str]:
"""
Yield shield event.
Processes a shield event chunk and yields a formatted SSE token
event indicating shield validation results.
Yields a "No Violation" token if no violation is detected, or a
violation message if a shield violation occurs. Increments
validation error metrics when violations are present.
"""
if chunk.event.payload.event_type == "step_complete":
violation = chunk.event.payload.step_details.violation
if not violation:
yield format_stream_data(
{
"event": "token",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": "No Violation",
},
}
)
else:
# Metric for LLM validation errors
metrics.llm_calls_validation_errors_total.inc()
violation = (
f"Violation: {violation.user_message} (Metadata: {violation.metadata})"
)
yield format_stream_data(
{
"event": "token",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": violation,
},
}
)
# -----------------------------------
# Inference handling
# -----------------------------------
def _handle_inference_event(chunk: Any, chunk_id: int) -> Iterator[str]:
"""
Yield inference step event.
Yield formatted Server-Sent Events (SSE) strings for inference
step events during streaming.
Processes inference-related streaming chunks, yielding SSE
events for step start, text token deltas, and tool call deltas.
Supports both string and ToolCall object tool calls.
"""
if chunk.event.payload.event_type == "step_start":
yield format_stream_data(
{
"event": "token",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": "",
},
}
)
elif chunk.event.payload.event_type == "step_progress":
if chunk.event.payload.delta.type == "tool_call":
if isinstance(chunk.event.payload.delta.tool_call, str):
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": chunk.event.payload.delta.tool_call,
},
}
)
elif isinstance(chunk.event.payload.delta.tool_call, ToolCall):
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": chunk.event.payload.delta.tool_call.tool_name,
},
}
)
elif chunk.event.payload.delta.type == "text":
yield format_stream_data(
{
"event": "token",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": chunk.event.payload.delta.text,
},
}
)
# -----------------------------------
# Tool Execution handling
# -----------------------------------
# pylint: disable=R1702,R0912
def _handle_tool_execution_event(
chunk: Any, chunk_id: int, metadata_map: dict
) -> Iterator[str]:
"""
Yield tool call event.
Processes tool execution events from a streaming chunk and
yields formatted Server-Sent Events (SSE) strings.
Handles both tool call initiation and completion, including
tool call arguments, responses, and summaries. Extracts and
updates document metadata from knowledge search tool responses
when present.
Parameters:
chunk_id (int): Unique identifier for the current streaming
chunk. metadata_map (dict): Dictionary to be updated with
document metadata extracted from tool responses.
Yields:
str: SSE-formatted event strings representing tool call
events and responses.
"""
if chunk.event.payload.event_type == "step_start":
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": "",
},
}
)
elif chunk.event.payload.event_type == "step_complete":
for t in chunk.event.payload.step_details.tool_calls:
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": {
"tool_name": t.tool_name,
"arguments": t.arguments,
},
},
}
)
for r in chunk.event.payload.step_details.tool_responses:
if r.tool_name == "query_from_memory":
inserted_context = interleaved_content_as_str(r.content)
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": {
"tool_name": r.tool_name,
"response": f"Fetched {len(inserted_context)} bytes from memory",
},
},
}
)
elif r.tool_name == "knowledge_search" and r.content:
summary = ""
for i, text_content_item in enumerate(r.content):
if isinstance(text_content_item, TextContentItem):
if i == 0:
summary = text_content_item.text
newline_pos = summary.find("\n")
if newline_pos > 0:
summary = summary[:newline_pos]
try:
parsed_metadata = parse_knowledge_search_metadata(
text_content_item.text, strict=False
)
metadata_map.update(parsed_metadata)
except ValueError as e:
logger.exception(
"Error processing metadata from text; position=%s",
getattr(e, "position", "unknown"),
)
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": {
"tool_name": r.tool_name,
"summary": summary,
},
},
}
)
else:
yield format_stream_data(
{
"event": "tool_call",
"data": {
"id": chunk_id,
"role": chunk.event.payload.step_type,
"token": {
"tool_name": r.tool_name,
"response": interleaved_content_as_str(r.content),
},
},
}
)
# -----------------------------------
# Catch-all for everything else
# -----------------------------------
def _handle_heartbeat_event(chunk_id: int) -> Iterator[str]:
"""
Yield a heartbeat event.
Yield a heartbeat event as a Server-Sent Event (SSE) for the
given chunk ID.
Parameters:
chunk_id (int): The identifier for the current streaming
chunk.
Yields:
str: SSE-formatted heartbeat event string.
"""
yield format_stream_data(
{
"event": "heartbeat",
"data": {
"id": chunk_id,
"token": "heartbeat",
},
}
)
@router.post("/streaming_query")
async def streaming_query_endpoint_handler( # pylint: disable=too-many-locals
_request: Request,
query_request: QueryRequest,
auth: Annotated[AuthTuple, Depends(auth_dependency)],
mcp_headers: dict[str, dict[str, str]] = Depends(mcp_headers_dependency),
) -> StreamingResponse:
"""
Handle request to the /streaming_query endpoint.
This endpoint receives a query request, authenticates the user,
selects the appropriate model and provider, and streams
incremental response events from the Llama Stack backend to the
client. Events include start, token updates, tool calls, turn
completions, errors, and end-of-stream metadata. Optionally
stores the conversation transcript if enabled in configuration.
Returns:
StreamingResponse: An HTTP streaming response yielding
SSE-formatted events for the query lifecycle.
Raises:
HTTPException: Returns HTTP 500 if unable to connect to the
Llama Stack server.
"""
check_configuration_loaded(configuration)
llama_stack_config = configuration.llama_stack_configuration
logger.info("LLama stack config: %s", llama_stack_config)
user_id, _user_name, token = auth
user_conversation: UserConversation | None = None
if query_request.conversation_id:
user_conversation = validate_conversation_ownership(
user_id=user_id, conversation_id=query_request.conversation_id
)
if user_conversation is None:
logger.warning(
"User %s attempted to query conversation %s they don't own",
user_id,
query_request.conversation_id,
)
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail={
"response": "Access denied",
"cause": "You do not have permission to access this conversation",
},
)
try:
# try to get Llama Stack client
client = AsyncLlamaStackClientHolder().get_client()
llama_stack_model_id, model_id, provider_id = select_model_and_provider_id(
await client.models.list(),
*evaluate_model_hints(
user_conversation=user_conversation, query_request=query_request
),
)
response, conversation_id = await retrieve_response(
client,
llama_stack_model_id,
query_request,
token,
mcp_headers=mcp_headers,
)
metadata_map: dict[str, dict[str, Any]] = {}
async def response_generator(turn_response: Any) -> AsyncIterator[str]:
"""
Generate SSE formatted streaming response.
Asynchronously generates a stream of Server-Sent Events
(SSE) representing incremental responses from a
language model turn.
Yields start, token, tool call, turn completion, and
end events as SSE-formatted strings. Collects the
complete response for transcript storage if enabled.
"""
chunk_id = 0
complete_response = "No response from the model"
# Send start event
yield stream_start_event(conversation_id)
async for chunk in turn_response:
for event in stream_build_event(chunk, chunk_id, metadata_map):
if (
json.loads(event.replace("data: ", ""))["event"]
== "turn_complete"
):
complete_response = json.loads(event.replace("data: ", ""))[
"data"
]["token"]
chunk_id += 1
yield event
yield stream_end_event(metadata_map)
if not is_transcripts_enabled():
logger.debug("Transcript collection is disabled in the configuration")
else:
store_transcript(
user_id=user_id,
conversation_id=conversation_id,
model_id=model_id,
provider_id=provider_id,
query_is_valid=True, # TODO(lucasagomes): implement as part of query validation
query=query_request.query,
query_request=query_request,
response=complete_response,
rag_chunks=[], # TODO(lucasagomes): implement rag_chunks
truncated=False, # TODO(lucasagomes): implement truncation as part
# of quota work
attachments=query_request.attachments or [],
)
persist_user_conversation_details(
user_id=user_id,
conversation_id=conversation_id,
model=model_id,
provider_id=provider_id,
)
# Update metrics for the LLM call
metrics.llm_calls_total.labels(provider_id, model_id).inc()
return StreamingResponse(response_generator(response))
# connection to Llama Stack server
except APIConnectionError as e:
# Update metrics for the LLM call failure
metrics.llm_calls_failures_total.inc()
logger.error("Unable to connect to Llama Stack: %s", e)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail={
"response": "Unable to connect to Llama Stack",
"cause": str(e),
},
) from e
async def retrieve_response(
client: AsyncLlamaStackClient,
model_id: str,
query_request: QueryRequest,
token: str,
mcp_headers: dict[str, dict[str, str]] | None = None,
) -> tuple[Any, str]:
"""
Retrieve response from LLMs and agents.
Asynchronously retrieves a streaming response and conversation
ID from the Llama Stack agent for a given user query.
This function configures input/output shields, system prompt,
and tool usage based on the request and environment. It
prepares the agent with appropriate headers and toolgroups,
validates attachments if present, and initiates a streaming
turn with the user's query and any provided documents.
Parameters:
model_id (str): Identifier of the model to use for the query.
query_request (QueryRequest): The user's query and associated metadata.
token (str): Authentication token for downstream services.
mcp_headers (dict[str, dict[str, str]], optional):
Multi-cluster proxy headers for tool integrations.
Returns:
tuple: A tuple containing the streaming response object
and the conversation ID.
"""
available_input_shields = [
shield.identifier
for shield in filter(is_input_shield, await client.shields.list())
]
available_output_shields = [
shield.identifier
for shield in filter(is_output_shield, await client.shields.list())
]
if not available_input_shields and not available_output_shields:
logger.info("No available shields. Disabling safety")
else:
logger.info(
"Available input shields: %s, output shields: %s",
available_input_shields,
available_output_shields,
)
# use system prompt from request or default one
system_prompt = get_system_prompt(query_request, configuration)
logger.debug("Using system prompt: %s", system_prompt)
# TODO(lucasagomes): redact attachments content before sending to LLM
# if attachments are provided, validate them
if query_request.attachments:
validate_attachments_metadata(query_request.attachments)
agent, conversation_id, session_id = await get_agent(
client,
model_id,
system_prompt,
available_input_shields,
available_output_shields,
query_request.conversation_id,
query_request.no_tools or False,
)
logger.debug("Conversation ID: %s, session ID: %s", conversation_id, session_id)
# bypass tools and MCP servers if no_tools is True
if query_request.no_tools:
mcp_headers = {}
agent.extra_headers = {}
toolgroups = None
else:
# preserve compatibility when mcp_headers is not provided
if mcp_headers is None:
mcp_headers = {}
mcp_headers = handle_mcp_headers_with_toolgroups(mcp_headers, configuration)
if not mcp_headers and token:
for mcp_server in configuration.mcp_servers:
mcp_headers[mcp_server.url] = {
"Authorization": f"Bearer {token}",
}
agent.extra_headers = {
"X-LlamaStack-Provider-Data": json.dumps(
{
"mcp_headers": mcp_headers,
}
),
}
vector_db_ids = [
vector_db.identifier for vector_db in await client.vector_dbs.list()
]
toolgroups = (get_rag_toolgroups(vector_db_ids) or []) + [
mcp_server.name for mcp_server in configuration.mcp_servers
]
# Convert empty list to None for consistency with existing behavior
if not toolgroups:
toolgroups = None
response = await agent.create_turn(
messages=[UserMessage(role="user", content=query_request.query)],
session_id=session_id,
documents=query_request.get_documents(),
stream=True,
toolgroups=toolgroups,
)
return response, conversation_id