benjamin5607 commited on
Commit
e84ddda
ยท
verified ยท
1 Parent(s): 3246f24

Update utils/llm.py

Browse files
Files changed (1) hide show
  1. utils/llm.py +24 -19
utils/llm.py CHANGED
@@ -1,45 +1,50 @@
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
 
4
- # 1. ๋ฌด๋ฃŒ ๋ชจ๋ธ ์„ค์ • (Qwen2.5-72B๊ฐ€ ํ•œ๊ตญ์–ด ์„ฑ๋Šฅ ๋ฏธ์ณค๊ณ  ๊ณต์งœ์ž„)
5
  REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
6
 
7
- def get_ai_response(user_query, persona, context_data=None):
8
- """
9
- Hugging Face ๋ฌด๋ฃŒ Serverless API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ต๋ณ€ ์ƒ์„ฑ
10
- """
 
 
 
 
 
11
 
12
- # Secrets์—์„œ HF ํ† ํฐ ๊ฐ€์ ธ์˜ค๊ธฐ (์—†์œผ๋ฉด ๊ณต์šฉ API๋กœ ์‹œ๋„ํ•˜์ง€๋งŒ, ํ† ํฐ ์žˆ๋Š” ๊ฒŒ ์•ˆ์ •์ )
13
  hf_token = st.secrets.get("HF_TOKEN", None)
14
-
15
- # ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
16
  client = InferenceClient(model=REPO_ID, token=hf_token)
17
 
18
- # 2. ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
19
  system_prompt = persona['system_prompt']
20
 
21
- # ํƒ€๋กœ/ํ’์ˆ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
22
  context_str = ""
23
  if context_data:
24
- card_names = [f"{card['name']}({card['desc']})" for card in context_data]
25
- context_str = f"\n[์ฐธ๊ณ  ๋ฐ์ดํ„ฐ]: {', '.join(card_names)}\n"
 
 
 
 
 
 
 
 
26
 
27
- # AI์—๊ฒŒ ๋ณด๋‚ผ ๋ฉ”์‹œ์ง€ (์ฑ„ํŒ… ํฌ๋งท)
28
  messages = [
29
  {"role": "system", "content": system_prompt},
30
- {"role": "user", "content": f"{context_str}\n์‚ฌ์šฉ์ž ๊ณ ๋ฏผ: {user_query}\n\n(์œ„ ํŽ˜๋ฅด์†Œ๋‚˜์— ๋งž์ถฐ์„œ ๋‹ต๋ณ€ํ•ด)"}
31
  ]
32
 
33
  try:
34
- # 3. ๋ฌด๋ฃŒ ์ถ”๋ก  ์š”์ฒญ (์ŠคํŠธ๋ฆฌ๋ฐ ์—†์ด ํ•œ๋ฐฉ์— ๋ฐ›๊ธฐ)
35
  response = client.chat_completion(
36
  messages=messages,
37
- max_tokens=500, # ๋‹ต๋ณ€ ๊ธธ์ด ์ œํ•œ
38
- temperature=0.8, # ์ฐฝ์˜๋ ฅ ์ˆ˜์น˜ (0.7~0.9 ์ถ”์ฒœ)
39
  top_p=0.9,
40
  )
41
  return response.choices[0].message.content
42
 
43
  except Exception as e:
44
- # ๋ฌด๋ฃŒ ํ‹ฐ์–ด๋ผ ๊ฐ€๋” ๋ฐ”์˜๋ฉด ์—๋Ÿฌ ๋‚  ์ˆ˜ ์žˆ์Œ
45
- return f"๐Ÿ’€ (์‹ ๋ น๋‹˜์ด ๋ฌด๋ฃŒ ์„œ๋ฒ„ ๊ณผ๋ถ€ํ•˜๋กœ ๊ธฐ์ ˆํ•˜์…จ์Šต๋‹ˆ๋‹ค... ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.)\n์—๋Ÿฌ: {str(e)}"
 
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
 
 
4
  REPO_ID = "Qwen/Qwen2.5-72B-Instruct"
5
 
6
+ # ์–ธ์–ด ์ฝ”๋“œ ๋งคํ•‘
7
+ LANG_CODE = {
8
+ "ํ•œ๊ตญ์–ด": "Korean",
9
+ "English": "English",
10
+ "ไธญๆ–‡": "Traditional Chinese",
11
+ "ๆ—ฅๆœฌ่ชž": "Japanese"
12
+ }
13
+
14
+ def get_ai_response(user_query, persona, context_data=None, user_lang="ํ•œ๊ตญ์–ด"): # user_lang ์ถ”๊ฐ€
15
 
 
16
  hf_token = st.secrets.get("HF_TOKEN", None)
 
 
17
  client = InferenceClient(model=REPO_ID, token=hf_token)
18
 
 
19
  system_prompt = persona['system_prompt']
20
 
21
+ # ๋ฌธ๋งฅ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
22
  context_str = ""
23
  if context_data:
24
+ # ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌ์ŠคํŠธ(ํƒ€๋กœ)์ธ์ง€ ๋”•์…”๋„ˆ๋ฆฌ(ํ’์ˆ˜/์‚ฌ์ฃผ)์ธ์ง€ ํ™•์ธ
25
+ if isinstance(context_data, list):
26
+ info = [f"{card['name']} ({card['desc']})" for card in context_data]
27
+ context_str = f"\n[Card Data]: {', '.join(info)}\n"
28
+ else: # dict
29
+ context_str = f"\n[Analysis Data]: {str(context_data)}\n"
30
+
31
+ # โญ ํ•ต์‹ฌ: ๋‹ต๋ณ€ ์–ธ์–ด ๊ฐ•์ œ ์„ค์ •
32
+ target_language = LANG_CODE.get(user_lang, "Korean")
33
+ instruction = f"\n(IMPORTANT: You MUST answer in {target_language}. Maintain the persona's tone in that language.)"
34
 
 
35
  messages = [
36
  {"role": "system", "content": system_prompt},
37
+ {"role": "user", "content": f"{context_str}\nUser Query: {user_query}\n{instruction}"}
38
  ]
39
 
40
  try:
 
41
  response = client.chat_completion(
42
  messages=messages,
43
+ max_tokens=1000,
44
+ temperature=0.7,
45
  top_p=0.9,
46
  )
47
  return response.choices[0].message.content
48
 
49
  except Exception as e:
50
+ return f"Error: {str(e)}"