ginipick commited on
Commit
e882cc6
Β·
verified Β·
1 Parent(s): 560c356

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -34
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
- # 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)
 
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)