benjamin5607's picture
Rename backend/utils/llm.py to utils/llm.py
71b82b8 verified
Raw
History Blame Contribute Delete
1.3 kB
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)}"