Spaces:
Runtime error
Runtime error
Download backend/intent_classifier.py from horribleProgram/IntentAPI: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/spaces/horribleProgram/IntentAPI/resolve/762a6f54f1b2fe468ffe91c3d7eec9cdd4aa2694/backend/intent_classifier.py
- Command line
-
hf download hf://spaces/horribleProgram/IntentAPI@762a6f54f1b2fe468ffe91c3d7eec9cdd4aa2694/backend/intent_classifier.py
-
curl -L -o intent_classifier.py https://huggingface.co/spaces/horribleProgram/IntentAPI/resolve/762a6f54f1b2fe468ffe91c3d7eec9cdd4aa2694/backend/intent_classifier.py
1.51 kB
| import json | |
| import os | |
| from pathlib import Path | |
| import yaml | |
| from openai import OpenAI | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| _INTENTS_PATH = Path(__file__).parent.parent / "intents.yaml" | |
| def _load_taxonomy() -> dict: | |
| with open(_INTENTS_PATH) as f: | |
| return yaml.safe_load(f)["intents"] | |
| def _build_system_prompt(taxonomy: dict) -> str: | |
| lines = ["You are an intent classifier. Given a user message, return JSON with keys: domain, intent, confidence (high/medium/low)."] | |
| lines.append("\nKnown intents (domain → intent: example utterances):\n") | |
| for domain, intents in taxonomy.items(): | |
| for intent, data in intents.items(): | |
| utterances = data.get("utterances", []) | |
| examples = "; ".join(utterances[:2]) | |
| lines.append(f" {domain}.{intent}: \"{examples}\"") | |
| lines.append('\nIf nothing matches, return {"domain": "unknown", "intent": "unknown", "confidence": "low"}.') | |
| lines.append("Respond with JSON only, no prose.") | |
| return "\n".join(lines) | |
| def classify(utterance: str) -> dict: | |
| taxonomy = _load_taxonomy() | |
| system_prompt = _build_system_prompt(taxonomy) | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": utterance}, | |
| ], | |
| response_format={"type": "json_object"}, | |
| temperature=0, | |
| ) | |
| return json.loads(response.choices[0].message.content) | |