Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
4 |
-
|
|
|
5 |
import asyncio
|
6 |
import subprocess
|
7 |
|
@@ -22,16 +23,17 @@ SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
22 |
conversation_history = []
|
23 |
|
24 |
# API ํค ์ค์ ๋ฐ ์ ๋ฆฌ
|
25 |
-
|
26 |
-
if not
|
27 |
# ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ ๊ฒฝ์ฐ, ์ฌ๊ธฐ์ API ํค๋ฅผ ์ง์ ์
๋ ฅํ์ธ์
|
28 |
-
|
29 |
else:
|
30 |
# ํ๊ฒฝ ๋ณ์์์ ๊ฐ์ ธ์จ API ํค์ ๊ณต๋ฐฑ ๋ฐ ์ค๋ฐ๊ฟ ๋ฌธ์ ์ ๊ฑฐ
|
31 |
-
|
32 |
|
33 |
-
#
|
34 |
-
|
|
|
35 |
|
36 |
class MyClient(discord.Client):
|
37 |
def __init__(self, *args, **kwargs):
|
@@ -80,7 +82,7 @@ async def generate_response(message):
|
|
80 |
logging.debug(f'Conversation history updated: {conversation_history}')
|
81 |
|
82 |
try:
|
83 |
-
#
|
84 |
messages = [
|
85 |
{
|
86 |
"role": "system",
|
@@ -97,22 +99,48 @@ async def generate_response(message):
|
|
97 |
|
98 |
logging.debug(f'Messages to be sent to the model: {messages}')
|
99 |
|
100 |
-
#
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
112 |
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
except Exception as e:
|
118 |
logging.error(f"Error in generate_response: {e}")
|
|
|
1 |
import discord
|
2 |
import logging
|
3 |
import os
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
import asyncio
|
7 |
import subprocess
|
8 |
|
|
|
23 |
conversation_history = []
|
24 |
|
25 |
# API ํค ์ค์ ๋ฐ ์ ๋ฆฌ
|
26 |
+
API_KEY = os.getenv("OPENAI_API_KEY") # ๊ธฐ์กด API ํค๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉ
|
27 |
+
if not API_KEY:
|
28 |
# ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ ๊ฒฝ์ฐ, ์ฌ๊ธฐ์ API ํค๋ฅผ ์ง์ ์
๋ ฅํ์ธ์
|
29 |
+
API_KEY = "your_api_key_here" # ์ค์ ํค๋ก ๊ต์ฒด ํ์
|
30 |
else:
|
31 |
# ํ๊ฒฝ ๋ณ์์์ ๊ฐ์ ธ์จ API ํค์ ๊ณต๋ฐฑ ๋ฐ ์ค๋ฐ๊ฟ ๋ฌธ์ ์ ๊ฑฐ
|
32 |
+
API_KEY = API_KEY.strip()
|
33 |
|
34 |
+
# Fireworks API ์ค์
|
35 |
+
FIREWORKS_API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
|
36 |
+
FIREWORKS_MODEL = "accounts/fireworks/models/qwen3-30b-a3b"
|
37 |
|
38 |
class MyClient(discord.Client):
|
39 |
def __init__(self, *args, **kwargs):
|
|
|
82 |
logging.debug(f'Conversation history updated: {conversation_history}')
|
83 |
|
84 |
try:
|
85 |
+
# ๋ฉ์์ง ํ์ ์ค๋น
|
86 |
messages = [
|
87 |
{
|
88 |
"role": "system",
|
|
|
99 |
|
100 |
logging.debug(f'Messages to be sent to the model: {messages}')
|
101 |
|
102 |
+
# Fireworks API ์์ฒญ ํ์ด๋ก๋ ๊ตฌ์ฑ
|
103 |
+
payload = {
|
104 |
+
"model": FIREWORKS_MODEL,
|
105 |
+
"max_tokens": 1800,
|
106 |
+
"top_p": 0.85,
|
107 |
+
"top_k": 40,
|
108 |
+
"presence_penalty": 0,
|
109 |
+
"frequency_penalty": 0,
|
110 |
+
"temperature": 0.7,
|
111 |
+
"messages": messages
|
112 |
+
}
|
113 |
|
114 |
+
headers = {
|
115 |
+
"Accept": "application/json",
|
116 |
+
"Content-Type": "application/json",
|
117 |
+
"Authorization": f"Bearer {API_KEY}"
|
118 |
+
}
|
119 |
|
120 |
+
# ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก API ํธ์ถ
|
121 |
+
loop = asyncio.get_event_loop()
|
122 |
+
response = await loop.run_in_executor(
|
123 |
+
None,
|
124 |
+
lambda: requests.post(
|
125 |
+
FIREWORKS_API_URL,
|
126 |
+
headers=headers,
|
127 |
+
data=json.dumps(payload),
|
128 |
+
timeout=60
|
129 |
+
)
|
130 |
+
)
|
131 |
|
132 |
+
# ์๋ต ์ฒ๋ฆฌ
|
133 |
+
if response.status_code == 200:
|
134 |
+
response_json = response.json()
|
135 |
+
full_response_text = response_json["choices"][0]["message"]["content"]
|
136 |
+
logging.debug(f'Full model response: {full_response_text}')
|
137 |
+
|
138 |
+
conversation_history.append({"role": "assistant", "content": full_response_text})
|
139 |
+
|
140 |
+
return f"{user_mention}, {full_response_text}"
|
141 |
+
else:
|
142 |
+
logging.error(f"API ์๋ต ์ค๋ฅ: {response.status_code} - {response.text}")
|
143 |
+
return f"{user_mention}, ์ฃ์กํฉ๋๋ค. API ์๋ต ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค (์ํ ์ฝ๋: {response.status_code})."
|
144 |
|
145 |
except Exception as e:
|
146 |
logging.error(f"Error in generate_response: {e}")
|