Spaces:
Sleeping
Sleeping
Download extractor.py from killbizz/memorymind: direct link, hf CLI and curl.
- Browser
- Download file 1.94 kB
-
https://huggingface.co/spaces/killbizz/memorymind/resolve/main/extractor.py
- Command line
-
hf download hf://spaces/killbizz/memorymind/extractor.py
-
curl -L -o extractor.py https://huggingface.co/spaces/killbizz/memorymind/resolve/main/extractor.py
1.94 kB
| import json | |
| import os | |
| from dotenv import load_dotenv | |
| from groq import Groq | |
| load_dotenv() | |
| _client = Groq(api_key=os.getenv("GROQ_API_KEY")) | |
| _SYSTEM_PROMPT = """You are a fact extractor. Analyze the conversation and extract personal facts about the user. | |
| Return ONLY a valid JSON array. Each item must have exactly two keys: | |
| - "type": one of "personal", "preference", "goal", "problem", "context" | |
| - "fact": a short, clear statement written in third person | |
| Rules: | |
| - Only extract facts explicitly stated by the user — do not infer or guess | |
| - Each fact must be self-contained and specific | |
| - If nothing is extractable, return [] | |
| - Never include facts about the assistant | |
| Examples of good output: | |
| [ | |
| {"type": "personal", "fact": "user's name is Stefan"}, | |
| {"type": "preference", "fact": "user prefers dark mode"}, | |
| {"type": "goal", "fact": "user wants to become a senior engineer"}, | |
| {"type": "problem", "fact": "user is struggling with time management"}, | |
| {"type": "context", "fact": "user is working on a Python project"} | |
| ]""" | |
| def extract_facts(user_message: str, bot_reply: str) -> list[dict]: | |
| conversation = f"User: {user_message}\nAssistant: {bot_reply}" | |
| try: | |
| response = _client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| messages=[ | |
| {"role": "system", "content": _SYSTEM_PROMPT}, | |
| {"role": "user", "content": conversation}, | |
| ], | |
| temperature=0.1, | |
| max_tokens=512, | |
| ) | |
| raw = response.choices[0].message.content.strip() | |
| if raw.startswith("```"): | |
| raw = raw.split("```")[1] | |
| if raw.startswith("json"): | |
| raw = raw[4:] | |
| raw = raw.strip() | |
| facts = json.loads(raw) | |
| if not isinstance(facts, list): | |
| return [] | |
| return [f for f in facts if "type" in f and "fact" in f] | |
| except Exception: | |
| return [] | |