risdom / chatgpt_api.py
yosuke-i's picture
Update chatgpt_api.py
7463ddd verified
raw
history blame
No virus
661 Bytes
import os
import requests
# OpenAI API キーを設定する
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "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")
def get_chatgpt_response(input_text):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
data = {
"model": "gpt-4",
"messages": [{"role": "user", "content": input_text}]
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=data)
response_json = response.json()
output_text = response_json["choices"][0]["message"]["content"]
return output_text