fantos commited on
Commit
85ac4c3
ยท
verified ยท
1 Parent(s): c72d7d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -21
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import discord
2
  import logging
3
  import os
4
- from openai import OpenAI
 
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
- 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
  else:
30
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์˜จ API ํ‚ค์˜ ๊ณต๋ฐฑ ๋ฐ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž ์ œ๊ฑฐ
31
- OPENAI_API_KEY = OPENAI_API_KEY.strip()
32
 
33
- # OpenAI ํด๋ผ์ด์–ธํŠธ ์„ค์ •
34
- openai_client = OpenAI(api_key=OPENAI_API_KEY)
 
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
- # OpenAI API ํ˜ธ์ถœ์„ ์œ„ํ•œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ
101
- loop = asyncio.get_event_loop()
102
- response = await loop.run_in_executor(None, lambda: openai_client.chat.completions.create(
103
- model="gpt-4.1-mini", # ๋˜๋Š” gpt-4.1-mini์™€ ์œ ์‚ฌํ•œ ๋‹ค๋ฅธ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ
104
- messages=messages,
105
- temperature=0.7,
106
- max_tokens=1800,
107
- top_p=0.85
108
- ))
 
 
109
 
110
- full_response_text = response.choices[0].message.content
111
- logging.debug(f'Full model response: {full_response_text}')
 
 
 
112
 
113
- conversation_history.append({"role": "assistant", "content": full_response_text})
 
 
 
 
 
 
 
 
 
 
114
 
115
- return f"{user_mention}, {full_response_text}"
 
 
 
 
 
 
 
 
 
 
 
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}")