File size: 2,249 Bytes
735de60
cb0edac
735de60
 
 
 
 
 
 
 
cb0edac
 
 
735de60
 
 
 
 
 
 
 
 
 
 
 
 
cb0edac
 
275a51d
cb0edac
 
 
 
735de60
cb0edac
735de60
 
cb0edac
735de60
 
 
 
 
 
 
 
 
 
 
 
cb0edac
 
 
 
 
 
 
735de60
 
 
 
cb0edac
 
735de60
 
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
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