Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
4 |
-
from
|
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 |
-
#
|
19 |
-
|
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 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
loop = asyncio.get_event_loop()
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
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'))
|