Spaces:
Running
Running
Update prompt_engine.py
Browse files- prompt_engine.py +13 -23
prompt_engine.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import json
|
2 |
import os
|
3 |
import requests
|
|
|
4 |
|
5 |
class PromptGenerator:
|
6 |
def __init__(self):
|
@@ -167,33 +168,22 @@ class PromptGenerator:
|
|
167 |
if not api_key:
|
168 |
return "OpenAI APIキーが設定されていません。"
|
169 |
|
170 |
-
# OpenAI API
|
171 |
-
|
172 |
|
173 |
-
#
|
174 |
-
|
175 |
-
"
|
176 |
-
|
177 |
-
}
|
178 |
-
|
179 |
-
# リクエストボディ
|
180 |
-
data = {
|
181 |
-
"model": "o4-mini-2025-04-16",
|
182 |
-
"max_tokens": 2048,
|
183 |
-
"temperature": 0.75,
|
184 |
-
"messages": [
|
185 |
{"role": "system", "content": self.system_prompt},
|
186 |
{"role": "user", "content": user_message}
|
187 |
-
]
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
response = requests.post(url, headers=headers, json=data)
|
192 |
-
response.raise_for_status()
|
193 |
|
194 |
-
#
|
195 |
-
|
196 |
-
return result["choices"][0]["message"]["content"]
|
197 |
|
198 |
except Exception as e:
|
199 |
return f"OpenAI APIエラー: {str(e)}"
|
|
|
1 |
import json
|
2 |
import os
|
3 |
import requests
|
4 |
+
from openai import OpenAI
|
5 |
|
6 |
class PromptGenerator:
|
7 |
def __init__(self):
|
|
|
168 |
if not api_key:
|
169 |
return "OpenAI APIキーが設定されていません。"
|
170 |
|
171 |
+
# OpenAI APIクライアントを初期化
|
172 |
+
client = OpenAI(api_key=api_key)
|
173 |
|
174 |
+
# APIリクエスト送信
|
175 |
+
response = client.chat.completions.create(
|
176 |
+
model="o4-mini-2025-04-16",
|
177 |
+
messages=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
{"role": "system", "content": self.system_prompt},
|
179 |
{"role": "user", "content": user_message}
|
180 |
+
],
|
181 |
+
max_tokens=2048,
|
182 |
+
temperature=0.75
|
183 |
+
)
|
|
|
|
|
184 |
|
185 |
+
# レスポンスの取得
|
186 |
+
return response.choices[0].message.content
|
|
|
187 |
|
188 |
except Exception as e:
|
189 |
return f"OpenAI APIエラー: {str(e)}"
|