File size: 2,308 Bytes
ba2fc46
370480b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# backend/src/services/tools/cms_agent.py
import json
from langchain.agents import create_agent
from backend.src.services.llm.factory import get_llm_model
from backend.src.services.tools.cms_tool import CMSQueryTool
from typing import Optional, Dict

# --- THE CMS EXPERT PROMPT (ANTI-YAP VERSION 🤐) ---
CMS_SYSTEM_PROMPT = """You are a Sanity GROQ Query Generator.
Your goal is to query the database based on the user's request.

--- KNOWLEDGE BASE (SCHEMA) ---
{schema_map}

--- RULES (READ CAREFULLY) ---
1. **NO EXPLANATIONS:** Do NOT say "Here is the query" or "I will search for...".
2. **JUST THE QUERY:** Directly call the 'cms_query_tool' with the GROQ string.
3. **USE THE SCHEMA:** Look at the schema map above. If `price` is inside `variants`, use `variants[].price`.
4. **SYNTAX:** `*[_type == "product" && title match "Blue*"]`

--- ERROR HANDLING ---
If the query fails or returns empty, just say: "No products found matching your criteria."
Do NOT make up fake products from Amazon or other websites.

User Input: {input}
"""

# --- AGENT ADAPTER ---
class AgentAdapter:
    def __init__(self, agent):
        self.agent = agent
    
    async def ainvoke(self, input_dict):
        # Hum input ko thoda modify karke bhejenge taake AI focus kare
        user_text = input_dict.get("input", "")
        # Force instruction appended to user query
        strict_input = f"{user_text} (Return ONLY the GROQ query tool call. Do not explain.)"
        
        payload = {"messages": [("user", strict_input)]}
        result = await self.agent.ainvoke(payload)
        last_message = result["messages"][-1]
        return {"output": last_message.content}

# --- DYNAMIC AGENT FACTORY ---
def get_cms_agent(
    user_id: str, 
    schema_map: dict,
    llm_credentials: Optional[Dict[str, str]] = None
):
    # 1. Load User's LLM
    llm = get_llm_model(credentials=llm_credentials)
    
    # 2. Initialize Tool
    tool = CMSQueryTool(user_id=str(user_id))
    tools = [tool]

    # Convert schema to string
    schema_str = json.dumps(schema_map, indent=2)

    # 3. Create Agent
    agent_runnable = create_agent(
        model=llm,
        tools=tools,
        system_prompt=CMS_SYSTEM_PROMPT.format(schema_map=schema_str, input="{input}")
    )
    
    return AgentAdapter(agent_runnable)