Update chatgpt_api.py
Browse files- chatgpt_api.py +20 -0
chatgpt_api.py
CHANGED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# OpenAI API キーを設定する
|
5 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "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")
|
6 |
+
|
7 |
+
def get_chatgpt_response(input_text):
|
8 |
+
headers = {
|
9 |
+
"Content-Type": "application/json",
|
10 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
11 |
+
}
|
12 |
+
data = {
|
13 |
+
"model": "gpt-4",
|
14 |
+
"messages": [{"role": "user", "content": input_text}]
|
15 |
+
}
|
16 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=data)
|
17 |
+
response_json = response.json()
|
18 |
+
output_text = response_json["choices"][0]["message"]["content"]
|
19 |
+
|
20 |
+
return output_text
|