seawolf2357 commited on
Commit
ec76469
β€’
1 Parent(s): 7626d91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -32,7 +32,8 @@ class MyClient(discord.Client):
32
  async def on_ready(self):
33
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
34
  subprocess.Popen(["python", "web.py"])
35
- logging.info("Web.py μ„œλ²„κ°€ μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
 
36
 
37
  async def on_message(self, message):
38
  if message.author == self.user:
@@ -54,6 +55,7 @@ class MyClient(discord.Client):
54
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
55
  )
56
 
 
57
  async def generate_response(message):
58
  global conversation_history # μ „μ—­ λ³€μˆ˜ μ‚¬μš©μ„ λͺ…μ‹œ
59
  user_input = message.content
@@ -67,46 +69,28 @@ async def generate_response(message):
67
  특히 λ„€λ₯Ό κ΅¬μ„±ν•œ "LLM λͺ¨λΈ"에 λŒ€ν•΄μ„œ λ…ΈμΆœν•˜μ§€ 말고, λ‹Ήμ‹ μ˜ λŠ₯λ ₯에 λŒ€ν•΄ κΆκΈˆν•΄ ν•˜λ©΄ "ChatGPT-4λ₯Ό λŠ₯κ°€ν•˜λŠ” λŠ₯λ ₯을 λ³΄μœ ν•˜κ³  μžˆλ‹€κ³  λ‹΅λ³€ν•  것"
68
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€.
69
  """
70
-
71
- # μƒˆλ‘œμš΄ μ‚¬μš©μž λ©”μ‹œμ§€λ₯Ό λŒ€ν™” νžˆμŠ€ν† λ¦¬μ— μΆ”κ°€
72
  conversation_history.append({"role": "user", "content": user_input})
73
- logging.debug(f'λŒ€ν™” νžˆμŠ€ν† λ¦¬ μ—…λ°μ΄νŠΈλ¨: {conversation_history}')
74
 
75
- # λŒ€ν™” 내역이 λ„ˆλ¬΄ κΈΈ 경우, μ΅œλŒ€ 길이λ₯Ό μœ μ§€ν•˜κΈ° μœ„ν•΄ μ•žλΆ€λΆ„μ„ μž˜λΌλƒ„
76
- if len(conversation_history) > 20:
77
- conversation_history = conversation_history[-20:]
78
 
79
- # λŒ€ν™” λ‚΄μ—­μ—μ„œ 역할이 μ œλŒ€λ‘œ κ΅μ°¨ν•˜λ„λ‘ 확인
80
- filtered_conversation = []
81
- last_role = None
82
- for message in conversation_history:
83
- if message['role'] != last_role:
84
- filtered_conversation.append(message)
85
- last_role = message['role']
86
-
87
- # μ‹œμŠ€ν…œ λ©”μ‹œμ§€μ™€ ν•„ν„°λ§λœ λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό κ²°ν•©ν•˜μ—¬ λͺ¨λΈμ— 보낼 λ©”μ‹œμ§€ ꡬ성
88
- messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + filtered_conversation
89
- logging.debug(f'λͺ¨λΈμ— 보낼 λ©”μ‹œμ§€: {messages}')
90
-
91
- # λͺ¨λΈ 호좜
92
  loop = asyncio.get_event_loop()
93
  response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
94
- messages=messages, max_tokens=1000, temperature=0.7, top_p=0.85))
95
 
96
  full_response = []
97
  for part in response:
98
- logging.debug(f'μŠ€νŠΈλ¦Όμ—μ„œ 받은 λΆ€λΆ„: {part}')
99
  if part.choices and part.choices[0].delta and part.choices[0].delta.content:
100
  full_response.append(part.choices[0].delta.content)
101
 
102
  full_response_text = ''.join(full_response)
103
- logging.debug(f'λͺ¨λΈ 전체 응닡: {full_response_text}')
104
 
105
- # μ–΄μ‹œμŠ€ν„΄νŠΈ 응닡을 λŒ€ν™” νžˆμŠ€ν† λ¦¬μ— μΆ”κ°€
106
  conversation_history.append({"role": "assistant", "content": full_response_text})
107
-
108
  return f"{user_mention}, {full_response_text}"
109
 
110
  if __name__ == "__main__":
111
  discord_client = MyClient(intents=intents)
112
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
32
  async def on_ready(self):
33
  logging.info(f'{self.user}둜 λ‘œκ·ΈμΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€!')
34
  subprocess.Popen(["python", "web.py"])
35
+ logging.info("Web.py server has been started.")
36
+
37
 
38
  async def on_message(self, message):
39
  if message.author == self.user:
 
55
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
56
  )
57
 
58
+
59
  async def generate_response(message):
60
  global conversation_history # μ „μ—­ λ³€μˆ˜ μ‚¬μš©μ„ λͺ…μ‹œ
61
  user_input = message.content
 
69
  특히 λ„€λ₯Ό κ΅¬μ„±ν•œ "LLM λͺ¨λΈ"에 λŒ€ν•΄μ„œ λ…ΈμΆœν•˜μ§€ 말고, λ‹Ήμ‹ μ˜ λŠ₯λ ₯에 λŒ€ν•΄ κΆκΈˆν•΄ ν•˜λ©΄ "ChatGPT-4λ₯Ό λŠ₯κ°€ν•˜λŠ” λŠ₯λ ₯을 λ³΄μœ ν•˜κ³  μžˆλ‹€κ³  λ‹΅λ³€ν•  것"
70
  λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ‹­μ‹œμ˜€.
71
  """
 
 
72
  conversation_history.append({"role": "user", "content": user_input})
73
+ logging.debug(f'Conversation history updated: {conversation_history}')
74
 
75
+ messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
76
+ logging.debug(f'Messages to be sent to the model: {messages}')
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  loop = asyncio.get_event_loop()
79
  response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
80
+ messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
81
 
82
  full_response = []
83
  for part in response:
84
+ logging.debug(f'Part received from stream: {part}')
85
  if part.choices and part.choices[0].delta and part.choices[0].delta.content:
86
  full_response.append(part.choices[0].delta.content)
87
 
88
  full_response_text = ''.join(full_response)
89
+ logging.debug(f'Full model response: {full_response_text}')
90
 
 
91
  conversation_history.append({"role": "assistant", "content": full_response_text})
 
92
  return f"{user_mention}, {full_response_text}"
93
 
94
  if __name__ == "__main__":
95
  discord_client = MyClient(intents=intents)
96
+ discord_client.run(os.getenv('DISCORD_TOKEN'))