MCPcopy Create free account
hub / github.com/nlweb-ai/NLWeb / ask_handler

Function ask_handler

AskAgent/python/webserver/routes/api.py:31–88  ·  view source on GitHub ↗

Handle /ask endpoint for generating answers

(request: web.Request)

Source from the content-addressed store, hash-verified

29
30
31async 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)

Callers

nothing calls this directly

Calls 5

get_paramFunction · 0.90
handle_streaming_askFunction · 0.85
handle_regular_askFunction · 0.85
getMethod · 0.80
warningMethod · 0.45

Tested by

no test coverage detected