-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
46 lines (36 loc) · 969 Bytes
/
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
46
import os
import sys
from datetime import datetime
import urllib.request
from dotenv import load_dotenv
import openai
from openai import OpenAI
load_dotenv()
# Fail if no commandline argument is provided
if len(sys.argv) < 2:
print("Please provide an text")
exit(1)
text_prompt = sys.argv[1]
client = OpenAI()
response = ""
try:
completion = client.chat.completions.create(
model=os.getenv("CHAT_MODEL") or "gpt-4o",
messages=[
{"role": "user", "content": text_prompt}
]
)
response = completion.choices[0].message.content
except openai.OpenAIError as e:
print(e.http_status)
print(e.error)
exit(1)
print(response)
# Store the resposne in a file
if not os.path.isdir("chat"):
os.mkdir("chat")
cur_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
with open(f"chat/{cur_time}.txt", "w", encoding="utf-8") as f:
f.write(text_prompt)
f.write("\n\n---\n\n")
f.write(response)