File size: 1,651 Bytes
b171cab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -------------------------------
# πŸ‘‡ HuggingFace OpenAI-Compatible Client
# -------------------------------
from openai import OpenAI
import os
from utils.constants import (
    ROUTER_CHAT_URL,
    ROUTER_MODEL,
    REQUEST_TIMEOUT_SECONDS_DEFAULT,
)
from utils.persona import AI_GYNO_PERSONA_V3

# Force correct model name
ROUTER_MODEL = "meta-llama/Llama-3.1-8B-Instruct"

# HF Token
token = os.getenv("HF_API_TOKEN") or os.getenv("HF_TOKEN")

# HF Router client
client = OpenAI(
    base_url="https://huggingface.co/static-proxy/router.huggingface.co/v1",
    api_key=token,
)


def chat(user_message: str, mode: str = "patient") -> str:
    """

    Uses HuggingFace Router with OpenAI SDK to get chat completions.

    """

    if not token:
        return "⚠ Set HF_API_TOKEN or HF_TOKEN in your environment."

    # Patient-friendly language vs Clinical doctor mode
    style = (
        "Use simple, reassuring language."
        if mode == "patient"
        else "Use concise clinical phrasing with differentials and next steps."
    )

    system_prompt = AI_GYNO_PERSONA_V3 + f"\nMode: {mode}. {style}"

    try:
        completion = client.chat.completions.create(
            model=ROUTER_MODEL,
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_message},
            ],
            max_tokens=400,
            temperature=0.2,
            timeout=REQUEST_TIMEOUT_SECONDS_DEFAULT,
        )

        return completion.choices[0].message.content.strip()

    except Exception as e:
        return f"❌ Error: {str(e)}"