ginipick commited on
Commit
cdc7c7d
ยท
verified ยท
1 Parent(s): 310d9d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -24
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import discord
2
  import logging
3
  import os
4
- from huggingface_hub import InferenceClient
5
  import asyncio
6
  import subprocess
7
 
@@ -15,8 +15,8 @@ intents.messages = True
15
  intents.guilds = True
16
  intents.guild_messages = True
17
 
18
- # ์ถ”๋ก  API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
19
- hf_client = InferenceClient("deepseek-ai/DeepSeek-V3-0324", token=os.getenv("HF_TOKEN"))
20
 
21
  # ํŠน์ • ์ฑ„๋„ ID
22
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
@@ -28,13 +28,12 @@ class MyClient(discord.Client):
28
  def __init__(self, *args, **kwargs):
29
  super().__init__(*args, **kwargs)
30
  self.is_processing = False
31
-
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:
40
  return
@@ -42,24 +41,25 @@ class MyClient(discord.Client):
42
  return
43
  if self.is_processing:
44
  return
 
45
  self.is_processing = True
46
  try:
47
  response = await generate_response(message)
48
  await message.channel.send(response)
49
  finally:
50
  self.is_processing = False
51
-
52
  def is_message_in_specific_channel(self, message):
53
  # ๋ฉ”์‹œ์ง€๊ฐ€ ์ง€์ •๋œ ์ฑ„๋„์ด๊ฑฐ๋‚˜, ํ•ด๋‹น ์ฑ„๋„์˜ ์“ฐ๋ ˆ๋“œ์ธ ๊ฒฝ์šฐ True ๋ฐ˜ํ™˜
54
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
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
62
  user_mention = message.author.mention
 
63
  system_message = f"{user_mention}, DISCORD์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
64
  system_prefix = """
65
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค. ์ถœ๋ ฅ์‹œ markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ.
@@ -72,28 +72,42 @@ async def generate_response(message):
72
  ํŠนํžˆ ๋„ˆ๋ฅผ ๊ตฌ์„ฑํ•œ "LLM ๋ชจ๋ธ"์— ๋Œ€ํ•ด์„œ ๋…ธ์ถœํ•˜์ง€ ๋ง๊ณ , ๋‹น์‹ ์˜ ๋Šฅ๋ ฅ์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•ด ํ•˜๋ฉด "ChatGPT-4๋ฅผ ๋Šฅ๊ฐ€ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋‹ต๋ณ€ํ•  ๊ฒƒ"
73
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
74
  """
 
75
  conversation_history.append({"role": "user", "content": user_input})
76
  logging.debug(f'Conversation history updated: {conversation_history}')
77
-
78
- messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + conversation_history
79
- logging.debug(f'Messages to be sent to the model: {messages}')
80
-
 
 
 
 
 
 
 
81
  loop = asyncio.get_event_loop()
82
- response = await loop.run_in_executor(None, lambda: hf_client.chat_completion(
83
- messages, max_tokens=1000, stream=True, temperature=0.7, top_p=0.85))
84
-
85
- full_response = []
86
- for part in response:
87
- logging.debug(f'Part received from stream: {part}')
88
- if part.choices and part.choices[0].delta and part.choices[0].delta.content:
89
- full_response.append(part.choices[0].delta.content)
90
-
91
- full_response_text = ''.join(full_response)
 
 
 
 
 
92
  logging.debug(f'Full model response: {full_response_text}')
93
-
94
  conversation_history.append({"role": "assistant", "content": full_response_text})
 
95
  return f"{user_mention}, {full_response_text}"
96
 
97
  if __name__ == "__main__":
98
  discord_client = MyClient(intents=intents)
99
- discord_client.run(os.getenv('DISCORD_TOKEN'))
 
1
  import discord
2
  import logging
3
  import os
4
+ from openai import OpenAI
5
  import asyncio
6
  import subprocess
7
 
 
15
  intents.guilds = True
16
  intents.guild_messages = True
17
 
