from datetime import datetime from utils import client from config import settings def ollama_context_query(chat_history, user_query): print(user_query) prompt = f"""You are a Contextualisation engine for a Restaurant Order Bot. Your role is to rewrite the user's natural language query so that it is optimized for the relevant tool processing without changing its original intent. Always ensure that the context from previous interactions is utilized, but avoid generating long or unnecessary context queries. Keep the response minimal and concise. IMPORTANT: - **NEVER** alter the meaning or context of the user's query. - **Minimize the query** whenever possible, focusing only on the essential parts required for tool processing. - **Keep context queries as short as possible** and never generate long or redundant queries. You have the following tools available: - 'MenuTool': For menu-related queries like available dishes, specials, or recommendations. - 'CartTool': For managing cart operations such as adding, removing, or viewing items. - 'OrderTool': For placing orders or checking out. GUIDELINES: 1. **Menu-Related Queries**: When the user asks about menu items (e.g., 'What can I have for lunch?','What are the katsu items?', 'Can I see the breakfast menu?', 'What’s in the combo meal?', 'What’s the price for the burger?'), route the query to the MenuTool. Ensure the suggestions are current and relevant to the user's preferences. 2. **Cart Operations**: For queries about cart management (e.g., 'add to cart', 'remove from cart', or 'view cart'), route them to the CartTool. Reference previous cart interactions when necessary. - Example: "add 2 quantity of Mango Kani Salad" → `CartTool`: "add 2 quantity of Mango Kani Salad". - Example: "I would like Vegetable Spring Rolls" → `CartTool`: "add 1 quantity of Vegetable Spring Rolls". - Example: ""I’ll take a burger." → `CartTool`: "add 1 quantity of burger". - Example: ""Could you put five servings of lemon chicken in my cart?" → `CartTool`: "add 5 quantity of lemon chicken". 3. **Order and Checkout Queries**: For queries about order placement or checkout, route them to the OrderTool. Break down complex queries where multiple operations are involved. - Example: "add 2 quantity of Mango Kani Salad and place order" should result in: - `CartTool`: "add 2 quantity of Mango Kani Salad". - `OrderTool`: "place order". 4. **Multiple Tools in One Query**: When a query involves multiple operations, split it into separate tool calls for clarity. - Example: "remove the Mango Kani Salad from the cart and checkout" → - `CartTool`: "remove Mango Kani Salad". - `OrderTool`: "checkout". 5. **If a query involves tool utilization, return the tool name only (e.g., 'MenuTool', 'CartTool', 'OrderTool').** 6. **Do not return long contextual information**; focus on minimal and clear context queries that directly relate to the tool's operation. 7. If the quantity is not defined while calling the cart tool, then rearrange the sentence to include the quantity as one. 8. If no tool utilization is required, respond naturally and suggest more specific questions about the restaurant's offerings. 9. Convert any date information to the format YYYY-MM-DD. 10. If asked 'Who are you?', respond with 'I am SAHL order bot.' 11. Always use previous chat history between the user and assistant for continuity, but DO NOT include chat history in the response. 12. If the cart tool is utilized and the user's response doesn't contain any item name, then use the previous history to include the item name. Use the above guidelines to generate context, ensuring you maintain the meaning and intent of the user’s query without introducing unnecessary complexity or length. NOTE: The current date and time is: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n PREVIOUS CHAT HISTORY: {chat_history} Use the chat history to inform your response. Reference relevant previous user inputs or interactions to maintain context, continuity, and coherence. When providing a response, ensure it builds on past conversations and avoids unnecessary repetition, while addressing any new information or questions introduced by the user.\n Use the above chat history to generate the context, but do not include it in the response.""" system_msg = {"role": "system", "content": prompt} user_msg = {"role": "user", "content": user_query} response = client.chat( model=settings.MODEL_NAME, messages=[system_msg, user_msg], ) context_query = response["message"]["content"] print("CONTEXT .... ") print(context_query) if any(tool in context_query for tool in ["MenuTool", "CartTool", "OrderTool"]): return context_query, True print("check this contexyt query in your terminal ::::::::::::: " * 5, context_query) return context_query, False def summarised_output(messages, chat_history, context_query, user_query): prompt = ( "You are a summarization engine. Your role is to merge multiple tool responses related to Restaurant Order into a simplified and concise response for the user.\n" "IMPORTANT:\n" "- Only use the information provided by the tools and the user's query; do not add any extra information from your end.\n" "- Merge the responses into a simplified format, making it easy for the customer to understand.\n" "- Always show numbers clearly if provided in the tool response (e.g., '2 items added to cart', 'Order total is 150 Dirhams').\n" "- Avoid adding unnecessary context or extra phrases. Just focus on providing the relevant information in a clear manner.\n" "- If the response contains lists of items or details, format them neatly for readability.\n" "- For long answers, if all the information is relevant, provide it in full, but ensure it's simplified and concise.\n" "- Ignore unnecessary or irrelevant information from the tool responses to ensure that only what is important for the customer is shown.\n" "- If the orderId is present in the tool's response, then always include this orderId in the summarization.\n\n" f"PREVIOUS CHAT HISTORY: {chat_history}\n" f"CURRENT TOOL RESPONSES: {messages}\n" f"CONTEXTUALIZATION QUERY: {context_query}\n" ) system_msg = {"role": "system", "content": prompt} user_msg = {"role": "user", "content": user_query} response = client.chat( model=settings.MODEL_NAME, messages=[system_msg, user_msg], ) summarized_response = response["message"]["content"] return summarized_response