-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
35 lines (28 loc) · 867 Bytes
/
gpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
from dotenv import load_dotenv
import os
load_dotenv()
# Set up your proxy from .env
proxy = {
'http': os.getenv("HTTP_PROXY"),
'https': os.getenv("HTTPS_PROXY")
}
# Set your OpenAI API key
api_key = os.getenv("OPENAI_API_KEY")
# Define the API endpoint URL
url = 'https://api.openai.com/v1/chat/completions'
# Set your request headers
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
def chatgpt(req):
data = {
'model': 'gpt-3.5-turbo',
'messages': [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": req}
]
}
gptresponse = requests.post(url, headers=headers, json=data, proxies=proxy)
return gptresponse.json()['choices'][0]['message']['content']