18
+ # OpenAI ํด๋ผ์ด์–ธํŠธ ์„ค์ •
19
+ openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
20
 
21
  # ํŠน์ • ์ฑ„๋„ ID
22
  SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
 
28
  def __init__(self, *args, **kwargs):
29
  super().__init__(*args, **kwargs)
30
  self.is_processing = False
31
+
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
  async def on_message(self, message):
38
  if message.author == self.user:
39
  return
 
41
  return
42
  if self.is_processing:
43
  return
44
+
45
  self.is_processing = True
46
  try:
47
  response = await generate_response(message)
48
  await message.channel.send(response)
49
  finally:
50
  self.is_processing = False
51
+
52
  def is_message_in_specific_channel(self, message):
53
  # ๋ฉ”์‹œ์ง€๊ฐ€ ์ง€์ •๋œ ์ฑ„๋„์ด๊ฑฐ๋‚˜, ํ•ด๋‹น ์ฑ„๋„์˜ ์“ฐ๋ ˆ๋“œ์ธ ๊ฒฝ์šฐ True ๋ฐ˜ํ™˜
54
  return message.channel.id == SPECIFIC_CHANNEL_ID or (
55
  isinstance(message.channel, discord.Thread) and message.channel.parent_id == SPECIFIC_CHANNEL_ID
56
  )
57
 
 
58
  async def generate_response(message):
59
  global conversation_history # ์ „์—ญ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ช…์‹œ
60
  user_input = message.content
61
  user_mention = message.author.mention
62
+
63
  system_message = f"{user_mention}, DISCORD์—์„œ ์‚ฌ์šฉ์ž๋“ค์˜ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."
64
  system_prefix = """
65
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค. ์ถœ๋ ฅ์‹œ markdown ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ.
 
72
  ํŠนํžˆ ๋„ˆ๋ฅผ ๊ตฌ์„ฑํ•œ "LLM ๋ชจ๋ธ"์— ๋Œ€ํ•ด์„œ ๋…ธ์ถœํ•˜์ง€ ๋ง๊ณ , ๋‹น์‹ ์˜ ๋Šฅ๋ ฅ์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•ด ํ•˜๋ฉด "ChatGPT-4๋ฅผ ๋Šฅ๊ฐ€ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๋‹ต๋ณ€ํ•  ๊ฒƒ"
73
  ๋ฐ˜๋“œ์‹œ ํ•œ๊ธ€๋กœ ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
74
  """
75
+
76
  conversation_history.append({"role": "user", "content": user_input})
77
  logging.debug(f'Conversation history updated: {conversation_history}')
78
+
79
+ # OpenAI API์— ๋งž๊ฒŒ ๋ฉ”์‹œ์ง€ ํ˜•์‹ ์กฐ์ •
80
+ formatted_messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
81
+ for msg in conversation_history:
82
+ formatted_messages.append({
83
+ "role": msg["role"],
84
+ "content": [{"type": "input_text", "text": msg["content"]}]
85
+ })
86
+
87
+ logging.debug(f'Messages to be sent to the model: {formatted_messages}')
88
+
89
  loop = asyncio.get_event_loop()
90
+
91
+ # OpenAI API ํ˜ธ์ถœ
92
+ response = await loop.run_in_executor(None, lambda: openai_client.responses.create(
93
+ model="gpt-4.1-mini",
94
+ input=formatted_messages,
95
+ text={"format": {"type": "text"}},
96
+ reasoning={},
97
+ tools=[],
98
+ temperature=0.7,
99
+ max_output_tokens=1000,
100
+ top_p=0.85,
101
+ store=True
102
+ ))
103
+
104
+ full_response_text = response.output.content.text
105
  logging.debug(f'Full model response: {full_response_text}')
106
+
107
  conversation_history.append({"role": "assistant", "content": full_response_text})
108
+
109
  return f"{user_mention}, {full_response_text}"
110
 
111
  if __name__ == "__main__":
112
  discord_client = MyClient(intents=intents)
113
+ discord_client.run(os.getenv('DISCORD_TOKEN'))