-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.py
45 lines (37 loc) · 1.32 KB
/
chat.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
36
37
38
39
40
41
42
43
44
45
import argparse
import requests
import os
def chat_with_gpt4(text):
"""
Initiates a chat with GPT-4 using the provided text. Uses environment variable "OPENAI_API_KEY" for authorization.
Raises an exception if the environment variable is not found.
"""
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise Exception("Couldn't find the 'OPENAI_API_KEY' environment variable!")
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
data = {
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": text},
],
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
raise Exception(
f"Request to GPT-4 API failed with status {response.status_code}. The response is: {response.text}"
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("text", help="The text to be used as prompt for GPT-4.")
args = parser.parse_args()
print(chat_with_gpt4(args.text))
if __name__ == "__main__":
main()