File size: 1,297 Bytes
78de86a
18784a8
 
 
c091a07
a4e1873
e84ddda
a4e1873
78de86a
 
 
 
 
 
 
18784a8
 
c091a07
18784a8
 
78de86a
e84ddda
 
a4e1873
c091a07
18784a8
 
e84ddda
18784a8
c091a07
18784a8
a4e1873
18784a8
 
e84ddda
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
import os
from huggingface_hub import InferenceClient

REPO_ID = "Qwen/Qwen2.5-72B-Instruct" 

LANG_CODE = {"한국어": "Korean", "English": "English", "中文": "Traditional Chinese", "日本語": "Japanese"}

def get_ai_response(user_query, persona, context_data=None, user_lang="한국어"):
    # Streamlit secrets 대신 os.getenv 사용 (혹은 직접 키 입력)
    hf_token = os.getenv("HF_TOKEN") 
    
    # 토큰 없으면 에러 방지 (실제 배포땐 꼭 설정해야 함)
    if not hf_token:
        print("Warning: HF_TOKEN not found.")
        
    client = InferenceClient(model=REPO_ID, token=hf_token)
    system_prompt = persona['system_prompt']
    
    context_str = ""
    if context_data:
        context_str = f"\n[Data]: {str(context_data)}\n"

    target_language = LANG_CODE.get(user_lang, "Korean")
    instruction = f"\n(IMPORTANT: Answer strictly in {target_language}.)"

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": f"{context_str}\nUser Query: {user_query}\n{instruction}"}
    ]

    try:
        response = client.chat_completion(messages=messages, max_tokens=1000, temperature=0.7)
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"