seawolf2357 commited on
Commit
922d19a
Β·
verified Β·
1 Parent(s): cb69e60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -15,7 +15,6 @@ intents.messages = True
15
  # hf_client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv("HF_TOKEN"))
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
17
 
18
-
19
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  λ³€μˆ˜
20
  conversation_history = []
21
 
@@ -31,13 +30,14 @@ class MyClient(discord.Client):
31
  logging.info('μžμ‹ μ˜ λ©”μ‹œμ§€λŠ” λ¬΄μ‹œν•©λ‹ˆλ‹€.')
32
  return
33
 
34
- logging.debug(f'Receiving message: {message.content}')
35
  response = await generate_response(message.content)
36
  await message.channel.send(response)
37
 
38
  async def generate_response(user_input):
39
- system_message = "DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— μΉœμ ˆν•˜κ²Œ λ‹΅ν•˜λŠ” μ „λ¬Έ AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€. λŒ€ν™”λ₯Ό 계속 이어가고, 이전 응닡을 μ°Έκ³ ν•˜μ‹­μ‹œμ˜€."
40
  system_prefix = """
 
41
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 좜λ ₯μ‹œ λ„μ›Œμ“°κΈ°λ₯Ό ν•˜κ³  markdown으둜 좜λ ₯ν•˜λΌ.
42
  μ§ˆλ¬Έμ— μ ν•©ν•œ 닡변을 μ œκ³΅ν•˜λ©°, κ°€λŠ₯ν•œ ν•œ ꡬ체적이고 도움이 λ˜λŠ” 닡변을 μ œκ³΅ν•˜μ‹­μ‹œμ˜€.
43
  λͺ¨λ“  닡변을 ν•œκΈ€λ‘œ ν•˜κ³ , λŒ€ν™” λ‚΄μš©μ„ κΈ°μ–΅ν•˜μ‹­μ‹œμ˜€.
@@ -45,25 +45,29 @@ async def generate_response(user_input):
45
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€.
46
  """
47
 
 
48
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬ 관리
49
  global conversation_history
50
  conversation_history.append({"role": "user", "content": user_input})
 
51
 
52
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
 
53
 
54
  # 동기 ν•¨μˆ˜λ₯Ό λΉ„λ™κΈ°λ‘œ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ 래퍼 μ‚¬μš©, stream=true둜 λ³€κ²½
55
  loop = asyncio.get_event_loop()
56
  response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
57
- messages, max_tokens=250, stream=True, temperature=0.9, top_p=0.9))
58
 
59
  # 슀트리밍 응닡을 μ²˜λ¦¬ν•˜λŠ” 둜직 μΆ”κ°€
60
  full_response = ""
61
  for part in response:
62
- full_response += part.choices[0].delta.content.strip()
 
63
 
64
  conversation_history.append({"role": "assistant", "content": full_response})
 
65
 
66
- logging.debug(f'Model response: {full_response}')
67
  return full_response
68
 
69
  # λ””μŠ€μ½”λ“œ 봇 μΈμŠ€ν„΄μŠ€ 생성 및 μ‹€ν–‰
 
15
  # hf_client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv("HF_TOKEN"))
16
  hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus", token=os.getenv("HF_TOKEN"))
17
 
 
18
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό μ €μž₯ν•  λ³€μˆ˜
19
  conversation_history = []
20
 
 
30
  logging.info('μžμ‹ μ˜ λ©”μ‹œμ§€λŠ” λ¬΄μ‹œν•©λ‹ˆλ‹€.')
31
  return
32
 
33
+ logging.debug(f'Receiving message: {message.content}') # μž…λ ₯ λ©”μ‹œμ§€ λ‘œκΉ…
34
  response = await generate_response(message.content)
35
  await message.channel.send(response)
36
 
37
  async def generate_response(user_input):
38
+ system_message = "DISCORDμ—μ„œ μ‚¬μš©μžλ“€μ˜ μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μ „λ¬Έ AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€. λŒ€ν™”λ₯Ό 계속 이어가고, 이전 응닡을 μ°Έκ³ ν•˜μ‹­μ‹œμ˜€."
39
  system_prefix = """
40
+
41
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€. 좜λ ₯μ‹œ λ„μ›Œμ“°κΈ°λ₯Ό ν•˜κ³  markdown으둜 좜λ ₯ν•˜λΌ.
42
  μ§ˆλ¬Έμ— μ ν•©ν•œ 닡변을 μ œκ³΅ν•˜λ©°, κ°€λŠ₯ν•œ ν•œ ꡬ체적이고 도움이 λ˜λŠ” 닡변을 μ œκ³΅ν•˜μ‹­μ‹œμ˜€.
43
  λͺ¨λ“  닡변을 ν•œκΈ€λ‘œ ν•˜κ³ , λŒ€ν™” λ‚΄μš©μ„ κΈ°μ–΅ν•˜μ‹­μ‹œμ˜€.
 
45
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€.
46
  """
47
 
48
+
49
  # λŒ€ν™” νžˆμŠ€ν† λ¦¬ 관리
50
  global conversation_history
51
  conversation_history.append({"role": "user", "content": user_input})
52
+ logging.debug(f'Conversation history updated: {conversation_history}') # λŒ€ν™” νžˆμŠ€ν† λ¦¬ λ‘œκΉ…
53
 
54
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
55
+ logging.debug(f'Messages to be sent to the model: {messages}') # λͺ¨λΈλ‘œ 전솑될 λ©”μ‹œμ§€ λ‘œκΉ…
56
 
57
  # 동기 ν•¨μˆ˜λ₯Ό λΉ„λ™κΈ°λ‘œ μ²˜λ¦¬ν•˜κΈ° μœ„ν•œ 래퍼 μ‚¬μš©, stream=true둜 λ³€κ²½
58
  loop = asyncio.get_event_loop()
59
  response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
60
+ messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
61
 
62
  # 슀트리밍 응닡을 μ²˜λ¦¬ν•˜λŠ” 둜직 μΆ”κ°€
63
  full_response = ""
64
  for part in response:
65
+ if part.choices and part.choices[0].delta.content: # 델타가 μžˆλŠ”μ§€ 확인
66
+ full_response += part.choices[0].delta.content.strip()
67
 
68
  conversation_history.append({"role": "assistant", "content": full_response})
69
+ logging.debug(f'Model response: {full_response}') # 응닡 λ‘œκΉ…
70
 
 
71
  return full_response
72
 
73
  # λ””μŠ€μ½”λ“œ 봇 μΈμŠ€ν„΄μŠ€ 생성 및 μ‹€ν–‰