Spaces:
Sleeping
Sleeping
| from app.agent.gemini_client import get_client, json_generation_config | |
| import json | |
| INTENT_SCHEMA = { | |
| "type": "object", | |
| "properties": { | |
| "domain": { "type": "string" }, | |
| "entity": { "type": "string" }, | |
| "refined_query": { "type": "string" } | |
| }, | |
| "required": ["domain", "entity", "refined_query"] | |
| } | |
| def analyze_query(query: str): | |
| client = get_client() | |
| prompt = f""" | |
| You are a search query optimization assistant. | |
| User query: "{query}" | |
| Your task: | |
| 1. Identify the domain (e.g., technology, automotive, science, business, etc.) | |
| 2. Extract the main entity being asked about | |
| 3. Refine the query for optimal web search results by: | |
| - Making it more specific and searchable | |
| - Adding relevant keywords (like "2024", "2025", "latest", "current" for recent info) | |
| - Removing conversational filler words | |
| - Keeping it concise and search-engine friendly | |
| Examples: | |
| - "what are the new tesla cars" → "latest Tesla car models 2024 2025" | |
| - "tell me about iphone features" → "iPhone 15 Pro features specifications" | |
| - "best laptops" → "best laptops 2024 reviews" | |
| Return the refined query that will get the best search results. | |
| """ | |
| response = client.models.generate_content( | |
| model="gemini-flash-latest", | |
| contents=prompt, | |
| config=json_generation_config(schema=INTENT_SCHEMA) | |
| ) | |
| # Gemini now guarantees valid JSON | |
| return json.loads(response.text) | |