| 19 | _vector_db_cache = {} |
| 20 | |
| 21 | class WhoHandler (NLWebHandler) : |
| 22 | |
| 23 | def __init__(self, query_params, http_handler): |
| 24 | # Remove site parameter - we'll use nlweb_sites |
| 25 | if 'site' in query_params: |
| 26 | del query_params['site'] |
| 27 | |
| 28 | # Keep prev_queries if provided for context, but don't use 'prev' format |
| 29 | # The who handler can use previous queries to understand follow-up questions |
| 30 | if 'prev' in query_params: |
| 31 | del query_params['prev'] |
| 32 | # Keep prev_queries for context if provided |
| 33 | super().__init__(query_params, http_handler) |
| 34 | |
| 35 | |
| 36 | async def send_message(self, message): |
| 37 | """Override send_message to ensure URLs point to gateway endpoint with site parameter.""" |
| 38 | # Check if message contains results with URLs |
| 39 | if isinstance(message, dict): |
| 40 | # Handle messages with 'content' field (results) |
| 41 | if 'content' in message and isinstance(message['content'], list): |
| 42 | for result in message['content']: |
| 43 | if 'url' in result: |
| 44 | url = result['url'] |
| 45 | # If URL doesn't start with http:// or https://, convert to gateway URL |
| 46 | if not url.startswith(('http://', 'https://')): |
| 47 | site_type = result.get('@type', '') |
| 48 | result['url'] = build_nlweb_gateway_url(url, self.query, site_type) |
| 49 | logger.debug(f"Modified URL from '{url}' to '{result['url']}'") |
| 50 | |
| 51 | # Handle single result messages |
| 52 | elif 'url' in message: |
| 53 | url = message['url'] |
| 54 | if not url.startswith(('http://', 'https://')): |
| 55 | site_type = message.get('@type', '') |
| 56 | message['url'] = build_nlweb_gateway_url(url, self.query, site_type) |
| 57 | logger.debug(f"Modified URL from '{url}' to '{message['url']}'") |
| 58 | |
| 59 | # Call parent class's send_message with modified message |
| 60 | await super().send_message(message) |
| 61 | |
| 62 | |
| 63 | async def whoQueryRewrite(self): |
| 64 | ans_struc = { |
| 65 | "rewritten_queries": ["query1", "query2", "query3", "query4", "query5"], |
| 66 | "query_count": "Number of queries generated (1-5)" |
| 67 | } |
| 68 | |
| 69 | prompt = f""" |
| 70 | You are helping to rewrite a complex search query into simpler keyword queries for a very simple |
| 71 | vector embedding search, which can easily get distracted. |
| 72 | The search engine works best with short, focused queries containing important keywords. |
| 73 | |
| 74 | Take the following query and break it down into up to 5 simpler search queries. |
| 75 | Each query should: |
| 76 | - Contain no more than 3 words |
| 77 | - Focus on the most important keywords and concepts |
| 78 | - Be diverse to cover different aspects of the original query |
no outgoing calls
no test coverage detected