import httpx import os from pathlib import Path USER_SESSIONS = {} GLOBAL_CONTEXT_PATH = Path(__file__).parent / "global_context.txt" with open(GLOBAL_CONTEXT_PATH, "r", encoding="utf-8") as f: GLOBAL_PROMPT = f.read() HF_TOKEN = os.getenv("HF_TOKEN") HF_MODEL_ID = os.getenv("HF_MODEL_ID") def build_prompt(user_id: str, user_message: str) -> str: history = USER_SESSIONS.setdefault(user_id, []) history.append(f"User: {user_message}") history_text = "\n".join(history) full_prompt = f"""{GLOBAL_PROMPT} Conversation: {history_text} Assistant:""" return full_prompt async def call_hf_inference(prompt: str) -> str: if not HF_TOKEN or not HF_MODEL_ID: return "Hugging Face token or model ID not set A." url = f"https://api-inference.huggingface.co/models/{HF_MODEL_ID}" headers = { "Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json" } payload = {"inputs": prompt} async with httpx.AsyncClient() as client: try: response = await client.post(url, headers=headers, json=payload, timeout=60) response.raise_for_status() except httpx.HTTPStatusError as exc: print(f"HTTP error: {exc.response.status_code}") print(f"Response: {exc.response.text}") return "HTTP error" except httpx.RequestError as exc: print(f"Request error: {exc}") return "Request error" except Exception as exc: print(f"Other error: {exc}") return "Unknown error" data = response.json() # The response format may vary by model; adjust as needed if isinstance(data, list) and "generated_text" in data[0]: return data[0]["generated_text"].strip() elif isinstance(data, dict) and "error" in data: return f"Error: {data['error']}" else: return str(data) async def get_chat_response(user_id: str, user_message: str) -> str: # prompt = build_prompt(user_id, user_message) # If you want to use the prompt with history, uncomment the above line and use 'prompt' below answer = await call_hf_inference(user_message) # USER_SESSIONS[user_id].append(f"Assistant: {answer}") return answer