Update app.py
Browse files
app.py
CHANGED
@@ -15,15 +15,21 @@ intents.messages = True
|
|
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"))
|
23 |
|
24 |
# λν νμ€ν 리λ₯Ό μ μ₯ν μ μ λ³μ
|
25 |
conversation_history = []
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
class MyClient(discord.Client):
|
28 |
def __init__(self, *args, **kwargs):
|
29 |
super().__init__(*args, **kwargs)
|
@@ -76,37 +82,44 @@ async def generate_response(message):
|
|
76 |
conversation_history.append({"role": "user", "content": user_input})
|
77 |
logging.debug(f'Conversation history updated: {conversation_history}')
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
discord_client = MyClient(intents=intents)
|
|
|
15 |
intents.guilds = True
|
16 |
intents.guild_messages = True
|
17 |
|
|
|
|
|
|
|
18 |
# νΉμ μ±λ ID
|
19 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
20 |
|
21 |
# λν νμ€ν 리λ₯Ό μ μ₯ν μ μ λ³μ
|
22 |
conversation_history = []
|
23 |
|
24 |
+
# API ν€ μ€μ - νκ²½ λ³μκ° μλ κ²½μ° μ§μ μ§μ
|
25 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
26 |
+
if not OPENAI_API_KEY:
|
27 |
+
# νκ²½ λ³μκ° μ€μ λμ§ μμμ κ²½μ°, μ¬κΈ°μ API ν€λ₯Ό μ§μ μ
λ ₯νμΈμ
|
28 |
+
OPENAI_API_KEY = "your_openai_api_key_here" # μ€μ ν€λ‘ κ΅μ²΄ νμ
|
29 |
+
|
30 |
+
# OpenAI ν΄λΌμ΄μΈνΈ μ€μ
|
31 |
+
openai_client = OpenAI(api_key=OPENAI_API_KEY)
|
32 |
+
|
33 |
class MyClient(discord.Client):
|
34 |
def __init__(self, *args, **kwargs):
|
35 |
super().__init__(*args, **kwargs)
|
|
|
82 |
conversation_history.append({"role": "user", "content": user_input})
|
83 |
logging.debug(f'Conversation history updated: {conversation_history}')
|
84 |
|
85 |
+
try:
|
86 |
+
# μμ€ν
λ©μμ§μ μ¬μ©μ μ
λ ₯μ ν¬ν¨ν λ©μμ§ μμ±
|
87 |
+
messages = [
|
88 |
+
{
|
89 |
+
"role": "system",
|
90 |
+
"content": f"{system_prefix} {system_message}"
|
91 |
+
}
|
92 |
+
]
|
93 |
+
|
94 |
+
# λν κΈ°λ‘μμ λ©μμ§ μΆκ°
|
95 |
+
for msg in conversation_history:
|
96 |
+
messages.append({
|
97 |
+
"role": msg["role"],
|
98 |
+
"content": msg["content"]
|
99 |
+
})
|
100 |
+
|
101 |
+
logging.debug(f'Messages to be sent to the model: {messages}')
|
102 |
+
|
103 |
+
# OpenAI API νΈμΆμ μν λΉλκΈ° μ²λ¦¬
|
104 |
+
loop = asyncio.get_event_loop()
|
105 |
+
response = await loop.run_in_executor(None, lambda: openai_client.chat.completions.create(
|
106 |
+
model="gpt-4-1106-preview", # λλ gpt-4.1-miniμ μ μ¬ν λ€λ₯Έ μ¬μ© κ°λ₯ν λͺ¨λΈ
|
107 |
+
messages=messages,
|
108 |
+
temperature=0.7,
|
109 |
+
max_tokens=1000,
|
110 |
+
top_p=0.85
|
111 |
+
))
|
112 |
+
|
113 |
+
full_response_text = response.choices[0].message.content
|
114 |
+
logging.debug(f'Full model response: {full_response_text}')
|
115 |
+
|
116 |
+
conversation_history.append({"role": "assistant", "content": full_response_text})
|
117 |
+
|
118 |
+
return f"{user_mention}, {full_response_text}"
|
119 |
+
|
120 |
+
except Exception as e:
|
121 |
+
logging.error(f"Error in generate_response: {e}")
|
122 |
+
return f"{user_mention}, μ£μ‘ν©λλ€. μλ΅μ μμ±νλ μ€ μ€λ₯κ° λ°μνμ΅λλ€. μ μ ν λ€μ μλν΄ μ£ΌμΈμ."
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
discord_client = MyClient(intents=intents)
|