Download agentic_workflow/intent.py from grazz7/RAG_Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/grazz7/RAG_Chatbot/resolve/main/agentic_workflow/intent.py
- Command line
-
hf download hf://grazz7/RAG_Chatbot/agentic_workflow/intent.py
-
curl -L -o intent.py https://huggingface.co/grazz7/RAG_Chatbot/resolve/main/agentic_workflow/intent.py
1.85 kB
| import json | |
| import chromadb | |
| from agentic_workflow.config import LLM_MODEL | |
| from langchain_ollama import ChatOllama | |
| import re | |
| from langchain_core.messages import HumanMessage, SystemMessage | |
| client = chromadb.PersistentClient(path="./chroma_db") | |
| llm = ChatOllama(model=LLM_MODEL, temperature=0) | |
| # intent analysis | |
| def analyse_intent(query: str) -> dict: | |
| system = """ | |
| You are an intent classifier. Given a user query, return ONLY valid JSON with: | |
| - "intent": one of "factual", "conversational", "follow_up", "sensitive" | |
| - "needs_context": true if retrieval from a knowledge base is needed, false otherwise | |
| - "needs_history": true if this looks like a follow-up to a prior turn, false otherwise | |
| Rules: | |
| - Greetings / chit-chat → conversational, needs_context: false | |
| - Questions about facts / documents → factual, needs_context: true | |
| - "you said earlier" / pronouns like "it" / "that" → follow_up, needs_history: true | |
| - Personal data, health, finance → sensitive | |
| Return ONLY the JSON object. No explanation. | |
| """ | |
| response = llm.invoke([ | |
| SystemMessage(content=system), | |
| HumanMessage(content=query) | |
| ]) | |
| raw = response.content.strip() | |
| # Strip markdown fences if present | |
| raw = re.sub(r"^```(?:json)?\s*|\s*```$", "", raw).strip() | |
| try: | |
| parsed = json.loads(raw) | |
| except json.JSONDecodeError: | |
| parsed = {} | |
| return { | |
| "intent": "factual", | |
| "needs_context": True, | |
| "needs_history": False, | |
| **parsed | |
| } | |
| # query= "How do I top-up the value of my card when I am abroad" | |
| # trace = [] | |
| # intent_result = analyse_intent(query) | |
| # trace.append({ | |
| # "step": "intent_analysis", | |
| # "result": intent_result | |
| # }) | |
| # print(f"[Intent] {intent_result}") |