Handle /ask endpoint for generating answers
(request: web.Request)
| 29 | |
| 30 | |
| 31 | async def ask_handler(request: web.Request) -> web.Response: |
| 32 | """Handle /ask endpoint for generating answers""" |
| 33 | |
| 34 | # Get query parameters |
| 35 | query_params = dict(request.query) |
| 36 | |
| 37 | # For POST requests, merge body parameters |
| 38 | if request.method == 'POST': |
| 39 | try: |
| 40 | if request.content_type == 'application/json': |
| 41 | body_data = await request.json() |
| 42 | query_params.update(body_data) |
| 43 | elif request.content_type == 'application/x-www-form-urlencoded': |
| 44 | body_data = await request.post() |
| 45 | query_params.update(dict(body_data)) |
| 46 | except Exception as e: |
| 47 | logger.warning(f"Failed to parse POST body: {e}") |
| 48 | |
| 49 | # Detect and flatten v0.55 structured request format |
| 50 | if 'query' in query_params and isinstance(query_params['query'], dict): |
| 51 | q = query_params.pop('query') |
| 52 | query_params['query'] = q.get('text', '') |
| 53 | if 'site' in q: |
| 54 | query_params['site'] = q['site'] |
| 55 | # Pass through extra query fields (scorer, itemType, etc.) |
| 56 | for k, v in q.items(): |
| 57 | if k not in ('text', 'site') and k not in query_params: |
| 58 | query_params[k] = v |
| 59 | |
| 60 | if 'context' in query_params and isinstance(query_params['context'], dict): |
| 61 | ctx = query_params.pop('context') |
| 62 | if 'prev' in ctx: |
| 63 | query_params['prev'] = ctx['prev'] |
| 64 | |
| 65 | if 'prefer' in query_params and isinstance(query_params['prefer'], dict): |
| 66 | pref = query_params.pop('prefer') |
| 67 | if 'streaming' in pref: |
| 68 | query_params['streaming'] = str(pref['streaming']) |
| 69 | if 'mode' in pref: |
| 70 | query_params['mode'] = pref['mode'] |
| 71 | if 'response_format' in pref: |
| 72 | query_params['response_format'] = pref['response_format'] |
| 73 | |
| 74 | if 'meta' in query_params and isinstance(query_params['meta'], dict): |
| 75 | meta = query_params.pop('meta') |
| 76 | query_params['_protocol_version'] = meta.get('version', '0.55') |
| 77 | else: |
| 78 | query_params['_protocol_version'] = '0.55' |
| 79 | |
| 80 | # Check if SSE streaming is requested |
| 81 | is_sse = request.get('is_sse', False) |
| 82 | streaming = get_param(query_params, "streaming", str, "True") |
| 83 | streaming = streaming not in ["False", "false", "0"] |
| 84 | |
| 85 | if is_sse or streaming: |
| 86 | return await handle_streaming_ask(request, query_params) |
| 87 | else: |
| 88 | return await handle_regular_ask(request, query_params) |
nothing calls this directly
no test coverage detected